| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/desktop/internal/workspacestate" |
| 9 | "reasonix/internal/agent" |
| 10 | "reasonix/internal/session" |
| 11 | ) |
| 12 | |
| 13 | func TestHistoricalReceiptResolutionPreservesLiveOwners(t *testing.T) { |
| 14 | for _, retired := range []int{0, 1, 2} { |
| 15 | t.Run(string(rune('0'+retired)), func(t *testing.T) { |
| 16 | app, selector, _ := historicalArchiveFixture(t) |
| 17 | first, err := app.ImportHistoricalSession(selector.Source.SourceKey) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | second, err := app.desktopSessionService("").Create(t.Context(), session.CreateOptions{}) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | ids := []string{first.Session.SessionID, second.Ref().SessionID} |
| 26 | if err := app.workspaceRegistry().AttachSession(t.Context(), "", workspacestate.GlobalWorkspaceID, ids[1], ""); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | rewriteHistoricalRegistry(t, app, func(state *workspacestate.State) { state.SourceMappings = map[string]workspacestate.SourceMapping{} }) |
| 30 | for i := range retired { |
| 31 | if err := app.workspaceRegistry().ArchiveSession(t.Context(), ids[i]); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | state, err := app.workspaceRegistry().Load(t.Context()) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if err := app.workspaceRegistry().BeginPurge(t.Context(), ids[i], state.Generation); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | } |
| 42 | receipts := []desktopMigrationReceipt{{TargetSessionID: ids[0], ContentDigest: "same"}, {TargetSessionID: ids[1], ContentDigest: "same"}} |
| 43 | target, err := app.selectCanonicalConversionTarget(t.Context(), desktopMigrationSource{scope: "global"}, selector.Source.Path, "same", receipts) |
| 44 | if retired == 0 { |
| 45 | if !errors.Is(err, workspacestate.ErrMutationConflict) { |
| 46 | t.Fatalf("picked competing live owner %s: %v", target, err) |
| 47 | } |
| 48 | } else if err != nil || target == "" || retired == 1 && target != ids[1] { |
| 49 | t.Fatalf("unique owner not recovered: %s %v", target, err) |
| 50 | } |
| 51 | }) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestHistoricalGlobalSourceIgnoresRemovedProjectMetadata(t *testing.T) { |
| 56 | isolateDesktopUserDirs(t) |
| 57 | path, _, head := migrationSingleDAGFixture(t) |
| 58 | missing := filepath.Join(t.TempDir(), "removed-project") |
| 59 | if err := agent.SaveBranchMetaPreserveUpdated(path, agent.BranchMeta{Scope: "project", WorkspaceRoot: missing}); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | app := newHistoricalLifecycleApp(t) |
| 63 | for _, selectedHead := range []string{"", head} { |
| 64 | selector := SessionSelector{Source: &SessionSourceRef{Path: path, HeadID: selectedHead}} |
| 65 | target, err := app.resolveSourceSessionTarget(selector, false) |
| 66 | if err != nil || target.Scope != "global" || target.Source == nil { |
| 67 | t.Fatalf("stale project blocks global history: %+v %v", target, err) |
| 68 | } |
| 69 | } |
| 70 | local := recoverHistoricalRuntimeOwner(SessionTarget{Scope: "project", WorkspaceRoot: missing, Source: &SessionSourceRef{Path: filepath.Join(missing, ".reasonix", "sessions", "history.jsonl")}}) |
| 71 | if local.Scope != "project" { |
| 72 | t.Fatal("project source silently moved to another workspace") |
| 73 | } |
| 74 | } |
| 75 |