Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions scripts/acquisition-readiness-audit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { evaluatePilotReadinessText } from "./lib/pilot-readiness.mjs";
import { hasDuplicateJsonObjectKeys } from "./normalize-commercial-readiness-evidence.mjs";

const fatalUtf8Decoder = new TextDecoder("utf-8", { fatal: true });
const now = new Date().toISOString();
const outputDir = process.env.NOEMA_ACQUISITION_AUDIT_OUTPUT_DIR
|| join(process.cwd(), "artifacts", "acquisition-readiness", now.slice(0, 10).replace(/-/g, ""));
Expand Down Expand Up @@ -53,8 +55,19 @@ function readJson(path) {
if (!existsSync(path)) {
return { ok: false, reason: "missing", path };
}

let text;
try {
text = fatalUtf8Decoder.decode(readFileSync(path));
} catch {
return { ok: false, reason: "invalid_utf8", path };
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

try {
return { ok: true, path, value: JSON.parse(readFileSync(path, "utf8")) };
if (hasDuplicateJsonObjectKeys(text)) {
return { ok: false, reason: "duplicate_json_key", path };
}
return { ok: true, path, value: JSON.parse(text) };
} catch (error) {
return { ok: false, reason: "invalid_json", path, error: error.message };
}
Expand Down Expand Up @@ -443,4 +456,4 @@ if (!output.passed) {
}
process.exit(1);
}
}
}
67 changes: 67 additions & 0 deletions test/acquisition-json-integrity-current-main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { spawnSync } from "node:child_process";
import { describe, expect, it } from "vitest";

function runAudit(saleablePath: string, outputDir: string) {
return spawnSync("node", ["scripts/acquisition-readiness-audit.mjs"], {
cwd: process.cwd(),
env: {
...process.env,
NOEMA_ACQUISITION_AUDIT_OUTPUT_DIR: outputDir,
NOEMA_REVENUE_EVIDENCE_PATH: join(outputDir, "missing-revenue.json"),
NOEMA_TRANSFER_EVIDENCE_PATH: join(outputDir, "missing-transfer.json"),
NOEMA_SALEABLE_AUDIT_PATH: saleablePath,
NOEMA_DATA_ROOM_MANIFEST_PATH: join(outputDir, "missing-data-room.json"),
},
encoding: "utf8",
});
}

function saleableEvidenceCheck(outputDir: string) {
const audit = JSON.parse(
readFileSync(join(outputDir, "acquisition-audit.json"), "utf8"),
);
return audit.checks.find(
(check: { name: string }) => check.name === "saleable readiness evidence present",
);
}

describe("acquisition readiness JSON integrity", () => {
it("rejects duplicate decoded object keys instead of accepting JSON.parse last-key-wins", () => {
const temp = mkdtempSync(join(tmpdir(), "noema-acq-duplicate-json-"));
const saleablePath = join(temp, "saleable.json");
writeFileSync(
saleablePath,
'{"objective":"NOEMA-GOAL-SALEABLE-2026-07-02","passed":true,"p\\u0061ssed":false}',
);

const result = runAudit(saleablePath, temp);

expect(result.status).toBe(1);
expect(saleableEvidenceCheck(temp)).toMatchObject({
pass: false,
details: { reason: "duplicate_json_key", path: saleablePath },
});
});

it("rejects malformed UTF-8 before JSON parsing can normalize replacement characters", () => {
const temp = mkdtempSync(join(tmpdir(), "noema-acq-invalid-utf8-"));
const saleablePath = join(temp, "saleable.json");
const prefix = Buffer.from(
'{"objective":"NOEMA-GOAL-SALEABLE-2026-07-02","passed":true,"note":"',
"utf8",
);
const suffix = Buffer.from('"}', "utf8");
writeFileSync(saleablePath, Buffer.concat([prefix, Buffer.from([0x80]), suffix]));

const result = runAudit(saleablePath, temp);

expect(result.status).toBe(1);
expect(saleableEvidenceCheck(temp)).toMatchObject({
pass: false,
details: { reason: "invalid_utf8", path: saleablePath },
});
});
});
Loading