pluginE2EAdmissionScripts.test.ts 5.4 KB

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