| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "slices" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/provider" |
| 11 | "reasonix/internal/session" |
| 12 | ) |
| 13 | |
| 14 | func TestMigratedSingleHeadArchiveDoesNotResurrectLegacyRow(t *testing.T) { |
| 15 | isolateDesktopUserDirs(t) |
| 16 | path, _, head := migrationSingleDAGFixture(t) |
| 17 | before, err := desktopSourceFingerprint(path) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | app := NewApp() |
| 22 | app.ctx = t.Context() |
| 23 | pinDesktopSessionRoot(t, app) |
| 24 | installNoopRuntimeEvents(app) |
| 25 | t.Cleanup(app.closeSessionServices) |
| 26 | if err := app.migrateDesktopSessionsV5(t.Context()); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | installSessionCatalogForTest(t, app, filepath.Dir(path), "global", "") |
| 30 | state, err := app.workspaceRegistry().Load(t.Context()) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | mapping, ok := state.SourceMappings[desktopSourceKey(path, head)] |
| 35 | if !ok { |
| 36 | t.Fatal("upgrade did not record the historical head") |
| 37 | } |
| 38 | ref := session.SessionRef{HostID: localDesktopHostID, SessionID: mapping.SessionID} |
| 39 | if aliases := sourceAliases(state, mapping.WorkspaceID, mapping.SessionID); !slices.Contains(aliases, "path\x00"+path) { |
| 40 | t.Fatalf("canonical identity is missing its displayed path alias: %q", aliases) |
| 41 | } |
| 42 | assertLists := func(want int) { |
| 43 | t.Helper() |
| 44 | page, err := app.ListProjectTopics(ProjectTopicPageRequest{Scope: "global", Limit: 50}) |
| 45 | if err != nil || len(page.Items) != want { |
| 46 | t.Errorf("sidebar = %+v, %v; want %d rows", page.Items, err, want) |
| 47 | } |
| 48 | if rows := app.listSessionsFromDir(filepath.Dir(path), ""); len(rows) != want { |
| 49 | t.Errorf("history = %+v; want %d rows", rows, want) |
| 50 | } |
| 51 | } |
| 52 | assertLists(1) |
| 53 | if result, err := app.ArchiveSessionTarget(SessionSelector{SessionPath: path}); err != nil || !result.Committed { |
| 54 | t.Fatalf("archive historical path = %+v, %v", result, err) |
| 55 | } |
| 56 | assertLists(0) |
| 57 | root := app.desktopSessions.root |
| 58 | app.stopSessionCatalog(time.Second) |
| 59 | app.closeSessionServices() |
| 60 | app = NewApp() |
| 61 | app.ctx = t.Context() |
| 62 | app.desktopSessions.root = root |
| 63 | installNoopRuntimeEvents(app) |
| 64 | t.Cleanup(app.closeSessionServices) |
| 65 | if err := app.migrateDesktopSessionsV5(t.Context()); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | installSessionCatalogForTest(t, app, filepath.Dir(path), "global", "") |
| 69 | assertLists(0) |
| 70 | if result, err := app.RestoreSessionTarget(SessionSelector{Ref: &ref}); err != nil || !result.Committed { |
| 71 | t.Fatalf("restore = %+v, %v", result, err) |
| 72 | } |
| 73 | assertLists(1) |
| 74 | if after, err := desktopSourceFingerprint(path); err != nil || after != before { |
| 75 | t.Fatalf("lifecycle changed retained history: %v", err) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestArchivedHistoricalHeadDoesNotHideUnadoptedSibling(t *testing.T) { |
| 80 | isolateDesktopUserDirs(t) |
| 81 | path, legacy, _ := migrationSingleDAGFixture(t) |
| 82 | head, err := legacy.ForkHead(path, legacy.Snapshot()[1].ID, agent.HeadKindFork, "sibling") |
| 83 | if err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | legacy.Add(provider.Message{Role: provider.RoleAssistant, Content: "sibling answer"}) |
| 87 | if err := legacy.Save(path); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | app := NewApp() |
| 91 | app.ctx = t.Context() |
| 92 | pinDesktopSessionRoot(t, app) |
| 93 | installNoopRuntimeEvents(app) |
| 94 | t.Cleanup(app.closeSessionServices) |
| 95 | installSessionCatalogForTest(t, app, filepath.Dir(path), "global", "") |
| 96 | selector := SessionSelector{Source: &SessionSourceRef{HostID: localDesktopHostID, Path: path, HeadID: head}} |
| 97 | if result, err := app.ArchiveSessionTarget(selector); err != nil || !result.Committed { |
| 98 | t.Fatalf("archive one head = %+v, %v", result, err) |
| 99 | } |
| 100 | page, err := app.ListProjectTopics(ProjectTopicPageRequest{Scope: "global", Limit: 50}) |
| 101 | if err != nil || len(page.Items) != 1 || page.Items[0].Source == nil || page.Items[0].Source.HeadID == head { |
| 102 | t.Fatalf("unadopted sidebar sibling = %+v, %v", page.Items, err) |
| 103 | } |
| 104 | rows := app.listSessionsFromDir(filepath.Dir(path), "") |
| 105 | if len(rows) != 1 || rows[0].Source == nil || rows[0].Source.HeadID == head { |
| 106 | t.Fatalf("unadopted history sibling = %+v", rows) |
| 107 | } |
| 108 | if result, err := app.RestoreSessionTarget(selector); err != nil || !result.Committed { |
| 109 | t.Fatalf("restore one head = %+v, %v", result, err) |
| 110 | } |
| 111 | page, err = app.ListProjectTopics(ProjectTopicPageRequest{Scope: "global", Limit: 50}) |
| 112 | if err != nil || len(page.Items) != 2 { |
| 113 | t.Fatalf("restored heads = %+v, %v", page.Items, err) |
| 114 | } |
| 115 | } |
| 116 |