import { afterEach, describe, expect, test } from "bun:test"; import { mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { evaluationHandoffPatch, hasSharedTikTokCheckout, isRunDisabled, satelliteWorkMode } from "../src/runtime_mode"; const root = path.resolve(import.meta.dir, "../.."); describe("satellite work modes", () => { test("one setting selects Run or Evaluate without filtering task types", () => { expect(satelliteWorkMode({ SATELLITE_WORK_MODE: "run" })).toBe("run"); expect(satelliteWorkMode({ SATELLITE_WORK_MODE: "evaluate" })).toBe("evaluate"); expect(satelliteWorkMode({ SATELLITE_WORK_MODE: "archive" })).toBe("archive"); expect(satelliteWorkMode({ DISABLE_RUN: "true" })).toBe("evaluate"); expect(satelliteWorkMode({})).toBeNull(); expect(isRunDisabled({ SATELLITE_WORK_MODE: "evaluate" })).toBe(true); expect(isRunDisabled({ SATELLITE_WORK_MODE: "run" })).toBe(false); expect(isRunDisabled({ SATELLITE_WORK_MODE: "archive" })).toBe(true); expect(evaluationHandoffPatch({ SATELLITE_WORK_MODE: "evaluate" })).toEqual({ executorNodeId: null }); expect(() => satelliteWorkMode({ SATELLITE_WORK_MODE: "run", DISABLE_RUN: "true" })).toThrow(); expect(() => satelliteWorkMode({ SATELLITE_WORK_MODE: "invalid" })).toThrow(); }); test("only an explicit true disables run claims", () => { expect(isRunDisabled({ DISABLE_RUN: "true" })).toBe(true); expect(isRunDisabled({ DISABLE_RUN: " TRUE " })).toBe(true); expect(isRunDisabled({ DISABLE_RUN: "false" })).toBe(false); expect(isRunDisabled({ DISABLE_RUN: "1" })).toBe(false); expect(isRunDisabled({ DISABLE_RUN: undefined })).toBe(false); }); test("READY evaluations release their node binding only in evaluate-only mode", () => { expect(evaluationHandoffPatch({ DISABLE_RUN: "true" })).toEqual({ executorNodeId: null }); expect(evaluationHandoffPatch({ DISABLE_RUN: "false" })).toEqual({}); for (const name of ["evaluator.ts", "feature_evaluator.ts", "security_evaluator.ts"]) { const source = readFileSync(path.join(root, "satellite/src", name), "utf8"); expect(source).toContain("...evaluationHandoffPatch()"); } }); test("standalone startup advertises zero run capacity and skips only the scheduler", () => { const source = readFileSync(path.join(root, "satellite/src/index.ts"), "utf8"); expect(source).toContain("const FIX_CAPACITY = DISABLE_RUN ? 0 : 1"); expect(source).toContain('superviseLoop("evaluator", startEvaluator)'); expect(source).toContain('if (DISABLE_RUN)'); expect(source).toContain('superviseLoop("scheduler", startScheduler)'); expect(source).toContain("fixCapacity: FIX_CAPACITY"); }); test("archive mode starts neither loop and advertises zero capacity in both phases", () => { const source = readFileSync(path.join(root, "satellite/src/index.ts"), "utf8"); expect(source).toContain('const ARCHIVE_MODE = WORK_MODE === "archive"'); expect(source).toContain('WORK_MODE === "run" || ARCHIVE_MODE ? 0 : Number(process.env.EVAL_PARALLEL || 3)'); expect(source).toContain("serving historical files only"); }); }); describe("node with a shared AI_WORKSPACE/TikTok checkout", () => { const origFetch = globalThis.fetch; const origWorkspace = process.env.AI_WORKSPACE; const workspaces: string[] = []; afterEach(() => { globalThis.fetch = origFetch; if (origWorkspace === undefined) delete process.env.AI_WORKSPACE; else process.env.AI_WORKSPACE = origWorkspace; for (const dir of workspaces.splice(0)) rmSync(dir, { recursive: true, force: true }); }); function workspace(sharedTikTok: boolean): string { const dir = mkdtempSync(path.join(tmpdir(), "satellite-ws-")); workspaces.push(dir); if (sharedTikTok) mkdirSync(path.join(dir, "TikTok")); process.env.AI_WORKSPACE = dir; return dir; } /** Records every control-plane POST; the eval claim answers with `item`. */ function mockControl(item: unknown) { const posts: { url: string; body: Record }[] = []; globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { const url = String(input); posts.push({ url, body: JSON.parse(String(init?.body ?? "{}")) }); return Response.json(url.endsWith("/claim/work-eval") ? { item } : { ok: true }); }) as typeof fetch; return posts; } test("starts with unset mode and its eval claim declares it cannot take security items", async () => { const dir = workspace(true); expect(hasSharedTikTokCheckout(dir)).toBe(true); expect(satelliteWorkMode({})).toBeNull(); const index = readFileSync(path.join(root, "satellite/src/index.ts"), "utf8"); expect(index).not.toContain("Security Evaluate requires an AI workspace without shared TikTok"); expect(index).toContain('superviseLoop("evaluator", startEvaluator)'); const { makeClusterHost } = await import("../src/cluster_host"); const posts = mockControl(null); expect(await makeClusterHost().claimAnyEval!(["bug-1"], ["feature-1"])).toBeNull(); expect(posts).toHaveLength(1); expect(posts[0].url).toEndWith("/api/internal/claim/work-eval"); expect(posts[0].body.skipSecurity).toBe(true); expect(posts[0].body.excludeIds).toEqual(["bug-1"]); expect(posts[0].body.excludeFeatureIds).toEqual(["feature-1"]); }); test("a node without the shared copy still claims security items", async () => { expect(hasSharedTikTokCheckout(workspace(false))).toBe(false); const { makeClusterHost } = await import("../src/cluster_host"); const posts = mockControl(null); await makeClusterHost().claimAnyEval!([], []); expect(posts[0].body.skipSecurity).toBe(false); }); test("a security item from an old control plane is released, not run, and excluded afterwards", async () => { workspace(true); const { makeClusterHost } = await import("../src/cluster_host"); const host = makeClusterHost(); const posts = mockControl({ kind: "security", security: { id: "sec-shared-1" } }); expect(await host.claimAnyEval!([], [])).toBeNull(); const release = posts.find((p) => p.url.endsWith("/api/internal/security/sec-shared-1/update")); expect(release?.body).toEqual({ status: "initializing", patch: { executorNodeId: null, failureReason: null } }); const next = mockControl(null); await host.claimAnyEval!([], []); expect(next[0].body.excludeSecurityIds).toContain("sec-shared-1"); }); });