| 1 | package memory |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // TestStoreDeleteByIDRemovesFromAllDirs covers the ID-reference path of |
| 10 | // Archive: an ID may name the same fact in several directories (migration |
| 11 | // duplicates share the deterministic legacy ID), so deletion by ID must |
| 12 | // archive from every directory — the unqualified-name path already does. |
| 13 | func TestStoreDeleteByIDRemovesFromAllDirs(t *testing.T) { |
| 14 | dir := t.TempDir() |
| 15 | s := Store{ |
| 16 | Dir: filepath.Join(dir, "project", "memory"), |
| 17 | GlobalDir: filepath.Join(dir, "global"), |
| 18 | } |
| 19 | |
| 20 | name := "prefers-tabs" |
| 21 | for _, d := range []string{s.Dir, s.GlobalDir} { |
| 22 | if err := os.MkdirAll(d, 0o755); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | m := Memory{Name: name, Description: "user pref", Type: TypeUser, Body: "use tabs"} |
| 26 | if err := os.WriteFile(filepath.Join(d, name+".md"), []byte(render(m, name)), 0o644); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if err := reindexIn(d, name, m); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Both copies share one identity (same name + scope → same legacy ID). |
| 35 | list := s.List() |
| 36 | if len(list) != 1 { |
| 37 | t.Fatalf("want 1 deduplicated memory, got %d", len(list)) |
| 38 | } |
| 39 | |
| 40 | // Delete by the identity ID, not the name. |
| 41 | if err := s.Delete(list[0].ID); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | for _, d := range []string{s.Dir, s.GlobalDir} { |
| 45 | if _, err := os.Stat(filepath.Join(d, name+".md")); !os.IsNotExist(err) { |
| 46 | t.Fatalf("copy in %s should be gone after ID delete", d) |
| 47 | } |
| 48 | } |
| 49 | if idx := s.Index(); idx != "" { |
| 50 | t.Fatalf("Index() should be empty after deleting all entries, got:\n%s", idx) |
| 51 | } |
| 52 | } |
| 53 |