| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import assert from "node:assert/strict";
- import { createHash } from "node:crypto";
- import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import test from "node:test";
- import * as notices from "./third-party-notices.mjs";
- const hash = (bytes) => createHash("sha256").update(bytes).digest("hex");
- const noticeText = "Copyright Example Authors\nMIT license text\n";
- async function fixture(t) {
- const root = await mkdtemp(join(tmpdir(), "zcode-notices-"));
- t.after(() => rm(root, { recursive: true, force: true }));
- await writeFile(join(root, notices.noticesFileName), noticeText);
- return root;
- }
- async function writeJson(root, file, value) {
- await writeFile(join(root, file), JSON.stringify(value));
- }
- async function inventory(root, overrides = {}) {
- await mkdir(join(root, "third-party"), { recursive: true });
- await writeJson(root, "third-party/inventory.json", {
- schemaVersion: 1,
- noticesSha256: hash(noticeText),
- inputs: {},
- reviewRequired: [],
- ...overrides,
- });
- }
- async function assertBuildOutputs(root) {
- const destination = join(root, "dist");
- await notices.stageThirdPartyNotices(destination, root);
- assert.equal(await readFile(join(destination, notices.noticesFileName), "utf8"), noticeText);
- const assets = [];
- const plugin = notices.thirdPartyNoticesVitePlugin(root);
- plugin.configResolved({ base: "/app/" });
- await plugin.generateBundle.call({ emitFile: (asset) => assets.push(asset) });
- assert.equal(assets.length, 1);
- assert.equal(assets[0].fileName, notices.noticesFileName);
- assert.equal(assets[0].source.toString("utf8"), noticeText);
- assert.equal(
- plugin.transformIndexHtml.handler()[0].attrs.href,
- `/app/${notices.noticesFileName}`,
- );
- }
- test("build copies and emits notices without an inventory", async (t) => {
- const root = await fixture(t);
- await assertBuildOutputs(root);
- });
- test("editing a registered skill does not block build but explicit verification fails", async (t) => {
- const root = await fixture(t);
- const file = ".agents/skills/agent-browser/SKILL.md";
- await mkdir(join(root, ".agents/skills/agent-browser"), { recursive: true });
- await writeFile(join(root, file), "updated skill\n");
- await inventory(root, { inputs: { [file]: hash("old skill\n") } });
- await assertBuildOutputs(root);
- await assert.rejects(notices.readVerifiedNotices(root), /Third-party input changed/u);
- });
- test("changed notices can be packaged while explicit verification detects the mismatch", async (t) => {
- const root = await fixture(t);
- await inventory(root, { noticesSha256: hash("previous notices") });
- await assertBuildOutputs(root);
- await assert.rejects(notices.readVerifiedNotices(root), /Third-party notices changed/u);
- });
- test("strict release verification still rejects unresolved materials", async (t) => {
- const root = await fixture(t);
- await inventory(root, { reviewRequired: [{ id: "example", reason: "pending review" }] });
- await assertBuildOutputs(root);
- assert.equal((await notices.readVerifiedNotices(root)).toString("utf8"), noticeText);
- await assert.rejects(
- notices.readVerifiedNotices(root, { requireComplete: true }),
- /Unresolved third-party material obligations/u,
- );
- });
- test("Node staging copies existing text without enforcing its recorded hash", async (t) => {
- const root = await fixture(t);
- await mkdir(join(root, "third-party/runtime"), { recursive: true });
- const source = { version: "24.14.0", file: "LICENSE.node.txt", sha256: hash("old text") };
- await writeJson(root, "third-party/runtime/sources.json", { node: [source] });
- await writeFile(join(root, source.file), noticeText);
- const destination = join(root, "node");
- await notices.stageNodeNotices(destination, source.version, root);
- assert.equal(await readFile(join(destination, "LICENSE.node.txt"), "utf8"), noticeText);
- assert.deepEqual(
- JSON.parse(await readFile(join(destination, "NODE-SOURCES.json"), "utf8")),
- source,
- );
- });
- test("native staging tolerates stale inputs, hashes and provenance while explicit generation verifies", async (t) => {
- const root = await fixture(t);
- await mkdir(join(root, "third-party/native-search"), { recursive: true });
- await writeFile(join(root, "native-config.json"), "updated versions");
- const material = {
- inputs: { "native-config.json": hash("old versions") },
- scope: "fixture",
- components: [
- {
- id: "ripgrep",
- version: "1.0.0",
- notices: [{ file: notices.noticesFileName, sha256: hash("old notice") }],
- },
- ],
- archives: [],
- };
- await writeJson(root, "third-party/native-search/sources.json", material);
- const binaryPath = join(root, "rg");
- await writeFile(binaryPath, "fixture binary");
- const plan = {
- artifacts: [{ toolId: "ripgrep", version: "2.0.0", binaryPath, archiveSha256: "unregistered" }],
- };
- for (const builtFromSource of [false, true]) {
- await notices.stageNativeSearchNotices(plan, root, { builtFromSource });
- assert.ok((await readFile(join(root, "THIRD-PARTY-NOTICES.txt"), "utf8")).includes(noticeText));
- const staged = JSON.parse(await readFile(join(root, "SOURCES.json"), "utf8"));
- assert.equal(staged.binary.version, "2.0.0");
- assert.equal(staged.binary.sha256, hash("fixture binary"));
- }
- await assert.rejects(
- notices.readNativeSearchNotices(root, { verify: true }),
- /Native license versions changed/u,
- );
- material.inputs = {};
- await writeJson(root, "third-party/native-search/sources.json", material);
- await assert.rejects(
- notices.readNativeSearchNotices(root, { verify: true }),
- /Changed native notice/u,
- );
- });
- test("missing notices still surface the underlying file error", async (t) => {
- const root = await fixture(t);
- await rm(join(root, notices.noticesFileName));
- await assert.rejects(notices.stageThirdPartyNotices(join(root, "dist"), root), {
- code: "ENOENT",
- });
- });
|