e2eRemoteReleaseRequirement.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { dirname, join } from "node:path";
  4. import { fileURLToPath } from "node:url";
  5. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  6. import {
  7. assertRemoteReleaseStaged,
  8. listSpecsRequiringRemoteRelease,
  9. REMOTE_RELEASE_DIR_ENV,
  10. REMOTE_RELEASE_CONSUMER_MODULES,
  11. } from "./e2e/helpers/e2e-remote-release-requirement.js";
  12. let desktopDir: string;
  13. async function writeSource(path: string, text: string) {
  14. await mkdir(dirname(join(desktopDir, path)), { recursive: true });
  15. await writeFile(join(desktopDir, path), text, "utf8");
  16. }
  17. beforeEach(async () => {
  18. desktopDir = await mkdtemp(join(tmpdir(), "zcode-remote-release-requirement-"));
  19. await writeSource("test/e2e/helpers/ssh-remote-p0.ts", "export const ssh = 1;\n");
  20. await writeSource(
  21. "test/e2e/helpers/ssh-remote-lifecycle.ts",
  22. 'import { ssh } from "./ssh-remote-p0.js";\nexport const lifecycle = ssh;\n',
  23. );
  24. await writeSource(
  25. "test/e2e/helpers/desktop-app.ts",
  26. 'import { lifecycle } from "#e2e/helpers/desktop-app-cycle";\nexport const app = 1;\n',
  27. );
  28. // 循环 import 不能让扫描死循环。
  29. await writeSource(
  30. "test/e2e/helpers/desktop-app-cycle.ts",
  31. 'import { app } from "./desktop-app.js";\nexport const lifecycle = app;\n',
  32. );
  33. });
  34. afterEach(async () => {
  35. await rm(desktopDir, { force: true, recursive: true });
  36. });
  37. describe("listSpecsRequiringRemoteRelease", () => {
  38. it("follows #e2e aliases and relative imports transitively", async () => {
  39. await writeSource(
  40. "test/e2e/remote/direct.test.ts",
  41. 'import { ssh } from "#e2e/helpers/ssh-remote-p0";\n',
  42. );
  43. await writeSource(
  44. "test/e2e/remote/transitive.test.ts",
  45. 'import { lifecycle } from "../helpers/ssh-remote-lifecycle.js";\n',
  46. );
  47. await writeSource(
  48. "test/e2e/remote/dynamic.test.ts",
  49. 'const mod = await import("#e2e/helpers/ssh-remote-p0");\n',
  50. );
  51. await writeSource(
  52. "test/e2e/remote/reexport.test.ts",
  53. 'export { ssh } from "#e2e/helpers/ssh-remote-p0";\n',
  54. );
  55. // 类型 import 也计入:多准备不会出错,少准备会让用例在兜底处失败。
  56. await writeSource(
  57. "test/e2e/remote/type-only.test.ts",
  58. 'import type { ssh } from "#e2e/helpers/ssh-remote-p0";\n',
  59. );
  60. await writeSource(
  61. "test/e2e/local/plain.test.ts",
  62. 'import { app } from "#e2e/helpers/desktop-app";\nimport { x } from "@zcode/shared";\n',
  63. );
  64. const required = await listSpecsRequiringRemoteRelease({
  65. desktopDir,
  66. specFiles: [
  67. "test/e2e/remote/direct.test.ts",
  68. "test/e2e/remote/transitive.test.ts",
  69. "test/e2e/remote/dynamic.test.ts",
  70. "test/e2e/remote/reexport.test.ts",
  71. "test/e2e/remote/type-only.test.ts",
  72. "test/e2e/local/plain.test.ts",
  73. ],
  74. });
  75. expect(required).toEqual([
  76. "test/e2e/remote/direct.test.ts",
  77. "test/e2e/remote/transitive.test.ts",
  78. "test/e2e/remote/dynamic.test.ts",
  79. "test/e2e/remote/reexport.test.ts",
  80. "test/e2e/remote/type-only.test.ts",
  81. ]);
  82. });
  83. it("returns nothing when no scheduled spec reaches a consumer module", async () => {
  84. await writeSource(
  85. "test/e2e/local/plain.test.ts",
  86. 'import { app } from "#e2e/helpers/desktop-app";\n',
  87. );
  88. await expect(
  89. listSpecsRequiringRemoteRelease({
  90. desktopDir,
  91. specFiles: ["test/e2e/local/plain.test.ts"],
  92. }),
  93. ).resolves.toEqual([]);
  94. });
  95. it("counts every module that reads the staged release as a consumer", async () => {
  96. for (const modulePath of REMOTE_RELEASE_CONSUMER_MODULES) {
  97. await writeSource(modulePath, "export {};\n");
  98. }
  99. await writeSource(
  100. "test/e2e/remote/update.test.ts",
  101. 'import "#e2e/helpers/source-server-update-fixture";\n',
  102. );
  103. await expect(
  104. listSpecsRequiringRemoteRelease({
  105. desktopDir,
  106. specFiles: ["test/e2e/remote/update.test.ts"],
  107. }),
  108. ).resolves.toEqual(["test/e2e/remote/update.test.ts"]);
  109. });
  110. it("covers the real SSH formal specs in this repository", async () => {
  111. // 真实仓库回归:已知只经 ssh-remote-lifecycle 间接引用 SSH helper 的用例必须被识别。
  112. const realDesktopDir = fileURLToPath(new URL("..", import.meta.url));
  113. const spec =
  114. "test/e2e/conversation-session/remote/installation/conversation-session-ssh-remote-daemon-starting-reuse.test.ts";
  115. const plain =
  116. "test/e2e/conversation-session/context/conversation-session-instruction-file-delta.test.ts";
  117. await expect(
  118. listSpecsRequiringRemoteRelease({ desktopDir: realDesktopDir, specFiles: [spec, plain] }),
  119. ).resolves.toEqual([spec]);
  120. });
  121. });
  122. describe("assertRemoteReleaseStaged", () => {
  123. it("fails with a clear message when onPrepare did not stage the release", async () => {
  124. expect(() => assertRemoteReleaseStaged("CASE-1", {})).toThrow(
  125. /CASE-1.*需要 remote release.*onPrepare 没有准备/u,
  126. );
  127. expect(() =>
  128. assertRemoteReleaseStaged("CASE-1", { [REMOTE_RELEASE_DIR_ENV]: join(desktopDir, "nope") }),
  129. ).toThrow(/onPrepare 没有准备/u);
  130. });
  131. it("passes when the staged descriptor exists", async () => {
  132. const releaseDir = join(desktopDir, "staged");
  133. await mkdir(releaseDir, { recursive: true });
  134. await writeFile(join(releaseDir, "source-server-release.json"), "{}", "utf8");
  135. expect(() =>
  136. assertRemoteReleaseStaged("CASE-1", { [REMOTE_RELEASE_DIR_ENV]: releaseDir }),
  137. ).not.toThrow();
  138. });
  139. it("skips the check for packaged apps, which never read mock-cdn", () => {
  140. expect(() =>
  141. assertRemoteReleaseStaged("CASE-1", {
  142. ZCODE_E2E_PACKAGED_APP_PATH: "/Applications/ZCode.app",
  143. }),
  144. ).not.toThrow();
  145. });
  146. });