pluginE2EAdmissionScripts.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { spawnSync } from "node:child_process";
  2. import { mkdir, rm, writeFile, readFile } from "node:fs/promises";
  3. import { resolve } from "node:path";
  4. import { afterAll, beforeAll, describe, expect, it } from "vitest";
  5. const repoRoot = process.cwd();
  6. const desktopDir = resolve(repoRoot, "packages/desktop");
  7. const pendingSpec =
  8. "./packages/desktop/test/e2e/plugins/manual-review/pending/plugin-promotion-check-probe.test.ts";
  9. const manualOnlySpec =
  10. "./packages/desktop/test/e2e/plugins/manual-review/pending/plugin-cdn-zip-official-cdn-smoke.test.ts";
  11. const fixtureCheckCase = "plugin-fixture-check-probe";
  12. const formalProbeSpec = resolve(desktopDir, "test/e2e/plugins", `${fixtureCheckCase}.test.ts`);
  13. const formalProbeFixture = resolve(
  14. desktopDir,
  15. "test/e2e/fixtures/upstream/plugin-management",
  16. `${fixtureCheckCase}.json`,
  17. );
  18. const formalProbeManifest = resolve(
  19. desktopDir,
  20. "test/e2e/fixtures/cases/plugins",
  21. `${fixtureCheckCase}.json`,
  22. );
  23. beforeAll(async () => {
  24. await writeProbeFile(formalProbeSpec, 'describe("E2E_PLUGIN_FIXTURE_CHECK_PROBE", () => {});\n');
  25. await writeProbeFile(
  26. formalProbeFixture,
  27. `${JSON.stringify(
  28. {
  29. version: 1,
  30. // 修复原因:pending 与 formal 共用 canonical fixture identity,probe 也必须模拟完整合同。
  31. caseName: fixtureCheckCase,
  32. spec: `./test/e2e/plugins/${fixtureCheckCase}.test.ts`,
  33. fixtures: [
  34. {
  35. id: fixtureCheckCase,
  36. match: {
  37. method: "POST",
  38. pathIncludes: "/messages",
  39. bodyIncludes: ["E2E_PLUGIN_FIXTURE_CHECK_PROBE"],
  40. },
  41. response: { statusCode: 200, text: "ok" },
  42. },
  43. ],
  44. },
  45. null,
  46. 2,
  47. )}\n`,
  48. );
  49. await writeProbeFile(
  50. formalProbeManifest,
  51. `${JSON.stringify(
  52. {
  53. version: 1,
  54. caseName: fixtureCheckCase,
  55. spec: `./test/e2e/plugins/${fixtureCheckCase}.test.ts`,
  56. providerFixtures: [
  57. "./test/e2e/fixtures/upstream/common.json",
  58. `./test/e2e/fixtures/upstream/plugin-management/${fixtureCheckCase}.json`,
  59. ],
  60. requests: [
  61. {
  62. id: fixtureCheckCase,
  63. kind: "main",
  64. source: "synthetic",
  65. syntheticReason:
  66. "Exercise plugin fixture-check routing without a promoted product case.",
  67. },
  68. ],
  69. },
  70. null,
  71. 2,
  72. )}\n`,
  73. );
  74. // 候选已经转正时,准入测试仍使用自己的 pending fixture,不能依赖产品文件的审核状态。
  75. await writeProbeFile(
  76. resolve(repoRoot, pendingSpec),
  77. 'describe("E2E_PLUGIN_FIXTURE_CHECK_PROBE", () => {});\n',
  78. );
  79. for (const file of [formalProbeFixture, formalProbeManifest]) {
  80. await writeProbeFile(
  81. file.replace(fixtureCheckCase, "plugin-promotion-check-probe"),
  82. (await readFile(file, "utf8")).replaceAll(fixtureCheckCase, "plugin-promotion-check-probe"),
  83. );
  84. }
  85. });
  86. afterAll(async () => {
  87. for (const file of [
  88. formalProbeSpec,
  89. formalProbeFixture,
  90. formalProbeManifest,
  91. resolve(repoRoot, pendingSpec),
  92. formalProbeFixture.replace(fixtureCheckCase, "plugin-promotion-check-probe"),
  93. formalProbeManifest.replace(fixtureCheckCase, "plugin-promotion-check-probe"),
  94. ]) {
  95. await rm(file, { force: true });
  96. }
  97. });
  98. describe("plugin lifecycle E2E admission scripts", () => {
  99. it("dry-runs promotion but requires explicit human review before applying", () => {
  100. const dryRun = runScript("promote-plugin-e2e.mjs", ["--spec", pendingSpec]);
  101. expect(dryRun.status).toBe(0);
  102. expect(dryRun.stdout).toContain("Dry run plugin E2E promotion");
  103. expect(dryRun.stdout).toContain(
  104. "Do not apply until screenshots and behavior have passed human review",
  105. );
  106. const unreviewedApply = runScript("promote-plugin-e2e.mjs", ["--spec", pendingSpec, "--apply"]);
  107. expect(unreviewedApply.status).toBe(1);
  108. expect(unreviewedApply.stdout).toContain("Use --reviewed when applying plugin promotion");
  109. });
  110. it("rejects Docker admission for a pending manual-review spec", () => {
  111. const result = runScript("admit-plugin-e2e-docker.mjs", ["--spec", pendingSpec]);
  112. expect(result.status).toBe(1);
  113. expect(result.stdout).toContain("Spec must be a promoted formal plugin case");
  114. });
  115. it("routes promoted plugin specs to plugin case manifests and fixtures", () => {
  116. const result = runScript("check-e2e-fixtures.mjs", ["--spec", formalProbeSpec]);
  117. expect(result.status).toBe(0);
  118. expect(result.stdout).toContain(`Case: ${fixtureCheckCase}`);
  119. expect(result.stdout).toContain("Fixture check passed");
  120. });
  121. it("never promotes the real official CDN manual smoke", () => {
  122. const result = runScript("promote-plugin-e2e.mjs", ["--spec", manualOnlySpec, "--reviewed"]);
  123. expect(result.status).toBe(1);
  124. expect(result.stdout).toContain("manual-only and cannot be promoted");
  125. });
  126. });
  127. function runScript(scriptName: string, args: string[]) {
  128. return spawnSync(
  129. process.execPath,
  130. [resolve(repoRoot, "packages/desktop/scripts", scriptName), ...args],
  131. { cwd: repoRoot, encoding: "utf-8" },
  132. );
  133. }
  134. async function writeProbeFile(path: string, content: string): Promise<void> {
  135. await mkdir(resolve(path, ".."), { recursive: true });
  136. await writeFile(path, content, "utf-8");
  137. }