| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "reflect" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/desktop/internal/workspacestate" |
| 9 | "reasonix/internal/session" |
| 10 | ) |
| 11 | |
| 12 | type workspaceInfoProbe struct{ ids []string } |
| 13 | |
| 14 | func (p *workspaceInfoProbe) Stat(_ context.Context, ref session.SessionRef) (session.SessionInfo, error) { |
| 15 | p.ids = append(p.ids, ref.SessionID) |
| 16 | return session.SessionInfo{SessionID: ref.SessionID, MetadataStatus: session.MetadataReady}, nil |
| 17 | } |
| 18 | |
| 19 | func TestWorkspaceMetadataReadsOnlyRegisteredMembers(t *testing.T) { |
| 20 | probe := &workspaceInfoProbe{} |
| 21 | for _, ids := range [][]string{{"a", "b", "a"}, {"c"}, {"d", "e"}} { |
| 22 | if _, err := listWorkspaceSessionInfo(t.Context(), probe, ids); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | } |
| 26 | if !reflect.DeepEqual(probe.ids, []string{"a", "b", "c", "d", "e"}) { |
| 27 | t.Fatalf("repeated or unrelated reads: %v", probe.ids) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestWorkspacePendingMetadataIsNotBlank(t *testing.T) { |
| 32 | for _, status := range []string{session.MetadataPending, session.MetadataFailed} { |
| 33 | row := workspaceSessionRow("global", "old", session.SessionInfo{MetadataStatus: status}, true, false, nil) |
| 34 | if row.Blank { |
| 35 | t.Fatalf("%s metadata was classified as blank", status) |
| 36 | } |
| 37 | } |
| 38 | row := workspaceSessionRow("global", "missing", session.SessionInfo{}, false, false, nil) |
| 39 | if row.Blank { |
| 40 | t.Fatal("missing metadata was classified as blank") |
| 41 | } |
| 42 | row = workspaceSessionRow("global", "empty", session.SessionInfo{MetadataStatus: session.MetadataReady}, true, false, nil) |
| 43 | if !row.Blank { |
| 44 | t.Fatal("ready empty session should be blank") |
| 45 | } |
| 46 | row = workspaceSessionRow("global", "named", session.SessionInfo{MetadataStatus: session.MetadataReady, Title: "Saved title"}, true, false, nil) |
| 47 | if row.Blank { |
| 48 | t.Fatal("named session should not be hidden") |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestCanonicalSessionTopicIdentityUsesTargetPresentation(t *testing.T) { |
| 53 | state := workspacestate.State{Presentation: map[string]workspacestate.Presentation{ |
| 54 | "target": {TopicID: "topic-target", Title: "Target"}, |
| 55 | }} |
| 56 | topicID, title := canonicalSessionTopicIdentity(state, "target") |
| 57 | if topicID != "topic-target" || title != "Target" { |
| 58 | t.Fatalf("identity = %q/%q, want target presentation", topicID, title) |
| 59 | } |
| 60 | topicID, title = canonicalSessionTopicIdentity(state, "missing") |
| 61 | if topicID != "canonical-missing" || title != "" { |
| 62 | t.Fatalf("fallback identity = %q/%q", topicID, title) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestCanonicalBindingUsesRenamedSessionTitleAndRuntimeIdentity(t *testing.T) { |
| 67 | isolateDesktopUserDirs(t) |
| 68 | app := NewApp() |
| 69 | t.Cleanup(app.closeSessionServices) |
| 70 | ws, err := app.ensureDesktopWorkspace(t.Context(), "global", "") |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | runtime, err := app.desktopSessionService("").Create(t.Context(), session.CreateOptions{SessionID: "renamed-binding", Origin: session.SessionOriginNew}) |
| 75 | if err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | ref := runtime.Ref() |
| 79 | if err := app.workspaceRegistry().AttachSession(t.Context(), "", ws, ref.SessionID, ""); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | old := "Old fork title" |
| 83 | if err := app.workspaceRegistry().UpdatePresentation(t.Context(), []string{ref.SessionID}, &old, nil); err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | if _, err := app.RenameSessionTarget(SessionSelector{Ref: &ref}, "Renamed B"); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | state, err := app.workspaceRegistry().Load(t.Context()) |
| 90 | if err != nil { |
| 91 | t.Fatal(err) |
| 92 | } |
| 93 | tab := &WorkspaceTab{ID: "binding", SessionID: ref.SessionID, HistoricalSource: &SessionSourceRef{Path: "/fixture/old.jsonl"}} |
| 94 | app.tabs[tab.ID] = tab |
| 95 | if err := app.commitCanonicalSessionBinding(tab, nil, ref, state.Workspaces[ws], 0); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | if tab.HistoricalSource != nil { |
| 99 | t.Fatal("canonical binding retained the preparation action") |
| 100 | } |
| 101 | if tab.TopicTitle != "Renamed B" { |
| 102 | t.Fatalf("reopened title=%q", tab.TopicTitle) |
| 103 | } |
| 104 | snapshot := app.GetRuntimeStateSnapshot() |
| 105 | if len(snapshot.Topics) != 1 || snapshot.Topics[0].Node.Session == nil || snapshot.Topics[0].Node.Session.SessionID != ref.SessionID { |
| 106 | t.Fatalf("runtime lost canonical identity: %+v", snapshot.Topics) |
| 107 | } |
| 108 | } |
| 109 |