import assert from 'node:assert/strict'; import { test } from 'node:test'; // @ts-ignore -- built-in view cores are plain portable modules without .d.ts files. import core from '../core.mjs'; const nodes = [ { nodeId: 'repo-root', name: 'repo root', kind: 'general', mode: 'base', lifecycle: 'terminal', status: 'active', cwd: '/repo', parent: null, created: '2026-07-12T00:00:00.000Z' }, // Spawn provenance deliberately stays after unsubscribe; topology must not read it. { nodeId: 'worktree-child', name: 'worktree child', kind: 'developer', mode: 'base', lifecycle: 'terminal', status: 'active', cwd: '/repo/.crouter/worktrees/child', parent: 'repo-root', created: '2026-07-12T00:01:00.000Z' }, { nodeId: 'new-subscription', name: 'new subscription', kind: 'developer', mode: 'base', lifecycle: 'terminal', status: 'active', cwd: '/repo/.crouter/worktrees/new', parent: null, created: '2026-07-12T00:02:00.000Z' }, { nodeId: 'repo-other', name: 'repo elsewhere', kind: 'general', mode: 'base', lifecycle: 'terminal', status: 'idle', cwd: '/repo', parent: null, created: '2026-07-12T00:03:00.000Z' }, { nodeId: 'other-root', name: 'other cwd root', kind: 'general', mode: 'base', lifecycle: 'terminal', status: 'active', cwd: '/elsewhere', parent: null, created: '2026-07-12T00:04:00.000Z' }, ]; function refreshWith(subscriptions: Record) { let state: any = core.init({}); const ctx: any = { get state() { return state; }, set(next: unknown) { state = typeof next === 'function' ? (next as (prev: unknown) => unknown)(state) : next; }, async resolve(source: unknown) { if (source === core.sources.panesSource) return { ok: true, data: [{ pane: '%1', node: 'repo-root' }] }; if (source === core.sources.cwdSource) return { ok: true, data: '/repo' }; if (source === core.sources.nodesSource) return { ok: true, data: nodes }; if (source === core.sources.subscriptionsSource) return { ok: true, data: new Map(Object.entries(subscriptions)) }; if (source === core.sources.attentionSource) return { ok: true, data: {} }; throw new Error('unexpected source'); }, signal: { clearBanner() {}, quit() {}, setBanner() {}, setMode() {}, setStatus() {}, setSubtitle() {} }, }; return core.intents.refresh(ctx).then(() => state); } const rowIds = (state: any): string[] => state.rows.filter((row: any) => row.kind === 'node').map((row: any) => row.nodeId); test('refresh follows the live subscription graph across worktree cwd boundaries and keeps elsewhere cwd-scoped', async () => { const state = await refreshWith({ 'repo-root': ['worktree-child'] }); assert.deepEqual(rowIds(state), ['repo-root', 'worktree-child', 'repo-other']); assert.equal(state.currentRoot, 'repo-root'); assert.equal(state.nodesHere, 2); assert.ok(!rowIds(state).includes('other-root')); }); test('refresh removes unsubscribed spawn descendants and adds new subscriptions from the authoritative spine', async () => { const unsubscribed = await refreshWith({}); assert.deepEqual(rowIds(unsubscribed), ['repo-root', 'repo-other']); const subscribed = await refreshWith({ 'repo-root': ['new-subscription'] }); assert.deepEqual(rowIds(subscribed), ['repo-root', 'new-subscription', 'repo-other']); });