import assert from 'node:assert/strict'; import test from 'node:test'; import React, { act } from 'react'; import { createRoot } from 'react-dom/client'; import { JSDOM } from 'jsdom'; import type { AISession } from '../../infrastructure/ai/types'; import { useAIState } from './useAIState'; import { setLatestAISessionsSnapshot } from './aiStateSnapshots'; // Global session manager (issue #4098 phase 4): exportSessionsBackup with a // session-id subset exports only those chats; without one it exports the // whole store as before (#4018). test('exportSessionsBackup exports a single-chat subset or the whole store', async () => { const dom = new JSDOM('
', { url: 'http://localhost' }); for (const name of ['window', 'document', 'localStorage', 'CustomEvent', 'Event'] as const) { Object.defineProperty(globalThis, name, { configurable: true, value: name === 'window' ? dom.window : dom.window[name] }); } Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', { configurable: true, value: true }); const sessions: AISession[] = [ { id: 'chat-a', title: 'A', agentId: 'catty', scope: { type: 'terminal', targetId: 't-1' }, messages: [], createdAt: 1, updatedAt: 10 }, { id: 'chat-b', title: 'B', agentId: 'codex', scope: { type: 'workspace', targetId: 'w-1' }, messages: [], createdAt: 2, updatedAt: 20 }, ] as AISession[]; // Seed the module snapshot so useAIState boots with the live store. setLatestAISessionsSnapshot(sessions); const exportCalls: string[] = []; (dom.window as unknown as { netcatty: unknown }).netcatty = { aiSessionsBackupExport: async ({ json }: { json: string }) => { exportCalls.push(json); return { success: true, filePath: '/tmp/export.json' }; }, }; let ai: ReturnType