package.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import assert from "node:assert/strict";
  2. import { spawnSync } from "node:child_process";
  3. import test from "node:test";
  4. import { readFile } from "node:fs/promises";
  5. interface PackageContract {
  6. version?: string;
  7. private?: boolean;
  8. license?: string;
  9. bin?: Record<string, string>;
  10. files?: string[];
  11. scripts?: Record<string, string>;
  12. }
  13. test("npm package contract exposes only the compiled private CLI", async () => {
  14. const [packageText, sourceEntry, compiledEntry] = await Promise.all([
  15. readFile(new URL("../../package.json", import.meta.url), "utf8"),
  16. readFile(new URL("../../src/index.ts", import.meta.url), "utf8"),
  17. readFile(new URL("../src/index.js", import.meta.url), "utf8"),
  18. ]);
  19. const packageJson = JSON.parse(packageText) as PackageContract;
  20. assert.equal(packageJson.private, true);
  21. assert.equal(packageJson.version, "0.1.1");
  22. assert.equal(packageJson.license, "UNLICENSED");
  23. assert.deepEqual(packageJson.bin, { "codex-chief-of-staff": "dist/src/index.js" });
  24. assert.deepEqual(packageJson.files, ["dist/src", "README.md"]);
  25. assert.equal(packageJson.scripts?.compile, "tsc -p tsconfig.json");
  26. assert.equal(packageJson.scripts?.build, undefined);
  27. for (const lifecycle of ["prepack", "prepare", "preinstall", "install", "postinstall"]) {
  28. assert.equal(packageJson.scripts?.[lifecycle], undefined);
  29. }
  30. assert.match(packageJson.scripts?.test ?? "", /npm run compile/);
  31. assert.match(packageJson.scripts?.smoke ?? "", /npm run compile/);
  32. assert.equal(
  33. packageJson.scripts?.["verify:runtime"],
  34. "node scripts/verify-generated-runtime.mjs",
  35. );
  36. assert.match(packageJson.scripts?.["pack:verified"] ?? "", /npm run verify:runtime/);
  37. assert.match(packageJson.scripts?.["pack:verified"] ?? "", /npm pack --ignore-scripts/);
  38. assert.match(sourceEntry, /^#!\/usr\/bin\/env node\n/);
  39. assert.match(compiledEntry, /^#!\/usr\/bin\/env node\n/);
  40. });
  41. test("Git ignore rules admit only the compiled source runtime", () => {
  42. const repository = new URL("../..", import.meta.url);
  43. const checkIgnored = (path: string) => spawnSync(
  44. "git",
  45. ["check-ignore", "--no-index", "--quiet", path],
  46. { cwd: repository, encoding: "utf8" },
  47. ).status;
  48. assert.equal(checkIgnored("dist/src/index.js"), 1);
  49. assert.equal(checkIgnored("dist/test/package.test.js"), 0);
  50. assert.equal(checkIgnored("dist/other-output.js"), 0);
  51. });
  52. test("packed-artifact verifier rejects a missing tarball argument", () => {
  53. const verifier = new URL("../../scripts/verify-packed-package.mjs", import.meta.url);
  54. const result = spawnSync(process.execPath, [verifier.pathname], { encoding: "utf8" });
  55. assert.notEqual(result.status, 0);
  56. assert.match(result.stderr, /Packed-package verification failed: usage:/);
  57. });