Files
OmniRoute/tests/unit/ops-scripts.test.ts
Diego Rodrigues de Sa e Souza 5d9ecf6f8b chore(quality): conserta base-red de release/v3.8.36 (gates + 7 testes + build MDX) (#4915)
A base tinha base-red sistêmica herdada de PRs de outras sessões, bloqueando
TODOS os PRs do ciclo (o TIA roda a suíte full em fail-safe p/ diffs hub).

4 Fast Quality Gates:
- test-discovery (#4877): live-server-allowlist.test.ts em tests/unit/server/
  (não-coletado) + vitest → nunca rodava. Convertido p/ node:test em tests/unit/security/.
- any-budget:t11 (#4664): 3 explicit-any em tokenRefresh.ts tipados (sem crescer file-size).
- docs-symbols (#4868): rotas inexistentes → /api/system/version e
  PUT /api/providers/{id} {isActive:false}.
- docs-all fabricated-claim (#4868 + #4718): 5 bin/*.sh reais criados (rollback,
  snapshot-data, restore-data, restore-policies, cold-start-bench) + _ops-common.sh
  (snapshot VACUUM INTO, guards de confirmação/TTY, testes de contrato); NODE_EXTRA_CA_CERTS
  (env de runtime Node) na allowlist do checker.

7 testes unit base-red (de features alheias à quota):
- oauth-providers-config (#4664): teste alinhado ao provider codebuddy-cn do registry.
- antigravity-model-aliases (#4636): maxOutputTokens esperado 32769→16384 (cap intencional).
- provider-request-capture #4091 (#4861): exemplo do teste trocado de mcp__ (que #4861
  isenta de cloak por causa dos 400s de assimetria de histórico) para um tool de terceiro
  cloakável — preserva o invariante de #4091 SEM reverter #4861.
- combo-error-response: convertido de vitest p/ node:test (era coletado pelo glob node:test
  e crashava); api/** e server/** removidos do vitest.config (config morta).

Build MDX (dast-smoke, #4679):
- docs/ops/RELEASE_GREEN.md não tinha frontmatter `title` → fumadocs-mdx rejeitava no
  webpack compile ("invalid frontmatter: title expected string"), quebrando o next build
  (e o deploy). Frontmatter title adicionado (único doc do collection sem ele).

17/17 Fast Quality Gates + suíte unit completa (17737 testes, 0 fail) + vitest verdes localmente.

Co-authored-by: Diego Rodrigues de Sa e Souza <souzamiriamrodrigues790@gmail.com>
2026-06-24 07:00:16 -03:00

185 lines
7.1 KiB
TypeScript

/**
* tests/unit/ops-scripts.test.ts
*
* Contract tests for the incident-runbook ops scripts under bin/ (referenced by
* docs/INCIDENT_RESPONSE.md and docs/PERF_BUDGETS.md):
* rollback.sh · snapshot-data.sh · restore-data.sh · restore-policies.sh ·
* cold-start-bench.sh
*
* These scripts touch deploys and the SQLite store, so the suite pins down the
* safety contract rather than full ops behavior: every script is executable
* bash with strict mode, prints usage on --help, the restore commands refuse to
* run without a snapshot id, and a snapshot→restore round-trip works while the
* non-interactive TTY guard blocks an unattended destructive restore.
*/
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const ROOT = path.resolve(import.meta.dirname, "..", "..");
const BIN = path.join(ROOT, "bin");
const SCRIPTS = [
"rollback.sh",
"snapshot-data.sh",
"restore-data.sh",
"restore-policies.sh",
"cold-start-bench.sh",
];
const hasSqlite3 = spawnSync("sqlite3", ["--version"], { stdio: "ignore" }).status === 0;
/** Run a bin/ script with a NON-tty stdin (so the TTY guard engages). */
function runScript(script: string, args: string[], env: Record<string, string> = {}) {
return spawnSync("bash", [path.join(BIN, script), ...args], {
encoding: "utf8",
stdio: ["ignore", "pipe", "pipe"],
env: { ...process.env, ...env },
});
}
describe("ops runbook scripts (bin/*.sh)", () => {
it("every script exists, is executable, and uses bash + strict mode", () => {
for (const s of SCRIPTS) {
const p = path.join(BIN, s);
assert.ok(fs.existsSync(p), `${s} is missing`);
assert.ok(fs.statSync(p).mode & 0o111, `${s} is not executable (chmod +x)`);
const body = fs.readFileSync(p, "utf8");
assert.ok(body.startsWith("#!/usr/bin/env bash"), `${s} missing bash shebang`);
assert.ok(body.includes("set -euo pipefail"), `${s} missing 'set -euo pipefail'`);
}
});
it("the shared helper and every script pass `bash -n` (syntax check)", () => {
for (const s of [...SCRIPTS, "_ops-common.sh"]) {
const r = spawnSync("bash", ["-n", path.join(BIN, s)], { encoding: "utf8" });
assert.equal(r.status, 0, `${s} has a syntax error: ${r.stderr}`);
}
});
it("--help exits 0 with a usage banner for every script", () => {
for (const s of SCRIPTS) {
const r = runScript(s, ["--help"]);
assert.equal(r.status, 0, `${s} --help exited ${r.status}: ${r.stderr}`);
assert.match(r.stdout, /Usage:/, `${s} --help printed no usage banner`);
}
});
it("restore scripts refuse to run without a snapshot id", () => {
for (const s of ["restore-data.sh", "restore-policies.sh"]) {
const r = runScript(s, []);
assert.notEqual(r.status, 0, `${s} should fail without an id`);
assert.match(r.stderr, /snapshot id required/, `${s} wrong error: ${r.stderr}`);
}
});
it("snapshot → restore-data round-trips, and the TTY guard blocks unattended restores", async () => {
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "omni-ops-"));
try {
const Database = (await import("better-sqlite3")).default;
const dbPath = path.join(dataDir, "storage.sqlite");
let db = new Database(dbPath);
db.exec(
"CREATE TABLE api_keys (id TEXT PRIMARY KEY, name TEXT);" +
"INSERT INTO api_keys VALUES ('k1','orig');"
);
db.close();
const env = { DATA_DIR: dataDir };
const snap = runScript("snapshot-data.sh", ["--label", "test"], env);
assert.equal(snap.status, 0, `snapshot failed: ${snap.stderr}`);
const id = snap.stdout.trim();
assert.ok(id, "snapshot id not printed on stdout");
assert.ok(
fs.existsSync(path.join(dataDir, "db_backups", `snapshot_${id}`, "storage.sqlite")),
"snapshot dir not created"
);
// Mutate the live DB so a successful restore is observable.
db = new Database(dbPath);
db.exec("UPDATE api_keys SET name='changed' WHERE id='k1';");
db.close();
// Guard: non-interactive restore WITHOUT --yes must refuse (nothing destroyed).
const blocked = runScript("restore-data.sh", [id], env);
assert.notEqual(blocked.status, 0, "restore without --yes should be blocked");
assert.match(blocked.stderr, /TTY/, `expected TTY guard, got: ${blocked.stderr}`);
db = new Database(dbPath);
assert.equal(
(db.prepare("SELECT name FROM api_keys WHERE id='k1'").get() as { name: string }).name,
"changed",
"blocked restore must not have changed the DB"
);
db.close();
// With --yes it reverts to the snapshot.
const ok = runScript("restore-data.sh", [id, "--yes"], env);
assert.equal(ok.status, 0, `restore failed: ${ok.stderr}`);
db = new Database(dbPath);
assert.equal(
(db.prepare("SELECT name FROM api_keys WHERE id='k1'").get() as { name: string }).name,
"orig",
"restore did not revert the row"
);
db.close();
} finally {
fs.rmSync(dataDir, { recursive: true, force: true });
}
});
it(
"restore-policies replaces only api_key* tables, preserving other tables",
{ skip: hasSqlite3 ? false : "sqlite3 CLI not installed" },
async () => {
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "omni-pol-"));
try {
const Database = (await import("better-sqlite3")).default;
const dbPath = path.join(dataDir, "storage.sqlite");
let db = new Database(dbPath);
db.exec(
"CREATE TABLE api_keys (id TEXT PRIMARY KEY, name TEXT);" +
"INSERT INTO api_keys VALUES ('k1','orig');" +
"CREATE TABLE sessions (id TEXT PRIMARY KEY);" +
"INSERT INTO sessions VALUES ('s-old');"
);
db.close();
const env = { DATA_DIR: dataDir };
const id = runScript("snapshot-data.sh", [], env).stdout.trim();
assert.ok(id, "snapshot id not printed");
// Change BOTH a policy table and a non-policy table after the snapshot.
db = new Database(dbPath);
db.exec(
"UPDATE api_keys SET name='changed' WHERE id='k1';" +
"INSERT INTO sessions VALUES ('s-new');"
);
db.close();
const r = runScript("restore-policies.sh", [id, "--yes"], env);
assert.equal(r.status, 0, `restore-policies failed: ${r.stderr}`);
db = new Database(dbPath);
// Policy table reverted…
assert.equal(
(db.prepare("SELECT name FROM api_keys WHERE id='k1'").get() as { name: string }).name,
"orig",
"api_keys policy was not restored"
);
// …non-policy table left intact (live usage not rewound).
assert.equal(
(db.prepare("SELECT COUNT(*) c FROM sessions").get() as { c: number }).c,
2,
"non-policy table must not be touched by restore-policies"
);
db.close();
} finally {
fs.rmSync(dataDir, { recursive: true, force: true });
}
}
);
});