| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/config" |
| 11 | ) |
| 12 | |
| 13 | func TestRestoreGlobalTopicSessionReindexesProjectTree(t *testing.T) { |
| 14 | isolateDesktopUserDirs(t) |
| 15 | |
| 16 | dir := config.SessionDir() |
| 17 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 18 | t.Fatalf("mkdir sessions: %v", err) |
| 19 | } |
| 20 | sessionPath := writeLegacySession(t, dir, "restore-global.jsonl", "restore global history", time.Now().Add(-time.Hour)) |
| 21 | topicID := legacySessionTopicID(sessionPath) |
| 22 | app := NewApp() |
| 23 | |
| 24 | nodes := waitForCatalogTopic(t, app, "global", "", topicID) |
| 25 | if len(nodes) != 1 || len(nodes[0].Children) != 1 || nodes[0].Children[0].TopicID != topicID { |
| 26 | t.Fatalf("legacy session should start in Global, got %#v", nodes) |
| 27 | } |
| 28 | // Catalog publication does not fence the legacy branch-meta migration. |
| 29 | // Wait for the sidecar postcondition before moving it so Windows does not |
| 30 | // race the migration writer's exclusive file handle. |
| 31 | waitFor(t, "restore-global.jsonl.meta to carry the migrated topic", func() bool { |
| 32 | meta, ok, err := agent.LoadBranchMeta(sessionPath) |
| 33 | return err == nil && ok && strings.TrimSpace(meta.TopicID) == topicID |
| 34 | }) |
| 35 | if err := app.TrashTopic(topicID); err != nil { |
| 36 | t.Fatalf("trash global topic: %v", err) |
| 37 | } |
| 38 | ref := assertLegacyLifecycle(t, app, sessionPath, "archived") |
| 39 | |
| 40 | if err := app.RestoreCanonicalSession(ref); err != nil { |
| 41 | t.Fatalf("restore global session: %v", err) |
| 42 | } |
| 43 | if got := app.ListTrashedSessions(); len(got) != 0 { |
| 44 | t.Fatalf("trash should be empty after restore, got %#v", got) |
| 45 | } |
| 46 | nodes = waitForCatalogTopic(t, app, "global", "", topicID) |
| 47 | if len(nodes) != 1 || nodes[0].Kind != "global_folder" || len(nodes[0].Children) != 1 || nodes[0].Children[0].TopicID != topicID { |
| 48 | t.Fatalf("restored global session should reappear in Global, got %#v", nodes) |
| 49 | } |
| 50 | } |
| 51 |