stageRemoteAssetsCli.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { EventEmitter } from "node:events";
  2. import { readFile } from "node:fs/promises";
  3. import { fileURLToPath } from "node:url";
  4. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  5. const { spawn, begin, collect } = vi.hoisted(() => ({
  6. spawn: vi.fn(),
  7. begin: vi.fn(),
  8. collect: vi.fn(),
  9. }));
  10. vi.mock("node:child_process", () => ({ spawn }));
  11. const releaseModulePath = fileURLToPath(
  12. new URL("../src/packaging/remoteAssetRelease.ts", import.meta.url),
  13. );
  14. const cliModulePath = fileURLToPath(
  15. new URL("../src/packaging/stageRemoteAssetsCli.ts", import.meta.url),
  16. );
  17. const allTargets = ["linux-x64", "linux-arm64", "darwin-x64", "darwin-arm64"];
  18. beforeEach(() => {
  19. vi.resetModules();
  20. vi.clearAllMocks();
  21. begin.mockResolvedValue(undefined);
  22. collect.mockResolvedValue([]);
  23. spawn.mockImplementation(() => {
  24. const child = new EventEmitter();
  25. queueMicrotask(() => child.emit("exit", 0));
  26. return child;
  27. });
  28. vi.doMock(releaseModulePath, () => ({
  29. SOURCE_SERVER_REMOTE_RELEASE_TARGETS: allTargets,
  30. beginSourceServerRemoteReleaseGeneration: begin,
  31. collectSourceServerRemoteReleaseArchives: collect,
  32. }));
  33. });
  34. afterEach(() => {
  35. vi.restoreAllMocks();
  36. vi.unstubAllEnvs();
  37. vi.doUnmock(releaseModulePath);
  38. });
  39. async function runCli(args: string[]) {
  40. vi.spyOn(process, "argv", "get").mockReturnValue([process.execPath, cliModulePath, ...args]);
  41. await import(cliModulePath);
  42. }
  43. describe("remote asset staging CLI", () => {
  44. it("wires the root Linux command to the existing staging entry", async () => {
  45. const rootPackage = JSON.parse(
  46. await readFile(new URL("../../../package.json", import.meta.url), "utf8"),
  47. );
  48. expect(rootPackage.scripts["prepare:remote-assets:linux"]).toBe(
  49. "pnpm --filter @zcode/server-cli stage:remote-assets --linux",
  50. );
  51. });
  52. it.each([
  53. [undefined, "production"],
  54. [" ", "production"],
  55. [" TEST ", "test"],
  56. ["production", "production"],
  57. ])("stages only Linux targets with ZCODE_ENV=%s", async (input, expected) => {
  58. vi.stubEnv("ZCODE_ENV", input);
  59. await runCli(["--linux"]);
  60. expect(spawn.mock.calls.map((call) => call[1])).toEqual([
  61. ["--filter", "@zcode/server-cli", "build"],
  62. [
  63. "--filter",
  64. "@zcode/server-cli",
  65. "exec",
  66. "tsx",
  67. "src/packaging/stageCli.ts",
  68. "--target",
  69. "linux-x64",
  70. "--skip-component-archives",
  71. ],
  72. [
  73. "--filter",
  74. "@zcode/server-cli",
  75. "exec",
  76. "tsx",
  77. "src/packaging/stageCli.ts",
  78. "--target",
  79. "linux-arm64",
  80. "--skip-component-archives",
  81. "--skip-agent-build",
  82. ],
  83. ]);
  84. for (const call of spawn.mock.calls) {
  85. expect(call[2].env.ZCODE_ENV).toBe(expected);
  86. }
  87. expect(collect).toHaveBeenCalledWith(
  88. expect.objectContaining({ targets: ["linux-x64", "linux-arm64"] }),
  89. );
  90. expect(begin.mock.invocationCallOrder[0]).toBeLessThan(spawn.mock.invocationCallOrder[0]!);
  91. expect(collect.mock.invocationCallOrder[0]).toBeGreaterThan(
  92. spawn.mock.invocationCallOrder.at(-1)!,
  93. );
  94. });
  95. it("preserves all four targets and environment for the default entry", async () => {
  96. vi.stubEnv("ZCODE_ENV", undefined);
  97. await runCli([]);
  98. expect(spawn).toHaveBeenCalledTimes(5);
  99. expect(spawn.mock.calls.slice(1).map((call) => call[1][6])).toEqual(allTargets);
  100. for (const call of spawn.mock.calls) {
  101. expect(call[1]).not.toContain("--skip-component-archives");
  102. }
  103. expect(spawn.mock.calls[0]![2].env?.ZCODE_ENV).toBeUndefined();
  104. expect(collect).toHaveBeenCalledWith(expect.objectContaining({ targets: allTargets }));
  105. });
  106. // E2E onPrepare 已在本轮构建 agent bundle;调用方声明后,第一个 target 也复用,不再构建第二轮。
  107. it("skips the agent build for every target when the caller already built it", async () => {
  108. vi.stubEnv("ZCODE_ENV", "test");
  109. await runCli(["--linux", "--skip-agent-build"]);
  110. const stageCalls = spawn.mock.calls.slice(1).map((call) => call[1]);
  111. expect(stageCalls).toHaveLength(2);
  112. for (const args of stageCalls) {
  113. expect(args.filter((arg: string) => arg === "--skip-agent-build")).toHaveLength(1);
  114. }
  115. });
  116. it("rejects invalid Linux environments before invalidating or building", async () => {
  117. vi.stubEnv("ZCODE_ENV", "invalid");
  118. await expect(runCli(["--linux"])).rejects.toThrow(/Invalid ZCODE_ENV/);
  119. expect(begin).not.toHaveBeenCalled();
  120. expect(spawn).not.toHaveBeenCalled();
  121. expect(collect).not.toHaveBeenCalled();
  122. });
  123. it.each([
  124. ["win32", true],
  125. ["darwin", false],
  126. ["linux", false],
  127. ])("spawns pnpm through a shell only on Windows (platform=%s)", async (platform, shell) => {
  128. const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform")!;
  129. Object.defineProperty(process, "platform", { ...originalPlatform, value: platform });
  130. try {
  131. await runCli(["--linux"]);
  132. } finally {
  133. Object.defineProperty(process, "platform", originalPlatform);
  134. }
  135. expect(spawn).toHaveBeenCalledTimes(3);
  136. for (const call of spawn.mock.calls) {
  137. expect(call[0]).toBe("pnpm");
  138. expect(call[2].shell).toBe(shell);
  139. }
  140. });
  141. it("does not publish when the second Linux target fails", async () => {
  142. spawn.mockImplementation((_command, args: string[]) => {
  143. const child = new EventEmitter();
  144. queueMicrotask(() => child.emit("exit", args.includes("linux-arm64") ? 1 : 0));
  145. return child;
  146. });
  147. await expect(runCli(["--linux"])).rejects.toThrow(/exited with code 1/);
  148. expect(begin).toHaveBeenCalledOnce();
  149. expect(collect).not.toHaveBeenCalled();
  150. });
  151. });