third-party-notices.test.mjs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import assert from "node:assert/strict";
  2. import { createHash } from "node:crypto";
  3. import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. import test from "node:test";
  7. import * as notices from "./third-party-notices.mjs";
  8. const hash = (bytes) => createHash("sha256").update(bytes).digest("hex");
  9. const noticeText = "Copyright Example Authors\nMIT license text\n";
  10. async function fixture(t) {
  11. const root = await mkdtemp(join(tmpdir(), "zcode-notices-"));
  12. t.after(() => rm(root, { recursive: true, force: true }));
  13. await writeFile(join(root, notices.noticesFileName), noticeText);
  14. return root;
  15. }
  16. async function writeJson(root, file, value) {
  17. await writeFile(join(root, file), JSON.stringify(value));
  18. }
  19. async function inventory(root, overrides = {}) {
  20. await mkdir(join(root, "third-party"), { recursive: true });
  21. await writeJson(root, "third-party/inventory.json", {
  22. schemaVersion: 1,
  23. noticesSha256: hash(noticeText),
  24. inputs: {},
  25. reviewRequired: [],
  26. ...overrides,
  27. });
  28. }
  29. async function assertBuildOutputs(root) {
  30. const destination = join(root, "dist");
  31. await notices.stageThirdPartyNotices(destination, root);
  32. assert.equal(await readFile(join(destination, notices.noticesFileName), "utf8"), noticeText);
  33. const assets = [];
  34. const plugin = notices.thirdPartyNoticesVitePlugin(root);
  35. plugin.configResolved({ base: "/app/" });
  36. await plugin.generateBundle.call({ emitFile: (asset) => assets.push(asset) });
  37. assert.equal(assets.length, 1);
  38. assert.equal(assets[0].fileName, notices.noticesFileName);
  39. assert.equal(assets[0].source.toString("utf8"), noticeText);
  40. assert.equal(
  41. plugin.transformIndexHtml.handler()[0].attrs.href,
  42. `/app/${notices.noticesFileName}`,
  43. );
  44. }
  45. test("build copies and emits notices without an inventory", async (t) => {
  46. const root = await fixture(t);
  47. await assertBuildOutputs(root);
  48. });
  49. test("editing a registered skill does not block build but explicit verification fails", async (t) => {
  50. const root = await fixture(t);
  51. const file = ".agents/skills/agent-browser/SKILL.md";
  52. await mkdir(join(root, ".agents/skills/agent-browser"), { recursive: true });
  53. await writeFile(join(root, file), "updated skill\n");
  54. await inventory(root, { inputs: { [file]: hash("old skill\n") } });
  55. await assertBuildOutputs(root);
  56. await assert.rejects(notices.readVerifiedNotices(root), /Third-party input changed/u);
  57. });
  58. test("changed notices can be packaged while explicit verification detects the mismatch", async (t) => {
  59. const root = await fixture(t);
  60. await inventory(root, { noticesSha256: hash("previous notices") });
  61. await assertBuildOutputs(root);
  62. await assert.rejects(notices.readVerifiedNotices(root), /Third-party notices changed/u);
  63. });
  64. test("strict release verification still rejects unresolved materials", async (t) => {
  65. const root = await fixture(t);
  66. await inventory(root, { reviewRequired: [{ id: "example", reason: "pending review" }] });
  67. await assertBuildOutputs(root);
  68. assert.equal((await notices.readVerifiedNotices(root)).toString("utf8"), noticeText);
  69. await assert.rejects(
  70. notices.readVerifiedNotices(root, { requireComplete: true }),
  71. /Unresolved third-party material obligations/u,
  72. );
  73. });
  74. test("Node staging copies existing text without enforcing its recorded hash", async (t) => {
  75. const root = await fixture(t);
  76. await mkdir(join(root, "third-party/runtime"), { recursive: true });
  77. const source = { version: "24.14.0", file: "LICENSE.node.txt", sha256: hash("old text") };
  78. await writeJson(root, "third-party/runtime/sources.json", { node: [source] });
  79. await writeFile(join(root, source.file), noticeText);
  80. const destination = join(root, "node");
  81. await notices.stageNodeNotices(destination, source.version, root);
  82. assert.equal(await readFile(join(destination, "LICENSE.node.txt"), "utf8"), noticeText);
  83. assert.deepEqual(
  84. JSON.parse(await readFile(join(destination, "NODE-SOURCES.json"), "utf8")),
  85. source,
  86. );
  87. });
  88. test("native staging tolerates stale inputs, hashes and provenance while explicit generation verifies", async (t) => {
  89. const root = await fixture(t);
  90. await mkdir(join(root, "third-party/native-search"), { recursive: true });
  91. await writeFile(join(root, "native-config.json"), "updated versions");
  92. const material = {
  93. inputs: { "native-config.json": hash("old versions") },
  94. scope: "fixture",
  95. components: [
  96. {
  97. id: "ripgrep",
  98. version: "1.0.0",
  99. notices: [{ file: notices.noticesFileName, sha256: hash("old notice") }],
  100. },
  101. ],
  102. archives: [],
  103. };
  104. await writeJson(root, "third-party/native-search/sources.json", material);
  105. const binaryPath = join(root, "rg");
  106. await writeFile(binaryPath, "fixture binary");
  107. const plan = {
  108. artifacts: [{ toolId: "ripgrep", version: "2.0.0", binaryPath, archiveSha256: "unregistered" }],
  109. };
  110. for (const builtFromSource of [false, true]) {
  111. await notices.stageNativeSearchNotices(plan, root, { builtFromSource });
  112. assert.ok((await readFile(join(root, "THIRD-PARTY-NOTICES.txt"), "utf8")).includes(noticeText));
  113. const staged = JSON.parse(await readFile(join(root, "SOURCES.json"), "utf8"));
  114. assert.equal(staged.binary.version, "2.0.0");
  115. assert.equal(staged.binary.sha256, hash("fixture binary"));
  116. }
  117. await assert.rejects(
  118. notices.readNativeSearchNotices(root, { verify: true }),
  119. /Native license versions changed/u,
  120. );
  121. material.inputs = {};
  122. await writeJson(root, "third-party/native-search/sources.json", material);
  123. await assert.rejects(
  124. notices.readNativeSearchNotices(root, { verify: true }),
  125. /Changed native notice/u,
  126. );
  127. });
  128. test("missing notices still surface the underlying file error", async (t) => {
  129. const root = await fixture(t);
  130. await rm(join(root, notices.noticesFileName));
  131. await assert.rejects(notices.stageThirdPartyNotices(join(root, "dist"), root), {
  132. code: "ENOENT",
  133. });
  134. });