返回 DeepSeek-Reasonix
remember_test.go
根目录 / internal / memory / remember_test.go
1 package memory
2
3 import (
4 "context"
5 "encoding/json"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10 )
11
12 // TestRememberToolSaves drives the tool the way the agent does — raw JSON args —
13 // and verifies the fact lands in the store and the index.
14 func TestRememberToolSaves(t *testing.T) {
15 store := Store{Dir: t.TempDir()}
16 tl := NewRememberTool(store)
17
18 if tl.Name() != "remember" || tl.ReadOnly() {
19 t.Fatalf("unexpected tool identity: name=%q readonly=%v", tl.Name(), tl.ReadOnly())
20 }
21 // Schema must be valid JSON the provider can forward.
22 if !json.Valid(tl.Schema()) {
23 t.Fatal("remember schema is not valid JSON")
24 }
25
26 args := []byte(`{"name":"likes-go","title":"Likes Go","description":"User likes Go","type":"user","scope":"global","body":"Default to Go for backend work."}`)
27 out, err := tl.Execute(context.Background(), args)
28 if err != nil {
29 t.Fatalf("Execute: %v", err)
30 }
31 if !strings.Contains(out, "Saved memory") {
32 t.Fatalf("unexpected tool output: %q", out)
33 }
34 if strings.Contains(out, store.Dir) || !strings.Contains(out, "global/likes-go.md") {
35 t.Fatalf("tool output must use a stable reference, got %q", out)
36 }
37
38 list := store.List()
39 if len(list) != 1 || list[0].Name != "likes-go" || list[0].Type != TypeUser || list[0].Scope != FactScopeGlobal {
40 t.Fatalf("memory not saved correctly: %+v", list)
41 }
42 if list[0].Title != "Likes Go" {
43 t.Fatalf("title not persisted through the tool: %q", list[0].Title)
44 }
45 }
46
47 func TestRememberToolDefaultsToProjectScope(t *testing.T) {
48 root := t.TempDir()
49 store := Store{Dir: root + "/project", GlobalDir: root + "/global"}
50 if _, err := NewRememberTool(store).Execute(context.Background(), []byte(`{"name":"project-feedback","description":"current project only","type":"feedback","body":"body"}`)); err != nil {
51 t.Fatal(err)
52 }
53 list := store.List()
54 if len(list) != 1 || list[0].Scope != FactScopeProject {
55 t.Fatalf("memory = %+v, want project scope", list)
56 }
57 }
58
59 func TestRememberToolUpdateWithoutScopePreservesLegacyGlobalScope(t *testing.T) {
60 root := t.TempDir()
61 store := Store{Dir: filepath.Join(root, "project"), GlobalDir: filepath.Join(root, "global")}
62 if err := os.MkdirAll(store.GlobalDir, 0o755); err != nil {
63 t.Fatal(err)
64 }
65 legacy := "---\nname: no-emoji\ndescription: legacy global feedback\nmetadata:\n type: feedback\n---\n\nAvoid emoji.\n"
66 globalPath := filepath.Join(store.GlobalDir, "no-emoji.md")
67 if err := os.WriteFile(globalPath, []byte(legacy), 0o644); err != nil {
68 t.Fatal(err)
69 }
70 if err := reindexIn(store.GlobalDir, "no-emoji", Memory{Name: "no-emoji", Description: "legacy global feedback", Type: TypeFeedback, Scope: FactScopeGlobal}); err != nil {
71 t.Fatal(err)
72 }
73
74 out, err := NewRememberTool(store).Execute(context.Background(), []byte(`{"name":"no-emoji","description":"updated global feedback","type":"feedback","body":"Avoid emoji in every project."}`))
75 if err != nil {
76 t.Fatal(err)
77 }
78 if !strings.Contains(out, "(global background)") {
79 t.Fatalf("tool output did not report inherited global scope: %q", out)
80 }
81 if _, err := os.Stat(globalPath); err != nil {
82 t.Fatalf("global memory was not kept active: %v", err)
83 }
84 if _, err := os.Stat(filepath.Join(store.Dir, "no-emoji.md")); !os.IsNotExist(err) {
85 t.Fatalf("omitted-scope update created a project copy, stat err=%v", err)
86 }
87 list := store.List()
88 if len(list) != 1 || list[0].Scope != FactScopeGlobal || !strings.Contains(list[0].Body, "every project") {
89 t.Fatalf("updated memory = %+v, want one global active copy", list)
90 }
91 }
92
93 func TestRememberToolUpdatesScopeQualifiedReference(t *testing.T) {
94 root := t.TempDir()
95 store := Store{Dir: filepath.Join(root, "project"), GlobalDir: filepath.Join(root, "global")}
96 if _, err := store.SaveWithOptions(Memory{Name: "project/shared.md", Description: "project", Body: "project body"}, SaveOptions{}); err != nil {
97 t.Fatal(err)
98 }
99 if _, err := store.SaveWithOptions(Memory{Name: "global/shared.md", Description: "global", Body: "global body"}, SaveOptions{}); err != nil {
100 t.Fatal(err)
101 }
102
103 out, err := NewRememberTool(store).Execute(context.Background(), []byte(`{"name":"global/shared.md","expected_revision":1,"description":"updated global","body":"global v2"}`))
104 if err != nil {
105 t.Fatal(err)
106 }
107 if !strings.Contains(out, "global/shared.md") || strings.Contains(out, root) {
108 t.Fatalf("qualified update output = %q", out)
109 }
110 global, ok := store.Read("global/shared.md")
111 if !ok || global.Name != "shared" || global.Scope != FactScopeGlobal || global.Revision != 2 || global.Body != "global v2" {
112 t.Fatalf("updated global = %+v, ok=%v", global, ok)
113 }
114 project, ok := store.Read("project/shared.md")
115 if !ok || project.Revision != 1 || project.Body != "project body" {
116 t.Fatalf("project fact changed = %+v, ok=%v", project, ok)
117 }
118 }
119
120 // TestRememberToolValidates rejects calls missing required fields rather than
121 // writing an empty memory.
122 func TestRememberToolValidates(t *testing.T) {
123 tl := NewRememberTool(Store{Dir: t.TempDir()})
124 if _, err := tl.Execute(context.Background(), []byte(`{"description":"d"}`)); err == nil {
125 t.Fatal("expected error when body is missing")
126 }
127 if _, err := tl.Execute(context.Background(), []byte(`{"body":"b"}`)); err == nil {
128 t.Fatal("expected error when description is missing")
129 }
130 if _, err := tl.Execute(context.Background(), []byte(`{"description":"d","body":"b","scope":"workspace"}`)); err == nil {
131 t.Fatal("expected error for an unknown scope")
132 }
133 }
134
135 // TestRememberToolQueuesNote verifies a save injects a turn-tail note so the
136 // fact applies this session, not only the next.
137 func TestRememberToolQueuesNote(t *testing.T) {
138 q := &fakeQueue{}
139 ctx := WithQueue(context.Background(), q)
140 tl := NewRememberTool(Store{Dir: t.TempDir()})
141 if _, err := tl.Execute(ctx, []byte(`{"name":"uses-rmb","description":"balance is RMB","type":"user","body":"b"}`)); err != nil {
142 t.Fatal(err)
143 }
144 if len(q.notes) != 1 || !strings.Contains(q.notes[0], "uses-rmb") || !strings.Contains(q.notes[0], "\nb") {
145 t.Fatalf("expected one queued note with the saved memory name and body, got %v", q.notes)
146 }
147 }
148
149 func TestRememberToolQueuesResolvedNameWhenUpdatingByID(t *testing.T) {
150 store := Store{Dir: t.TempDir()}
151 first, err := store.SaveWithOptions(Memory{Name: "stable-name", Description: "before", Body: "before"}, SaveOptions{})
152 if err != nil {
153 t.Fatal(err)
154 }
155 q := &fakeQueue{}
156 ctx := WithQueue(context.Background(), q)
157 args := []byte(`{"id":"` + first.Memory.ID + `","expected_revision":1,"description":"after","body":"after"}`)
158 if _, err := NewRememberTool(store).Execute(ctx, args); err != nil {
159 t.Fatal(err)
160 }
161 if len(q.notes) != 1 || !strings.Contains(q.notes[0], "stable-name") {
162 t.Fatalf("queued note did not use the resolved memory name: %v", q.notes)
163 }
164 }
165
165 lines GO