fix(recovery): restore source removed by production documentation merge
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
/**
|
||||
* AGENT-04 / GOV-06 -- the doctor is itself gated, and is itself proven able to fail.
|
||||
*
|
||||
* Two things this file exists for:
|
||||
*
|
||||
* 1. `pnpm siax:doctor` is otherwise invoked only by `make doctor`, i.e. by
|
||||
* whoever remembers. Running it here puts it inside `pnpm run test`, which
|
||||
* is the BLOCKING "CI (SIAX Cloud) / quality" job -- no .gitea/workflows
|
||||
* change required (that path is protected on this repo).
|
||||
*
|
||||
* 2. The defect being fixed was a check that COULD NOT FAIL: the old doctor
|
||||
* reported 40/40 and byte-identical output with `operationalState` set to
|
||||
* "running" (false) and to "down" (true). So a green doctor is not enough
|
||||
* evidence here -- the negative cases are asserted too, against a fixture
|
||||
* tree via SIAX_DOCTOR_ROOT.
|
||||
*
|
||||
* Offline only. Nothing here touches the network: `--live` is opt-in and is
|
||||
* deliberately never exercised by CI.
|
||||
*/
|
||||
import { afterAll, describe, expect, it } from "vitest";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import path from "node:path";
|
||||
|
||||
const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..");
|
||||
const DOCTOR = path.join(REPO_ROOT, "scripts", "siax-doctor.mjs");
|
||||
|
||||
type DoctorResult = { id: string; label: string; mode: string; pass: boolean; detail: string | null };
|
||||
type DoctorReport = { total: number; failed: number; live: boolean; results: DoctorResult[] };
|
||||
|
||||
function runDoctor(root?: string): { code: number; report: DoctorReport } {
|
||||
const env = { ...process.env, ...(root ? { SIAX_DOCTOR_ROOT: root } : {}) };
|
||||
try {
|
||||
const stdout = execFileSync(process.execPath, [DOCTOR, "--json"], { env, encoding: "utf8" });
|
||||
return { code: 0, report: JSON.parse(stdout) as DoctorReport };
|
||||
} catch (e) {
|
||||
const err = e as { status?: number; stdout?: string };
|
||||
return { code: err.status ?? -1, report: JSON.parse(err.stdout ?? "{}") as DoctorReport };
|
||||
}
|
||||
}
|
||||
|
||||
/** A fixture tree carrying only the two files the GOV-06 checks read. */
|
||||
function fixture(repo: unknown, evidence: unknown): string {
|
||||
const dir = mkdtempSync(path.join(tmpdir(), "siax-doctor-fixture-"));
|
||||
writeFileSync(path.join(dir, "siax.repo.v3.json"), JSON.stringify(repo, null, 2));
|
||||
mkdirSync(path.join(dir, "evidence"), { recursive: true });
|
||||
writeFileSync(path.join(dir, "evidence", "operational-state.json"), JSON.stringify(evidence, null, 2));
|
||||
return dir;
|
||||
}
|
||||
|
||||
const REAL_REPO = JSON.parse(readFileSync(path.join(REPO_ROOT, "siax.repo.v3.json"), "utf8"));
|
||||
const REAL_EVIDENCE = JSON.parse(
|
||||
readFileSync(path.join(REPO_ROOT, "evidence", "operational-state.json"), "utf8"),
|
||||
);
|
||||
|
||||
// FIXTURE-BAS: den HISTORISKA, dokumenterade lögner-paret (down + 402/404)
|
||||
// som en SYNTETISK post — mutationstesterna nedan muterar denna bas, inte
|
||||
// repots NUVARande evidens (som med rätta är running + 200 sedan flottan-
|
||||
// cutovern: testerna ska aldrig flippa när verkligheten flippar).
|
||||
const HISTORIC_EVIDENCE = {
|
||||
...REAL_EVIDENCE,
|
||||
operationalState: "down",
|
||||
checkedAt: "2026-09-07T00:00:00Z",
|
||||
probes: [
|
||||
{ fqdn: "0pt.app", env: "prod", url: "https://0pt.app/api/health", httpStatus: 402, ok: false, note: "server: Vercel (historisk proba, fixture)" },
|
||||
{ fqdn: "api.0pt.app", env: "prod", url: "https://api.0pt.app/api/health", httpStatus: 404, ok: false, note: "server: Vercel (historisk proba, fixture)" },
|
||||
],
|
||||
};
|
||||
|
||||
/** The GOV-06 failure detail for a fixture, or null when GOV-06 passed. */
|
||||
function gov06Failure(root: string): string | null {
|
||||
const { report } = runDoctor(root);
|
||||
const failed = report.results.filter((r) => r.id.startsWith("GOV-06") && !r.pass);
|
||||
return failed.length > 0 ? failed.map((r) => r.detail).join(" | ") : null;
|
||||
}
|
||||
|
||||
describe("AGENT-04: siax-doctor passes on this repo", () => {
|
||||
it("exits 0 with zero failures", () => {
|
||||
const { code, report } = runDoctor();
|
||||
const failures = report.results.filter((r) => !r.pass);
|
||||
expect(failures.map((f) => `${f.id}: ${f.detail}`).join("\n")).toBe("");
|
||||
expect(report.failed).toBe(0);
|
||||
expect(code).toBe(0);
|
||||
});
|
||||
|
||||
it("runs offline by default -- CI must never depend on reaching a host", () => {
|
||||
const { report } = runDoctor();
|
||||
expect(report.live).toBe(false);
|
||||
expect(report.results.every((r) => r.mode === "offline")).toBe(true);
|
||||
});
|
||||
|
||||
it("still carries the truth checks (they cannot be quietly deleted)", () => {
|
||||
const { report } = runDoctor();
|
||||
const ids = new Set(report.results.map((r) => r.id));
|
||||
for (const id of ["GOV-06", "NET-01", "NET-01/REPO-06", "SEC-06/P0-13", "REPO-04/GOV-06"]) {
|
||||
expect(ids.has(id), `siax-doctor no longer runs any "${id}" check`).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("GOV-06: a FALSE operationalState is detected (the historical defect)", () => {
|
||||
const dirs: string[] = [];
|
||||
const track = (d: string) => {
|
||||
dirs.push(d);
|
||||
return d;
|
||||
};
|
||||
afterAll(() => {
|
||||
for (const d of dirs) rmSync(d, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("accepts the honest pairing (down + 402/404, den historiska lögner-posten) as the control", () => {
|
||||
expect(gov06Failure(track(fixture({ ...REAL_REPO, operationalState: "down" }, HISTORIC_EVIDENCE)))).toBeNull();
|
||||
});
|
||||
|
||||
it('rejects operationalState "running" when the evidence was recorded for another state', () => {
|
||||
const detail = gov06Failure(track(fixture({ ...REAL_REPO, operationalState: "running" }, HISTORIC_EVIDENCE)));
|
||||
expect(detail).toMatch(/running/);
|
||||
expect(detail).toMatch(/operational-state\.json/);
|
||||
});
|
||||
|
||||
it('rejects operationalState "running" when the recorded probes are 402/404', () => {
|
||||
const detail = gov06Failure(
|
||||
track(
|
||||
fixture(
|
||||
{ ...REAL_REPO, operationalState: "running" },
|
||||
{ ...HISTORIC_EVIDENCE, operationalState: "running" },
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(detail).toMatch(/but the recorded probes say otherwise/);
|
||||
expect(detail).toMatch(/402|404/);
|
||||
});
|
||||
|
||||
it("rejects an `ok: true` hand-written next to a non-2xx status", () => {
|
||||
const detail = gov06Failure(
|
||||
track(
|
||||
fixture(
|
||||
{ ...REAL_REPO, operationalState: "running" },
|
||||
{
|
||||
...HISTORIC_EVIDENCE,
|
||||
operationalState: "running",
|
||||
probes: (HISTORIC_EVIDENCE.probes as { ok: boolean }[]).map((p) => ({ ...p, ok: true })),
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(detail).toMatch(/records ok=true next to httpStatus/);
|
||||
});
|
||||
|
||||
it('rejects operationalState "down" when a prod host is recorded as answering', () => {
|
||||
const detail = gov06Failure(
|
||||
track(
|
||||
fixture({ ...REAL_REPO, operationalState: "down" }, {
|
||||
...HISTORIC_EVIDENCE,
|
||||
probes: (HISTORIC_EVIDENCE.probes as { httpStatus: number; ok: boolean }[]).map((p) => ({
|
||||
...p,
|
||||
httpStatus: 200,
|
||||
ok: true,
|
||||
})),
|
||||
}),
|
||||
),
|
||||
);
|
||||
expect(detail).toMatch(/"down" but these prod hosts answered/);
|
||||
});
|
||||
|
||||
it("rejects a liveness claim backed only by stale evidence", () => {
|
||||
const stale = new Date(Date.now() - 400 * 86_400_000).toISOString();
|
||||
const detail = gov06Failure(
|
||||
track(
|
||||
fixture(
|
||||
{ ...REAL_REPO, operationalState: "running" },
|
||||
{
|
||||
...HISTORIC_EVIDENCE,
|
||||
operationalState: "running",
|
||||
checkedAt: stale,
|
||||
probes: (HISTORIC_EVIDENCE.probes as { httpStatus: number; ok: boolean }[]).map((p) => ({
|
||||
...p,
|
||||
httpStatus: 200,
|
||||
ok: true,
|
||||
})),
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(detail).toMatch(/unrefreshed liveness claim/);
|
||||
});
|
||||
|
||||
it("rejects a prod hostname with no probe on record at all", () => {
|
||||
const detail = gov06Failure(
|
||||
track(
|
||||
fixture(
|
||||
{
|
||||
...REAL_REPO,
|
||||
domains: [
|
||||
...(REAL_REPO.domains as unknown[]),
|
||||
{ fqdn: "invented.0pt.app", env: "prod", purpose: "x", registrar: "x" },
|
||||
],
|
||||
},
|
||||
HISTORIC_EVIDENCE,
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(detail).toMatch(/no probe recorded for: invented\.0pt\.app/);
|
||||
});
|
||||
|
||||
it("rejects a malformed hostname (NET-01)", () => {
|
||||
const { report } = runDoctor(
|
||||
track(
|
||||
fixture(
|
||||
{
|
||||
...REAL_REPO,
|
||||
domains: [{ fqdn: "https://0pt.app/health", env: "prod", purpose: "x", registrar: "x" }],
|
||||
},
|
||||
HISTORIC_EVIDENCE,
|
||||
),
|
||||
),
|
||||
);
|
||||
const net01 = report.results.find((r) => r.id === "NET-01" && !r.pass);
|
||||
expect(net01?.detail).toMatch(/not a bare lowercase hostname/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user