|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { |
| 4 | + applyWorkspaceRuntimeStateEvent, |
| 5 | + buildWorkbenchStateFromBootstrap, |
| 6 | +} from "../apps/web/src/shared/utils/workspace.ts"; |
| 7 | +import { createDefaultWorkbenchState } from "../apps/web/src/state/workbench-core.ts"; |
| 8 | +import { |
| 9 | + resetWorkspaceViewBaselines, |
| 10 | + shouldPersistWorkspaceView, |
| 11 | +} from "../apps/web/src/features/workspace/workspace-view-persistence.ts"; |
| 12 | + |
| 13 | +const APP_SETTINGS = { |
| 14 | + agentProvider: "claude" as const, |
| 15 | + agentCommand: "claude", |
| 16 | + idlePolicy: { |
| 17 | + enabled: true, |
| 18 | + idleMinutes: 10, |
| 19 | + maxActive: 3, |
| 20 | + pressure: true, |
| 21 | + }, |
| 22 | + completionNotifications: { |
| 23 | + enabled: true, |
| 24 | + onlyWhenBackground: true, |
| 25 | + }, |
| 26 | + terminalCompatibilityMode: "standard" as const, |
| 27 | +}; |
| 28 | + |
| 29 | +const createWorkspaceSnapshot = (activeSessionId: string) => ({ |
| 30 | + workspace: { |
| 31 | + workspace_id: "ws-1", |
| 32 | + title: "Workspace 1", |
| 33 | + project_path: "/tmp/ws-1", |
| 34 | + source_kind: "local" as const, |
| 35 | + source_value: "/tmp/ws-1", |
| 36 | + git_url: null, |
| 37 | + target: { type: "native" as const }, |
| 38 | + idle_policy: { |
| 39 | + enabled: true, |
| 40 | + idle_minutes: 10, |
| 41 | + max_active: 3, |
| 42 | + pressure: true, |
| 43 | + }, |
| 44 | + }, |
| 45 | + sessions: [ |
| 46 | + { |
| 47 | + id: 1, |
| 48 | + title: "Session 1", |
| 49 | + status: "idle" as const, |
| 50 | + mode: "branch" as const, |
| 51 | + auto_feed: true, |
| 52 | + queue: [], |
| 53 | + messages: [], |
| 54 | + stream: "", |
| 55 | + unread: 0, |
| 56 | + last_active_at: 1, |
| 57 | + claude_session_id: null, |
| 58 | + }, |
| 59 | + { |
| 60 | + id: 2, |
| 61 | + title: "Session 2", |
| 62 | + status: "waiting" as const, |
| 63 | + mode: "branch" as const, |
| 64 | + auto_feed: true, |
| 65 | + queue: [], |
| 66 | + messages: [], |
| 67 | + stream: "", |
| 68 | + unread: 0, |
| 69 | + last_active_at: 2, |
| 70 | + claude_session_id: null, |
| 71 | + }, |
| 72 | + ], |
| 73 | + archive: [], |
| 74 | + view_state: { |
| 75 | + active_session_id: activeSessionId, |
| 76 | + active_pane_id: `pane-${activeSessionId}`, |
| 77 | + active_terminal_id: "", |
| 78 | + pane_layout: { |
| 79 | + type: "leaf" as const, |
| 80 | + id: `pane-${activeSessionId}`, |
| 81 | + sessionId: activeSessionId, |
| 82 | + }, |
| 83 | + file_preview: { |
| 84 | + path: "", |
| 85 | + content: "", |
| 86 | + mode: "preview" as const, |
| 87 | + originalContent: "", |
| 88 | + modifiedContent: "", |
| 89 | + dirty: false, |
| 90 | + }, |
| 91 | + }, |
| 92 | + terminals: [], |
| 93 | +}); |
| 94 | + |
| 95 | +test.afterEach(() => { |
| 96 | + resetWorkspaceViewBaselines(); |
| 97 | +}); |
| 98 | + |
| 99 | +test("buildWorkbenchStateFromBootstrap seeds workspace view persistence baselines for hydrated tabs", () => { |
| 100 | + const state = buildWorkbenchStateFromBootstrap( |
| 101 | + createDefaultWorkbenchState(), |
| 102 | + { |
| 103 | + ui_state: { |
| 104 | + open_workspace_ids: ["ws-1"], |
| 105 | + active_workspace_id: "ws-1", |
| 106 | + layout: { |
| 107 | + left_width: 320, |
| 108 | + right_width: 320, |
| 109 | + right_split: 64, |
| 110 | + show_code_panel: false, |
| 111 | + show_terminal_panel: false, |
| 112 | + }, |
| 113 | + }, |
| 114 | + workspaces: [createWorkspaceSnapshot("2")], |
| 115 | + }, |
| 116 | + "en", |
| 117 | + APP_SETTINGS, |
| 118 | + ); |
| 119 | + |
| 120 | + const tab = state.tabs[0]; |
| 121 | + assert.equal(shouldPersistWorkspaceView(tab), false); |
| 122 | + assert.equal(shouldPersistWorkspaceView({ ...tab, activeSessionId: "1" }), true); |
| 123 | +}); |
| 124 | + |
| 125 | +test("runtime state events refresh workspace view persistence baselines", () => { |
| 126 | + const initial = buildWorkbenchStateFromBootstrap( |
| 127 | + createDefaultWorkbenchState(), |
| 128 | + { |
| 129 | + ui_state: { |
| 130 | + open_workspace_ids: ["ws-1"], |
| 131 | + active_workspace_id: "ws-1", |
| 132 | + layout: { |
| 133 | + left_width: 320, |
| 134 | + right_width: 320, |
| 135 | + right_split: 64, |
| 136 | + show_code_panel: false, |
| 137 | + show_terminal_panel: false, |
| 138 | + }, |
| 139 | + }, |
| 140 | + workspaces: [createWorkspaceSnapshot("2")], |
| 141 | + }, |
| 142 | + "en", |
| 143 | + APP_SETTINGS, |
| 144 | + ); |
| 145 | + |
| 146 | + const next = applyWorkspaceRuntimeStateEvent(initial, { |
| 147 | + workspace_id: "ws-1", |
| 148 | + view_state: { |
| 149 | + active_session_id: "1", |
| 150 | + active_pane_id: "pane-1", |
| 151 | + active_terminal_id: "", |
| 152 | + pane_layout: { |
| 153 | + type: "leaf", |
| 154 | + id: "pane-1", |
| 155 | + sessionId: "1", |
| 156 | + }, |
| 157 | + file_preview: { |
| 158 | + path: "", |
| 159 | + content: "", |
| 160 | + mode: "preview", |
| 161 | + originalContent: "", |
| 162 | + modifiedContent: "", |
| 163 | + dirty: false, |
| 164 | + }, |
| 165 | + }, |
| 166 | + }); |
| 167 | + |
| 168 | + const tab = next.tabs[0]; |
| 169 | + assert.equal(shouldPersistWorkspaceView(tab), false); |
| 170 | + assert.equal(shouldPersistWorkspaceView({ ...tab, activePaneId: "pane-2" }), true); |
| 171 | +}); |
0 commit comments