import assert from "node:assert/strict"; import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; import ts from "../../web/node_modules/typescript/lib/typescript.js"; const root = path.resolve(import.meta.dirname, "../.."); const sourcePath = path.join(root, "web/protocol/device-budget.ts"); const source = fs.readFileSync(sourcePath, "utf8"); const output = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, fileName: sourcePath, reportDiagnostics: true }); assert.deepEqual(output.diagnostics, []); const budget = await import(`data:text/javascript;base64,${Buffer.from(output.outputText).toString("base64")}`); const identity = JSON.parse(fs.readFileSync(path.join(root, "tests/golden/M14-01D/probe-identity-report.json"), "utf8")); const selection = budget.selectDeviceBudget({ schemaVersion: 1, identitySha256: identity.identitySha256, webgl2: identity.adapter.webgl2, webgpu: identity.adapter.webgpu, hardwareConcurrency: identity.browser.hardwareConcurrency, deviceMemory: identity.browser.deviceMemory }); assert.equal(selection.identitySha256, identity.identitySha256); assert.equal(selection.tier, "CONSERVATIVE"); assert.equal(selection.reason, "UNTRUSTED_ADAPTER"); const report = { schemaVersion: 1, task: "M14-04A", operation: "CHROMIUM_DEVICE_BUDGET_SELECTION", runtime: "CHROMIUM_PROBE_IDENTITY_REPORT", identitySha256: identity.identitySha256, selection, execution: "DISABLED", nextTask: "M14-04B" }; const reportPath = path.join(root, "tests/golden/M14-04A/chromium-device-budget-report.json"); const manifestPath = path.join(root, "tests/golden/M14-04A/manifest.json"); const sha256 = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"); const relative = (file) => path.relative(root, file).replaceAll(path.sep, "/"); if (process.env.UPDATE_M14_04A_REPORT === "1") { fs.mkdirSync(path.dirname(reportPath), { recursive: true }); fs.writeFileSync(reportPath, `${JSON.stringify(report, null, 2)}\n`); const artifactPaths = { parentManifest: path.join(root, "tests/golden/M14-01E/manifest.json"), checker: path.join(root, "tools/web/check-chromium-device-budget.mjs"), protocol: sourcePath, unit: path.join(root, "web/tests/unit/device-budget.test.mjs"), package: path.join(root, "web/package.json"), report: reportPath }; const artifacts = Object.fromEntries(Object.entries(artifactPaths).map(([name, file]) => [name, { path: relative(file), sha256: sha256(file) }])); fs.writeFileSync(manifestPath, `${JSON.stringify({ schemaVersion: 1, task: "M14-04A", parentTask: "M14-01E", enablingTask: false, parityStateChange: false, runtime: "CHROMIUM_PROBE_IDENTITY_REPORT", operation: "CHROMIUM_DEVICE_BUDGET_SELECTION", artifacts, nextTask: "M14-04B" }, null, 2)}\n`); } assert.deepEqual(JSON.parse(fs.readFileSync(reportPath, "utf8")), report); const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); assert.deepEqual({ task: manifest.task, parentTask: manifest.parentTask, nextTask: manifest.nextTask }, { task: "M14-04A", parentTask: "M14-01E", nextTask: "M14-04B" }); for (const artifact of Object.values(manifest.artifacts)) assert.equal(sha256(path.join(root, artifact.path)), artifact.sha256, artifact.path); process.stdout.write(`chromium-device-budget-ok tier=${selection.tier} reason=${selection.reason} identity=${selection.identitySha256} execution=DISABLED next=${manifest.nextTask}\n`);