admit-plugin-e2e-docker.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env node
  2. import { existsSync, readFileSync, writeFileSync } from "node:fs";
  3. import { basename, isAbsolute, relative, resolve } from "node:path";
  4. const desktopDir = resolve(import.meta.dirname, "..");
  5. const repoRoot = resolve(desktopDir, "../..");
  6. const formalPluginDir = resolve(desktopDir, "test/e2e/plugins");
  7. const containerScript = resolve(repoRoot, "scripts/test-desktop-e2e-container.sh");
  8. const fixtureDir = resolve(desktopDir, "test/e2e/fixtures/upstream/plugin-management");
  9. const manifestDir = resolve(desktopDir, "test/e2e/fixtures/cases/plugins");
  10. const suitePattern = /^(PLUGIN_MANAGEMENT_VERIFIED_E2E_SPEC=")([^"]*)(")$/m;
  11. const options = parseArgs(process.argv.slice(2));
  12. if (options.help || !options.spec) {
  13. printUsage();
  14. process.exit(options.help ? 0 : 1);
  15. }
  16. const result = admit(options);
  17. printResult(result);
  18. process.exit(result.errors.length > 0 ? 1 : 0);
  19. function parseArgs(args) {
  20. const options = { apply: false, artifact: "", help: false, spec: "", verified: false };
  21. for (let index = 0; index < args.length; index += 1) {
  22. const arg = args[index];
  23. if (arg === "--" || !arg.trim()) continue;
  24. if (arg === "--help" || arg === "-h") options.help = true;
  25. else if (arg === "--apply") options.apply = true;
  26. else if (arg === "--verified") options.verified = true;
  27. else if (arg === "--artifact") options.artifact = args[++index] ?? "";
  28. else if (arg.startsWith("--artifact=")) options.artifact = arg.slice("--artifact=".length);
  29. else if (arg === "--spec") options.spec = args[++index] ?? "";
  30. else if (arg.startsWith("--spec=")) options.spec = arg.slice("--spec=".length);
  31. else if (!options.spec) options.spec = arg;
  32. }
  33. return options;
  34. }
  35. function printUsage() {
  36. console.log(
  37. "Usage: pnpm --filter @zcode/desktop e2e:docker:admit:plugins -- --spec ./test/e2e/plugins/<scenario>/<case>.test.ts --verified --artifact <path> --apply",
  38. );
  39. }
  40. function admit({ apply, artifact, spec, verified }) {
  41. const errors = [];
  42. const warnings = [];
  43. const actions = [];
  44. const specPath = resolvePath(spec);
  45. const relativeFormal = relative(formalPluginDir, specPath);
  46. const fileName = basename(specPath);
  47. const caseName = fileName.replace(/\.test\.ts$/u, "");
  48. const fixturePath = resolve(fixtureDir, `${caseName}.json`);
  49. const manifestPath = resolve(manifestDir, `${caseName}.json`);
  50. const specEntry = `./${relative(desktopDir, specPath).replaceAll("\\", "/")}`;
  51. const script = readFileSync(containerScript, "utf8");
  52. const match = script.match(suitePattern);
  53. const specs = match?.[2].split(",").filter(Boolean) ?? [];
  54. if (
  55. relativeFormal.startsWith("..") ||
  56. isAbsolute(relativeFormal) ||
  57. relativeFormal.includes("manual-review")
  58. ) {
  59. errors.push(`Spec must be a promoted formal plugin case: ${relative(repoRoot, specPath)}`);
  60. }
  61. if (!existsSync(specPath))
  62. errors.push(`Formal plugin spec missing: ${relative(repoRoot, specPath)}`);
  63. if (!existsSync(fixturePath))
  64. errors.push(`Case fixture missing: ${relative(repoRoot, fixturePath)}`);
  65. if (!existsSync(manifestPath))
  66. errors.push(`Case manifest missing: ${relative(repoRoot, manifestPath)}`);
  67. if (!match)
  68. errors.push("PLUGIN_MANAGEMENT_VERIFIED_E2E_SPEC assignment is missing from container script.");
  69. if (apply && !verified) errors.push("Use --verified only after replay-isolated Docker passes.");
  70. if (!artifact)
  71. warnings.push("Record the passing Docker artifact path before applying admission.");
  72. else if (!existsSync(resolvePath(artifact)))
  73. warnings.push(`Artifact path not found locally: ${artifact}`);
  74. if (errors.length === 0)
  75. actions.push(
  76. `${specs.includes(specEntry) ? "Keep" : "Add"} ${specEntry} in plugins-verified preset.`,
  77. );
  78. if (apply && errors.length === 0 && match && !specs.includes(specEntry)) {
  79. writeFileSync(
  80. containerScript,
  81. script.replace(suitePattern, `$1${[...specs, specEntry].join(",")}$3`),
  82. "utf8",
  83. );
  84. }
  85. return { actions, apply, errors, warnings, caseName };
  86. }
  87. function resolvePath(value) {
  88. const candidates = [
  89. resolve(process.cwd(), value),
  90. resolve(desktopDir, value),
  91. resolve(repoRoot, value),
  92. ];
  93. return candidates.find(existsSync) ?? candidates[0];
  94. }
  95. function printResult(result) {
  96. console.log(`${result.apply ? "Apply" : "Dry run"} plugin Docker admission: ${result.caseName}`);
  97. for (const action of result.actions) console.log(`- ${action}`);
  98. for (const warning of result.warnings) console.log(`Warning: ${warning}`);
  99. for (const error of result.errors) console.log(`Error: ${error}`);
  100. }