| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "slices" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/desktop/internal/workspacestate" |
| 9 | ) |
| 10 | |
| 11 | func TestWorkspaceSourceAliasesKeepReviewedSessionsIndependent(t *testing.T) { |
| 12 | path := filepath.Join(t.TempDir(), "history.jsonl") |
| 13 | key := desktopSourceKey(path, "") |
| 14 | state := workspacestate.State{ |
| 15 | Workspaces: map[string]workspacestate.Workspace{"w": {ID: "w", SessionIDs: []string{"original", "reviewed"}}}, |
| 16 | SourceMappings: map[string]workspacestate.SourceMapping{ |
| 17 | key: {SourceKey: key, Path: path, WorkspaceID: "w", SessionID: "original"}, |
| 18 | key + ":review:v2": {SourceKey: key + ":review:v2", Path: path, WorkspaceID: "w", SessionID: "reviewed"}, |
| 19 | }, |
| 20 | } |
| 21 | aliases := workspaceSourceAliases(state, "w") |
| 22 | if !slices.Contains(aliases["original"], "source\x00local\x00"+key) { |
| 23 | t.Fatal("original source must still resolve to its canonical owner") |
| 24 | } |
| 25 | if !slices.Contains(aliases["reviewed"], "source\x00local\x00"+key+":review:v2") { |
| 26 | t.Fatal("reviewed source must retain its versioned identity") |
| 27 | } |
| 28 | for _, alias := range aliases["original"] { |
| 29 | if slices.Contains(aliases["reviewed"], alias) { |
| 30 | t.Fatalf("independent sessions share a sidebar identity: %q", alias) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestHistoricalVersionSourceKeyRequiresSourceAndVersion(t *testing.T) { |
| 36 | for key, want := range map[string]bool{ |
| 37 | "abc:review:v2": true, |
| 38 | "abc": false, |
| 39 | "abc:review:": false, |
| 40 | ":review:v2": false, |
| 41 | } { |
| 42 | if got := historicalVersionSourceKey(key); got != want { |
| 43 | t.Errorf("historicalVersionSourceKey(%q) = %v, want %v", key, got, want) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestWorkspaceSourceAliasesRejectAmbiguousPathOwnership(t *testing.T) { |
| 49 | path := filepath.Join(t.TempDir(), "history.jsonl") |
| 50 | state := workspacestate.State{SourceMappings: map[string]workspacestate.SourceMapping{ |
| 51 | "old-spelling": {SourceKey: "old-spelling", Path: path, WorkspaceID: "w", SessionID: "first"}, |
| 52 | "new-spelling": {SourceKey: "new-spelling", Path: path, WorkspaceID: "w", SessionID: "second"}, |
| 53 | }} |
| 54 | aliases := workspaceSourceAliases(state, "w") |
| 55 | for _, alias := range aliases["first"] { |
| 56 | if slices.Contains(aliases["second"], alias) { |
| 57 | t.Fatalf("ambiguous normalized source was assigned to two sessions: %q", alias) |
| 58 | } |
| 59 | } |
| 60 | if !slices.Contains(aliases["first"], "source\x00local\x00old-spelling") || !slices.Contains(aliases["second"], "source\x00local\x00new-spelling") { |
| 61 | t.Fatal("exact durable receipts must remain independently addressable") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestWorkspacePlaceholderRuntimeIdentity(t *testing.T) { |
| 66 | isolateDesktopUserDirs(t) |
| 67 | a := NewApp() |
| 68 | root := t.TempDir() |
| 69 | nodes := a.projectTreeRuntimeTopics([]catalogRuntimeSnapshot{{ |
| 70 | tabID: "starting", scope: "project", workspaceRoot: root, topicID: "draft", open: true, |
| 71 | }}) |
| 72 | if len(nodes) != 1 || !slices.Contains(nodes[0].Node.IdentityAliases, "topic\x00draft") { |
| 73 | t.Fatalf("startup must identify its owned placeholder: %+v", nodes) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestWorkspaceCanonicalPlaceholderAliasIsUnambiguous(t *testing.T) { |
| 78 | state := workspacestate.State{ |
| 79 | Workspaces: map[string]workspacestate.Workspace{"w": {ID: "w", SessionIDs: []string{"first"}}}, |
| 80 | Presentation: map[string]workspacestate.Presentation{"first": {TopicID: "draft"}, "branch": {TopicID: "draft"}}, |
| 81 | } |
| 82 | if !slices.Contains(sourceAliases(state, "w", "first"), "topic\x00draft") { |
| 83 | t.Fatal("the first durable session must retire its placeholder") |
| 84 | } |
| 85 | w := state.Workspaces["w"] |
| 86 | w.SessionIDs = append(w.SessionIDs, "branch") |
| 87 | state.Workspaces["w"] = w |
| 88 | for _, id := range w.SessionIDs { |
| 89 | if slices.Contains(sourceAliases(state, "w", id), "topic\x00draft") { |
| 90 | t.Fatal("a shared topic is not a unique session alias") |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 |