Files
loki/scripts/site-template/lib/ai.test.ts
CatoandClaude c83c8f473e feat(starter): kickoff and new-site.sh both produce the fleet starter (#1138)
scripts/site-template is now bitbaum/fleet templates/app — Next.js 16,
better-auth accounts (password, link, passkey, 2FA, OrangeCat/bitbaum sign-in
as env-gated OIDC), en/de/fr, nonce CSP, ai-kit/mail-kit/limitkit/bip-kit,
tests and CI — copied at a pinned fleet commit by
scripts/templates/sync-starter.sh. CI proves the copy is exact
(sync-starter.sh --check), and starter-sync.yml opens the PR when fleet's
starter moves. Never edited here.

The kickoff's "nextjs-tailwind" starter seeds the same app from
src/lib/starter-app.generated.ts (generated from the directory, pinned by
scripts/test/starter-app.ts), minus the workflows register-site.sh adds with
the site's CD. The repo is seeded in one Trees API call with inline contents
instead of a blob POST per file (~85 for this starter). The widget token rides
in .env.production; the starter's .gitleaks.toml allows NEXT_PUBLIC_* values.
The new-from-scratch picker reads the light template list, so the starter's
file bodies never reach a client bundle.

Both paths give the site its database and auth/seal secrets ON THE BOX
(scripts/hetzner/provision-db-on-box.sh: role + database, ~/.db-credentials,
/opt/<slug>/shared/.env; existing secrets never replaced; nothing printed), and
record the db in the register row so every deploy applies drizzle/.
register-site.sh seeds the starter's full workflow set to starter repos and
the golden CI floor to anything else, never overwriting another repo's ci.yml.

test-site-template.sh follows the starter (verify SSOT, design tokens instead
of a local palette) and its comment stripper is fixed: the sed range deleted
the page from the doc comment to EOF, so every day-zero check had been passing
against an empty string. check-template-contrast.mjs and the starter-lock
generator are retired with the files they checked.


Claude-Session: https://claude.ai/code/session_01W7K2Gm5wcbfCi7THtmV1Hy

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-11 13:39:14 -07:00

62 lines
2.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { ask, freeAvailable, freeLinks, MAX_PROMPT } from "./ai";
/** A vendor that answers every request with `reply`, OpenAI-shaped. */
function vendor(reply: string, seen: string[] = []): typeof fetch {
return (async (input: RequestInfo | URL, init?: RequestInit) => {
seen.push(String(input));
void init;
return new Response(
JSON.stringify({ id: "x", choices: [{ message: { role: "assistant", content: reply } }] }),
{ status: 200, headers: { "content-type": "application/json" } },
);
}) as typeof fetch;
}
describe("free chain", () => {
it("is empty — and the feature says so — without any vendor key", () => {
expect(freeLinks({})).toEqual([]);
expect(freeAvailable({})).toBe(false);
});
it("switches on with any one vendor key", () => {
expect(freeAvailable({ GROQ_API_KEY: "k" })).toBe(true);
});
});
describe("ask", () => {
it("refuses an empty or oversized prompt before calling anyone", async () => {
const seen: string[] = [];
const env = { GROQ_API_KEY: "k" };
expect(await ask(" ", { env, fetchImpl: vendor("x", seen) })).toEqual({
ok: false,
error: "empty_prompt",
});
expect(await ask("x".repeat(MAX_PROMPT + 1), { env, fetchImpl: vendor("x", seen) })).toEqual({
ok: false,
error: "too_long",
});
expect(seen).toEqual([]);
});
it("says not_configured rather than failing when no key exists", async () => {
expect(await ask("hello", { env: {} })).toEqual({ ok: false, error: "not_configured" });
});
it("answers through the free chain", async () => {
const outcome = await ask("hello", { env: { GROQ_API_KEY: "k" }, fetchImpl: vendor("hi") });
expect(outcome).toMatchObject({ ok: true, text: "hi", ownModel: false });
});
it("serves a person's own key only to their own model, at their vendor", async () => {
const seen: string[] = [];
const outcome = await ask("hello", {
env: { GROQ_API_KEY: "server-key" },
ownModel: { vendor: "openrouter", apiKey: "sk-or-own", model: "some/model" },
fetchImpl: vendor("mine", seen),
});
expect(outcome).toMatchObject({ ok: true, text: "mine", ownModel: true });
expect(seen.every((url) => url.includes("openrouter.ai"))).toBe(true);
});
});