| 1 | package memory |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestNormalizeSubjectKey(t *testing.T) { |
| 9 | for in, want := range map[string]string{ |
| 10 | "project.package_manager": "project.package_manager", |
| 11 | " Project.Package Manager": "project.package_manager", |
| 12 | "project..release-branch": "project.release-branch", |
| 13 | "项目.包管理": "", |
| 14 | "...": "", |
| 15 | } { |
| 16 | if got := NormalizeSubjectKey(in); got != want { |
| 17 | t.Fatalf("NormalizeSubjectKey(%q) = %q, want %q", in, got, want) |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // The npm -> pnpm scenario: a second fact answering a held subject is |
| 23 | // rejected with directions to the holder, and updating the holder lands the |
| 24 | // new value as a revision. |
| 25 | func TestSubjectKeyRejectsSecondActiveValueWithDirections(t *testing.T) { |
| 26 | store := recallTestStore(t) |
| 27 | saved, err := store.SaveWithOptions(Memory{ |
| 28 | Name: "package-manager", Title: "Package manager", |
| 29 | Description: "This project uses npm", SubjectKey: "project.package_manager", |
| 30 | Body: "This project uses npm.", |
| 31 | }, SaveOptions{}) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | |
| 36 | _, err = store.SaveWithOptions(Memory{ |
| 37 | Name: "dependency-tooling", Title: "Dependency tooling", |
| 38 | Description: "We migrated to pnpm", SubjectKey: "project.package_manager", |
| 39 | Body: "We migrated to pnpm.", |
| 40 | }, SaveOptions{}) |
| 41 | if err == nil { |
| 42 | t.Fatal("a second active value for a held subject must be rejected") |
| 43 | } |
| 44 | for _, want := range []string{saved.Memory.ID, "update that id", "uses npm"} { |
| 45 | if !strings.Contains(err.Error(), want) { |
| 46 | t.Fatalf("conflict error must carry %q for the model to act on, got: %v", want, err) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | updated, err := store.SaveWithOptions(Memory{ |
| 51 | ID: saved.Memory.ID, Description: "We migrated to pnpm", Body: "We migrated to pnpm.", |
| 52 | }, SaveOptions{}) |
| 53 | if err != nil || updated.Memory.Revision != 2 { |
| 54 | t.Fatalf("updating the holder must land the new value as a revision: %+v %v", updated.Memory, err) |
| 55 | } |
| 56 | if NormalizeSubjectKey(updated.Memory.SubjectKey) != "project.package_manager" { |
| 57 | t.Fatalf("subject key must inherit on update, got %+v", updated.Memory) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestSubjectKeyScopesAreIndependentAndProjectShadowsGlobal(t *testing.T) { |
| 62 | store := recallTestStore(t) |
| 63 | recallTestWrite(t, store.GlobalDir, Memory{ |
| 64 | ID: "mem-global-style", Name: "response-style-global", Title: "Response style", |
| 65 | Description: "Prefers detailed answers", Type: TypeUser, Scope: FactScopeGlobal, |
| 66 | Activation: ActivationRelevant, SubjectKey: "user.response_style", |
| 67 | Body: "Prefers long, detailed answers.", |
| 68 | }) |
| 69 | if _, err := store.SaveWithOptions(Memory{ |
| 70 | Name: "response-style-here", Title: "Response style for this repo", |
| 71 | Description: "结论先行,宁短勿长", Scope: FactScopeProject, |
| 72 | SubjectKey: "user.response_style", Keywords: "response style answers", |
| 73 | Body: "结论先行,宁短勿长。", |
| 74 | }, SaveOptions{}); err != nil { |
| 75 | t.Fatalf("the same subject in another scope is an override, not a conflict: %v", err) |
| 76 | } |
| 77 | |
| 78 | result := AutoRecall(store, "response style preference for answers", RecallOptions{}) |
| 79 | for _, hit := range result.Hits { |
| 80 | if hit.Memory.ID == "mem-global-style" { |
| 81 | t.Fatalf("project subject holder must shadow the global one in recall: %+v", result.Hits) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestSubjectConflictSkipsSelfAndUnkeyedFacts(t *testing.T) { |
| 87 | store := recallTestStore(t) |
| 88 | saved, err := store.SaveWithOptions(Memory{ |
| 89 | Name: "release-branch", Description: "release/1.21", |
| 90 | SubjectKey: "project.release_branch", Body: "release/1.21", |
| 91 | }, SaveOptions{}) |
| 92 | if err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | if _, err := store.SaveWithOptions(Memory{ |
| 96 | ID: saved.Memory.ID, Description: "release/1.22", Body: "release/1.22", |
| 97 | SubjectKey: "project.release_branch", |
| 98 | }, SaveOptions{}); err != nil { |
| 99 | t.Fatalf("re-saving the holder itself must not self-conflict: %v", err) |
| 100 | } |
| 101 | if _, err := store.SaveWithOptions(Memory{ |
| 102 | Name: "unrelated-note", Description: "no subject here", Body: "narrative fact", |
| 103 | }, SaveOptions{}); err != nil { |
| 104 | t.Fatalf("facts without a subject key are exempt: %v", err) |
| 105 | } |
| 106 | } |
| 107 |