返回 DeepSeek-Reasonix
write_declare_test.go
根目录 / internal / tool / builtin / write_declare_test.go
1 package builtin
2
3 import (
4 "encoding/json"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "reasonix/internal/sandbox"
10 "reasonix/internal/tool"
11 )
12
13 func TestFileToolsDeclareParentDirectories(t *testing.T) {
14 work := t.TempDir()
15 w := writeFile{workDir: work}
16 decl, err := w.DeclareWriteAccess(json.RawMessage(`{"path":"pkg/a.go","content":"x"}`))
17 if err != nil {
18 t.Fatal(err)
19 }
20 if len(decl.Directories) != 1 || decl.Directories[0] != filepath.Join(work, "pkg") {
21 t.Fatalf("write_file dirs = %v", decl.Directories)
22 }
23 m := moveFile{workDir: work}
24 decl, err = m.DeclareWriteAccess(json.RawMessage(`{"source_path":"a.go","destination_path":"out/b.go"}`))
25 if err != nil {
26 t.Fatal(err)
27 }
28 if len(decl.Directories) != 2 {
29 t.Fatalf("move_file should declare both parents, got %v", decl.Directories)
30 }
31 }
32
33 func TestBashDeclareWriteAccessRequiresJustification(t *testing.T) {
34 var b bash
35 if _, err := b.DeclareWriteAccess(json.RawMessage(`{"command":"cp a ~/.local/bin/a","additional_write_dirs":["~/.local"]}`)); err == nil {
36 t.Fatal("missing justification should fail")
37 }
38 decl, err := b.DeclareWriteAccess(json.RawMessage(`{"command":"cp a ~/.local/bin/a","additional_write_dirs":["~/.local"],"justification":"install tool"}`))
39 if err != nil {
40 t.Fatal(err)
41 }
42 if len(decl.Directories) != 1 || decl.Directories[0] != "~/.local" || decl.Justification != "install tool" {
43 t.Fatalf("unexpected declaration %+v", decl)
44 }
45 }
46
47 func TestBashSchemaIncludesWriteDirFields(t *testing.T) {
48 schema := string(bash{}.Schema())
49 for _, want := range []string{"additional_write_dirs", "justification"} {
50 if !strings.Contains(schema, want) {
51 t.Fatalf("schema missing %s: %s", want, schema)
52 }
53 }
54 if string(bash{rootSet: sandbox.NewWritableRootSet(nil)}.Schema()) != schema {
55 t.Fatal("live write-root set must not change bash schema")
56 }
57 }
58
59 func TestBindWriteRootSetPreservesDeclare(t *testing.T) {
60 set := sandbox.NewWritableRootSet([]string{t.TempDir()})
61 tl := BindWriteRootSet(writeFile{workDir: t.TempDir()}, set)
62 if _, ok := tl.(tool.WriteAccessDeclarer); !ok {
63 t.Fatal("bound write_file must still declare write access")
64 }
65 }
66
66 lines GO