| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/provider" |
| 11 | "reasonix/internal/sessioncatalog" |
| 12 | ) |
| 13 | |
| 14 | func TestRetargetOpenTabsUsesUniqueLinearCompactedLeaf(t *testing.T) { |
| 15 | isolateDesktopUserDirs(t) |
| 16 | dir := desktopSessionDir(globalWorkspaceRoot()) |
| 17 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | save := func(path, id string, turns int, prefix string) { |
| 21 | t.Helper() |
| 22 | session := agent.NewSession("sys") |
| 23 | for i := range turns { |
| 24 | session.Add(provider.Message{Role: provider.RoleUser, Content: prefix + " question " + strings.Repeat("q", i+1)}) |
| 25 | session.Add(provider.Message{Role: provider.RoleAssistant, Content: prefix + " answer " + strings.Repeat("a", i+1)}) |
| 26 | } |
| 27 | if err := session.Save(path); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | meta := agent.BranchMeta{ID: id, Scope: "global", TopicID: "conversation", TopicTitle: "Compacted"} |
| 31 | if id != "root" { |
| 32 | meta.Recovered = true |
| 33 | meta.ParentID = "root" |
| 34 | meta.RecoveryDepth = 1 |
| 35 | } |
| 36 | if err := agent.SaveBranchMetaPreserveUpdated(path, meta); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | } |
| 40 | root := filepath.Join(dir, "root.jsonl") |
| 41 | leaf := filepath.Join(dir, "leaf.jsonl") |
| 42 | save(root, "root", 9, "root") |
| 43 | save(leaf, "leaf", 72, "compacted") |
| 44 | app := NewApp() |
| 45 | installSessionCatalogForTest(t, app, dir, "global", "") |
| 46 | |
| 47 | topic, ok := app.catalogTopicRecord("global", "", "conversation") |
| 48 | if !ok || topic.RepresentativePath != leaf { |
| 49 | t.Fatalf("representative = %q ok=%v, want compacted leaf %q", topic.RepresentativePath, ok, leaf) |
| 50 | } |
| 51 | if got := app.catalogSessionPathForTopic("global", "", "conversation"); got != leaf { |
| 52 | t.Fatalf("catalog session path = %q, want compacted leaf %q", got, leaf) |
| 53 | } |
| 54 | if got := sessioncatalog.CanonicalSessionPathForTopic(topic.Sessions, root); got != "" { |
| 55 | t.Fatalf("content canonical = %q, want unresolved", got) |
| 56 | } |
| 57 | idleRoot := &WorkspaceTab{ID: "root", Scope: "global", TopicID: "conversation", SessionPath: root} |
| 58 | idleLeaf := &WorkspaceTab{ID: "leaf", Scope: "global", TopicID: "conversation", SessionPath: leaf} |
| 59 | app.tabs = map[string]*WorkspaceTab{"root": idleRoot, "leaf": idleLeaf} |
| 60 | |
| 61 | app.retargetOpenTabsToContinuations() |
| 62 | if idleRoot.SessionPath != leaf { |
| 63 | t.Fatalf("root tab path = %q, want compacted leaf %q", idleRoot.SessionPath, leaf) |
| 64 | } |
| 65 | if idleLeaf.SessionPath != leaf { |
| 66 | t.Fatalf("leaf tab regressed to %q, want %q", idleLeaf.SessionPath, leaf) |
| 67 | } |
| 68 | } |
| 69 |