studio.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import { join } from "node:path";
  3. const accessMock = vi.fn();
  4. const spawnMock = vi.fn(() => ({
  5. on: vi.fn(),
  6. }));
  7. const logMock = vi.fn();
  8. const logErrorMock = vi.fn();
  9. vi.mock("node:fs/promises", () => ({
  10. access: accessMock,
  11. mkdir: vi.fn(),
  12. readFile: vi.fn(async () => ""),
  13. writeFile: vi.fn(),
  14. }));
  15. vi.mock("node:child_process", () => ({
  16. spawn: spawnMock,
  17. }));
  18. vi.mock("../utils.js", () => ({
  19. findProjectRoot: vi.fn(() => "/project"),
  20. log: logMock,
  21. logError: logErrorMock,
  22. }));
  23. describe("studio command", () => {
  24. beforeEach(() => {
  25. vi.clearAllMocks();
  26. vi.resetModules();
  27. });
  28. it("launches TypeScript sources through tsx in monorepo mode", async () => {
  29. const tsEntry = join("/project", "packages", "studio", "src", "api", "index.ts");
  30. accessMock.mockImplementation(async (path: string) => {
  31. if (path === tsEntry) {
  32. return;
  33. }
  34. throw new Error(`missing: ${path}`);
  35. });
  36. const { createStudioCommand } = await import("../commands/studio.js");
  37. await createStudioCommand().parseAsync(["node", "studio", "--port", "9001"]);
  38. expect(spawnMock).toHaveBeenCalledWith(
  39. "npx",
  40. ["tsx", tsEntry, "/project"],
  41. expect.objectContaining({
  42. cwd: "/project",
  43. stdio: "inherit",
  44. env: expect.objectContaining({ INKOS_STUDIO_PORT: "9001" }),
  45. }),
  46. );
  47. });
  48. it("launches built JavaScript entries through node", async () => {
  49. const jsEntry = join(
  50. "/project",
  51. "node_modules",
  52. "@actalk",
  53. "inkos-studio",
  54. "dist",
  55. "api",
  56. "index.js",
  57. );
  58. accessMock.mockImplementation(async (path: string) => {
  59. if (path === jsEntry) {
  60. return;
  61. }
  62. throw new Error(`missing: ${path}`);
  63. });
  64. const { createStudioCommand } = await import("../commands/studio.js");
  65. await createStudioCommand().parseAsync(["node", "studio", "--port", "4567"]);
  66. expect(spawnMock).toHaveBeenCalledWith(
  67. "node",
  68. [jsEntry, "/project"],
  69. expect.objectContaining({
  70. cwd: "/project",
  71. stdio: "inherit",
  72. env: expect.objectContaining({ INKOS_STUDIO_PORT: "4567" }),
  73. }),
  74. );
  75. });
  76. });