studio.test.ts 2.1 KB

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