import { spawn } from "node:child_process"; import fs from "node:fs/promises"; import path from "node:path"; import { afterEach, describe, expect, test } from "vitest"; import { temporaryProject, testDeclarationEnvironment } from "./helpers.js"; const roots: string[] = []; afterEach(async () => { await Promise.all(roots.splice(0).map((root) => fs.rm(root, { recursive: true, force: true }))); }); describe("thought stream independent consumer command", () => { test("processes a durable producer backlog without producer handoff", async () => { const project = await temporaryProject(); roots.push(project); await fs.cp(path.join(process.cwd(), "agents"), path.join(project, "agents"), { recursive: true }); await fs.cp(path.join(process.cwd(), "prompts"), path.join(project, "prompts"), { recursive: true }); await fs.cp(path.join(process.cwd(), "fixtures", "vault"), path.join(project, "vault"), { recursive: true }); const scan = await run(["scan", "--root", path.join(project, "vault"), "--source", "filesystem:fixture"], project); expect(scan.code).toBe(0); expect(JSON.parse(scan.stdout)).toEqual({ scan: expect.objectContaining({ added: 3 }) }); const consume = await run(["consume", "--once"], project); expect(consume.code).toBe(0); const output = JSON.parse(consume.stdout) as { runs: Array<{ runId: string }> }; expect(output.runs).toHaveLength(3); }, 15_000); }); async function run(args: string[], project: string): Promise<{ code: number | null; stdout: string; stderr: string }> { return await new Promise((resolve, reject) => { const child = spawn(process.execPath, ["--import", "tsx", "src/cli.ts", ...args], { cwd: path.resolve(import.meta.dirname, ".."), env: { ...process.env, ...testDeclarationEnvironment, THOUGHTSTREAM_ROOT: project }, stdio: ["ignore", "pipe", "pipe"], }); let stdout = ""; let stderr = ""; child.stdout.on("data", (chunk) => { stdout += String(chunk); }); child.stderr.on("data", (chunk) => { stderr += String(chunk); }); child.once("error", reject); child.once("exit", (code) => resolve({ code, stdout, stderr })); }); }