import { afterEach, describe, expect, it, vi } from "bun:test"; import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import type { AssistantMessage, UserMessage } from "@sayknow-cli/ai"; import { exportFromFile, exportSessionToHtml } from "@sayknow-cli/coding-agent/export/html"; import { BlobStore, EphemeralBlobStore, externalizeImageDataSync } from "@sayknow-cli/coding-agent/session/blob-store"; import { SessionManager, type SessionMessageEntry } from "@sayknow-cli/coding-agent/session/session-manager"; const tempDirs: string[] = []; afterEach(async () => { vi.restoreAllMocks(); for (const dir of tempDirs.splice(0)) await fs.promises.rm(dir, { recursive: true, force: true }); }); function makeTempDir(): string { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "skc-resident-cache-")); tempDirs.push(dir); return dir; } function assistantMessage(text: string): AssistantMessage { return { role: "assistant" as const, content: [{ type: "text" as const, text }], api: "anthropic-messages" as const, provider: "anthropic" as const, model: "test-model", stopReason: "stop", usage: { input: 1, output: 1, cacheRead: 0, cacheWrite: 0, totalTokens: 2, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, }, timestamp: Date.now(), }; } function firstMessageEntry(sm: SessionManager): SessionMessageEntry { const entry = sm.getEntries().find((e): e is SessionMessageEntry => e.type === "message"); if (!entry) throw new Error("Expected message entry"); return entry; } function residentCacheRoot(sm: SessionManager): string { const artifactsDir = sm.getArtifactsDir(); if (!artifactsDir) throw new Error("Expected artifacts dir"); return path.join(artifactsDir, "resident-cache"); } function residentCacheDirs(sm: SessionManager): string[] { const root = residentCacheRoot(sm); return fs.existsSync(root) ? fs.readdirSync(root).map(name => path.join(root, name)) : []; } function activeResidentCacheDir(sm: SessionManager): string { const dirs = residentCacheDirs(sm).filter(dir => path.basename(dir).startsWith(sm.getSessionId())); if (dirs.length !== 1) throw new Error(`Expected one active resident cache dir, got ${dirs.length}`); return dirs[0]!; } async function createPersistedLargeTextSession( text: string, ): Promise<{ sm: SessionManager; sessionFile: string; artifactsDir: string; cacheDir: string; entryId: string }> { const root = makeTempDir(); const sm = SessionManager.create(root, path.join(root, "sessions")); const entryId = sm.appendMessage(assistantMessage(text)); await sm.ensureOnDisk(); await sm.flush(); const sessionFile = sm.getSessionFile(); const artifactsDir = sm.getArtifactsDir(); if (!sessionFile || !artifactsDir) throw new Error("Expected persisted session paths"); const cacheDir = activeResidentCacheDir(sm); expect(fs.existsSync(cacheDir)).toBe(true); return { sm, sessionFile, artifactsDir, cacheDir, entryId }; } function expectResidentPlaceholder(value: unknown): void { const serialized = JSON.stringify(value); expect(serialized).toContain("Session resident text blob missing"); expect(serialized).toContain("original content unavailable"); expect(serialized).not.toContain("blob:sha256:"); expect(serialized).not.toContain("__skcResidentBlob"); } async function expectFileContainsResidentPlaceholder(filePath: string): Promise { const bytes = await Bun.file(filePath).text(); const match = bytes.match(/