Files
loki/scripts/test/autopilot-defaults.ts
CatoandClaude Fable 5.1 95c40a9cd8 refactor: rename the product from FleetCrown to Loki (#695)
The product, its assistant, the repo, the host and every unit on the box now
share one name. 638 files, ~3,000 mentions: UI copy, config, env keys
(FLEETCROWN_* -> LOKI_*), systemd units (fleetcrown-* -> loki-*), docs,
articles, the deploy workflow other repos reuse, and nine file names.

- Three article slugs carried the old name and had been shared; permanent
  redirects keep those links alive (next.config.ts).
- test-no-retired-name.sh now also forbids the old product name across the
  whole repo, so the rename cannot creep back through templates and examples.
- ASSISTANT_STACK collapses the product and the assistant into one entry,
  because they are now the same name.

Infra was cut over ahead of this merge: /opt/loki, loki-* units, the `loki`
database and role, loki.orangecat.ch (old host 308s to it; /api, /widget,
/embed keep proxying), GitHub repos bitbaum/loki and bitbaum/loki-releases.
The Resend sending domain stays fleetcrown.orangecat.ch until the new domain
is verified in DNS.


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

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-13 16:13:10 -07:00

137 lines
5.9 KiB
TypeScript

/**
* Inline self-tests for autopilot defaults — keeps the new-user experience
* and the SSOT chain (constant → schema → queries → UI hook → settings UI)
* aligned after the 2026-06-11 mode collapse. The bash bridge has been
* retired (Session 1-2 of killing-the-bash-daemon); tests that asserted
* on bash mechanics moved out with it.
* Run: npm run test:autopilot-defaults
*/
import { readFileSync } from "fs";
import { DEFAULT_AUTO_INJECT_MODE } from "@/lib/constants/control";
import { AUTO_INJECT_MODE_VALUES, normalizeAutoInjectMode } from "@/config/beacon";
function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(message);
}
function runTests(): void {
let passed = 0;
const check = (label: string, fn: () => void) => {
fn();
passed += 1;
console.log(` ✓ ${label}`);
};
// ── Default + enum invariants ────────────────────────────────────────────
check("DEFAULT_AUTO_INJECT_MODE is 'on'", () => {
// 2026-06-11 — collapsed from beacon (L3 popup) to binary on. New users
// get fire-when-ready by default; safety rails (status:working, blockers,
// health, no-op fuse) still apply.
assert(DEFAULT_AUTO_INJECT_MODE === "on", `expected on, got ${DEFAULT_AUTO_INJECT_MODE}`);
});
check("AUTO_INJECT_MODE_VALUES is exactly ['off', 'on']", () => {
assert(
AUTO_INJECT_MODE_VALUES.length === 2,
`expected 2 values, got ${AUTO_INJECT_MODE_VALUES.length}`,
);
assert(AUTO_INJECT_MODE_VALUES[0] === "off", "first value must be 'off'");
assert(AUTO_INJECT_MODE_VALUES[1] === "on", "second value must be 'on'");
});
check("normalizeAutoInjectMode collapses legacy values", () => {
assert(normalizeAutoInjectMode("off") === "off", "off → off");
assert(normalizeAutoInjectMode("on") === "on", "on → on");
assert(normalizeAutoInjectMode("queue_only") === "on", "queue_only → on");
assert(normalizeAutoInjectMode("beacon") === "on", "beacon → on");
assert(normalizeAutoInjectMode("next_best") === "on", "next_best → on");
assert(normalizeAutoInjectMode("strategist") === "on", "strategist → on");
assert(normalizeAutoInjectMode(null) === "on", "null → on");
assert(normalizeAutoInjectMode(undefined) === "on", "undefined → on");
assert(normalizeAutoInjectMode("garbage") === "on", "unknown → on (safe default)");
});
// ── SSOT chain — every layer reads the same constant ────────────────────
check("Drizzle schema column default matches", () => {
const schema = readFileSync("src/db/schema/beacon-settings.ts", "utf8");
assert(/default\("on"\)/.test(schema), "beacon-settings.ts schema must default to 'on'");
});
check("Queries DEFAULTS sources the constant (no drift)", () => {
const queries = readFileSync("src/db/queries/beacon-settings.ts", "utf8");
assert(
/auto_inject_mode:\s*DEFAULT_AUTO_INJECT_MODE/.test(queries),
"queries must use DEFAULT_AUTO_INJECT_MODE constant — no inline string defaults",
);
});
check("Dispatch route falls back to DEFAULT_AUTO_INJECT_MODE", () => {
const route = readFileSync("src/app/api/control/dispatch/route.ts", "utf8");
assert(
/DEFAULT_AUTO_INJECT_MODE/.test(route),
"dispatch route must use DEFAULT_AUTO_INJECT_MODE when no settings row exists",
);
assert(
!/"queue_only"|"beacon"|"next_best"|"strategist"/.test(route),
"dispatch route must not reference any retired mode value",
);
});
check("Coercer returns the constant for unknown values", () => {
const queries = readFileSync("src/db/queries/beacon-settings.ts", "utf8");
assert(
/coerceAutoInjectMode[\s\S]*DEFAULT_AUTO_INJECT_MODE/.test(queries),
"coerceAutoInjectMode must return DEFAULT_AUTO_INJECT_MODE on miss",
);
});
check("Dispatch route returns nextbest on empty queue when on", () => {
const gates = readFileSync("src/lib/orchestration/dispatch-gates.ts", "utf8");
assert(
/queueLength > 0 \? "queue" : "nextbest"/.test(gates),
"dispatch-gates must return nextbest when queue is empty",
);
});
check("Automation policy hook seeds from DEFAULT_AUTO_INJECT_MODE", () => {
const hook = readFileSync("src/hooks/use-automation-policy.ts", "utf8");
assert(
/useState<AutoInjectMode>\(DEFAULT_AUTO_INJECT_MODE\)/.test(hook),
"useAutomationPolicy must not default to off before settings load",
);
assert(
/LOKI_REFRESH_EVENT/.test(hook),
"useAutomationPolicy must refetch on LOKI_REFRESH_EVENT",
);
});
check("Settings UI initial state seeds from the constant", () => {
const ui = readFileSync("src/components/settings/BeaconSettings.tsx", "utf8");
assert(
/useState<AutoInjectMode>\(DEFAULT_AUTO_INJECT_MODE\)/.test(ui),
"BeaconSettings must seed from DEFAULT_AUTO_INJECT_MODE — never an inline literal",
);
});
check("Inject route accepts runner bearer token", () => {
const route = readFileSync("src/app/api/inject/route.ts", "utf8");
// The invariant is "this route accepts a Bearer token", not "it calls one
// particular helper". getApiActor resolves the cookie session first and
// then delegates to getApiUserId for the Bearer path, so it satisfies the
// same requirement while additionally reporting WHICH path matched (used to
// decide whether a dispatch announces its outcome). Asserting the literal
// name would have failed that refactor while the property still held.
assert(
/getApiUserId|getApiActor/.test(route),
"/api/inject must authenticate via getApiUserId or getApiActor — both accept the runner's Bearer token",
);
assert(!/getSessionUserId/.test(route), "/api/inject must not require browser session only");
});
console.log(`\n${passed} passed`);
}
runTests();