import { mkdtempSync, rmSync, writeFileSync } from 'fs'; import { join } from 'path'; import { tmpdir } from 'os'; import { pathToFileURL } from 'url'; import { describe, expect, it } from 'vitest'; import { PiEmbeddedBackend } from './piEmbedded.js'; function createModule(source: string): { moduleUrl: string; cleanup: () => void } { const dir = mkdtempSync(join(tmpdir(), 'flynn-pi-embedded-')); const file = join(dir, 'module.mjs'); writeFileSync(file, source, 'utf-8'); return { moduleUrl: pathToFileURL(file).href, cleanup: () => rmSync(dir, { recursive: true, force: true }), }; } describe('PiEmbeddedBackend', () => { it('returns text from a createAgentSession/run response object', async () => { const mod = createModule(` export function createAgentSession() { return { run(payload) { return { text: "pi says: " + payload.input }; }, }; } `); try { const backend = new PiEmbeddedBackend({ module: mod.moduleUrl, timeoutMs: 2000 }); const result = await backend.process({ prompt: 'hello', history: [] }); expect(result).toBe('pi says: hello'); } finally { mod.cleanup(); } }); it('falls back to string payload when object payload is rejected', async () => { const mod = createModule(` export function createAgentSession() { return { run(payload) { if (typeof payload !== "string") { throw new Error("expected string payload"); } return "echo " + payload; }, }; } `); try { const backend = new PiEmbeddedBackend({ module: mod.moduleUrl, timeoutMs: 2000 }); const result = await backend.process({ prompt: 'hello', history: [] }); expect(result).toContain('echo USER: hello'); } finally { mod.cleanup(); } }); it('throws when module has no supported session factory', async () => { const mod = createModule('export const version = "0.0.0";'); try { const backend = new PiEmbeddedBackend({ module: mod.moduleUrl, timeoutMs: 2000 }); await expect(backend.process({ prompt: 'hello', history: [] })) .rejects.toThrow('supported session factory'); } finally { mod.cleanup(); } }); it('throws when module cannot be loaded', async () => { const backend = new PiEmbeddedBackend({ module: '/definitely/missing/pi-module.mjs', timeoutMs: 2000 }); await expect(backend.process({ prompt: 'hello', history: [] })) .rejects.toThrow('Failed to load Pi embedded runtime module'); }); it('times out slow Pi requests', async () => { const mod = createModule(` export function createAgentSession() { return { run() { return new Promise(() => {}); }, }; } `); try { const backend = new PiEmbeddedBackend({ module: mod.moduleUrl, timeoutMs: 10 }); await expect(backend.process({ prompt: 'hello', history: [] })) .rejects.toThrow('timed out'); } finally { mod.cleanup(); } }); });