Files
ai-kit/test/images.test.js
Cato 3901ff0342 feat(spend): Google out of the free chain; paid AI off unless a feature opts in, and capped (1.26.0) (#112)
freeChain() put Google's Gemini API second, on a fleet-wide GEMINI_API_KEY
described as a free per-project tier. The project had billing; every app on
the box fell through to it whenever Groq's pool was spent, about $567 on
10-11 Nov 2026. Google is not in the chain any more; it bills.

- freeChain(): Groq, then OpenRouter :free. visionProviders() loses the
  Google seat; the free vision left is OpenRouter's :free Gemma id.
- spend: a provider is paid when marked so or when it is Google's Gemini
  API, xAI, Anthropic or OpenAI (by host or key name, also prefixed). A
  reader's own key is never paid by us.
- AI_PAID_DISABLED is on when unset; AI_PAID_OK / {PREFIX}_AI_PAID_OK opts an
  app in. usableChain() drops paid providers unless allowed.
- Daily caps (UTC): fleet 90 calls / 120,000 in / 60,000 out; per-app rows
  (about 60% of the fleet named), aliases, unknown apps 1 / 800 / 400, no-AI
  apps 0; xAI 0 until opted in, then 2 / 2,000 / 1,000; a 5,000-call free
  loop guard. Env overrides per prefix. A reached cap throws SpendLimitError
  ("limit reached") and never walks onto another paid vendor.
- images: every paid transport, the gemini one included, is gated the same
  way; ownKey for a person's own key.
- README, docs/providers.md and CHANGELOG say plainly that Google bills.

Tests: test/spend.test.js (kill switch on/off/missing, no xAI or Google
link without opt-in, caps and headroom, aliases, unknown app, env
overrides, cap hit without walking to another paid vendor, fleet cap,
token cap, BYOK past spent caps, free loop guard); chain, images and vision
tests updated. 425 pass.
2026-11-12 22:27:29 +01:00

116 lines
3.3 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { imageCallIsPaid, generateImage, sniffImageType } from "../dist/images.js";
const PNG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0, 0, 0, 0, 0]).toString("base64");
const json = (body, status = 200) => new Response(JSON.stringify(body), { status });
test("images endpoint returns bytes", async () => {
const r = await generateImage({
ownKey: true,
api: "images",
apiKey: "k",
model: "m",
prompt: "p",
fetch: async () => json({ data: [{ b64_json: PNG }] }),
});
assert.equal(r.ok, true);
assert.equal(r.image.mimeType, "image/png");
});
test("gemini inlineData and openrouter data URI", async () => {
const g = await generateImage({
ownKey: true,
api: "gemini",
apiKey: "k",
model: "m",
prompt: "p",
fetch: async (url, init) => {
assert.match(String(url), /:generateContent$/);
assert.equal(init.headers["x-goog-api-key"], "k");
return json({
candidates: [{ content: { parts: [{ text: "x" }, { inlineData: { data: PNG } }] } }],
});
},
});
assert.equal(g.ok, true);
const o = await generateImage({
ownKey: true,
api: "openrouter-chat",
apiKey: "k",
model: "m",
prompt: "p",
fetch: async () =>
json({
choices: [
{ message: { images: [{ image_url: { url: "data:image/png;base64," + PNG } }] } },
],
}),
});
assert.equal(o.ok, true);
});
test("failures are results, not throws", async () => {
const r = await generateImage({
ownKey: true,
api: "images",
apiKey: "k",
model: "m",
prompt: "p",
fetch: async () => json({ error: { message: "quota" } }, 429),
});
assert.deepEqual(r, { ok: false, status: 429, error: "quota" });
assert.equal(
(await generateImage({ ownKey: true, api: "images", apiKey: "", model: "m", prompt: "p" })).ok,
false,
);
const t = await generateImage({
ownKey: true,
api: "images",
apiKey: "k",
model: "m",
prompt: "p",
fetch: async () => {
throw new Error("x");
},
});
assert.deepEqual(t, { ok: false, error: "request failed" });
assert.equal(sniffImageType(new Uint8Array([0xff, 0xd8])), "image/jpeg");
});
test("a paid image transport is refused by default: the kill switch is on when unset", async () => {
let called = false;
const fetchSpy = async () => {
called = true;
return new Response("{}");
};
for (const api of ["gemini", "images"]) {
const r = await generateImage({
api,
apiKey: "platform-key",
model: "m",
prompt: "p",
env: {},
fetch: fetchSpy,
});
assert.equal(r.ok, false);
assert.match(r.error, /limit reached: paid AI is switched off/);
}
const notOptedIn = await generateImage({
api: "gemini",
apiKey: "platform-key",
model: "m",
prompt: "p",
env: { AI_PAID_DISABLED: "0" },
fetch: fetchSpy,
});
assert.match(notOptedIn.error, /has not opted in/);
assert.equal(called, false, "nothing was sent");
});
test("a free OpenRouter image id is not paid; a paid one is", () => {
assert.equal(imageCallIsPaid({ api: "openrouter-chat", model: "some/model:free" }), false);
assert.equal(imageCallIsPaid({ api: "openrouter-chat", model: "some/model" }), true);
assert.equal(imageCallIsPaid({ api: "gemini", model: "x" }), true);
});