|
| 1 | +import { Command } from "commander"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +describe("devices cli lazy runtime boundary", () => { |
| 5 | + beforeEach(() => { |
| 6 | + vi.resetModules(); |
| 7 | + }); |
| 8 | + |
| 9 | + afterEach(() => { |
| 10 | + vi.doUnmock("./devices-cli.runtime.js"); |
| 11 | + vi.resetModules(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("renders parent help without importing the devices runtime", async () => { |
| 15 | + const runtimeLoaded = vi.fn(); |
| 16 | + vi.doMock("./devices-cli.runtime.js", () => { |
| 17 | + runtimeLoaded(); |
| 18 | + return { |
| 19 | + runDevicesApproveCommand: vi.fn(), |
| 20 | + runDevicesClearCommand: vi.fn(), |
| 21 | + runDevicesListCommand: vi.fn(), |
| 22 | + runDevicesRejectCommand: vi.fn(), |
| 23 | + runDevicesRemoveCommand: vi.fn(), |
| 24 | + runDevicesRevokeCommand: vi.fn(), |
| 25 | + runDevicesRotateCommand: vi.fn(), |
| 26 | + }; |
| 27 | + }); |
| 28 | + |
| 29 | + const { registerDevicesCli } = await import("./devices-cli.js"); |
| 30 | + const program = new Command(); |
| 31 | + program.exitOverride(); |
| 32 | + program.configureOutput({ |
| 33 | + writeErr: () => {}, |
| 34 | + writeOut: () => {}, |
| 35 | + }); |
| 36 | + registerDevicesCli(program); |
| 37 | + |
| 38 | + await expect(program.parseAsync(["devices", "--help"], { from: "user" })).rejects.toMatchObject( |
| 39 | + { |
| 40 | + exitCode: 0, |
| 41 | + }, |
| 42 | + ); |
| 43 | + expect(runtimeLoaded).not.toHaveBeenCalled(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("loads the devices runtime for command actions", async () => { |
| 47 | + const runDevicesListCommand = vi.fn().mockResolvedValue(undefined); |
| 48 | + const runtimeLoaded = vi.fn(); |
| 49 | + vi.doMock("./devices-cli.runtime.js", () => { |
| 50 | + runtimeLoaded(); |
| 51 | + return { |
| 52 | + runDevicesApproveCommand: vi.fn(), |
| 53 | + runDevicesClearCommand: vi.fn(), |
| 54 | + runDevicesListCommand, |
| 55 | + runDevicesRejectCommand: vi.fn(), |
| 56 | + runDevicesRemoveCommand: vi.fn(), |
| 57 | + runDevicesRevokeCommand: vi.fn(), |
| 58 | + runDevicesRotateCommand: vi.fn(), |
| 59 | + }; |
| 60 | + }); |
| 61 | + |
| 62 | + const { registerDevicesCli } = await import("./devices-cli.js"); |
| 63 | + const program = new Command(); |
| 64 | + registerDevicesCli(program); |
| 65 | + |
| 66 | + await program.parseAsync(["devices", "list", "--json"], { from: "user" }); |
| 67 | + |
| 68 | + expect(runtimeLoaded).toHaveBeenCalledTimes(1); |
| 69 | + expect(runDevicesListCommand).toHaveBeenCalledWith(expect.objectContaining({ json: true })); |
| 70 | + }); |
| 71 | +}); |
0 commit comments