38 lines
3.8 KiB
JavaScript
38 lines
3.8 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import crypto from "node:crypto";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { createRequire } from "node:module";
|
|
import { fileURLToPath } from "node:url";
|
|
import ts from "../../web/node_modules/typescript/lib/typescript.js";
|
|
|
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
|
|
const reportPath = path.join(root, "tests/golden/M13-03E/sandbox-cancellation-report.json");
|
|
const manifestPath = path.join(root, "tests/golden/M13-03E/manifest.json");
|
|
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "m13-03e-cancellation-"));
|
|
const hashFile = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
|
for (const name of ["asset-path", "capability-gates", "scripting-platform"]) {
|
|
const source = fs.readFileSync(path.join(root, `web/protocol/${name}.ts`), "utf8");
|
|
const output = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }, fileName: `${name}.ts` }).outputText
|
|
.replace('require("./asset-path")', 'require("./asset-path.cjs")').replace('require("./capability-gates")', 'require("./capability-gates.cjs")');
|
|
fs.writeFileSync(path.join(temporary, `${name}.cjs`), output);
|
|
}
|
|
const protocol = createRequire(import.meta.url)(path.join(temporary, "scripting-platform.cjs"));
|
|
try {
|
|
const running = { schemaVersion: 1, jobId: "sandbox:cancel", workerGeneration: 5, baseRevision: 11, mainRevisionBefore: 11, status: "RUNNING" };
|
|
const cancelled = protocol.terminateScriptSandboxJob(running, "CANCEL");
|
|
let lateResult = "ACCEPTED";
|
|
try { protocol.rejectLateScriptSandboxResult(cancelled); } catch (error) { lateResult = error instanceof Error ? error.message.split(":", 1)[0] : String(error); }
|
|
const receipt = { status: cancelled.status, errorCode: cancelled.errorCode, workerGeneration: cancelled.workerGeneration, mainRevisionBefore: cancelled.mainRevisionBefore, mainRevisionAfter: cancelled.mainRevisionAfter, temporaryBytes: cancelled.temporaryBytes, publishedResults: cancelled.publishedResults, lateResults: cancelled.lateResults, committed: cancelled.committed, execution: cancelled.execution };
|
|
assert.deepEqual(receipt, { status: "CANCELLED", errorCode: "SCRIPT_SANDBOX_CANCELLED", workerGeneration: 5, mainRevisionBefore: 11, mainRevisionAfter: 11, temporaryBytes: 0, publishedResults: 0, lateResults: 0, committed: false, execution: "DISABLED" });
|
|
assert.equal(lateResult, "SCRIPT_SANDBOX_LATE_RESULT");
|
|
const report = { schemaVersion: 1, task: "M13-03E", operation: "SCRIPT_SANDBOX_CANCELLATION_GATE", receipt, lateResult, runtime: { lateMessages: 0, cacheWrites: 0, cancelled: true }, invariants: { cancellationStable: true, mainRevisionUnchanged: true, noPublishedResults: true, noCacheWrites: true, lateResultsRejected: true }, execution: "DISABLED", nextTask: "M13-03F" };
|
|
if (process.env.UPDATE_M13_03E_REPORT === "1") { fs.mkdirSync(path.dirname(reportPath), { recursive: true }); fs.writeFileSync(reportPath, JSON.stringify(report, null, 2) + "\n"); }
|
|
assert.deepEqual(JSON.parse(fs.readFileSync(reportPath, "utf8")), report);
|
|
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
assert.deepEqual({ schemaVersion: manifest.schemaVersion, task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { schemaVersion: 1, task: "M13-03E", parentTask: "M13-03D", nextTask: "M13-03F" });
|
|
for (const artifact of Object.values(manifest.artifacts)) assert.equal(hashFile(path.join(root, artifact.path)), artifact.sha256, `artifact hash mismatch ${artifact.path}`);
|
|
process.stdout.write(`script-sandbox-cancellation-ok status=${receipt.status} late=${lateResult} lateMessages=0 cacheWrites=0 next=${manifest.nextTask}\n`);
|
|
} finally { fs.rmSync(temporary, { recursive: true, force: true }); }
|