railyard/apps/console/test/add-app-component.test.ts
caelum0x 4016a68d18 feat(ai): BYOK AI gateway for Scribase projects with per-key caps, traces and a secure connect page (PY1.4)
Railyard side of PY1.4 (ADR RY-0003). Acceptance:
1. Bridge `ai` gives a Scribase project environment one key (ry_ai_..., shown
   once, to the Scribase control plane only, stored here as SHA-256; `rotate`
   revokes the old one) and a one-time link to /connect/ai (15 minutes), the
   only place a provider key is typed. It is sealed with AES-256-GCM under
   RAILYARD_CREDENTIALS_KEY, bound to org and provider, and only a
   four-character hint is ever read back.
2. BYOK only: POST /api/v1/ai/chat/completions (OpenAI-compatible) refuses
   with 403 byok_required when the org has not connected the provider the
   model names, and never calls Relay then. When it does call, the customer's
   key fills that provider's slot and every other slot carries a refusal
   marker, so Relay (caller credential wins) can never use our keys, not even
   on failover; a test pins the slot list to Relay's PROVIDER_KEY_SLOTS.
   Credentials in a request body are refused.
3. Daily and monthly token caps per key (429 with the period); usage (atomic
   rpc) and one trace per call (model, provider, status, tokens, latency),
   read per project key by the bridge `ai_usage` route for the Scribase tab;
   Relay traces carry tensorzero::tags for the same filter.
4. Copy says "your provider bills you directly"; no credits are included
   anywhere (copy lint over the connect page and every gateway message).
apps/relay/config/tenants/README.md records the tenant rules.

Migration: apps/console/db/migrations/0003_ai_gateway.sql (idempotent;
applied twice to a scratch local cluster by test/migration.test.ts).
Env: RELAY_URL (var), RELAY_TENANT_KEY, RAILYARD_CREDENTIALS_KEY (secrets).

Tests: console 63/63 (gateway 6, bridge 12, migration 3, repo 5), tsc clean,
tests/contract 3/3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 21:44:04 +03:00

87 lines
4.7 KiB
TypeScript

// Component test for the "Add app" flow (RYP-D1 acceptance 1, the fallback the
// dispatch allows when a browser install is blocked by the disk rule): the
// real AddAppView is rendered for every state the reducer passes through, and
// the rendered copy is linted with the vendored denylist (surface app-ui).
import { test, after } from "node:test";
import assert from "node:assert/strict";
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import ts from "typescript";
import { addAppFlow, CLICKS, type FlowAction, type FlowState } from "../lib/add-app-flow.ts";
import { errors, formatFindings, lintUnits } from "../../../policy/copy-lint.mjs";
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const OUT = join(ROOT, ".test-build");
after(() => rmSync(OUT, { recursive: true, force: true }));
// Transpile the .tsx components with the installed TypeScript; relative
// imports become absolute file URLs so they resolve to the real sources.
function build(rel: string): string {
const src = join(ROOT, rel);
let js = ts.transpileModule(readFileSync(src, "utf8"), {
compilerOptions: { jsx: ts.JsxEmit.ReactJSX, module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2022 },
}).outputText;
js = js.replace(/from\s+["'](\.{1,2}\/[^"']+)["']/g, (_m, spec: string) => {
const target = resolve(dirname(src), spec);
if (target.endsWith(".tsx")) return `from ${JSON.stringify(pathToFileURL(join(OUT, build(target.slice(ROOT.length + 1)))).href)}`;
return `from ${JSON.stringify(pathToFileURL(target).href)}`;
});
const outRel = rel.replace(/\.tsx$/, ".mjs");
mkdirSync(dirname(join(OUT, outRel)), { recursive: true });
writeFileSync(join(OUT, outRel), js);
return outRel;
}
function text(html: string): string[] {
return html
.replace(/<(script|style)[\s\S]*?<\/\1>/g, " ")
.split(/<[^>]+>/)
.map((s) => s.replace(/&quot;/g, '"').replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&#x27;/g, "'").trim())
.filter(Boolean);
}
test("Add app: list -> form -> snippet in 2 clicks, every state renders and passes the copy lint", async () => {
const { AddAppView } = await import(pathToFileURL(join(OUT, build("components/AddApp.tsx"))).href);
const React = (await import("react")).default;
const { renderToString } = await import("react-dom/server");
const noop = () => {};
const render = (state: FlowState) =>
renderToString(React.createElement(AddAppView, { state, remaining: 3, onAdd: noop, onName: noop, onCreate: noop, onClose: noop, onCopy: noop, copied: null }));
const snippet = { script: '<script defer src="https://insights.example/s.js" data-site="ry_site_abc"></script>', deploy: "npx railyard deploy --app my-app" };
const actions: FlowAction[] = [{ type: "click_add" }, { type: "type_name", name: "my-app" }, { type: "click_create" }, { type: "created", snippet }];
let state: FlowState = { step: "list" };
let clicks = 0;
const pages = [render(state)];
assert.match(pages[0], />Add app<\/button>/);
for (const a of actions) {
if (CLICKS.has(a.type)) clicks += 1;
state = addAppFlow(state, a);
pages.push(render(state));
}
assert.equal(state.step, "done");
assert.ok(clicks <= 3, `${clicks} clicks`);
const done = pages.at(-1)!;
assert.match(done, /data-testid="snippet"/);
assert.ok(done.includes("npx railyard deploy --app my-app"));
assert.ok(done.includes("data-site=&quot;ry_site_abc&quot;"));
assert.match(done, /aria-label="Copy deploy"/, "icon-only copy control has a label");
const units = pages.flatMap((html, i) => text(html).map((t) => ({ text: t, surface: "app-ui", where: `state ${i}` })));
const found = errors(lintUnits(units));
assert.deepEqual(found, [], formatFindings(found));
});
test("console source copy passes the vendored denylist (app-ui)", () => {
const files = ["app/page.tsx", "app/(app)/apps/page.tsx", "app/(app)/usage/page.tsx", "app/(app)/insights/page.tsx", "lib/insights-view.ts", "app/connect/ai/page.tsx", "lib/ai/messages.ts", "lib/ai/provision.ts", "app/(app)/billing/page.tsx", "app/device/page.tsx", "components/BillingActions.tsx", "components/AddApp.tsx", "components/Nav.tsx", "lib/apps.ts"];
const units = files.flatMap((f) => {
const src = readFileSync(join(ROOT, f), "utf8");
const strings = [...src.matchAll(/>([^<>{}]+)</g), ...src.matchAll(/"([^"\n]{3,})"/g), ...src.matchAll(/`([^`\n]{3,})`/g)].map((m) => m[1]);
return strings.map((t) => ({ text: t, surface: "app-ui", where: f, path: f }));
});
const found = errors(lintUnits(units));
assert.deepEqual(found, [], formatFindings(found));
});