stageRemoteAssetsCli.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. it("rejects invalid Linux environments before invalidating or building", async () => {
  107. vi.stubEnv("ZCODE_ENV", "invalid");
  108. await expect(runCli(["--linux"])).rejects.toThrow(/Invalid ZCODE_ENV/);
  109. expect(begin).not.toHaveBeenCalled();
  110. expect(spawn).not.toHaveBeenCalled();
  111. expect(collect).not.toHaveBeenCalled();
  112. });
  113. it.each([
  114. ["win32", true],
  115. ["darwin", false],
  116. ["linux", false],
  117. ])("spawns pnpm through a shell only on Windows (platform=%s)", async (platform, shell) => {
  118. const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform")!;
  119. Object.defineProperty(process, "platform", { ...originalPlatform, value: platform });
  120. try {
  121. await runCli(["--linux"]);
  122. } finally {
  123. Object.defineProperty(process, "platform", originalPlatform);
  124. }
  125. expect(spawn).toHaveBeenCalledTimes(3);
  126. for (const call of spawn.mock.calls) {
  127. expect(call[0]).toBe("pnpm");
  128. expect(call[2].shell).toBe(shell);
  129. }
  130. });
  131. it("does not publish when the second Linux target fails", async () => {
  132. spawn.mockImplementation((_command, args: string[]) => {
  133. const child = new EventEmitter();
  134. queueMicrotask(() => child.emit("exit", args.includes("linux-arm64") ? 1 : 0));
  135. return child;
  136. });
  137. await expect(runCli(["--linux"])).rejects.toThrow(/exited with code 1/);
  138. expect(begin).toHaveBeenCalledOnce();
  139. expect(collect).not.toHaveBeenCalled();
  140. });
  141. });