import { beforeAll, describe, expect, it } from 'vitest' import { importReleaseCheckoutModule, importWorkingTreeModuleCopy, materializeReleaseCheckout } from './release-checkout' // Pin pre-Stopping readers: optional status/mainAgent flags must remain plain working rows to old desktop/mobile clients. // Current clients must not infer Stopping from an old host that sends no flag. const PRE_CHANGE_REF = 'v1.4.214' const SUITE_TIMEOUT_MS = 180_000 const STATUS_FOLD = 'src/shared/structured-agent-session-agent-status.ts' // Both builds load a copy in the checkout cache, out of reach of `mobile/tsconfig.json`. const PHONE_ROW_READER = 'mobile/src/worktree/agent-row-display.ts' const WORKTREE_ID = 'repo::/worktree' const NOW = 1_000_000 type Summary = Record type Row = Record type Fold = (summary: Summary) => { state: string; mainAgent: Record } type HostRowModules = { collectRuntimeWorktreeAgentSources: (args: Record) => unknown attachRuntimeWorktreeAgentRows: (args: Record) => void } type Build = { fold: Fold agentDotState: (row: Row, now: number) => string agentDisplayLabel: (row: Row, now: number) => string host: HostRowModules } /** A working session's summary as the new host publishes it, JSON round-tripped. */ const STOPPING_SUMMARY: Summary = JSON.parse( JSON.stringify({ sessionId: 'session-1', workspaceId: WORKTREE_ID, agent: 'claude', status: 'working', latestPrompt: 'ship it', updatedAt: NOW, stopping: true }) ) /** A host's `worktree ps` rows for one structured session, JSON round-tripped. */ function publishRow(host: HostRowModules, mainAgent: Record): Row { const summary: Record = { worktreeId: WORKTREE_ID, status: 'inactive', hasHostSidebarActivity: false, agents: [] } const snapshot = { paneKey: 'tab-1:11111111-1111-4111-8111-111111111111', tabId: 'tab-1', worktreeId: WORKTREE_ID, connectionId: null, structuredHost: 'owned', state: 'working', prompt: 'ship it', lastAssistantMessage: 'running the tests', agentType: 'claude', receivedAt: NOW - 1_000, stateStartedAt: NOW - 60_000, mainAgent } const summaries = new Map([[WORKTREE_ID, summary]]) host.attachRuntimeWorktreeAgentRows({ summaries, pathIndex: { byPath: new Map(), byRealPath: new Map() }, missingWorktreeIds: new Set(), // Builds read workingTerminalEvidenceByWorktreeId, then terminalTitleEvidenceByWorktreeId, now terminalTitleEvidenceByRow. workingTerminalEvidenceByWorktreeId: new Map(), terminalTitleEvidenceByWorktreeId: new Map(), terminalTitleEvidenceByRow: new Map(), rowSources: host.collectRuntimeWorktreeAgentSources({ hookSnapshots: [snapshot], mirroredWorktreeIdByTabId: new Map(), connectedPtyEvidence: { tabIds: new Set(), paneKeys: new Set(), ptyIdByTerminalHandle: new Map() } }), orchestrationByPaneKey: null, // Old builds pass (summaries, pathIndex, missingIds, id), then (id), now (id, hostId). getSummary: (...args: unknown[]) => summaries.get(String(args.find((arg) => arg === WORKTREE_ID))) ?? null }) // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: JSON.parse of the rows the host just attached, which are plain objects. const [row] = JSON.parse(JSON.stringify(summary.agents)) as Row[] if (!row) { throw new Error('expected one row') } return row } async function loadBuild(ref: string | null): Promise { if (ref === null) { const [fold, display, sources, rows] = await Promise.all([ import('../../../src/shared/agent-session/view/structured-agent-session-agent-status'), importWorkingTreeModuleCopy(PHONE_ROW_READER), import('../../../src/main/runtime/workspaces/process-listing/runtime-worktree-agent-sources'), import('../../../src/main/runtime/workspaces/process-listing/runtime-worktree-agent-rows') ]) return { // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: this spec drives both builds' fold through one untyped surface. fold: fold.structuredAgentSessionAgentStatus as unknown as Fold, // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a dynamic import of a copy is typed unknown; this is the current phone's row reader by path. agentDotState: display.agentDotState as unknown as Build['agentDotState'], // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: as above, the current phone's row label. agentDisplayLabel: display.agentDisplayLabel as unknown as Build['agentDisplayLabel'], // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a dynamic import is typed by its module; this spec drives both builds through one untyped surface. host: { ...sources, ...rows } as unknown as HostRowModules } } const checkout = await materializeReleaseCheckout(ref) const [fold, display, sources, rows] = await Promise.all([ importReleaseCheckoutModule(checkout, STATUS_FOLD), importReleaseCheckoutModule(checkout, PHONE_ROW_READER), importReleaseCheckoutModule( checkout, 'src/main/runtime/workspaces/process-listing/runtime-worktree-agent-sources.ts' ), importReleaseCheckoutModule( checkout, 'src/main/runtime/workspaces/process-listing/runtime-worktree-agent-rows.ts' ) ]) return { // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a dynamic import is typed unknown; this is the old build's summary fold by path. fold: fold.structuredAgentSessionAgentStatus as unknown as Fold, // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a dynamic import is typed unknown; this is the old phone's row reader by path. agentDotState: display.agentDotState as unknown as Build['agentDotState'], // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: as above, the old phone's row label. agentDisplayLabel: display.agentDisplayLabel as unknown as Build['agentDisplayLabel'], // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a dynamic import is typed unknown; these two modules are the old host's row surface by path. host: { ...sources, ...rows } as unknown as HostRowModules } } let oldBuild: Build let newBuild: Build beforeAll(async () => { ;[oldBuild, newBuild] = await Promise.all([loadBuild(PRE_CHANGE_REF), loadBuild(null)]) }, SUITE_TIMEOUT_MS) describe('cross-version Stopping', () => { it('an OLD desktop folds a new host summary into the plain working row it always drew', () => { expect(oldBuild.fold(STOPPING_SUMMARY)).toEqual({ state: 'working', mainAgent: { state: 'working' } }) }) it("an OLD phone reads a new host's row as working, with its usual line", () => { const row = publishRow(newBuild.host, { state: 'working', stopping: true, stateStartedAt: NOW - 60_000 }) expect(row.mainAgent).toMatchObject({ stopping: true }) expect(oldBuild.agentDotState(row, NOW)).toBe('working') expect(oldBuild.agentDisplayLabel(row, NOW)).toBe('running the tests') expect(newBuild.agentDisplayLabel(row, NOW)).toBe('Stopping…') }) it('a NEW reader of an old host never reads Stopping, which that host never publishes', () => { const { stopping: _stopping, ...oldSummary } = STOPPING_SUMMARY expect(newBuild.fold(oldSummary)).toEqual({ state: 'working', mainAgent: { state: 'working' } }) const row = publishRow(oldBuild.host, { state: 'working', stateStartedAt: NOW - 60_000 }) expect(newBuild.agentDotState(row, NOW)).toBe('working') expect(newBuild.agentDisplayLabel(row, NOW)).toBe('running the tests') }) })