promote-plugin-e2e.mjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env node
  2. import { existsSync, readFileSync, renameSync, 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 pluginDir = resolve(desktopDir, "test/e2e/plugins");
  7. const pendingDir = resolve(pluginDir, "manual-review/pending");
  8. const matrixPath = resolve(
  9. repoRoot,
  10. "docs/testing/plugin-management-lifecycle-e2e-coverage-matrix.md",
  11. );
  12. const fixtureDir = resolve(desktopDir, "test/e2e/fixtures/upstream/plugin-management");
  13. const manifestDir = resolve(desktopDir, "test/e2e/fixtures/cases/plugins");
  14. const options = parseArgs(process.argv.slice(2));
  15. if (options.help || !options.spec) {
  16. printUsage();
  17. process.exit(options.help ? 0 : 1);
  18. }
  19. const result = promote(options);
  20. printResult(result);
  21. process.exit(result.errors.length > 0 ? 1 : 0);
  22. function parseArgs(args) {
  23. const options = { apply: false, help: false, reviewed: false, spec: "" };
  24. for (let index = 0; index < args.length; index += 1) {
  25. const arg = args[index];
  26. if (arg === "--" || !arg.trim()) continue;
  27. if (arg === "--help" || arg === "-h") options.help = true;
  28. else if (arg === "--apply") options.apply = true;
  29. else if (arg === "--reviewed") options.reviewed = true;
  30. else if (arg === "--spec") options.spec = args[++index] ?? "";
  31. else if (arg.startsWith("--spec=")) options.spec = arg.slice("--spec=".length);
  32. else if (!options.spec) options.spec = arg;
  33. }
  34. return options;
  35. }
  36. function printUsage() {
  37. console.log(
  38. "Usage: pnpm --filter @zcode/desktop e2e:promote:plugins -- --spec ./test/e2e/plugins/manual-review/pending/<case>.test.ts --reviewed --apply",
  39. );
  40. }
  41. function promote({ apply, reviewed, spec }) {
  42. const errors = [];
  43. const warnings = [];
  44. const actions = [];
  45. const sourcePath = resolvePath(spec);
  46. const fileName = basename(sourcePath);
  47. const caseName = fileName.replace(/\.test\.ts$/u, "");
  48. const targetPath = resolve(pluginDir, fileName);
  49. const pendingRelative = relative(pendingDir, sourcePath);
  50. const fixturePath = resolve(fixtureDir, `${caseName}.json`);
  51. const manifestPath = resolve(manifestDir, `${caseName}.json`);
  52. if (pendingRelative.startsWith("..") || isAbsolute(pendingRelative)) {
  53. errors.push(
  54. `Spec must be under plugin manual-review/pending: ${relative(repoRoot, sourcePath)}`,
  55. );
  56. }
  57. if (!sourcePath.endsWith(".test.ts") || !existsSync(sourcePath)) {
  58. errors.push(`Pending plugin spec not found: ${relative(repoRoot, sourcePath)}`);
  59. }
  60. if (existsSync(targetPath))
  61. errors.push(`Formal target already exists: ${relative(repoRoot, targetPath)}`);
  62. if (!existsSync(fixturePath))
  63. errors.push(`Case-local provider fixture missing: ${relative(repoRoot, fixturePath)}`);
  64. if (!existsSync(manifestPath))
  65. errors.push(`Plugin case manifest missing: ${relative(repoRoot, manifestPath)}`);
  66. if (existsSync(manifestPath)) {
  67. const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
  68. if (manifest.formalAdmission === "never") {
  69. errors.push(`Plugin case is manual-only and cannot be promoted: ${caseName}`);
  70. }
  71. }
  72. if (apply && !reviewed) errors.push("Use --reviewed when applying plugin promotion.");
  73. if (!reviewed)
  74. warnings.push("Do not apply until screenshots and behavior have passed human review.");
  75. if (errors.length === 0) {
  76. actions.push(`Move ${relative(repoRoot, sourcePath)} -> ${relative(repoRoot, targetPath)}`);
  77. actions.push(
  78. "Rewrite pending helper imports and update the case manifest and coverage matrix paths.",
  79. );
  80. }
  81. if (apply && errors.length === 0) {
  82. renameSync(sourcePath, targetPath);
  83. const source = readFileSync(targetPath, "utf8").replaceAll("../../../helpers/", "../helpers/");
  84. writeFileSync(targetPath, source, "utf8");
  85. const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
  86. manifest.spec = `./test/e2e/plugins/${fileName}`;
  87. writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`, "utf8");
  88. const matrix = readFileSync(matrixPath, "utf8");
  89. writeFileSync(
  90. matrixPath,
  91. matrix.replaceAll(`plugins/manual-review/pending/${fileName}`, `plugins/${fileName}`),
  92. "utf8",
  93. );
  94. }
  95. return {
  96. actions,
  97. apply,
  98. caseName,
  99. errors,
  100. fixturePath,
  101. manifestPath,
  102. sourcePath,
  103. targetPath,
  104. warnings,
  105. };
  106. }
  107. function resolvePath(value) {
  108. const candidates = [
  109. resolve(process.cwd(), value),
  110. resolve(desktopDir, value),
  111. resolve(repoRoot, value),
  112. ];
  113. return candidates.find(existsSync) ?? candidates[0];
  114. }
  115. function printResult(result) {
  116. console.log(`${result.apply ? "Apply" : "Dry run"} plugin E2E promotion: ${result.caseName}`);
  117. for (const action of result.actions) console.log(`- ${action}`);
  118. for (const warning of result.warnings) console.log(`Warning: ${warning}`);
  119. for (const error of result.errors) console.log(`Error: ${error}`);
  120. }