Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

71.0 KB
Raw Blame
import crypto from "node:crypto";
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { startFakeBilling, type FakeBilling } from "./helpers/fake-billing";
import { startLocalComfy, type LocalComfy } from "./helpers/fake-local-comfy";
import { apiAs, loadRoutes, sha256, type ComputerRoutes } from "./helpers/computer-tab";
import { tempDir } from "./helpers/temp-dir";
import { composeOnWorker, graphOfTask } from "./helpers/ticket-worker";

/**
 * Account runs on this install's GPUs go through the workers' line: each job of an account's run is a task for the
 * workers that belongs to the account, never a request to a ComfyUI address. The account sees only "In line", "Creating"
 * and its result; an account's own session can never take or read such a task; a task back in line keeps its place and
 * shows "In line" again; and with hosted runs off nothing changes. (The line's own rules for every job, one job per
 * worker among them: tests/worker-queue.test.ts.)
 */

vi.mock("next/server", async (importOriginal) => ({ ...(await importOriginal<typeof import("next/server")>()), connection: async () => {} }));
vi.setConfig({ testTimeout: 60_000 });

const dir = tempDir("fuobe-pool-account-runs-");
process.env.FUOBE_DATA_DIR = dir;
// Nothing answers here: no test may reach a real ComfyUI on this machine. (Not port 9: the tests point local AI
// helpers there.)
const DEAD = "http://127.0.0.1:7";
process.env.COMFYUI_URL = DEAD;
process.env.FUOBE_PUBLIC_SIGNUP = "1";
delete process.env.FUOBE_PASSWORD;
delete process.env.FUOBE_HOSTED_RUNS;
delete process.env.FUOBE_HOSTED_FREE;
delete process.env.FUOBE_BILLING_URL;
delete process.env.FUOBE_MODERATION_URL;
globalThis.__fuobeTestDiskFree = 1e13;

// The server's own fetch: recorded. Anything but the stand-ins answers 599 at once (no real network).
const realFetch = globalThis.fetch;
const fetched: string[] = [];
let allowed: string[] = [];
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
  const url = String(input instanceof Request ? input.url : input);
  fetched.push(url);
  if (allowed.some((a) => url.startsWith(a))) return realFetch(input, init);
  return new Response("not reachable in this test", { status: 599 });
}) as typeof fetch;

const auth = await import("@/server/auth");
const generationsRoute = await import("@/app/api/generations/route");
const { db } = await import("@/server/db");
const { resetRates } = await import("@/server/rate");
const { getSetting, setSetting } = await import("@/server/settings");
const { getGeneration, shownError, toGeneration } = await import("@/server/generations");
const tasks = await import("@/server/computer/tasks");
const pool = await import("@/server/computer/pool");
const hosted = await import("@/server/hosted");
const billing = await import("@/server/billing");
const { listAccountRuns } = await import("@/server/account-admin");
const { loadManifest, withoutSaveMetadata, planRelayOutputs, onComputer, OWN_OUTPUT_LIMITS } = await import("@/server/providers/comfyui");
const { accountTotalBytes, accountUsage, diskFloorBytes } = await import("@/server/account-files");
const { loadIndex } = await import("@/server/gpu/index-file");
const { allLanes } = await import("@/lib/lanes");
const { getModel } = await import("@/lib/catalog");
const { ComputerRunner } = await import("@/lib/computer-runner");
const { FILE_TROUBLE_WORDS, POOL_WORDS } = await import("@/lib/relay");
const { ENGINE_WORDS, isEngineWords } = await import("@/lib/engine-error");
const { ACCOUNT_MESSAGES } = await import("@/server/account-files");
const { catalogVersion } = await import("@/server/computer/catalog-version");
const { refusalReason } = await import("@/lib/comfy-reason");
const { runDetails } = await import("@/server/run-details");

const HOST = "localhost:3000";
const SITE_HEADERS = { host: HOST, "sec-fetch-site": "same-origin", origin: `http://${HOST}` };
const MIN = 60_000;
const sha = (s: string) => crypto.createHash("sha256").update(s).digest("hex");

let comfy: LocalComfy;
let routes: ComputerRoutes;
let fake: FakeBilling;
let accountId = "";
let cookie = "";
let computerAccount = "";
let computerCookie = "";

beforeAll(async () => {
  comfy = await startLocalComfy({ cors: "*", steps: 2, stepMs: 10, latencyMs: 10 });
  allowed = [comfy.url];
  routes = await loadRoutes();
  fake = await startFakeBilling();
  allowed.push(fake.url);
  auth.setPassword("install password");
  accountId = auth.createAccount("[email protected]", "long enough").id;
  cookie = `${auth.SESSION_COOKIE}=${auth.createSession(accountId).value}`;
  // An account whose own GPU is "this computer": its session reaches the computer routes as acct:<id>.
  computerAccount = auth.createAccount("[email protected]", "long enough").id;
  auth.setAccountGpu(computerAccount, { mode: "computer" });
  computerCookie = `${auth.SESSION_COOKIE}=${auth.createSession(computerAccount).value}`;
});
afterAll(async () => {
  globalThis.fetch = realFetch;
  globalThis.__fuobeTestDiskFree = undefined;
  for (const k of ["FUOBE_HOSTED_RUNS", "FUOBE_HOSTED_FREE", "FUOBE_BILLING_URL", "FUOBE_BILLING_TOKEN"]) delete process.env[k];
  await comfy.close();
  await fake.close();
});
beforeEach(() => {
  resetRates();
  billing.resetBillingCache();
  process.env.FUOBE_HOSTED_RUNS = "1";
  process.env.FUOBE_HOSTED_FREE = "1";
  delete process.env.FUOBE_BILLING_URL;
  process.env.COMFYUI_URL = DEAD;
  setSetting("comfyWhere", "workers");
  setSetting("comfyUrl", "");
  setSetting("engineMode", "open");
  setSetting("demoMode", "off");
  // People read the plain sentence only, unless a test says otherwise (Settings → Failed runs).
  setSetting("runFailureDetail", "friendly");
  setSetting("hostedOnlyWhatCanRun", "on");
  db().prepare("DELETE FROM relay_workers").run();
  db().prepare("UPDATE relay_tasks SET state = 'canceled' WHERE state IN ('waiting','sending','queued','running','collecting')").run();
  db().prepare("UPDATE generations SET status = 'canceled' WHERE status IN ('queued', 'running')").run();
  fetched.length = 0;
});

/** A worker the owner added and resumed, allowed every lane, able to run every workflow. */
function standInWorker(name = "stand-in", o: { paused?: boolean } = {}) {
  const w = pool.addWorker(name);
  const lanes = allLanes(loadIndex(), sha);
  pool.patchWorker(w.id, { paused: !!o.paused, hours: { always: true }, allow: Object.fromEntries(lanes.map((l) => [l.lane, { delayMs: 0 }])), claimAfterMs: 0 });
  db()
    .prepare("UPDATE relay_workers SET lanes_json = ?, facts_json = ?, catalog_version = ?, protocol = 3 WHERE id = ?")
    .run(JSON.stringify({ lanes: lanes.map((l) => l.lane), workflows: loadManifest().workflows.map((x) => x.id) }), JSON.stringify({ vramBytes: 96 * 2 ** 30 }), catalogVersion(), w.id);
  // Connected: its worker program beat a moment ago.
  db().prepare("UPDATE relay_workers SET seen_at = ?, paused_local = 0 WHERE id = ?").run(Date.now(), w.id);
  return w;
}

/**
 * The worker program's side, as `fuobe worker run` drives it: the shared runner with the worker's token, its own
 * ComfyUI, and its composer for job tickets.
 */
function workerRunner(token: string) {
  const api = apiAs(routes, null, { authorization: `Bearer ${token}` });
  return new ComputerRunner(
    {
      api,
      local: (url, init) => realFetch(url, init),
      socket: (url) => new WebSocket(url) as never,
      sha256,
      origin: "http://localhost:3000",
      sleep: (ms) => new Promise((r) => setTimeout(r, Math.min(ms, 50))),
      compose: (t) => composeOnWorker(t),
      beatExtra: () => ({ protocol: 3, version: "test", local: { paused: false, why: null, until: null }, gpu: { otherApps: [], tempC: null }, inventoryHash: null, warmLane: null, catalogVersion: catalogVersion() }),
    },
    { device: "worker", port: comfy.port, clientId: "worker-test" },
  );
}

const request = (url: string, o: { method?: string; body?: unknown; cookie?: string | null; headers?: Record<string, string> } = {}) => {
  const text = o.body === undefined ? undefined : JSON.stringify(o.body);
  const headers: Record<string, string> = { ...SITE_HEADERS, ...(o.cookie !== null && { cookie: o.cookie ?? cookie }), ...(text && { "content-type": "application/json", "content-length": String(Buffer.byteLength(text)) }), ...o.headers };
  return new Request(`http://${HOST}${url}`, { method: o.method ?? (text ? "POST" : "GET"), headers, body: text });
};
const run = async (body: Record<string, unknown>, c = cookie) => {
  const r = await generationsRoute.POST(request("/api/generations", { body, cookie: c }));
  return { status: r.status, body: (await r.json()) as { items?: { id: string }[]; code?: string; error?: string } };
};
const tick = () => globalThis.__fuobeRunner?.tick();
const taskOf = (generationId: string) => db().prepare("SELECT * FROM relay_tasks WHERE generation_id = ? ORDER BY created_at DESC").get(generationId) as import("@/server/computer/tasks").TaskRow | undefined;
const rowOf = (id: string) => db().prepare("SELECT * FROM relay_tasks WHERE id = ?").get(id) as import("@/server/computer/tasks").TaskRow;
async function until<T>(fn: () => T | Promise<T>, tries = 200, everyMs = 25): Promise<T> {
  let v = await fn();
  for (let i = 0; i < tries && !v; i++) {
    await tick();
    await new Promise((r) => setTimeout(r, everyMs));
    v = await fn();
  }
  return v;
}
/** Requests to a ComfyUI address the server made itself (the stand-in worker's own ComfyUI is the worker's, not the server's). */
const serverComfyCalls = () => fetched.filter((u) => u.startsWith(`${DEAD}/`) || /:818[89]\b|:819[01]\b/.test(u) || /\/(prompt|upload\/image|system_stats|history|view|queue|object_info)\b/.test(u));

/** An account's task made straight in the line (no run route), for the queue's own rules. */
let gens = 0;
function accountTask(o: { lane?: string; account?: string; rankAt?: number; notAfter?: number; workflow?: string } = {}) {
  const gid = `pool-gen-${++gens}`;
  const now = Date.now();
  db()
    .prepare("INSERT INTO generations (id, feature, model_id, engine, provider, op, kind, status, request_json, outputs_json, created_at, updated_at) VALUES (?, 'image', 'm', 'open', 'comfyui', 't2i', 'image', 'queued', ?, '[]', ?, ?)")
    .run(gid, JSON.stringify({ account: o.account ?? accountId, place: "pool" }), now, now);
  const lanes = allLanes(loadIndex(), sha);
  const t = tasks.createTask({
    principal: `acct:${o.account ?? accountId}`,
    generationId: gid,
    graph: { "1": { class_type: "SaveImage", inputs: { filename_prefix: "x" } } },
    pool: { pools: ["pool"], account: o.account ?? accountId, rankAt: o.rankAt ?? now, lane: o.lane ?? lanes[0]?.lane ?? "small", needs: { workflowId: o.workflow ?? loadManifest().workflows[0].id, files: [], packs: [], minVramBytes: 0 }, notAfter: o.notAfter },
  });
  return { id: t.id, generationId: gid };
}

type WorkerRow = import("@/server/computer/workers").WorkerRow;
const workerRow = (w: { id: string }) => db().prepare("SELECT * FROM relay_workers WHERE id = ?").get(w.id) as WorkerRow;
const beatAs = (w: { id: string }, now = Date.now()) =>
  pool.workerBeat(workerRow(w), pool.cleanBeat({ device: `wkr-${w.id}`, protocol: 3, version: "1", local: { paused: false, why: null, until: null }, gpu: {}, inventoryHash: null, running: [], stopped: [], catalogVersion: catalogVersion() }), now);
const claimAs = (w: { id: string }, running: string[] = [], now = Date.now()) => pool.workerClaim(workerRow(w), running, now);

describe("with hosted runs off, nothing changes", () => {
  it("an account with no GPU of its own gets today's answer, and no task is made", async () => {
    delete process.env.FUOBE_HOSTED_RUNS;
    standInWorker();
    expect(hosted.poolReady()).toBe(false);
    const before = (db().prepare("SELECT COUNT(*) AS n FROM relay_tasks").get() as { n: number }).n;
    const r = await run({ feature: "image", modelId: "oss-z-image-turbo", prompt: "a lighthouse" });
    expect(r.status).toBe(403);
    expect(r.body.code).toBe("connect-gpu");
    expect((db().prepare("SELECT COUNT(*) AS n FROM relay_tasks").get() as { n: number }).n).toBe(before);
    expect(serverComfyCalls()).toEqual([]);
  });

  it("switched on with nothing to answer for the runs (no billing service, no free runs), they stay closed", async () => {
    delete process.env.FUOBE_HOSTED_FREE;
    standInWorker();
    expect(hosted.poolReady()).toBe(false);
    const r = await run({ feature: "image", modelId: "oss-z-image-turbo", prompt: "a lighthouse" });
    expect(r.status).toBe(403);
    expect(r.body.code).toBe("connect-gpu");
    // A billing service that answers opens them.
    process.env.FUOBE_BILLING_URL = fake.url;
    process.env.FUOBE_BILLING_TOKEN = "test-billing-token";
    expect(hosted.poolReady()).toBe(true);
  });
});

describe("where hosted runs may go", () => {
  it("on the workers: a worker the owner allowed a lane, paused or not; none at all closes them", () => {
    expect(hosted.poolReady()).toBe(false);
    const w = standInWorker("paused one", { paused: true });
    expect(hosted.poolReady()).toBe(true);
    pool.patchWorker(w.id, { revoke: true });
    expect(hosted.poolReady()).toBe(false);
  });

  it("on this install's own ComfyUI server: only an address on this machine", () => {
    setSetting("comfyWhere", "server");
    setSetting("comfyUrl", "http://127.0.0.1:3399");
    expect(hosted.poolReady()).toBe(true);
    for (const url of ["http://10.0.0.5:8188", "https://comfy.example.com", "http://studio:3399"]) {
      setSetting("comfyUrl", url);
      expect(hosted.poolReady(), url).toBe(false);
    }
    setSetting("comfyWhere", "computer");
    setSetting("comfyUrl", "http://127.0.0.1:3399");
    expect(hosted.poolReady()).toBe(false);
  });
});

describe("an account's run on the workers", () => {
  it("with no ComfyUI address the server could reach, it becomes the account's task, and a stand-in worker renders it", async () => {
    const w = standInWorker();
    const r = await run({ feature: "image", modelId: "oss-z-image-turbo", prompt: "a lighthouse at dusk" });
    expect(r.status, JSON.stringify(r.body)).toBe(201);
    const id = r.body.items![0].id;
    const t = await until(() => taskOf(id));
    expect(t).toBeDefined();
    expect(t!.principal).toBe(`acct:${accountId}`);
    expect(t!.pools).toBe("pool");
    expect(t!.account).toBe(accountId);
    // It waits in line, in plain words, with its place.
    await until(() => getGeneration(id)?.message === POOL_WORDS.inLine);
    const waiting = getGeneration(id)!;
    expect(waiting.message).toBe(POOL_WORDS.inLine);
    expect(waiting.queue?.place).toBe(1);
    expect(waiting.place).toBe("pool");

    // The worker takes it through the same routes as every worker, and renders it on its own ComfyUI.
    const worker = workerRunner(w.token);
    const beat = await worker.beat();
    expect(beat?.claimableNow).toBe(1);
    await worker.claimAndRun();
    expect(rowOf(t!.id).state).toBe("done");
    const g = await until(() => {
      const x = getGeneration(id);
      return x && x.status !== "queued" && x.status !== "running" ? x : null;
    });
    expect(g?.status, g?.error ?? "").toBe("succeeded");
    expect(g!.outputs.length).toBeGreaterThan(0);
    // The server never called a ComfyUI address for it.
    expect(serverComfyCalls()).toEqual([]);
    // The workers' log: who took it, and how it ended.
    const log = db().prepare("SELECT event, worker_id, principal FROM worker_jobs WHERE task_id = ? ORDER BY id").all(t!.id) as { event: string; worker_id: string; principal: string }[];
    expect(log.map((x) => x.event)).toEqual(["claimed", "finished", "done"]);
    expect(log.every((x) => x.worker_id === w.id && x.principal === `acct:${accountId}`)).toBe(true);
  });

  it("an account's own session can never claim or read a task for the workers, even one of its own runs", async () => {
    standInWorker();
    const mine = accountTask({ account: computerAccount });
    const api = apiAs(routes, computerCookie);
    // Claim: nothing (a task for the workers is never a computer's own).
    const claim = await api("/api/gpu/computer/claim", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ device: "device-own-1", running: [] }) });
    expect(claim.status).toBe(204);
    // A worker takes it; the account then names it with any lease: unknown.
    const w = db().prepare("SELECT * FROM relay_workers LIMIT 1").get() as { id: string };
    beatAs(w);
    const got = claimAs(w)!;
    expect(got.id).toBe(mine.id);
    for (const [path, init] of [
      [`/api/gpu/computer/tasks/${mine.id}/report`, { method: "POST", headers: { "x-fuobe-lease": got.lease, "Content-Type": "application/json" }, body: JSON.stringify({ event: "running", value: 1, max: 2 }) }],
      [`/api/gpu/computer/tasks/${mine.id}/inputs/0`, { headers: { "x-fuobe-lease": got.lease } }],
      [`/api/gpu/computer/tasks/${mine.id}/outputs/0`, { headers: { "x-fuobe-lease": got.lease } }],
      [`/api/gpu/computer/tasks/${mine.id}/release`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ lease: got.lease }) }],
    ] as [string, RequestInit][]) {
      const res = await api(path, init);
      expect(res.status, path).toBe(404);
    }
    // A beat that names it (even under the worker's device) touches nothing of it, and hears no stop of it.
    tasks.setTask(mine.id, { stop: 1 });
    const beat = await api("/api/gpu/computer/beat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ device: `wkr-${w.id}`, running: [{ task: mine.id, value: 9, max: 9 }], stopped: [mine.id] }) });
    expect(beat.status).toBe(200);
    expect(((await beat.json()) as { stops: unknown[] }).stops).toEqual([]);
    expect(rowOf(mine.id).stop).toBe(1);
    expect(tasks.getTask(`acct:${computerAccount}`, mine.id)).toBeNull();
    // The server's own reads, as the run's account, still find it.
    expect(tasks.ownTask(`acct:${computerAccount}`, mine.id)?.id).toBe(mine.id);
  });

  it("refuses a run that no worker would ever take, before anything is queued", async () => {
    const w = standInWorker();
    pool.patchWorker(w.id, { allow: {} });
    pool.addWorker("lane-less");
    const before = (db().prepare("SELECT COUNT(*) AS n FROM relay_tasks").get() as { n: number }).n;
    const r = await run({ feature: "image", modelId: "oss-z-image-turbo", prompt: "a lighthouse" });
    expect(r.status).toBe(403);
    expect((db().prepare("SELECT COUNT(*) AS n FROM relay_tasks").get() as { n: number }).n).toBe(before);
  });

  it("refuses a run whose lane a worker is allowed but whose files no worker has, before anything is queued", async () => {
    const w = standInWorker();
    const endpoint = getModel("oss-z-image-turbo")!.routes.find((x) => x.provider === "comfyui")!.endpoint;
    const workflows = loadManifest().workflows.map((x) => x.id);
    const lanesNow = JSON.parse(workerRow(w).lanes_json ?? "{}") as { lanes: string[] };
    // Allowed every lane, but its last list of files lacks this workflow's: nothing it could ever run.
    db().prepare("UPDATE relay_workers SET lanes_json = ? WHERE id = ?").run(JSON.stringify({ lanes: [], workflows: workflows.filter((x) => x !== endpoint) }), w.id);
    const before = (db().prepare("SELECT COUNT(*) AS n FROM relay_tasks").get() as { n: number }).n;
    const r = await run({ feature: "image", modelId: "oss-z-image-turbo", prompt: "a lighthouse" });
    expect(r.status, JSON.stringify(r.body)).toBe(403);
    // It runs on the account's own GPU instead: Run opens Connect your GPU.
    expect(r.body.code).toBe("connect-gpu");
    expect((r.body as { ownSetup?: unknown }).ownSetup).toMatchObject({ need: "gpu" });
    expect((db().prepare("SELECT COUNT(*) AS n FROM relay_tasks").get() as { n: number }).n).toBe(before);
    // Its files are there again: the run goes in line.
    db().prepare("UPDATE relay_workers SET lanes_json = ? WHERE id = ?").run(JSON.stringify({ ...lanesNow, workflows }), w.id);
    expect((await run({ feature: "image", modelId: "oss-z-image-turbo", prompt: "a lighthouse" })).status).toBe(201);
  });

  it("a model whose files no worker has: the price check says its label, and the status labels it in the pickers", async () => {
    const { ownSetupLabel } = await import("@/lib/own-setup");
    const { modelOwnSetup } = await import("@/lib/pool-rules");
    const { visitorStatus } = await import("@/server/account-gpu");
    const w = standInWorker();
    const model = getModel("oss-z-image-turbo")!;
    const endpoints = new Set(model.routes.filter((x) => x.provider === "comfyui").map((x) => x.endpoint));
    const workflows = loadManifest().workflows.map((x) => x.id);
    db().prepare("UPDATE relay_workers SET lanes_json = ? WHERE id = ?").run(JSON.stringify({ lanes: [], workflows: workflows.filter((x) => !endpoints.has(x)) }), w.id);
    const r = await generationsRoute.POST(request("/api/generations?preview=1", { body: { feature: "image", modelId: model.id, prompt: "a lighthouse" } }));
    const quote = ((await r.json()) as { quote?: { allowed: boolean; text: string; ownSetup?: { need: "gpu"; vramGb?: number } } }).quote;
    expect(quote).toMatchObject({ allowed: false, ownSetup: { need: "gpu" } });
    expect(quote?.text).toBe(ownSetupLabel(quote!.ownSetup!));
    const pool = (await visitorStatus(auth.accountById(accountId)!)).viewer.pool;
    expect(pool?.own?.[model.id]).toEqual(quote?.ownSetup);
    expect(modelOwnSetup(pool, model)).toEqual(quote?.ownSetup);
    // A model the workers hold has no label.
    expect(pool?.own?.["oss-wan22-14b"]).toBeUndefined();
  });

  it("a file input in params.extra is one the run sent, never a name in the workers' folders", async () => {
    standInWorker();
    const uploadsRoute = await import("@/app/api/uploads/route");
    const { TINY_PNG } = await import("./helpers/fake-comfy-runner");
    const up = await uploadsRoute.POST(
      new Request(`http://${HOST}/api/uploads`, { method: "POST", headers: { ...SITE_HEADERS, cookie, "content-type": "image/png", "x-file-name": "me.png", "content-length": String(TINY_PNG.length) }, body: new Uint8Array(TINY_PNG) }),
    );
    expect(up.status).toBe(201);
    const { url } = (await up.json()) as { url: string };
    // A workflow that loads a picture: the name in params.extra would replace the picture the run sent.
    const r = await run({ feature: "image", modelId: "oss-rmbg", op: "bg-remove", inputs: { images: [url] }, params: { extra: { image: "fuobe_someone_elses.png" } } });
    expect(r.status, JSON.stringify(r.body)).toBe(201);
    const t = await until(() => taskOf(r.body.items![0].id));
    expect(t).toBeDefined();
    // It goes as a job ticket: no graph, and the name the run didn't send isn't in it.
    expect(t!.graph_json).toBeNull();
    expect(t!.ticket_json ?? "").not.toContain("fuobe_someone_elses.png");
    // The picture the worker's graph loads is the one this run sent, kept for its task.
    const graph = await graphOfTask(t!);
    const loaded = Object.values(graph)
      .filter((x) => x.class_type === "LoadImage")
      .map((x) => String(x.inputs.image));
    expect(loaded).toHaveLength(1);
    const staged = db().prepare("SELECT principal, task_id FROM relay_inputs WHERE name = ?").get(loaded[0]) as { principal: string; task_id: string | null } | undefined;
    expect(staged).toEqual({ principal: `acct:${accountId}`, task_id: t!.id });
  });

  it("a graph that names a file the run didn't send is refused before it is queued", async () => {
    const { graphProblem } = await import("@/lib/local-comfy");
    const graph = { "1": { class_type: "LoadImage", inputs: { image: "fuobe_someone_elses.png" } }, "2": { class_type: "SaveImage", inputs: { images: ["1", 0], filename_prefix: "x" } } };
    expect(graphProblem(graph as never, ["fuobe_mine.png"])).toBeTruthy();
    expect(graphProblem({ ...graph, "1": { class_type: "LoadImage", inputs: { image: "fuobe_mine.png" } } } as never, ["fuobe_mine.png"])).toBeNull();
  });
});

describe("the line", () => {
  it("a requeue keeps the run's place (rank_at), it shows In line again, and its wait limit is over", async () => {
    const w = standInWorker();
    const first = accountTask({ rankAt: Date.now() - 5 * MIN, notAfter: Date.now() + MIN });
    const later = accountTask({ rankAt: Date.now() });
    const rank = rowOf(first.id).rank_at;
    beatAs(w);
    expect(claimAs(w)?.id).toBe(first.id);
    // The worker pauses at once (another program on the GPU): the job goes back in line at no cost.
    pool.workerBeat(
      db().prepare("SELECT * FROM relay_workers WHERE id = ?").get(w.id) as never,
      pool.cleanBeat({ device: `wkr-${w.id}`, protocol: 3, version: "1", local: { paused: true, why: "gpu-busy:game.exe", until: null }, gpu: {}, running: [{ task: first.id }], stopped: [] }),
    );
    const back = rowOf(first.id);
    expect(back.state).toBe("waiting");
    expect(back.rank_at).toBe(rank);
    expect(back.requeues).toBe(0);
    // First in line again, ahead of the later run, and past its wait limit it still waits: a worker took it once.
    expect(pool.queueOfGeneration(first.generationId)?.place).toBe(1);
    expect(pool.queueOfGeneration(later.generationId)?.place).toBe(2);
    pool.sweepPool(Date.now() + 10 * MIN);
    expect(rowOf(first.id).state).toBe("waiting");
    // A run no worker has taken yet still fails at its wait limit, not charged.
    const never = accountTask({ notAfter: Date.now() + MIN });
    pool.sweepPool(Date.now() + 2 * MIN);
    expect(rowOf(never.id).state).toBe("failed");
    // Its row says In line, never a pause or a GPU word.
    const g = toGeneration(db().prepare("SELECT * FROM generations WHERE id = ?").get(first.generationId) as never);
    expect(g.message).toBe(POOL_WORDS.inLine);
  });

  it("back in line after it started, it never goes backwards: Creating at its highest share, across a reload, until the new attempt passes it", async () => {
    const w = standInWorker();
    const r = await run({ feature: "image", modelId: "oss-z-image-turbo", prompt: "a lighthouse at dusk" });
    expect(r.status, JSON.stringify(r.body)).toBe(201);
    const id = r.body.items![0].id;
    const t = (await until(() => taskOf(id)))!;
    beatAs(w);
    const got = claimAs(w)!;
    expect(got.id).toBe(t.id);
    tasks.report(rowOf(t.id), { event: "sent", promptId: got.promptId }, () => ({ error: "unused" }));
    tasks.report(rowOf(t.id), { event: "running", value: 3, max: 4 }, () => ({ error: "unused" }));
    const working = await until(() => {
      const g = getGeneration(id);
      return g?.status === "running" && (g.progress ?? 0) > 0 ? g : null;
    });
    // Creating, with the sampler's step as a percent.
    expect(working?.message).toBe("Creating · 75%");
    // The worker pauses at once (its owner pressed the pause): the run goes back in line.
    pool.workerBeat(
      db().prepare("SELECT * FROM relay_workers WHERE id = ?").get(w.id) as never,
      pool.cleanBeat({ device: `wkr-${w.id}`, protocol: 3, version: "1", local: { paused: true, why: "here-now", until: null }, gpu: {}, running: [{ task: t.id }], stopped: [] }),
    );
    const raw = () => db().prepare("SELECT status, progress, message FROM generations WHERE id = ?").get(id) as { status: string; progress: number | null; message: string | null };
    await until(() => raw().status === "queued");
    // The run itself is back in line, with no progress (the owner's views read that)…
    expect(raw()).toMatchObject({ status: "queued", progress: null, message: POOL_WORDS.inLine });
    // …while its person sees it still being made, at the highest share shown, with no percent and no place in line.
    const peak = working!.progress!;
    const back = getGeneration(id)!;
    expect(back).toMatchObject({ status: "running", message: POOL_WORDS.working, progress: peak });
    expect(back.queue).toBeUndefined();
    // A reload reads the same (the highest share is kept with the run).
    expect(toGeneration(db().prepare("SELECT * FROM generations WHERE id = ?").get(id) as never)).toMatchObject({ status: "running", message: POOL_WORDS.working, progress: peak });
    // A worker takes it again: below the highest share, still no percent and the same share…
    beatAs(w);
    const again = claimAs(w)!;
    expect(again.id).toBe(t.id);
    tasks.report(rowOf(t.id), { event: "sent", promptId: again.promptId }, () => ({ error: "unused" }));
    tasks.report(rowOf(t.id), { event: "running", value: 1, max: 4 }, () => ({ error: "unused" }));
    await until(() => raw().status === "running" && (raw().progress ?? 0) > 0);
    expect(raw().progress!).toBeLessThan(peak);
    expect(getGeneration(id)).toMatchObject({ status: "running", message: POOL_WORDS.working, progress: peak });
    // …and past it, its percent climbs again.
    tasks.report(rowOf(t.id), { event: "running", value: 4, max: 4 }, () => ({ error: "unused" }));
    await until(() => (raw().progress ?? 0) > peak);
    const past = getGeneration(id)!;
    expect(past.message).toBe("Creating · 100%");
    expect(past.progress!).toBeGreaterThan(peak);
  });

  it("place and wait count every task ahead in the order the workers serve them, whatever its lane", () => {
    const w = standInWorker();
    const lanes = allLanes(loadIndex(), sha).map((l) => l.lane);
    expect(lanes.length).toBeGreaterThan(1);
    const now = Date.now();
    accountTask({ lane: lanes[0], rankAt: now - 3 * MIN });
    accountTask({ lane: lanes[1], rankAt: now - 2 * MIN });
    const mine = accountTask({ lane: lanes[0], rankAt: now - MIN });
    beatAs(w);
    const place = pool.queueOfGeneration(mine.generationId);
    expect(place?.place).toBe(3);
    expect(place?.etaSeconds).toBeGreaterThan(0);
    // The billing service's line facts read the same line.
    const facts = hosted.queueFacts(accountId, loadManifest().workflows[0].id);
    expect(facts.ahead).toBe(3);
    expect(facts.workersOnline).toBe(1);
    expect(facts.mine.waiting).toBe(3);
  });

});

describe("what an account reads about its run", () => {
  it("a failed run on this install's GPUs shows one plain sentence and no Fix it; the owner reads the reason", () => {
    const now = Date.now();
    const id = `plain-${now}`;
    db()
      .prepare(
        "INSERT INTO generations (id, feature, model_id, engine, provider, op, kind, status, request_json, outputs_json, error, fix_json, created_at, updated_at) VALUES (?, 'image', 'oss-z-image-turbo', 'open', 'comfyui', 't2i', 'image', 'failed', ?, '[]', ?, ?, ?, ?)",
      )
      .run(id, JSON.stringify({ account: accountId, place: "pool", feature: "image", modelId: "oss-z-image-turbo" }), "Can't reach ComfyUI at http://10.0.0.5:8188 (C:\\Users\\x\\ComfyUI)", JSON.stringify({ code: "comfy_missing_node", items: [] }), now, now);
    const g = getGeneration(id)!;
    expect(g.error).toBe(POOL_WORDS.failed);
    expect(g.fix).toBeUndefined();
    const owner = listAccountRuns({ account: accountId }).items.find((x) => x.id === id);
    expect(owner?.detail).toContain("Can't reach ComfyUI");
    // Fuobe's own words about the account's side stay as they are.
    db().prepare("UPDATE generations SET error = ? WHERE id = ?").run("Your files have reached 5 GB. Delete some runs to make room.", id);
    expect(getGeneration(id)!.error).toBe("Your files have reached 5 GB. Delete some runs to make room.");
    for (const words of [ACCOUNT_MESSAGES.imageTooBig, ACCOUNT_MESSAGES.wrongType, ACCOUNT_MESSAGES.uploadsFull]) {
      db().prepare("UPDATE generations SET error = ? WHERE id = ?").run(words, id);
      expect(getGeneration(id)!.error).toBe(words);
    }
    // Words about the server's own state (its disk, how busy it is) are not the account's to act on: one plain sentence.
    for (const words of [ACCOUNT_MESSAGES.diskLow, ACCOUNT_MESSAGES.finishBusy, ACCOUNT_MESSAGES.finishRate, ACCOUNT_MESSAGES.uploadsBusy]) {
      db().prepare("UPDATE generations SET error = ? WHERE id = ?").run(words, id);
      expect(getGeneration(id)!.error, words).toBe(POOL_WORDS.failed);
    }
  });

  it("its output check is part of Creating, and a stopped run says Not charged only when its hold set something aside", () => {
    const now = Date.now();
    const insert = (id: string, status: string, request: Record<string, unknown>, stage: string | null = null) =>
      db()
        .prepare("INSERT INTO generations (id, feature, model_id, engine, provider, op, kind, status, stage, request_json, outputs_json, created_at, updated_at) VALUES (?, 'image', 'm', 'open', 'comfyui', 't2i', 'image', ?, ?, ?, '[]', ?, ?)")
        .run(id, status, stage, JSON.stringify({ account: accountId, ...request }), now, now);
    insert(`check-pool-${now}`, "running", { place: "pool" }, "checking");
    insert(`check-own-${now}`, "running", {}, "checking");
    const pooled = getGeneration(`check-pool-${now}`)!;
    expect(pooled.stage).toBeUndefined();
    expect(pooled.message).toBe(POOL_WORDS.working);
    // On the account's own GPU, as before.
    expect(getGeneration(`check-own-${now}`)!.stage).toBe("checking");
    insert(`charged-${now}`, "canceled", { place: "pool", hold: "h_1", charged: true });
    insert(`free-${now}`, "failed", { place: "pool", hold: "h_2" });
    expect(getGeneration(`charged-${now}`)!.notCharged).toBe(true);
    expect(getGeneration(`free-${now}`)!.notCharged).toBeUndefined();
    // Neither field ever reaches the page's copy of the request.
    expect("charged" in getGeneration(`charged-${now}`)!.request).toBe(false);
  });

  it("while it goes, only In line or Creating, whatever the technical words were", () => {
    const now = Date.now();
    const id = `words-${now}`;
    db()
      .prepare("INSERT INTO generations (id, feature, model_id, engine, provider, op, kind, status, request_json, outputs_json, message, created_at, updated_at) VALUES (?, 'image', 'm', 'open', 'comfyui', 't2i', 'image', 'running', ?, '[]', ?, ?, ?)")
      .run(id, JSON.stringify({ account: accountId, place: "pool" }), "In ComfyUI's queue · 2 ahead", now, now);
    expect(getGeneration(id)!.message).toBe(POOL_WORDS.working);
    db().prepare("UPDATE generations SET status = 'queued', message = 'Back in line for another GPU' WHERE id = ?").run(id);
    expect(getGeneration(id)!.message).toBe(POOL_WORDS.inLine);
  });
});

describe("nothing raw reaches an account from a run on this install's GPUs", () => {
  const put = (id: string, status: string, request: Record<string, unknown>, o: { error?: string; outputs?: unknown[] } = {}) => {
    const now = Date.now();
    db()
      .prepare("INSERT INTO generations (id, feature, model_id, engine, provider, op, kind, status, request_json, outputs_json, error, created_at, updated_at) VALUES (?, 'image', 'm', 'open', 'comfyui', 't2i', 'image', ?, ?, ?, ?, ?, ?)")
      .run(id, status, JSON.stringify({ account: accountId, ...request }), JSON.stringify(o.outputs ?? []), o.error ?? null, now, now);
  };

  it("a page that reads a step's failure from the table says the tile's sentence (a Mixed Media frame)", () => {
    const raw = "ComfyUI kept stopping on the GPUs. Press Retry.";
    put("shown-pool", "failed", { place: "pool" }, { error: raw });
    put("shown-own", "failed", {}, { error: raw });
    expect(shownError("shown-pool", raw)).toBe(POOL_WORDS.failed);
    expect(shownError("shown-own", raw)).toBe(raw);
    expect(shownError("shown-gone", raw)).toBe(raw);
  });

  it("words about the person's own files stay, so they know what to change", () => {
    for (const words of Object.values(FILE_TROUBLE_WORDS)) {
      const id = `files-${words.length}`;
      put(id, "failed", { place: "pool" }, { error: words });
      expect(getGeneration(id)!.error).toBe(words);
    }
  });

  it("a 3D finish's own trouble stays on the server; the account's own GPU runs keep theirs", () => {
    const model = { url: "/api/files/m1", kind: "model3d", meta: { finish: { turntable: "queued", error: "web GLB: exit 1" }, web: "/api/files/w1" } };
    put("finish-pool", "succeeded", { place: "pool" }, { outputs: [model] });
    put("finish-own", "succeeded", {}, { outputs: [model] });
    const pooled = getGeneration("finish-pool")!.outputs[0].meta as { finish: Record<string, unknown>; web: string };
    expect(pooled.finish).toEqual({ turntable: "queued" });
    expect(pooled.web).toBe("/api/files/w1");
    expect((getGeneration("finish-own")!.outputs[0].meta as { finish: Record<string, unknown> }).finish.error).toBe("web GLB: exit 1");
  });

  it("the owner's words for a task stopped after its tries: how many tries while hosted runs are on, as before otherwise", () => {
    delete process.env.FUOBE_HOSTED_RUNS;
    expect(pool.stoppedWords("silent", 3)).toBe("The GPU running this stopped answering, three times. Press Retry.");
    expect(pool.stoppedWords("other", 3)).toBe("The GPUs couldn't finish this run. Press Retry.");
    process.env.FUOBE_HOSTED_RUNS = "1";
    try {
      expect(pool.stoppedWords("silent", 3)).toBe("Stopped after 3 tries: the GPU stopped answering.");
      expect(pool.stoppedWords("down", 1)).toBe("Stopped after 1 try: ComfyUI kept stopping on the GPUs.");
      for (const kind of ["silent", "down", "stuck", "other"] as const) expect(pool.stoppedWords(kind, 3)).not.toMatch(/Press Retry/);
    } finally {
      delete process.env.FUOBE_HOSTED_RUNS;
    }
  });
});

describe("an account's run that a worker can't finish", () => {
  /** Words that must never reach an account's run on this install's GPUs. */
  const JARGON = /ComfyUI|GPU|worker|lane|Press Retry|refused|stopped answering|busy|restart/i;
  const MIN_ = 60_000;

  /** An account's run through the run route, waiting for the workers. */
  async function accountRun() {
    const r = await run({ feature: "image", modelId: "oss-z-image-turbo", prompt: "a lighthouse at dusk" });
    expect(r.status, JSON.stringify(r.body)).toBe(201);
    const id = r.body.items![0].id;
    const t = await until(() => taskOf(id));
    expect(t).toBeDefined();
    return { id, taskId: t!.id };
  }
  /** The worker program's own calls (its token), as `fuobe worker run` makes them. */
  function workerApi(token: string) {
    const api = apiAs(routes, null, { authorization: `Bearer ${token}` });
    const json = { "content-type": "application/json" };
    return {
      claim: async () => ((await (await api("/api/gpu/computer/claim", { method: "POST", headers: json, body: JSON.stringify({ device: "worker", running: [] }) })).json()) as { task: { id: string; lease: string; promptId: string } | null }).task,
      report: (t: { id: string; lease: string }, body: Record<string, unknown>) => api(`/api/gpu/computer/tasks/${t.id}/report`, { method: "POST", headers: { ...json, "x-fuobe-lease": t.lease }, body: JSON.stringify(body) }),
    };
  }
  /** The run once it ended, as the account reads it. */
  const ended = async (id: string) =>
    (await until(() => {
      const g = getGeneration(id);
      return g && g.status !== "queued" && g.status !== "running" ? g : null;
    }, 400))!;

  /** Each case, how it ends, and its words for the account (the failure sentence unless it is about the person's files). */
  const cases: [string, (w: ReturnType<typeof workerApi>, taskId: string) => Promise<void>, string?][] = [
    [
      // A failed attempt has one more try (at the back of the line, which is free here); the second fails the run.
      "given back twice (it stalled)",
      async (w) => {
        for (let i = 0; i < 2; i++) {
          const t = (await w.claim())!;
          await w.report(t, { event: "sent", promptId: t.promptId });
          await w.report(t, { event: "give-back", why: "stalled" });
        }
      },
    ],
    [
      "past its run limit twice",
      async (w) => {
        for (let i = 0; i < 2; i++) {
          const t = (await w.claim())!;
          await w.report(t, { event: "sent", promptId: t.promptId });
          await w.report(t, { event: "give-back", why: "run-limit" });
        }
      },
    ],
    [
      "its ComfyUI stopped twice on it",
      async (w) => {
        const t = (await w.claim())!;
        await w.report(t, { event: "sent", promptId: t.promptId });
        await w.report(t, { event: "failed", reason: "input-crashed" });
      },
      // Its files or settings stopped it: said so, since the person can change them.
      FILE_TROUBLE_WORDS.crashed,
    ],
    [
      "a file the worker refused",
      async (w) => {
        const t = (await w.claim())!;
        await w.report(t, { event: "failed", reason: "model-unverified" });
      },
    ],
    [
      "lost three times by workers that went quiet",
      async (w, taskId) => {
        for (let i = 0; i < 3; i++) {
          expect(await w.claim()).not.toBeNull();
          db().prepare("UPDATE relay_workers SET seen_at = ? WHERE kind = 'pool'").run(Date.now() - 3 * MIN_);
          pool.sweepPool(Date.now());
          expect(rowOf(taskId).requeues).toBe(i + 1);
          for (const x of db().prepare("SELECT id FROM relay_workers WHERE kind = 'pool'").all() as { id: string }[]) beatAs(x);
          // The retry rule: the same worker may take it again (no other worker is about).
        }
      },
    ],
  ];

  for (const [name, end, words] of cases) {
    it(`${name}: one plain sentence for the account, the reason for the owner`, async () => {
      const w = standInWorker();
      beatAs(w);
      const { id, taskId } = await accountRun();
      await end(workerApi(w.token), taskId);
      expect(rowOf(taskId).state).toBe("failed");
      const g = await ended(id);
      expect(g.status).toBe("failed");
      // Its files' words when they are about the person's files, else the plain sentence for its kind of failure.
      if (words) expect(g.error).toBe(words);
      else expect(isEngineWords(g.error), g.error ?? "").toBe(true);
      expect(g.error ?? "").not.toMatch(JARGON);
      expect(g.message ?? "").not.toMatch(JARGON);
      expect(g.fix).toBeUndefined();
      // The owner reads what happened (words about the person's files: the same words, nothing more behind them).
      const owner = listAccountRuns({ account: accountId }).items.find((x) => x.id === id);
      if (words) {
        expect(owner?.error).toBe(words);
        expect(owner?.detail).toBeUndefined();
        return;
      }
      expect(owner?.detail).toBeTruthy();
      expect(owner?.detail).not.toBe(g.error);
    });
  }

  it("a node that failed on the worker: the plain sentence for the account, the cleaned words for the owner only", async () => {
    const raw = [
      "VHS failed to extract audio from X:\\fuobe-worker\\work\\input\\fuobe_HFpqXpUDFs__.mp4:",
      "ffmpeg version 7.1-full_build-www.gyan.dev Copyright (c) 2000-2026 the FFmpeg developers",
      "  built with gcc 14.2.0 (Rev1, Built by MSYS2 project)",
      "  configuration: --enable-gpl --enable-version3 --enable-static",
      "  libavutil      59. 39.100 / 59. 39.100",
      "Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'X:\\fuobe-worker\\work\\input\\fuobe_HFpqXpUDFs__.mp4':",
      "Stream map '0:a:0' matches no streams.",
    ].join("\n");
    const w = standInWorker();
    beatAs(w);
    const { id, taskId } = await accountRun();
    const api = workerApi(w.token);
    const t = (await api.claim())!;
    await api.report(t, { event: "sent", promptId: t.promptId });
    const entry = { prompt: [1, t.promptId], status: { status_str: "error", completed: false, messages: [["execution_error", { node_type: "VHS_LoadVideo", node_id: "5", exception_message: raw }]] }, outputs: {} };
    expect((await api.report(t, { event: "finished", entry })).status).toBe(200);
    expect(rowOf(taskId).state).toBe("failed");
    const g = await ended(id);
    expect(g.status).toBe("failed");
    expect(g.error).toBe(ENGINE_WORDS["no-sound"]);
    // What is stored as what the person saw is that sentence too; the engine's words are kept apart, cleaned.
    const stored = db().prepare("SELECT error, detail FROM generations WHERE id = ?").get(id) as { error: string; detail: string };
    expect(stored.error).toBe(ENGINE_WORDS["no-sound"]);
    expect(stored.detail).toContain("VHS_LoadVideo: VHS failed to extract audio from fuobe_HFpqXpUDFs__.mp4");
    expect(stored.detail).toContain("matches no streams");
    expect(stored.detail).not.toMatch(/fuobe-worker|X:\\|ffmpeg version|Copyright|configuration:|libavutil|Input #0/);
    const owner = listAccountRuns({ account: accountId }).items.find((x) => x.id === id);
    expect(owner?.error).toBe(ENGINE_WORDS["no-sound"]);
    expect(owner?.detail).toBe(stored.detail);
    // Nothing of it in the run as the account reads it.
    expect(JSON.stringify(getGeneration(id))).not.toMatch(/VHS|fuobe-worker|ffmpeg/);
  });

  describe("ComfyUI's own reason, for the owner only", () => {
    const visitorList = async () => {
      const r = await generationsRoute.GET(request("/api/generations?limit=50"));
      return JSON.stringify(await r.json());
    };
    const storedOf = (id: string) => db().prepare("SELECT error, detail FROM generations WHERE id = ?").get(id) as { error: string; detail: string | null };

    it("a refusal: the worker's short reason is the owner's detail and \"Why it failed\"; the account reads the plain sentence", async () => {
      const w = standInWorker();
      beatAs(w);
      const { id, taskId } = await accountRun();
      const api = workerApi(w.token);
      const t = (await api.claim())!;
      const body = {
        error: { type: "missing_node_type", message: "Node 'Predict' not found. The custom node may not be installed.", details: "Node ID '#12'", extra_info: { node_id: "12", class_type: "InstantHMRPredict", node_title: "Predict" } },
        node_errors: {},
      };
      const why = refusalReason(body)!;
      expect(why).toBe("ComfyUI refused the job: node InstantHMRPredict (id 12) isn't installed");
      expect((await api.report(t, { event: "refused", status: 400, body, why })).status).toBe(200);
      expect(rowOf(taskId).comfy_why).toBe(why);
      const g = await ended(id);
      expect(g.status).toBe("failed");
      expect(isEngineWords(g.error), g.error ?? "").toBe(true);
      expect(storedOf(id).detail).toBe(why);
      const owner = listAccountRuns({ account: accountId }).items.find((x) => x.id === id);
      expect(owner?.detail).toBe(why);
      const details = runDetails(id)!;
      expect(details.comfyReason).toBe(why);
      expect(details.stored.detail).toBe(why);
      expect(details.jobs.at(-1)?.comfyWhy).toBe(why);
      // Never in what the account reads: its run, its list.
      expect(JSON.stringify(getGeneration(id))).not.toMatch(/InstantHMR|isn't installed|comfyReason|comfyWhy/);
      expect(await visitorList()).not.toMatch(/InstantHMR|isn't installed|comfyReason|comfyWhy|"detail"/);
    });

    it("an execution error: cleaned again on the server (the run's words, keys and folders out)", async () => {
      const w = standInWorker();
      beatAs(w);
      const { id, taskId } = await accountRun();
      const api = workerApi(w.token);
      const t = (await api.claim())!;
      await api.report(t, { event: "sent", promptId: t.promptId });
      const entry = { prompt: [1, t.promptId], status: { status_str: "error", completed: false, messages: [["execution_error", { node_type: "KSampler", node_id: "3", exception_type: "RuntimeError", exception_message: "shapes" }]] }, outputs: {} };
      // A worker that said too much: the server keeps none of it.
      const why = "RuntimeError in KSampler (node 3): bad input a lighthouse at dusk with hf_abcdefghijklmnopqrstu from X:\\fuobe-worker\\ComfyUI\\nodes.py";
      expect((await api.report(t, { event: "finished", entry, why })).status).toBe(200);
      expect(rowOf(taskId).state).toBe("failed");
      const g = await ended(id);
      expect(isEngineWords(g.error), g.error ?? "").toBe(true);
      const detail = storedOf(id).detail!;
      expect(detail).toBe("RuntimeError in KSampler (node 3): bad input [text] with [key] from nodes.py");
      expect(runDetails(id)!.comfyReason).toBe(detail);
      expect(JSON.stringify(getGeneration(id))).not.toMatch(/RuntimeError|KSampler/);
      expect(await visitorList()).not.toMatch(/RuntimeError|KSampler/);
    });

    it("with full details on, the person reads the cleaned reason under the plain sentence; nobody else does", async () => {
      setSetting("runFailureDetail", "full");
      const w = standInWorker();
      beatAs(w);
      const { id } = await accountRun();
      const api = workerApi(w.token);
      const t = (await api.claim())!;
      const body = { error: { type: "missing_node_type", message: "x", details: "Node ID '#12'", extra_info: { node_id: "12", class_type: "InstantHMRPredict" } }, node_errors: {} };
      await api.report(t, { event: "refused", status: 400, body, why: `${refusalReason(body)} from X:\\fuobe-worker\\ComfyUI with hf_abcdefghijklmnopqrstu` });
      const g = await ended(id);
      const why = "ComfyUI refused the job: node InstantHMRPredict (id 12) isn't installed from [folder] with [key]";
      // The plain sentence stays; the cleaned reason comes with it, in the run and the person's own list.
      expect(isEngineWords(g.error), g.error ?? "").toBe(true);
      expect(g.errorDetail).toBe(why);
      const mine = JSON.parse(await visitorList()) as { items: { id: string; errorDetail?: string }[] };
      expect(mine.items.find((x) => x.id === id)?.errorDetail).toBe(why);
      expect(JSON.stringify(mine)).not.toMatch(/fuobe-worker|hf_abc|X:\\\\/);
      // Another account: not in its list, and its look at the run is refused.
      const theirs = await generationsRoute.GET(request("/api/generations?limit=50", { cookie: computerCookie }));
      expect(JSON.stringify(await theirs.json())).not.toMatch(/InstantHMR/);
      const { proxy } = await import("@/proxy");
      const { NextRequest } = await import("next/server");
      const look = proxy(new NextRequest(`http://${HOST}/api/generations/${id}`, { headers: { ...SITE_HEADERS, cookie: computerCookie } }));
      expect([403, 404]).toContain(look.status);
      // A guest is asked to sign in.
      const guest = proxy(new NextRequest(`http://${HOST}/api/generations/${id}`, { headers: SITE_HEADERS }));
      expect(guest.headers.get("x-middleware-next")).not.toBe("1");
      // Friendly only: the same run reads the plain sentence alone.
      setSetting("runFailureDetail", "friendly");
  setSetting("hostedOnlyWhatCanRun", "on");
      expect(getGeneration(id)!.errorDetail).toBeUndefined();
      expect(await visitorList()).not.toMatch(/InstantHMR|errorDetail/);
    });

    it("a job the studio stopped keeps the reason its worker gave before", async () => {
      const w = standInWorker();
      beatAs(w);
      const { id, taskId } = await accountRun();
      const api = workerApi(w.token);
      const t = (await api.claim())!;
      tasks.setTask(taskId, { comfy_why: "CUDA out of memory in KSampler (node 7)" });
      tasks.endTask(t.id, "canceled", { error: "Stopped" });
      const g = await ended(id);
      expect(g.error).not.toMatch(/CUDA|KSampler/);
      expect(storedOf(id).detail).toBe("CUDA out of memory in KSampler (node 7)");
      expect(listAccountRuns({ account: accountId }).items.find((x) => x.id === id)?.detail).toBe("CUDA out of memory in KSampler (node 7)");
      expect(await visitorList()).not.toMatch(/CUDA|KSampler/);
    });

    it("the worker program reads ComfyUI's refusal itself, reports it, and says it when the job ends", async () => {
      const w = standInWorker();
      beatAs(w);
      const { id, taskId } = await accountRun();
      const said: { phase: string; why?: string }[] = [];
      const api = apiAs(routes, null, { authorization: `Bearer ${w.token}` });
      const worker = new ComputerRunner(
        {
          api,
          local: (url, init) => realFetch(url, init),
          socket: (url) => new WebSocket(url) as never,
          sha256,
          origin: "http://localhost:3000",
          sleep: (ms) => new Promise((r) => setTimeout(r, Math.min(ms, 50))),
          compose: (t) => composeOnWorker(t),
          ended: (_t, phase, why) => void said.push({ phase, why }),
          beatExtra: () => ({ protocol: 3, version: "test", local: { paused: false, why: null, until: null }, gpu: { otherApps: [], tempC: null }, inventoryHash: null, warmLane: null, catalogVersion: catalogVersion() }),
        },
        { device: "worker", port: comfy.port, clientId: "worker-test" },
      );
      comfy.opts.failNext = "missing_model";
      await worker.beat();
      await worker.claimAndRun();
      expect(rowOf(taskId).state).toBe("failed");
      const why = rowOf(taskId).comfy_why ?? "";
      expect(why).toMatch(/^ComfyUI refused the job: model file \S+\.\w+ not in the list \(/);
      expect(said.at(-1)).toEqual({ phase: "failed", why });
      const g = await ended(id);
      expect(isEngineWords(g.error), g.error ?? "").toBe(true);
      expect(storedOf(id).detail).toBe(why);
      expect(await visitorList()).not.toMatch(/refused the job|not in the list/);
    });
  });
});

describe("graphs and results for the workers", () => {
  it("every VideoHelperSuite save node saves no metadata", () => {
    const wf = withoutSaveMetadata({
      "1": { class_type: "VHS_VideoCombine", inputs: { save_metadata: true, filename_prefix: "x" } },
      "2": { class_type: "VHS_VideoCombine", inputs: {} },
      "3": { class_type: "SaveImage", inputs: {} },
      "4": { class_type: "VHS_SaveImageSequence", inputs: { save_metadata: true } },
      "5": { class_type: "VHS_LoadVideo", inputs: { video: "x.mp4" } },
      "6": { class_type: "SomeOtherSave", inputs: { save_metadata: true } },
    } as never) as unknown as Record<string, { inputs: Record<string, unknown> }>;
    expect(wf["1"].inputs.save_metadata).toBe(false);
    expect(wf["2"].inputs.save_metadata).toBe(false);
    expect(wf["3"].inputs.save_metadata).toBeUndefined();
    expect(wf["4"].inputs.save_metadata).toBe(false);
    expect(wf["5"].inputs).toEqual({ video: "x.mp4" });
    expect(wf["6"].inputs.save_metadata).toBe(true);
  });

  it("an account's clip run on the workers is queued with no metadata in its clip saves", async () => {
    standInWorker();
    const { comfyProvider } = await import("@/server/providers/comfyui");
    const { TINY_MP4 } = await import("./helpers/fake-comfy-runner");
    const prior = process.env.FUOBE_POOL_MEDIA_INPUTS;
    process.env.FUOBE_POOL_MEDIA_INPUTS = "image,video";
    try {
      const route = { provider: "comfyui", endpoint: "interpolate-rife-vfi", op: "interpolate" } as never;
      const sent = await hosted.runOnPool(accountId, async () => {
        const video = (await comfyProvider.prepareFile!(TINY_MP4, "video/mp4", "clip.mp4", route))!;
        return comfyProvider.submit({ route, input: { video, extra: { multiplier: 2 } } as never, generationId: `vhs-${Date.now()}` } as never);
      });
      const t = rowOf(String((sent.meta as { relay: string }).relay));
      expect(t.principal).toBe(`acct:${accountId}`);
      expect(t.pools).toBe("pool");
      const graph = await graphOfTask(t);
      const saves = Object.values(graph).filter((n) => n.class_type === "VHS_VideoCombine");
      expect(saves.length).toBeGreaterThan(0);
      for (const node of saves) expect(node.inputs.save_metadata).toBe(false);
      expect(serverComfyCalls()).toEqual([]);
    } finally {
      if (prior === undefined) delete process.env.FUOBE_POOL_MEDIA_INPUTS;
      else process.env.FUOBE_POOL_MEDIA_INPUTS = prior;
    }
  });

  it("an owner's job on the workers keeps no more results than its workflow makes, and nothing past the disk's free floor", () => {
    const entry = { status: { status_str: "success", completed: true }, outputs: { "9": { images: Array.from({ length: 40 }, (_, i) => ({ filename: `x_${i}.png`, subfolder: "", type: "output" })) } } };
    const t = { id: "owner-task-0001x", workflow_id: loadManifest().workflows.find((x) => x.outputs.length === 1)!.id, meta_batch: 0, prefix: null, extra_json: JSON.stringify({ outputs: 1 }), pools: "pool" };
    const plan = onComputer("owner", () => planRelayOutputs(t, [entry], false));
    expect("wanted" in plan && plan.wanted.length).toBe(16);
    // The owner's own computer keeps every result, as before.
    const own = onComputer("owner", () => planRelayOutputs({ ...t, pools: null }, [entry], false));
    expect("wanted" in own && own.wanted.length).toBe(40);
    globalThis.__fuobeTestDiskFree = 1;
    try {
      const full = onComputer("owner", () => planRelayOutputs(t, [entry], false));
      expect("error" in full && full.error).toMatch(/no room/);
      // The owner's own computer keeps its results whatever the disk has free, as before.
      const ownLow = onComputer("owner", () => planRelayOutputs({ ...t, pools: null }, [entry], false));
      expect("wanted" in ownLow && ownLow.wanted.length).toBe(40);
    } finally {
      globalThis.__fuobeTestDiskFree = 1e13;
    }
  });

  it("an account's result from its own GPU has the room its files have, as before; on the workers, never past the disk's free floor", () => {
    const entry = { status: { status_str: "success", completed: true }, outputs: { "9": { images: [{ filename: "x_0.png", subfolder: "", type: "output" }] } } };
    const t = { id: "acct-task-00001x", workflow_id: loadManifest().workflows.find((x) => x.outputs.length === 1)!.id, meta_batch: 0, prefix: null, extra_json: JSON.stringify({ outputs: 1 }), pools: null };
    // The disk has a megabyte more than the free space it keeps.
    globalThis.__fuobeTestDiskFree = diskFloorBytes() + 2 ** 20;
    try {
      const own = onComputer(`acct:${accountId}`, () => planRelayOutputs(t, [entry], false));
      const room = Math.min(OWN_OUTPUT_LIMITS.maxBytes, accountTotalBytes(accountId) - accountUsage(accountId).total);
      expect(room).toBeGreaterThan(2 ** 20);
      expect("wanted" in own && own.wanted.map((w) => w.maxBytes)).toEqual([room]);
      const pooled = onComputer(`acct:${accountId}`, () => planRelayOutputs({ ...t, pools: "pool" }, [entry], false));
      expect("wanted" in pooled && pooled.wanted.map((w) => w.maxBytes)).toEqual([2 ** 20]);
      // Right at the floor: its own GPU's result still has its room (never told its files are full).
      globalThis.__fuobeTestDiskFree = diskFloorBytes();
      const atFloor = onComputer(`acct:${accountId}`, () => planRelayOutputs(t, [entry], false));
      expect("wanted" in atFloor && atFloor.wanted.length).toBe(1);
    } finally {
      globalThis.__fuobeTestDiskFree = 1e13;
    }
  });
});

describe("nothing of a run on the workers goes to a ComfyUI address", () => {
  it("an account's picture is kept for its task with only its pixels' parts (the worker makes it again from its pixels), and never decoded here", async () => {
    const sharp = (await import("sharp")).default;
    const jpeg = await sharp({ create: { width: 8, height: 8, channels: 3, background: "#c33" } })
      .jpeg()
      .withExif({ IFD0: { Copyright: "private note", Artist: "someone" } })
      .toBuffer();
    expect((await sharp(jpeg).metadata()).exif).toBeDefined();
    const { comfyProvider, mediaInfoFor } = await import("@/server/providers/comfyui");
    const staged = await hosted.runOnPool(accountId, () => comfyProvider.prepareFile!(jpeg, "image/jpeg", "me.jpg"));
    const row = db().prepare("SELECT principal, content_type FROM relay_inputs WHERE name = ?").get(staged) as { principal: string; content_type: string };
    expect(row).toEqual({ principal: `acct:${accountId}`, content_type: "image/jpeg" });
    const fs = await import("node:fs");
    const path = await import("node:path");
    // Never decoded here: the same JPEG, byte for byte the same size, with what it carried besides its pixels blanked.
    const kept = fs.readFileSync(path.join(dir, "relay", "in", staged));
    expect(kept.length).toBe(jpeg.length);
    expect(kept.subarray(0, 2).equals(jpeg.subarray(0, 2))).toBe(true);
    expect(kept.includes(Buffer.from("private note"))).toBe(false);
    expect((await sharp(kept).metadata()).exif).toBeUndefined();
    // Its size as it shows (turned by its orientation), as the worker's upright copy has it.
    expect(hosted.runOnPool(accountId, () => mediaInfoFor(staged))).toMatchObject({ width: 8, height: 8 });
    // Something that isn't a picture is never kept as one.
    await expect(hosted.runOnPool(accountId, () => comfyProvider.prepareFile!(Buffer.from("not a picture"), "image/jpeg", "x.jpg"))).rejects.toThrow();
    expect(serverComfyCalls()).toEqual([]);
  });

  it("a run on the workers with no account behind it never sends a file or a prompt", async () => {
    const { comfyProvider } = await import("@/server/providers/comfyui");
    const png = Buffer.from("89504e470d0a1a0a", "hex");
    await expect(hosted.runOnPool(null, () => comfyProvider.prepareFile!(png, "image/png", "x.png"))).rejects.toThrow();
    const route = { provider: "comfyui", endpoint: loadManifest().workflows[0].id, op: "t2i" } as never;
    await expect(hosted.runOnPool(null, () => comfyProvider.submit({ route, input: { prompt: "a lighthouse" } as never, generationId: "guest-run-0001" } as never))).rejects.toThrow();
    expect(serverComfyCalls()).toEqual([]);
  });

  it("a run sent to this install's own ComfyUI before Settings moved to the workers is still followed there", async () => {
    const { comfyProvider } = await import("@/server/providers/comfyui");
    setSetting("comfyUrl", DEAD);
    // (It answers nothing here: the poll says so, and never goes round in circles.)
    await hosted
      .runOnPool(accountId, () => comfyProvider.poll({ externalId: crypto.randomUUID(), meta: { workflowId: loadManifest().workflows[0].id, generationId: "moved-0001" }, model: undefined } as never))
      .catch((e: Error) => expect(e.message).not.toMatch(/call stack/i));
    // Asked of the address it was sent to, not of the workers' line.
    expect(fetched.some((u) => u.startsWith(`${DEAD}/`))).toBe(true);
  });
});

describe("what a run on the workers is shaped by", () => {
  it("the workers' facts (the least VRAM and RAM of those allowed its lane), never the account's own computer's", async () => {
    const big = standInWorker("big");
    const small = standInWorker("small");
    const facts = (w: { id: string }, f: Record<string, unknown>) => db().prepare("UPDATE relay_workers SET facts_json = ? WHERE id = ?").run(JSON.stringify(f), w.id);
    facts(big, { vramBytes: 96 * 2 ** 30, ramBytes: 128 * 2 ** 30, device: "cuda:0 Test GPU" });
    facts(small, { vramBytes: 24 * 2 ** 30, ramBytes: 64 * 2 ** 30, device: "cuda:0 Other GPU" });
    expect(pool.poolFacts(null)).toEqual({ vramBytes: 24 * 2 ** 30, ramBytes: 64 * 2 ** 30, device: "cuda:0 Test GPU" });
    // A lane only the big one is allowed: its facts alone.
    const lane = allLanes(loadIndex(), sha)[0].lane;
    pool.patchWorker(small.id, { allow: {} });
    expect(pool.poolFacts(lane).vramBytes).toBe(96 * 2 ** 30);
    // Workers that name different kinds of device: none is assumed.
    pool.patchWorker(small.id, { allow: { [lane]: { delayMs: 0 } } });
    facts(small, { vramBytes: 24 * 2 ** 30, device: "mps" });
    expect(pool.poolFacts(null).device).toBeUndefined();
    facts(small, { vramBytes: 24 * 2 ** 30, ramBytes: 64 * 2 ** 30, device: "cuda:0 Other GPU" });

    // The account's own computer said something else: a run on the workers never reads it; a run on that computer does.
    tasks.beat(`acct:${computerAccount}`, { device: "device-own-facts", takeover: true, facts: { ramBytes: 2 ** 30, vramBytes: 2 ** 30, device: "mps" } });
    const { comfyRam, comfyDevice } = await import("@/server/providers/comfyui");
    expect(hosted.runOnPool(computerAccount, () => comfyRam())).toBe(64 * 2 ** 30);
    expect(await hosted.runOnPool(computerAccount, () => comfyDevice())).toBe("cuda");
    expect(onComputer(`acct:${computerAccount}`, () => comfyRam())).toBe(2 ** 30);
    expect(await onComputer(`acct:${computerAccount}`, () => comfyDevice())).toBe("mps");
    expect(serverComfyCalls()).toEqual([]);
  });
});

describe("files for runs on the workers", () => {
  it("an account may add only the kinds of file the workers take, whatever the policy allows", async () => {
    const uploadsRoute = await import("@/app/api/uploads/route");
    const { TINY_MP4, TINY_PNG } = await import("./helpers/fake-comfy-runner");
    standInWorker();
    process.env.FUOBE_BILLING_URL = fake.url;
    process.env.FUOBE_BILLING_TOKEN = "test-billing-token";
    const before = fake.policy;
    fake.policy = { ...before, hosted: true, uploads: { image: true, video: true, audio: true }, ttlS: 0 };
    const prior = process.env.FUOBE_POOL_MEDIA_INPUTS;
    delete process.env.FUOBE_POOL_MEDIA_INPUTS;
    try {
      const up = (bytes: Buffer, type: string, name: string) =>
        uploadsRoute.POST(new Request(`http://${HOST}/api/uploads`, { method: "POST", headers: { ...SITE_HEADERS, cookie, "content-type": type, "x-file-name": name, "content-length": String(bytes.length) }, body: new Uint8Array(bytes) }));
      expect((await up(TINY_PNG, "image/png", "me.png")).status).toBe(201);
      expect((await up(TINY_MP4, "video/mp4", "me.mp4")).status).toBe(403);
      // The workers take clips too: so may the account.
      process.env.FUOBE_POOL_MEDIA_INPUTS = "image,video";
      expect((await up(TINY_MP4, "video/mp4", "me.mp4")).status).toBe(201);
    } finally {
      fake.policy = before;
      if (prior === undefined) delete process.env.FUOBE_POOL_MEDIA_INPUTS;
      else process.env.FUOBE_POOL_MEDIA_INPUTS = prior;
      delete process.env.FUOBE_BILLING_URL;
      delete process.env.FUOBE_BILLING_TOKEN;
    }
  });
});

describe("what the site can make now", () => {
  const H3 = "oss-minimax-h3";
  const MINUTE = 60_000;
  /** Two workers: a large one with no limit of its own, and a small one that makes frames over 0.5 MP up to 5.2 s. */
  async function twoWorkers() {
    const big = standInWorker("big");
    const small = standInWorker("small");
    const lanes = allLanes(loadIndex(), sha).map((l) => l.lane);
    db()
      .prepare("UPDATE relay_workers SET allow_json = ? WHERE id = ?")
      .run(JSON.stringify(Object.fromEntries(lanes.map((l) => [l, { delayMs: 0, bigFrom: 0.5, bigMaxSeconds: 5.2 }]))), small.id);
    const capacity = await import("@/server/capacity");
    const lib = await import("@/lib/capacity");
    const view = () => {
      capacity.forgetCapacity();
      return capacity.capacityView();
    };
    const limit = () => lib.limitAt(view(), H3, "t2v", "768p", "16:9");
    /** The large worker paused on the website, its last beat while on `agoMs` ago (the few seconds kept are skipped). */
    const bigAway = (agoMs: number) => {
      db().prepare("UPDATE relay_workers SET paused_server = 1, available_at = ? WHERE id = ?").run(Date.now() - agoMs, big.id);
      capacity.forgetCapacity();
    };
    const bigBack = () => {
      db().prepare("UPDATE relay_workers SET paused_server = 0, seen_at = ? WHERE id = ?").run(Date.now(), big.id);
      capacity.forgetCapacity();
    };
    return { big, small, view, limit, bigAway, bigBack, capacity };
  }
  const h3Run = (duration: number) => run({ feature: "video", modelId: H3, op: "t2v", prompt: "a lighthouse at dusk", params: { duration, resolution: "768p", aspectRatio: "16:9" } });
  const askedOf = (id: string) => (JSON.parse((db().prepare("SELECT request_json FROM generations WHERE id = ?").get(id) as { request_json: string }).request_json) as { params?: { duration?: number; resolution?: string } }).params;

  it("keeps what a worker makes through a short drop, takes it away after 5 minutes gone, and gives it back as soon as the worker is on", async () => {
    const { limit, bigAway, bigBack, view } = await twoWorkers();
    // Both on: the large one makes any length.
    expect(view().m[H3]).toBeUndefined();
    expect(limit()).toBeNull();
    // Gone 2 minutes (a restart, a power cut): nothing changes.
    bigAway(2 * MINUTE);
    expect(limit()).toBeNull();
    // Gone 6 minutes: only what the small one makes.
    bigAway(6 * MINUTE);
    expect(limit()).toBe(5.2);
    expect(view().m[H3]?.t2v).toBeDefined();
    // Back: at once.
    bigBack();
    expect(limit()).toBeNull();
  });

  it("a beat while on marks the worker; a beat while paused on its PC doesn't", async () => {
    const { big } = await twoWorkers();
    const at = () => (db().prepare("SELECT available_at FROM relay_workers WHERE id = ?").get(big.id) as { available_at: number | null }).available_at;
    const beat = (paused: boolean, now: number) =>
      pool.workerBeat(
        db().prepare("SELECT * FROM relay_workers WHERE id = ?").get(big.id) as never,
        pool.cleanBeat({ device: `wkr-${big.id}`, protocol: 3, version: "1", local: { paused, why: paused ? "here" : null, until: null }, gpu: {}, running: [], stopped: [], catalogVersion: catalogVersion() }),
        now,
      );
    const t0 = Date.now();
    beat(false, t0);
    expect(at()).toBe(t0);
    beat(true, t0 + 5_000);
    expect(at()).toBe(t0);
  });

  it("a new run steps down to the nearest length made now (shorter first); one already in line is never changed", async () => {
    const { bigAway, bigBack } = await twoWorkers();
    // In line while the large worker is on: 10 s as asked.
    const before = await h3Run(10);
    expect(before.status, JSON.stringify(before.body)).toBe(201);
    const queuedId = before.body.items![0].id;
    expect(askedOf(queuedId)?.duration).toBe(10);
    // The large worker gone for 6 minutes: a new 10 s or 15 s run is made at 5 s, a 3 s one as asked.
    bigAway(6 * MINUTE);
    for (const [asked, made] of [
      [10, 5],
      [15, 5],
      [3, 3],
    ] as const) {
      const r = await h3Run(asked);
      expect(r.status, JSON.stringify(r.body)).toBe(201);
      expect(askedOf(r.body.items![0].id)).toMatchObject({ duration: made, resolution: "768p" });
      // (Stopped once checked: an account has a few runs going at most.)
      db().prepare("UPDATE generations SET status = 'canceled' WHERE id = ?").run(r.body.items![0].id);
      db().prepare("UPDATE relay_tasks SET state = 'canceled' WHERE generation_id = ?").run(r.body.items![0].id);
    }
    // The run already in line keeps its length and its place.
    await tick();
    expect(askedOf(queuedId)?.duration).toBe(10);
    expect(getGeneration(queuedId)?.status).toBe("queued");
    expect(taskOf(queuedId)?.state).toBe("waiting");
    bigBack();
    const after = await h3Run(10);
    expect(after.status, JSON.stringify(after.body)).toBe(201);
    expect(askedOf(after.body.items![0].id)?.duration).toBe(10);
  });

  it("a size no worker on now makes is refused in the plain words for a size not offered", async () => {
    const { small, bigAway, view } = await twoWorkers();
    db().prepare("UPDATE relay_workers SET ceiling_json = ? WHERE id = ?").run(JSON.stringify({ maxMegapixels: 0.3 }), small.id);
    bigAway(6 * MINUTE);
    expect(view().m[H3]?.t2v?.["768p"]).toBe(0);
    const { POOL_TOO_BIG } = await import("@/server/providers/comfyui");
    const r = await h3Run(5);
    expect(r.status).toBe(400);
    expect(r.body.error).toBe(POOL_TOO_BIG);
    expect(r.body.error).not.toMatch(/GPU|worker/i);
  });

  it("Settings → Workers' switch: on, only what is made now; off, every length and size as the models offer them", async () => {
    const { limit, bigAway, view } = await twoWorkers();
    const settingsRoute = await import("@/app/api/settings/route");
    const put = (v: string) => settingsRoute.PUT(new Request(`http://${HOST}/api/settings`, { method: "PUT", body: JSON.stringify({ hostedOnlyWhatCanRun: v }) }));
    bigAway(6 * MINUTE);
    expect((await put("off")).status).toBe(200);
    expect(getSetting("hostedOnlyWhatCanRun")).toBe("off");
    expect(view()).toEqual({ m: {} });
    expect(limit()).toBeNull();
    const off = await h3Run(10);
    expect(off.status, JSON.stringify(off.body)).toBe(201);
    expect(askedOf(off.body.items![0].id)?.duration).toBe(10);
    expect((await put("on")).status).toBe(200);
    expect(limit()).toBe(5.2);
    const on = await h3Run(10);
    expect(askedOf(on.body.items![0].id)?.duration).toBe(5);
    expect((await put("maybe")).status).toBe(400);
  });
});