66 lines
3.1 KiB
TypeScript
66 lines
3.1 KiB
TypeScript
import { pathToFileURL } from "node:url";
|
|
|
|
import { CustomerPortalUatError, parseCustomerPortalUatCommand, safeCustomerPortalUatError, type CustomerPortalCommercialFixtureCommand } from "./contracts";
|
|
import {
|
|
applyCustomerPortalCommercialFixture,
|
|
assessCustomerPortalCommercialFixture,
|
|
readCustomerPortalCommercialFixture,
|
|
type CustomerPortalCommercialFixtureAssessment,
|
|
} from "./commercial-fixture";
|
|
import { withCustomerPortalOperatorDatabase } from "./database";
|
|
|
|
type CommercialFixtureExecutionResult = {
|
|
readonly mode: CustomerPortalCommercialFixtureCommand["mode"];
|
|
readonly complete: boolean;
|
|
readonly assessment: CustomerPortalCommercialFixtureAssessment;
|
|
};
|
|
|
|
function writeResult(result: CommercialFixtureExecutionResult): void {
|
|
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
}
|
|
|
|
async function executeCommercialFixtureCommand(command: CustomerPortalCommercialFixtureCommand): Promise<CommercialFixtureExecutionResult> {
|
|
return withCustomerPortalOperatorDatabase(command, async (db) => {
|
|
const inspect = async () => assessCustomerPortalCommercialFixture(await readCustomerPortalCommercialFixture(db, command));
|
|
const initial = await inspect();
|
|
if (command.mode === "plan") {
|
|
return { mode: command.mode, complete: initial.action === "noop", assessment: initial };
|
|
}
|
|
if (command.mode === "verify") {
|
|
if (initial.action !== "noop") {
|
|
throw new CustomerPortalUatError("customer-portal UAT: commercial fixture verification found missing, drifted, or unsafe state");
|
|
}
|
|
return { mode: command.mode, complete: true, assessment: initial };
|
|
}
|
|
if (initial.action === "blocked") {
|
|
throw new CustomerPortalUatError("customer-portal UAT: commercial fixture apply is blocked by non-UAT or mismatched staging state");
|
|
}
|
|
|
|
const snapshot = await readCustomerPortalCommercialFixture(db, command);
|
|
const preApply = assessCustomerPortalCommercialFixture(snapshot);
|
|
if (preApply.action === "blocked") {
|
|
throw new CustomerPortalUatError("customer-portal UAT: commercial fixture state changed during preflight");
|
|
}
|
|
await applyCustomerPortalCommercialFixture(db, command, snapshot);
|
|
const final = await inspect();
|
|
if (final.action !== "noop") {
|
|
throw new CustomerPortalUatError("customer-portal UAT: commercial fixture did not converge to the canonical contract");
|
|
}
|
|
return { mode: command.mode, complete: true, assessment: final };
|
|
});
|
|
}
|
|
|
|
export async function main(argv: readonly string[] = process.argv.slice(2), env: NodeJS.ProcessEnv = process.env): Promise<void> {
|
|
const command = parseCustomerPortalUatCommand("commercial-fixture", argv, env);
|
|
if (command.target !== "commercial-fixture") throw new CustomerPortalUatError("customer-portal UAT: invalid commercial fixture command");
|
|
writeResult(await executeCommercialFixtureCommand(command));
|
|
}
|
|
|
|
const directEntry = process.argv[1] ? pathToFileURL(process.argv[1]).href === import.meta.url : false;
|
|
if (directEntry) {
|
|
void main().catch((error) => {
|
|
process.stderr.write(`${safeCustomerPortalUatError(error)}\n`);
|
|
process.exitCode = 1;
|
|
});
|
|
}
|