railyard/tests/contract/scribase-bridge.test.ts
caelum0x 5ccd834941 feat(watch): uptime checks with a public status page per app and owner email through Rally Send (PY2.2)
RYP-E4, dispatch 4.2 PY2.2 (Railyard side):
- lib/watch: checks (public https app addresses only; IPs, private names and
  our own hosts refused), 20 per org, a status page slug each. The box 1
  agent reads GET /api/v1/watch/agent/checks (ids and URLs only, bearer
  WATCH_AGENT_TOKEN) and posts results to /api/v1/watch/agent/results.
  Two failures in a row turn a check red; each change emails the owner once
  through Rally Send (product railyard, from status@, idempotency key per
  change; a failed send is retried on the next result). Older, unknown and
  future results change nothing.
- /status/<slug>: live results only (state, share of the last checks, bars),
  the check's name and never its address or any host or vendor name.
- The Watch screen: add by address, pause or resume, open the status page.
- Bridge route uptime (contract cases, uptime_check_keys): a check for the
  given address, or the app's latest live deploy; the first result is the
  slot's proof (check_result).
- Gated on RAILYARD_WATCH_LIVE (denylist rule railyard-slot-watch): off, no
  check runs, the pages 404, the nav has no Watch item and the slot answers
  arriving. The copy test lints each gated surface with the flag and proves it
  checks the flag first.
- Migration 0006 (watch_checks, watch_status; https only, history capped at
  90, cascades with the org).

Tests: watch 10/10, bridge 25/25, contract 3/3, migration 7/7 (0006 twice on a
scratch cluster), watch copy 7/7, console 108/108 with the bundle build,
tsc clean, SDK 7/7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 00:24:06 +03:00

101 lines
5.6 KiB
TypeScript

// Contract test for the Scribase bridge (Wave P PY1.0, RYP-E6). Runs every case
// of fixtures/scribase-bridge/contract.json, in order, against the Railyard
// handler with signed requests. The Scribase side (CA/adapters/railyard/
// bridge_client.rs) tests its requests and parsing against the same file.
// node --test tests/contract/scribase-bridge.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { MemoryRepo } from "../../apps/console/lib/repo.ts";
import { sign } from "../../apps/console/lib/standard-webhooks.ts";
import { pollDevice } from "../../apps/console/lib/tokens.ts";
import { handleBridge } from "../../apps/console/app/api/v1/bridge/scribase/service.ts";
import { MemoryFlagsStore } from "../../apps/console/lib/flags/store.ts";
import { TableFlagsBackend } from "../../apps/console/lib/flags/table-backend.ts";
import type { FlagsDeps } from "../../apps/console/lib/flags/service.ts";
import { MemoryWatchStore } from "../../apps/console/lib/watch/store.ts";
import type { WatchDeps } from "../../apps/console/lib/watch/service.ts";
const HERE = dirname(fileURLToPath(import.meta.url));
const contract = JSON.parse(readFileSync(join(HERE, "fixtures/scribase-bridge/contract.json"), "utf8"));
const SECRET = `whsec_${Buffer.from("contract-fixture-secret-32-bytes").toString("base64")}`;
const NOW = new Date("2026-09-29T12:00:00Z");
function matches(actual: unknown, expected: unknown): boolean {
if (expected && typeof expected === "object" && !Array.isArray(expected)) {
return !!actual && typeof actual === "object" && Object.entries(expected).every(([k, v]) => matches((actual as Record<string, unknown>)[k], v));
}
return JSON.stringify(actual) === JSON.stringify(expected);
}
/** A route-bound id in the contract's id_format (P-Y1). */
const rid = (route: string, k: number) => `msg_${route}_${k.toString(16).padStart(24, "0")}`;
function flagsDeps(): FlagsDeps {
const store = new MemoryFlagsStore();
const backend = new TableFlagsBackend(store);
return { store, backend, backends: { table: backend } };
}
function watchDeps(repo: MemoryRepo): WatchDeps {
return { store: new MemoryWatchStore(), repo, mailer: { send: async () => true }, consoleUrl: "https://railyard.example", agentToken: null, live: true };
}
async function send(repo: MemoryRepo, route: string, payload: unknown, id: string, secret = SECRET, flags: FlagsDeps = flagsDeps(), watch: WatchDeps = watchDeps(repo)) {
const body = JSON.stringify(payload);
const ts = String(Math.floor(NOW.getTime() / 1000));
return handleBridge({ repo, secret: SECRET, consoleUrl: "https://railyard.example", now: () => NOW, flags, watch }, route as never, { id, timestamp: ts, signature: await sign(secret, id, ts, body) }, body);
}
test("every contract case answers with its status and keys, in order", async () => {
const repo = new MemoryRepo(() => NOW);
const flags = flagsDeps();
const watch = watchDeps(repo);
let i = 0;
for (const c of contract.cases) {
const id = rid(c.route, ++i);
assert.match(id, new RegExp(contract.id_format));
const r = await send(repo, c.route, c.request, id, SECRET, flags, watch);
assert.equal(r.status, c.status, c.name);
assert.deepEqual(Object.keys(r.body).sort(), c.response_keys, c.name);
if (c.response_match) assert.ok(matches(r.body, c.response_match), `${c.name}: ${JSON.stringify(r.body)}`);
if (c.route === "claims" && r.status === 200) {
const t = await pollDevice(repo, r.body.claim as string, NOW);
assert.equal(t.status, 200, "the claim is exchanged once by the Scribase CLI");
}
if (c.route === "proof") assert.ok(contract.slot_state.states.includes(r.body.state), c.name);
if (c.route === "flags" && r.status === 200) {
const items = r.body.flags as Record<string, unknown>[];
for (const f of items) assert.deepEqual(Object.keys(f).sort(), contract.flags_item_keys, c.name);
if (c.flags_match) assert.ok(matches(items.map((f) => ({ key: f.key, enabled: f.enabled, value: f.value })), c.flags_match), c.name);
assert.match(r.body.client_key as string, /^ry_flags_/);
}
if (c.route === "uptime" && r.status === 200) {
for (const k of r.body.checks as Record<string, unknown>[]) assert.deepEqual(Object.keys(k).sort(), contract.uptime_check_keys, c.name);
}
}
});
test("an unsigned or wrongly signed request and a replayed id are refused as the contract says", async () => {
const repo = new MemoryRepo(() => NOW);
const other = `whsec_${Buffer.from("not-the-contract-secret-32-bytes").toString("base64")}`;
const bad = await send(repo, "orgs", contract.cases[0].request, rid("orgs", 901), other);
assert.equal(bad.status, contract.unsigned.status);
assert.deepEqual(Object.keys(bad.body), contract.unsigned.response_keys);
await send(repo, "orgs", contract.cases[0].request, rid("orgs", 902));
const again = await send(repo, "orgs", contract.cases[0].request, rid("orgs", 902));
assert.equal(again.status, contract.replayed_id.status);
const crossed = await send(repo, "claims", { scribase_org_id: "org_fixture", slot: "deploy" }, rid("proof", 903));
assert.equal(crossed.status, contract.wrong_route.status);
assert.deepEqual(Object.keys(crossed.body), contract.wrong_route.response_keys);
});
test("the signature vector both sides test against", async () => {
const v = contract.signature_vector;
assert.equal(await sign(v.secret, v.id, v.timestamp, v.body), v.signature);
assert.match(v.id, new RegExp(contract.id_format));
});