Files
pikasTech-unidesk/scripts/agentrun-cli-contract-test.ts
T
2026-06-10 05:24:06 +00:00

78 lines
3.5 KiB
TypeScript

import { readFileSync } from "node:fs";
import { agentRunHelp } from "./src/agentrun";
import { rootHelp } from "./src/help";
function assertCondition(condition: unknown, message: string, detail: unknown = {}): void {
if (!condition) throw new Error(`${message}: ${JSON.stringify(detail)}`);
}
const agentRunUsage = Array.isArray((agentRunHelp() as { usage?: unknown }).usage)
? ((agentRunHelp() as { usage: unknown[] }).usage).map(String)
: [];
assertCondition(
agentRunUsage.some((line) => line.includes("control-plane cleanup-runs --min-age-minutes 30 --limit 200 --dry-run"))
&& agentRunUsage.some((line) => line.includes("control-plane cleanup-runs --min-age-minutes 30 --limit 200 --confirm"))
&& agentRunUsage.some((line) => line.includes("control-plane cleanup-released-pvs --limit 200 --dry-run"))
&& agentRunUsage.some((line) => line.includes("control-plane cleanup-released-pvs --limit 200 --confirm")),
"AgentRun help must expose controlled CI workspace retention commands",
agentRunUsage,
);
assertCondition(
agentRunUsage.some((line) => line.includes("queue submit --json-stdin --dry-run"))
&& agentRunUsage.some((line) => line.includes("queue dispatch <taskId> --json-stdin --dry-run"))
&& agentRunUsage.some((line) => line.includes("queue commander --reader-id cli --limit 20")),
"AgentRun help must expose stdin-first queue dry-run and compact commander usage",
agentRunUsage,
);
const submitStdinIndex = agentRunUsage.findIndex((line) => line.includes("queue submit --json-stdin"));
const submitFileIndex = agentRunUsage.findIndex((line) => line.includes("queue submit --json-file"));
const dispatchStdinIndex = agentRunUsage.findIndex((line) => line.includes("queue dispatch <taskId> --json-stdin"));
const dispatchFileIndex = agentRunUsage.findIndex((line) => line.includes("queue dispatch <taskId> --json-file"));
assertCondition(
submitStdinIndex >= 0
&& dispatchStdinIndex >= 0
&& submitFileIndex > submitStdinIndex
&& dispatchFileIndex > dispatchStdinIndex
&& !agentRunUsage.some((line) => line.includes("queue submit --json-file <task.json> --dry-run"))
&& !agentRunUsage.some((line) => line.includes("queue dispatch <taskId> --json-file <dispatch.json> --dry-run")),
"AgentRun help must present heredoc/stdin before reusable file fallbacks",
agentRunUsage,
);
const globalHelp = JSON.stringify(rootHelp());
assertCondition(
globalHelp.includes("agentrun v01 queue|sessions|control-plane|git-mirror"),
"global help must index AgentRun v0.1 entrypoints",
rootHelp(),
);
const agentRunSource = readFileSync("scripts/src/agentrun.ts", "utf8");
const runtimeJsonFallback = "\"node <<'NODE' || printf '{}\\\\n'\"";
function statusScriptSection(label: string): string {
const start = agentRunSource.indexOf(`printf '${label}='`);
return start >= 0 ? agentRunSource.slice(start, start + 500) : "";
}
assertCondition(
statusScriptSection("pipelineRunCondition").includes(runtimeJsonFallback)
&& statusScriptSection("ciSummary").includes(runtimeJsonFallback),
"AgentRun control-plane status must degrade empty runtime JSON snippets instead of failing the whole status probe",
);
console.log(JSON.stringify({
ok: true,
checks: [
"AgentRun command help exposes cleanup-runs and cleanup-released-pvs",
"AgentRun command help exposes queue dry-run and compact commander usage",
"AgentRun command help presents heredoc/stdin before reusable file fallbacks",
"global help indexes AgentRun v0.1 entrypoints",
"AgentRun control-plane status degrades empty runtime JSON snippets",
],
}));