promote-plugin-e2e.mjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env node
  2. import { existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
  3. import { basename, isAbsolute, relative, resolve } from "node:path";
  4. import { rewriteFormalSpecImports } from "./lib/e2e-promotion-spec-imports.mjs";
  5. const desktopDir = resolve(import.meta.dirname, "..");
  6. const repoRoot = resolve(desktopDir, "../..");
  7. const pluginDir = resolve(desktopDir, "test/e2e/plugins");
  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/<scenario>/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 pendingRelative = relative(pluginDir, sourcePath).replaceAll("\\", "/");
  49. const targetPath = resolve(
  50. pluginDir,
  51. pendingRelative.replace(/(?:^|\/)manual-review\/pending\//u, (match) =>
  52. match.startsWith("/") ? "/" : "",
  53. ),
  54. );
  55. const fixturePath = resolve(fixtureDir, `${caseName}.json`);
  56. const manifestPath = resolve(manifestDir, `${caseName}.json`);
  57. if (
  58. pendingRelative.startsWith("..") ||
  59. isAbsolute(pendingRelative) ||
  60. !/(?:^|\/)manual-review\/pending\/[^/]+\.test\.ts$/u.test(pendingRelative)
  61. ) {
  62. errors.push(
  63. `Spec must be under plugin manual-review/pending: ${relative(repoRoot, sourcePath)}`,
  64. );
  65. }
  66. if (!sourcePath.endsWith(".test.ts") || !existsSync(sourcePath)) {
  67. errors.push(`Pending plugin spec not found: ${relative(repoRoot, sourcePath)}`);
  68. }
  69. if (existsSync(targetPath))
  70. errors.push(`Formal target already exists: ${relative(repoRoot, targetPath)}`);
  71. if (!existsSync(fixturePath))
  72. errors.push(`Case-local provider fixture missing: ${relative(repoRoot, fixturePath)}`);
  73. if (!existsSync(manifestPath))
  74. errors.push(`Plugin case manifest missing: ${relative(repoRoot, manifestPath)}`);
  75. if (existsSync(manifestPath)) {
  76. const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
  77. if (manifest.formalAdmission === "never") {
  78. errors.push(`Plugin case is manual-only and cannot be promoted: ${caseName}`);
  79. }
  80. }
  81. if (apply && !reviewed) errors.push("Use --reviewed when applying plugin promotion.");
  82. if (!reviewed)
  83. warnings.push("Do not apply until screenshots and behavior have passed human review.");
  84. if (errors.length === 0) {
  85. actions.push(`Move ${relative(repoRoot, sourcePath)} -> ${relative(repoRoot, targetPath)}`);
  86. actions.push(
  87. "Rewrite pending helper imports and update the case manifest and coverage matrix paths.",
  88. );
  89. }
  90. if (apply && errors.length === 0) {
  91. renameSync(sourcePath, targetPath);
  92. rewriteFormalSpecImports(targetPath);
  93. const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
  94. manifest.spec = `./${relative(desktopDir, targetPath).replaceAll("\\", "/")}`;
  95. writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`, "utf8");
  96. const matrix = readFileSync(matrixPath, "utf8");
  97. writeFileSync(
  98. matrixPath,
  99. matrix.replaceAll(
  100. `plugins/${pendingRelative}`,
  101. `plugins/${relative(pluginDir, targetPath).replaceAll("\\", "/")}`,
  102. ),
  103. "utf8",
  104. );
  105. }
  106. return {
  107. actions,
  108. apply,
  109. caseName,
  110. errors,
  111. fixturePath,
  112. manifestPath,
  113. sourcePath,
  114. targetPath,
  115. warnings,
  116. };
  117. }
  118. function resolvePath(value) {
  119. const candidates = [
  120. resolve(process.cwd(), value),
  121. resolve(desktopDir, value),
  122. resolve(repoRoot, value),
  123. ];
  124. return candidates.find(existsSync) ?? candidates[0];
  125. }
  126. function printResult(result) {
  127. console.log(`${result.apply ? "Apply" : "Dry run"} plugin E2E promotion: ${result.caseName}`);
  128. for (const action of result.actions) console.log(`- ${action}`);
  129. for (const warning of result.warnings) console.log(`Warning: ${warning}`);
  130. for (const error of result.errors) console.log(`Error: ${error}`);
  131. }