import { existsSync, mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { expect, it } from "vitest"; import { readVoiceHistory, VoiceHistory } from "../src/voice-history.ts"; it("retains the complete voice transcript when native Pi compacts model context", async () => { const root = mkdtempSync(join(tmpdir(), "voice-history-")); try { const history = new VoiceHistory(root, root, "main"); history.sessionManager.appendMessage({ role: "user", content: "earlier context", timestamp: Date.now(), }); history.update("original-user", "user", "The original spoken instruction."); history.update( "original-reply", "assistant", "The complete original generated reply.", ); history.markPlayback( "original-reply", "The complete original generated reply.", ); const kept = history.sessionManager.appendMessage({ role: "user", content: "later context", timestamp: Date.now(), }); history.sessionManager.appendCompaction( "Native Pi model-context summary.", kept, 1234, ); const resumed = new VoiceHistory(root, root, "main"); expect(await resumed.loadAll()).toMatchObject([ { id: "original-user", text: "The original spoken instruction." }, { id: "original-reply", text: "The complete original generated reply.", playback: "played", }, ]); } finally { rmSync(root, { recursive: true, force: true }); } }); it("resumes SDK history by main identity, isolates forks, preserves generated versus played interruption", () => { const root = mkdtempSync(join(tmpdir(), "voice-history-")); try { const history = new VoiceHistory(root, root, "main"); history.sessionManager.appendMessage({ role: "user", content: "hello", timestamp: Date.now(), }); history.update("reply", "assistant", "Hello, more words.", true); history.markPlayback("reply", "Hello"); const resumed = new VoiceHistory(root, root, "main"); expect(resumed.sessionManager.getSessionId()).toBe( history.sessionManager.getSessionId(), ); expect(resumed.snapshot()).toEqual([ { id: "reply", role: "assistant", text: "Hello, more words.", playedText: "Hello", interrupted: true, playback: "partial", }, ]); const fork = new VoiceHistory(root, root, "fork"); expect(fork.sessionManager.getSessionId()).not.toBe( history.sessionManager.getSessionId(), ); expect(fork.snapshot()).toEqual([]); resumed.markPlayback("reply", "Hi"); expect(resumed.snapshot()[0]?.playedText).toBe("Hello"); } finally { rmSync(root, { recursive: true, force: true }); } }); it("reads persisted history without audio startup or creating a missing scope", async () => { const root = mkdtempSync(join(tmpdir(), "voice-history-")); try { const history = new VoiceHistory(root, root, "main"); history.sessionManager.appendMessage({ role: "user", content: "hello", timestamp: Date.now(), }); history.update("reply", "assistant", "Generated but not played"); expect(await readVoiceHistory(root, root, "main")).toMatchObject([ { text: "Generated but not played", playback: "not-played" }, ]); const missing = join(root, "missing"); expect(await readVoiceHistory(root, missing, "new-session")).toEqual([]); expect(existsSync(missing)).toBe(false); } finally { rmSync(root, { recursive: true, force: true }); } }); it("does not append a growing history record for every generated token", () => { const root = mkdtempSync(join(tmpdir(), "voice-history-")); try { const history = new VoiceHistory(root, root, "main"); const before = history.sessionManager.getEntries().length; for (let i = 1; i <= 1000; i++) history.update("reply", "assistant", "x".repeat(i), false, false); expect(history.sessionManager.getEntries()).toHaveLength(before); history.update("reply", "assistant", "x".repeat(1000)); expect(history.sessionManager.getEntries()).toHaveLength(before + 1); } finally { rmSync(root, { recursive: true, force: true }); } }); it("bounds live pages but loads the complete archive on demand", async () => { const root = mkdtempSync(join(tmpdir(), "voice-history-")); try { const history = new VoiceHistory(root, root, "main"); for (let i = 0; i < 170; i++) history.update(String(i), "assistant", "x".repeat(13_000)); expect(history.snapshot()).toHaveLength(160); expect(history.snapshot()[0]?.text).toHaveLength(12_000); expect(history.snapshot()[0]?.playback).toBe("not-played"); expect(await history.loadAll()).toHaveLength(170); } finally { rmSync(root, { recursive: true, force: true }); } });