import { test, expect, vi } from "vitest"; import { spawn as nodeSpawn } from "node:child_process"; import path from "node:path"; import os from "node:os"; import { chmodSync, readFileSync, unlinkSync, writeFileSync, mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { buildApp as buildRealApp } from "../src/app.js"; import { loadConfig } from "../src/config.js"; import { SseHub } from "../src/sse/sse-hub.js"; import { MaintenanceBarrier } from "../src/runtime/maintenance-gate.js"; import { PiProcessManager } from "../src/pi/pi-process-manager.js"; import { validateDeclarativePiConfig } from "../src/pi/managed-config.js"; import Fastify from "fastify"; import { sessionRoutes } from "../src/routes/sessions.js"; import { MemoryCatalogRepository } from "../src/catalog/memory-repository.js"; const FAKE = path.resolve("../harness/tests/fake_pi/fake_pi_rpc.mjs"); const SCRIPT = path.resolve("../harness/tests/fake_pi/scripts/f1_disambiguation.json"); function operationalWorkspace(id = "default") { return { workspace: { schema_version: 4, id, name: id, language: "en" }, dwh: { engine: "postgres", database: "warehouse", schema: "public", supported_transports: ["postgres_direct"], }, } as const; } function sessionCatalog(defaultInteraction = "zai/glm-5.2", available = [defaultInteraction]) { return { defaultInteraction, embedding: { id: "ollama/qwen3-embedding:0.6b", dimensions: 1024 }, sessionModels: () => [], metadataModels: () => [], hasSession: (id: string) => available.includes(id), } as any; } const defaultWorkspaceRegistry = { list: vi.fn(async () => [{ id: "default", commit: "e".repeat(40), blob: "f".repeat(40), snapshotPath: `/data/workspace-registry/snapshots/${"e".repeat(40)}/default.yaml`, }]), read: vi.fn(async (id: string) => ({ workspace: operationalWorkspace(id), revision: { id, commit: "e".repeat(40), blob: "f".repeat(40), snapshotPath: `/data/workspace-registry/snapshots/${"e".repeat(40)}/${id}.yaml`, }, })), }; function buildApp(config: Parameters[0], deps: Record = {}) { const thtRunner = deps.thtRunner ? { qdrantEnsure: async () => ({ ok: true }), ensureInteractionLanguage: async () => ({ interaction_language: "en" }), ...(deps.thtRunner as object), } : undefined; return buildRealApp(config, { workspaceRuntimeSupport: () => true, ...deps, ...(thtRunner ? { thtRunner } : {}), workspaceRegistry: { ...defaultWorkspaceRegistry, ...(deps.workspaceRegistry as object | undefined) }, } as any); } function mutApp(thtRunner: any) { return buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), { thtRunner: { ollamaEnsure: async () => ({ ok: true }), ...thtRunner }, getSettings: () => ({ workspace: "w" }) as any, spawnFn: () => nodeSpawn("node", [FAKE, SCRIPT]) as any, }); } function deferred() { let resolve!: (value: T | PromiseLike) => void; let reject!: (reason?: unknown) => void; const promise = new Promise((onResolve, onReject) => { resolve = onResolve; reject = onReject; }); return { promise, resolve, reject }; } const aliceHeaders = { "x-thoth-principal-issuer": "portal", "x-thoth-principal-subject": "alice", "x-thoth-principal-display-name": "Alice", "x-thoth-is-admin": "0", }; test.each([undefined, null, "", "en--US", "en_US", "en\nIGNORE", "en