| 1 | package historycatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/agent" |
| 9 | "reasonix/internal/provider" |
| 10 | ) |
| 11 | |
| 12 | func TestReconcileRootDetectsMetaOnlyChange(t *testing.T) { |
| 13 | t.Parallel() |
| 14 | ctx := context.Background() |
| 15 | root := t.TempDir() |
| 16 | path := filepath.Join(root, "meta.jsonl") |
| 17 | saveMessages(t, path, provider.Message{Role: provider.RoleUser, Content: "stable content"}) |
| 18 | if err := agent.SaveBranchMeta(path, agent.BranchMeta{CustomTitle: "old"}); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | catalog, err := Open(ctx, Options{Path: filepath.Join(t.TempDir(), "history.sqlite")}) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | t.Cleanup(func() { _ = catalog.Close(context.Background()) }) |
| 26 | target := Root{Path: root, Source: "project", Scope: "project", WorkspaceRoot: root} |
| 27 | if err := catalog.ReconcileRoot(ctx, target); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if err := agent.SaveBranchMeta(path, agent.BranchMeta{CustomTitle: "replacement title with a different size"}); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if err := catalog.ReconcileRoot(ctx, target); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | var title string |
| 37 | if err := catalog.db.QueryRowContext(ctx, `SELECT custom_title FROM history_sources WHERE path=?`, path).Scan(&title); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if title != "replacement title with a different size" { |
| 41 | t.Fatalf("custom title = %q, want updated meta projection", title) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestRebuildReplacesStaleHistoryProjection(t *testing.T) { |
| 46 | t.Parallel() |
| 47 | ctx := context.Background() |
| 48 | databasePath := filepath.Join(t.TempDir(), "history.sqlite") |
| 49 | staleRoot := t.TempDir() |
| 50 | currentRoot := t.TempDir() |
| 51 | saveMessages(t, filepath.Join(staleRoot, "stale.jsonl"), provider.Message{Role: provider.RoleUser, Content: "obsolete unicorn marker"}) |
| 52 | saveMessages(t, filepath.Join(currentRoot, "current.jsonl"), provider.Message{Role: provider.RoleUser, Content: "current phoenix marker"}) |
| 53 | seed, err := Open(ctx, Options{Path: databasePath}) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | if err := seed.ReconcileRoot(ctx, Root{Path: staleRoot, Source: "global", Scope: "global"}); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | if err := seed.Close(ctx); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | if _, err := Rebuild(ctx, Options{Path: databasePath}, []Root{{Path: currentRoot, Source: "global", Scope: "global"}}); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | rebuilt, err := Open(ctx, Options{Path: databasePath}) |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | t.Cleanup(func() { _ = rebuilt.Close(context.Background()) }) |
| 71 | stale, err := rebuilt.Search(ctx, SearchRequest{Query: "unicorn", Roots: []string{staleRoot}}) |
| 72 | if err != nil || len(stale.Items) != 0 { |
| 73 | t.Fatalf("stale result=%#v err=%v", stale, err) |
| 74 | } |
| 75 | current, err := rebuilt.Search(ctx, SearchRequest{Query: "phoenix", Roots: []string{currentRoot}}) |
| 76 | if err != nil || len(current.Items) != 1 { |
| 77 | t.Fatalf("current result=%#v err=%v", current, err) |
| 78 | } |
| 79 | } |
| 80 |