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.

Your keys: add, check and remove a key

Adarshfa1e410f2f31
diff --git a/src/app/api/account/keys/[provider]/route.ts b/src/app/api/account/keys/[provider]/route.ts
new file mode 100644
index 00000000..ca00a016
--- /dev/null
+++ b/src/app/api/account/keys/[provider]/route.ts
@@ -0,0 +1,73 @@
+import { z } from "zod";
+import { isCloudProvider, NOT_ACCOUNT_KEYS, type CloudProvider } from "@/lib/your-keys";
+import { accountKeyInfo, cleanKey, removeAccountKey, saveAccountKey } from "@/server/account-keys";
+import { accountKeyRecord, requestAccount } from "@/server/auth";
+import { hit, unhit } from "@/server/rate";
+import { SecretBoxError } from "@/server/secret-box";
+import { checkKey, runsGoingMessage, runsOnKey, stopRunsOnKey } from "@/server/your-keys";
+
+/**
+ * Settings → Your keys: add, replace or remove the account's own fal.ai key or Replicate token. PUT checks the key with
+ * the provider first (a free call that runs nothing; `?test=1` checks only), then seals and saves it. A key is never
+ * logged, and never answered with: only its last 4 characters. Removing or replacing a key that runs are going on asks
+ * first (409 runs-going); with `stopRuns` they are stopped at the provider, with the old key, before it changes.
+ */
+const NO_STORE = { "Cache-Control": "no-store" };
+const answer = (status: number, body: Record<string, unknown>, headers: Record<string, string> = {}) => Response.json(body, { status, headers: { ...NO_STORE, ...headers } });
+
+/** Checks a minute: per account, and for every account together (the server is no key checker for stolen keys). */
+const CHECKS_PER_MINUTE = 10;
+const CHECKS_PER_MINUTE_ALL = 120;
+
+const Body = z.object({ key: z.string().max(400), stopRuns: z.boolean().optional() });
+
+/** The account asking and the provider named, or the answer for anyone else. */
+async function who(request: Request, ctx: RouteContext<"/api/account/keys/[provider]">): Promise<{ refused: Response } | { id: string; provider: CloudProvider }> {
+ const a = requestAccount(request);
+ if (!a) return { refused: answer(400, { error: NOT_ACCOUNT_KEYS }) };
+ const { provider } = await ctx.params;
+ if (!isCloudProvider(provider)) return { refused: answer(404, { error: "Keys are for fal.ai or Replicate" }) };
+ return { id: a.id, provider };
+}
+
+export async function PUT(request: Request, ctx: RouteContext<"/api/account/keys/[provider]">): Promise<Response> {
+ const w = await who(request, ctx);
+ if ("refused" in w) return w.refused;
+ const { id, provider } = w;
+ const mine = hit(`keys:${id}`, CHECKS_PER_MINUTE, 60_000);
+ const all = mine.ok ? hit("keys:all", CHECKS_PER_MINUTE_ALL, 60_000) : mine;
+ if (!all.ok) {
+ if (mine.ok) unhit(`keys:${id}`);
+ return answer(429, { error: "Too many checks. Wait a minute." }, { "Retry-After": String(all.retryAfterS) });
+ }
+ const parsed = Body.safeParse(await request.json().catch(() => null));
+ const key = parsed.success ? cleanKey(parsed.data.key) : null;
+ if (!parsed.success || !key) return answer(400, { error: "Paste the whole key, without spaces." });
+ const check = await checkKey(provider, key);
+ if (!check.ok) return answer(check.status, { error: check.error });
+ if (new URL(request.url).searchParams.get("test") === "1") return answer(200, { provider, ok: true, detail: check.detail });
+ // Replacing a key runs are going on: they stop first, with the key they were sent with.
+ const going = accountKeyRecord(id, provider) ? runsOnKey(id, provider) : [];
+ if (going.length && !parsed.data.stopRuns) return answer(409, { code: "runs-going", count: going.length, error: runsGoingMessage(going.length, true) });
+ if (going.length) await stopRunsOnKey(id, provider);
+ try {
+ const info = saveAccountKey(id, provider, key);
+ return answer(200, { provider, ...info, detail: check.detail });
+ } catch (e) {
+ if (e instanceof SecretBoxError) return answer(503, { error: e.message });
+ throw e;
+ }
+}
+
+export async function DELETE(request: Request, ctx: RouteContext<"/api/account/keys/[provider]">): Promise<Response> {
+ const w = await who(request, ctx);
+ if ("refused" in w) return w.refused;
+ const { id, provider } = w;
+ const body = (await request.json().catch(() => null)) as { stopRuns?: unknown } | null;
+ const stopRuns = body?.stopRuns === true || new URL(request.url).searchParams.get("stopRuns") === "1";
+ const going = accountKeyRecord(id, provider) ? runsOnKey(id, provider) : [];
+ if (going.length && !stopRuns) return answer(409, { code: "runs-going", count: going.length, error: runsGoingMessage(going.length, false) });
+ if (going.length) await stopRunsOnKey(id, provider);
+ removeAccountKey(id, provider);
+ return answer(200, { keys: accountKeyInfo(id) });
+}
diff --git a/src/app/api/account/keys/route.ts b/src/app/api/account/keys/route.ts
new file mode 100644
index 00000000..3a8c5af2
--- /dev/null
+++ b/src/app/api/account/keys/route.ts
@@ -0,0 +1,17 @@
+import { connection } from "next/server";
+import { NOT_ACCOUNT_KEYS, type YourKeys } from "@/lib/your-keys";
+import { accountEngineMode, accountKeyInfo, paidAllowed } from "@/server/account-keys";
+import { requestAccount } from "@/server/auth";
+
+/**
+ * Settings → Your keys (public sign-up): the account's own fal.ai key and Replicate token, as their last 4 characters,
+ * when they were added, and whether this server can still open them. Never a key. Accounts only: the studio's own keys
+ * are in Settings → Paid API configuration. PUT and DELETE are on /api/account/keys/[provider].
+ */
+export async function GET(request: Request) {
+ await connection();
+ const a = requestAccount(request);
+ if (!a) return Response.json({ error: NOT_ACCOUNT_KEYS }, { status: 400, headers: { "Cache-Control": "no-store" } });
+ const body: YourKeys = { keys: accountKeyInfo(a.id), engineMode: accountEngineMode(a.id), paidAllowed: paidAllowed() };
+ return Response.json(body, { headers: { "Cache-Control": "no-store" } });
+}
diff --git a/src/lib/your-keys.ts b/src/lib/your-keys.ts
index d3194fa3..10fc47ec 100644
--- a/src/lib/your-keys.ts
+++ b/src/lib/your-keys.ts
@@ -49,4 +49,6 @@ export const addKeyMessage = (p: CloudProvider) => `Add your ${CLOUD_KEY[p].labe
export const keyRejectedMessage = (p: CloudProvider) => `${CLOUD_KEY[p].label} didn't accept your key. Check it in Settings → Your keys.`;
export const balanceMessage = (p: CloudProvider) => `Your ${CLOUD_KEY[p].label} balance has run out. Top it up on ${CLOUD_KEY[p].label}, then run again.`;
export const keyRemovedMessage = (p: CloudProvider) => `Your ${CLOUD_KEY[p].label} key was removed, so this run stopped.`;
+/** The owner (or anyone not signed in as an account) on /api/account/keys. */
+export const NOT_ACCOUNT_KEYS = "Your keys are for accounts. The studio's own keys are in Settings → Paid API configuration.";
export const OPEN_ONLY_MESSAGE = "This studio runs open-source models only right now, so your keys aren't used.";
diff --git a/src/server/your-keys.ts b/src/server/your-keys.ts
new file mode 100644
index 00000000..f04a3fe5
--- /dev/null
+++ b/src/server/your-keys.ts
@@ -0,0 +1,44 @@
+import "server-only";
+import { settingProblem } from "@/lib/connections";
+import { CLOUD_KEY, type CloudProvider } from "@/lib/your-keys";
+import { db } from "./db";
+import { cancelGeneration } from "./generations";
+import { testFalKey, type KeyCheck } from "./providers/fal";
+import { testReplicateKey } from "./providers/replicate";
+
+/**
+ * Settings → Your keys (src/app/api/account/keys): checking a key an account typed, and the runs going on a key it
+ * removes or replaces. The key itself is only ever sent to its provider; nothing here logs it or answers with it.
+ */
+
+/** A key checked with its provider before it is saved: a free call that runs nothing, with the typed key only. */
+export async function checkKey(p: CloudProvider, key: string): Promise<{ ok: true; detail: string } | { ok: false; error: string; status: number }> {
+ const { label } = CLOUD_KEY[p];
+ const check: KeyCheck = p === "fal" ? await testFalKey(key) : await testReplicateKey(key);
+ if (check.ok) return { ok: true, detail: check.detail };
+ if (check.reason === "rejected") {
+ const hint = settingProblem(CLOUD_KEY[p].setting, key);
+ return { ok: false, status: 422, error: `${label} didn't accept this key. Copy it again from ${label}'s API keys page.${hint ? ` (${hint}.)` : ""}` };
+ }
+ if (check.reason === "unreachable") return { ok: false, status: 502, error: `Couldn't reach ${label} to check the key, so it wasn't saved. Try again in a moment.` };
+ return { ok: false, status: 502, error: `${label} answered ${check.status ?? "oddly"}, so the key couldn't be checked. Try again in a moment.` };
+}
+
+/** The account's runs going on a provider now (queued, running, or a step waiting to be sent there). */
+export function runsOnKey(accountId: string, p: CloudProvider): string[] {
+ return db()
+ .prepare("SELECT id FROM generations WHERE status IN ('queued', 'running') AND provider = ? AND json_extract(request_json, '$.account') = ?")
+ .pluck()
+ .all(p, accountId) as string[];
+}
+
+/** Stop the account's runs on a provider, at the provider, with the key still saved (before it is removed or replaced). */
+export async function stopRunsOnKey(accountId: string, p: CloudProvider): Promise<number> {
+ const ids = runsOnKey(accountId, p);
+ for (const id of ids) await cancelGeneration(id);
+ return ids.length;
+}
+
+/** What the 409 says when runs are going on the key. */
+export const runsGoingMessage = (n: number, replacing: boolean) =>
+ `${n === 1 ? "1 run is" : `${n} runs are`} using this key now. ${replacing ? "Replacing" : "Removing"} it stops ${n === 1 ? "it" : "them"}.`;
diff --git a/tests/account-keys-routes.test.ts b/tests/account-keys-routes.test.ts
new file mode 100644
index 00000000..33cc6acd
--- /dev/null
+++ b/tests/account-keys-routes.test.ts
@@ -0,0 +1,252 @@
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
+import { NextRequest } from "next/server";
+import { keysSeen, startFakeFal, startFakeReplicate, type FakeCloud } from "./helpers/fake-cloud";
+
+/**
+ * Settings → Your keys, the routes: an account adds, checks, replaces and removes its own fal.ai key or Replicate token.
+ * A key is checked with the provider (the typed key only, never another) before it is saved, and no answer, list or
+ * export ever holds a key or its sealed value. Removing or replacing a key that runs are going on asks first, then
+ * stops them with the old key.
+ */
+vi.mock("next/server", async (importOriginal) => ({ ...(await importOriginal<typeof import("next/server")>()), connection: async () => {} }));
+
+const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fuobe-keys-routes-"));
+process.env.FUOBE_DATA_DIR = dir;
+process.env.FUOBE_PUBLIC_SIGNUP = "1";
+delete process.env.FUOBE_PASSWORD;
+delete process.env.FUOBE_ENGINE_MODE;
+process.env.FAL_KEY = "owner-fal-env";
+process.env.REPLICATE_API_TOKEN = "owner-rep-env";
+globalThis.__fuobeTestDiskFree = 1e13;
+
+const auth = await import("@/server/auth");
+const keysRoute = await import("@/app/api/account/keys/route");
+const keyRoute = await import("@/app/api/account/keys/[provider]/route");
+const generationsRoute = await import("@/app/api/generations/route");
+const adminRoute = await import("@/app/api/admin/accounts/route");
+const meRoute = await import("@/app/api/auth/me/route");
+const statusRoute = await import("@/app/api/status/route");
+const modelsRoute = await import("@/app/api/models/route");
+const { proxy } = await import("@/proxy");
+const { setSetting } = await import("@/server/settings");
+const { resetRates } = await import("@/server/rate");
+const { db } = await import("@/server/db");
+const { accountMayChange, accountOwnRead } = await import("@/lib/guest-access");
+
+const GOOD_FAL = "acct-key-id-7d1e:acct-key-secret-3f9a";
+const NEW_FAL = "acct-key-id-8e2f:acct-key-secret-4c0b";
+const BAD_FAL = "wrong-key-id-0000:wrong-key-secret-0000";
+const GOOD_REP = "r8_accountReplicateToken5c21";
+
+let fal: FakeCloud;
+let rep: FakeCloud;
+let A = "";
+let cookie = "";
+beforeAll(async () => {
+ fal = await startFakeFal();
+ rep = await startFakeReplicate();
+ globalThis.__fuobeTestFalUrl = fal.url;
+ globalThis.__fuobeTestReplicateUrl = rep.url;
+ for (const k of [GOOD_FAL, NEW_FAL, "owner-fal-db", "owner-fal-env"]) fal.keys[k] = "good";
+ for (const k of [GOOD_REP, "owner-rep-db", "owner-rep-env"]) rep.keys[k] = "good";
+ fal.keys["outage-key-id-1111:outage-secret-1111"] = "outage";
+ setSetting("falKey", "owner-fal-db");
+ setSetting("replicateToken", "owner-rep-db");
+ auth.setPassword("install password");
+ A = auth.createAccount("[email protected]", "long enough").id;
+ cookie = `${auth.SESSION_COOKIE}=${auth.createSession(A).value}`;
+});
+afterAll(async () => {
+ globalThis.__fuobeTestFalUrl = undefined;
+ globalThis.__fuobeTestReplicateUrl = undefined;
+ globalThis.__fuobeTestDiskFree = undefined;
+ await fal.close();
+ await rep.close();
+ try {
+ fs.rmSync(dir, { recursive: true, force: true });
+ } catch {}
+});
+beforeEach(() => {
+ resetRates();
+ fal.log.length = 0;
+ rep.log.length = 0;
+ fal.pollsToFinish = 2;
+});
+
+const ctx = (provider: string) => ({ params: Promise.resolve({ provider }) });
+const req = (url: string, method: string, body?: unknown, who = cookie) =>
+ new Request(`http://localhost:3000${url}`, { method, headers: { cookie: who, host: "localhost:3000", "content-type": "application/json" }, ...(body !== undefined && { body: JSON.stringify(body) }) });
+const put = (provider: string, body: unknown, q = "", who = cookie) => keyRoute.PUT(req(`/api/account/keys/${provider}${q}`, "PUT", body, who), ctx(provider));
+const del = (provider: string, body?: unknown) => keyRoute.DELETE(req(`/api/account/keys/${provider}`, "DELETE", body), ctx(provider));
+const get = async () => (await keysRoute.GET(req("/api/account/keys", "GET"))).json();
+const ownerCookie = () => `${auth.SESSION_COOKIE}=${auth.createSession().value}`;
+/** Everything the account, the owner and their browsers can read, as text. */
+async function everythingReadable(): Promise<string> {
+ const texts = [
+ JSON.stringify(await get()),
+ await (await meRoute.GET(req("/api/auth/me", "GET"))).text(),
+ await (await statusRoute.GET(req("/api/status", "GET"))).text(),
+ await (await modelsRoute.GET(req("/api/models?kind=image", "GET"))).text(),
+ await (await adminRoute.GET(req("/api/admin/accounts", "GET", undefined, ownerCookie()))).text(),
+ await (await adminRoute.GET(req("/api/admin/accounts?format=csv", "GET", undefined, ownerCookie()))).text(),
+ JSON.stringify(auth.listAccounts()),
+ ];
+ return texts.join("\n");
+}
+const dataFiles = () =>
+ fs
+ .readdirSync(dir, { recursive: true })
+ .map((f) => path.join(dir, String(f)))
+ .filter((f) => fs.statSync(f).isFile())
+ .map((f) => ({ f, text: fs.readFileSync(f).toString("latin1") }));
+
+describe("adding a key", () => {
+ it("refuses a key the provider rejects, says how to fix it, saves nothing and never repeats the key", async () => {
+ const r = await put("fal", { key: BAD_FAL });
+ expect(r.status).toBe(422);
+ const text = await r.text();
+ expect(JSON.parse(text).error).toBe("fal.ai didn't accept this key. Copy it again from fal.ai's API keys page.");
+ expect(text).not.toContain("wrong-key");
+ // A key that doesn't look like one gets the format hint too.
+ const half = await put("fal", { key: "fal_only_one_half" });
+ expect((await half.json()).error).toMatch(/key_id:key_secret/);
+ // The check sent the typed key, and only that key, to fal.ai.
+ expect(keysSeen(fal.log)).toEqual([`Key ${BAD_FAL}`, "Key fal_only_one_half"]);
+ expect((await get()).keys.fal).toBeNull();
+ });
+
+ it("says when the provider can't be reached or answers oddly", async () => {
+ const odd = await put("fal", { key: "outage-key-id-1111:outage-secret-1111" });
+ expect(odd.status).toBe(502);
+ expect((await odd.json()).error).toBe("fal.ai answered 503, so the key couldn't be checked. Try again in a moment.");
+ globalThis.__fuobeTestFalUrl = "http://127.0.0.1:9";
+ try {
+ const down = await put("fal", { key: GOOD_FAL });
+ expect(down.status).toBe(502);
+ expect((await down.json()).error).toBe("Couldn't reach fal.ai to check the key, so it wasn't saved. Try again in a moment.");
+ } finally {
+ globalThis.__fuobeTestFalUrl = fal.url;
+ }
+ expect((await get()).keys.fal).toBeNull();
+ });
+
+ it("checks only, with ?test=1", async () => {
+ const r = await put("fal", { key: GOOD_FAL }, "?test=1");
+ expect(await r.json()).toEqual({ provider: "fal", ok: true, detail: "Key accepted" });
+ expect((await get()).keys.fal).toBeNull();
+ });
+
+ it("saves a key the provider accepts, answers with its last 4 characters only", async () => {
+ const r = await put("fal", { key: ` ${GOOD_FAL} ` });
+ expect(r.status).toBe(200);
+ const text = await r.text();
+ expect(JSON.parse(text)).toEqual({ provider: "fal", last4: "3f9a", addedAt: expect.any(Number), readable: true, detail: "Key accepted" });
+ expect(text).not.toContain("acct-key");
+ const rr = await put("replicate", { key: GOOD_REP });
+ expect(await rr.json()).toMatchObject({ provider: "replicate", last4: "5c21", detail: "Signed in as alice" });
+ expect(await get()).toEqual({
+ keys: { fal: { last4: "3f9a", addedAt: expect.any(Number), readable: true }, replicate: { last4: "5c21", addedAt: expect.any(Number), readable: true } },
+ engineMode: "both",
+ paidAllowed: true,
+ });
+ });
+
+ it("never shows the key or its sealed value anywhere: answers, lists, exports, status, models, files", async () => {
+ const sealed = auth.accountKeyRecord(A, "fal")!.sealed;
+ const all = await everythingReadable();
+ for (const secret of [GOOD_FAL, "acct-key-id-7d1e", "acct-key-secret-3f9a", GOOD_REP, sealed, "owner-fal-db", "owner-rep-db"]) expect(all).not.toContain(secret);
+ expect(all).toContain('"keys":["fal","replicate"]');
+ expect(all).toMatch(/routes-a@example\.com,[^,]+,[^,]+,false,true,true,false/);
+ for (const { f, text } of dataFiles()) for (const secret of [GOOD_FAL, "acct-key-secret-3f9a", GOOD_REP]) expect(text.includes(secret), f).toBe(false);
+ });
+
+ it("allows 10 checks a minute per account", async () => {
+ resetRates();
+ for (let i = 0; i < 10; i++) await put("fal", { key: GOOD_FAL }, "?test=1");
+ const r = await put("fal", { key: GOOD_FAL }, "?test=1");
+ expect(r.status).toBe(429);
+ expect((await r.json()).error).toBe("Too many checks. Wait a minute.");
+ });
+});
+
+describe("who may use the routes", () => {
+ it("the owner is told where the studio's keys are; an unknown provider is 404", async () => {
+ const owner = await put("fal", { key: GOOD_FAL }, "", ownerCookie());
+ expect(owner.status).toBe(400);
+ expect((await owner.json()).error).toMatch(/Settings → Paid API configuration/);
+ expect((await put("openai", { key: GOOD_FAL })).status).toBe(404);
+ });
+
+ it("the proxy lets an account reach its own keys, and keeps guests out", async () => {
+ expect(accountOwnRead("/api/account/keys")).toBe(true);
+ for (const m of ["PUT", "DELETE"]) {
+ expect(accountMayChange(m, "/api/account/keys/fal", "")).toBe(true);
+ expect(accountMayChange(m, "/api/account/keys/replicate", "")).toBe(true);
+ expect(accountMayChange(m, "/api/account/keys/openai", "")).toBe(false);
+ }
+ expect(accountMayChange("POST", "/api/account/keys/fal", "")).toBe(false);
+ const at = (url: string, method: string, who?: string) => proxy(new NextRequest(`http://localhost:3000${url}`, { method, headers: who ? { cookie: who } : {}, ...(method !== "GET" && { body: "{}" }) }));
+ const passed = (r: Response) => r.headers.get("x-middleware-next") === "1";
+ expect(passed(at("/api/account/keys", "GET", cookie))).toBe(true);
+ expect(passed(at("/api/account/keys/fal", "PUT", cookie))).toBe(true);
+ expect(passed(at("/api/account/keys/fal", "DELETE", cookie))).toBe(true);
+ expect(at("/api/account/keys/fal", "PUT").status).toBe(401);
+ expect(at("/api/account/keys", "GET").status).toBe(401);
+ // The studio's own key settings stay the owner's.
+ expect(at("/api/settings", "PUT", cookie).status).toBe(403);
+ expect(at("/api/providers/test", "POST", cookie).status).toBe(403);
+ });
+});
+
+describe("replacing and removing a key with runs going on it", () => {
+ const startRun = async () => {
+ fal.pollsToFinish = Infinity;
+ const text = JSON.stringify({ feature: "image", modelId: "nano-banana-pro", engine: "cloud", prompt: "a quiet harbour" });
+ const r = await generationsRoute.POST(new Request("http://localhost:3000/api/generations", { method: "POST", headers: { cookie, host: "localhost:3000", "content-type": "application/json", "content-length": String(text.length) }, body: text }));
+ expect(r.status).toBe(201);
+ const id = (await r.json()).items[0].id as string;
+ for (let i = 0; i < 100 && !db().prepare("SELECT external_id FROM generations WHERE id = ?").pluck().get(id); i++) await new Promise((r) => setTimeout(r, 10));
+ return id;
+ };
+ const status = (id: string) => db().prepare("SELECT status FROM generations WHERE id = ?").pluck().get(id);
+
+ it("replace: asks first, then stops the runs with the old key and saves the new one", async () => {
+ const id = await startRun();
+ const ask = await put("fal", { key: NEW_FAL });
+ expect(ask.status).toBe(409);
+ expect(await ask.json()).toEqual({ code: "runs-going", count: 1, error: "1 run is using this key now. Replacing it stops it." });
+ expect(["queued", "running"]).toContain(status(id));
+ fal.log.length = 0;
+ const r = await put("fal", { key: NEW_FAL, stopRuns: true });
+ expect(r.status).toBe(200);
+ expect((await r.json()).last4).toBe("4c0b");
+ expect(status(id)).toBe("canceled");
+ const cancel = fal.log.find((x) => x.path.endsWith("/cancel"));
+ expect(cancel?.authorization).toBe(`Key ${GOOD_FAL}`);
+ expect(fal.log.indexOf(cancel!)).toBeGreaterThan(fal.log.findIndex((x) => x.authorization === `Key ${NEW_FAL}`));
+ });
+
+ it("remove: asks first, then stops the runs with the key, and the key is gone", async () => {
+ const id = await startRun();
+ const ask = await del("fal");
+ expect(ask.status).toBe(409);
+ expect((await ask.json()).error).toBe("1 run is using this key now. Removing it stops it.");
+ fal.log.length = 0;
+ const r = await del("fal", { stopRuns: true });
+ expect(r.status).toBe(200);
+ expect((await r.json()).keys.fal).toBeNull();
+ expect(status(id)).toBe("canceled");
+ expect(keysSeen(fal.log)).toEqual([`Key ${NEW_FAL}`]);
+ expect(auth.accountKeyRecord(A, "fal")).toBeNull();
+ // Nothing more reaches fal.ai for the account, and a paid run asks for the key again.
+ fal.log.length = 0;
+ await globalThis.__fuobeRunner?.tick();
+ expect(fal.log).toEqual([]);
+ const again = await generationsRoute.POST(req("/api/generations", "POST", { feature: "image", modelId: "nano-banana-2", engine: "cloud", prompt: "x" }));
+ expect(again.status).toBe(403);
+ expect(again.headers.get("x-fuobe-add-key")).toBe("fal");
+ });
+});
diff --git a/tests/helpers/fake-cloud.ts b/tests/helpers/fake-cloud.ts
index 4d55449a..0f879b17 100644
--- a/tests/helpers/fake-cloud.ts
+++ b/tests/helpers/fake-cloud.ts
@@ -7,10 +7,11 @@ import http from "node:http";
* it with `node --experimental-strip-types`.
*
* What a key gets is scripted per key: "good" (works), "broke" (its balance has run out), "echo" (refused, and the
- * error repeats the request's Authorization header, like a proxy that echoes it). Any other key is refused (401).
+ * error repeats the request's Authorization header, like a proxy that echoes it), "outage" (503: says nothing about the key).
+ * Any other key is refused (401).
*/
-export type KeyMode = "good" | "broke" | "echo";
+export type KeyMode = "good" | "broke" | "echo" | "outage";
export interface CloudRequest {
method: string;
@@ -71,6 +72,7 @@ function refused(fake: FakeCloud, r: CloudRequest, res: http.ServerResponse, key
const mode = key ? fake.keys[key] : undefined;
if (mode === "good") return false;
if (mode === "broke") json(res, broke.status, { detail: broke.detail });
+ else if (mode === "outage") json(res, 503, { detail: "Service unavailable" });
else if (mode === "echo") json(res, 401, { detail: `Invalid credentials: ${r.authorization}` });
else json(res, 401, { detail: "Unauthorized" });
return true;