|
| 1 | +import { afterEach, expect, test, vi } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("@inquirer/prompts", () => ({ |
| 4 | + confirm: vi.fn(), |
| 5 | +})); |
| 6 | + |
| 7 | +import { confirm } from "@inquirer/prompts"; |
| 8 | + |
| 9 | +import { |
| 10 | + confirmThirdPartyPackageSource, |
| 11 | + requiresThirdPartyPackageSourceConfirmation, |
| 12 | +} from "../remote-source.js"; |
| 13 | + |
| 14 | +afterEach(() => { |
| 15 | + vi.restoreAllMocks(); |
| 16 | +}); |
| 17 | + |
| 18 | +test("requiresThirdPartyPackageSourceConfirmation skips halo.run URLs", () => { |
| 19 | + expect( |
| 20 | + requiresThirdPartyPackageSourceConfirmation("https://www.halo.run/plugins/demo-plugin.jar"), |
| 21 | + ).toBe(false); |
| 22 | +}); |
| 23 | + |
| 24 | +test("requiresThirdPartyPackageSourceConfirmation skips file URIs", () => { |
| 25 | + expect(requiresThirdPartyPackageSourceConfirmation("file:///tmp/demo-plugin.jar")).toBe(false); |
| 26 | +}); |
| 27 | + |
| 28 | +test("requiresThirdPartyPackageSourceConfirmation flags third-party URLs", () => { |
| 29 | + expect( |
| 30 | + requiresThirdPartyPackageSourceConfirmation("https://downloads.example.com/demo-plugin.jar"), |
| 31 | + ).toBe(true); |
| 32 | +}); |
| 33 | + |
| 34 | +test("confirmThirdPartyPackageSource skips prompting with --yes", async () => { |
| 35 | + await expect( |
| 36 | + confirmThirdPartyPackageSource( |
| 37 | + "https://downloads.example.com/demo-plugin.jar", |
| 38 | + { |
| 39 | + commandPath: "halo plugin install", |
| 40 | + actionLabel: "installing plugin", |
| 41 | + }, |
| 42 | + { yes: true }, |
| 43 | + ), |
| 44 | + ).resolves.toBe(true); |
| 45 | + |
| 46 | + expect(confirm).not.toHaveBeenCalled(); |
| 47 | +}); |
| 48 | + |
| 49 | +test("confirmThirdPartyPackageSource requires --yes outside interactive terminals", async () => { |
| 50 | + Object.defineProperty(process.stdin, "isTTY", { |
| 51 | + value: false, |
| 52 | + configurable: true, |
| 53 | + }); |
| 54 | + Object.defineProperty(process.stdout, "isTTY", { |
| 55 | + value: false, |
| 56 | + configurable: true, |
| 57 | + }); |
| 58 | + |
| 59 | + await expect( |
| 60 | + confirmThirdPartyPackageSource( |
| 61 | + "https://downloads.example.com/demo-plugin.jar", |
| 62 | + { |
| 63 | + commandPath: "halo plugin install", |
| 64 | + actionLabel: "installing plugin", |
| 65 | + }, |
| 66 | + {}, |
| 67 | + ), |
| 68 | + ).rejects.toThrow(/requires confirmation in interactive mode.*or use --yes/i); |
| 69 | +}); |
| 70 | + |
| 71 | +test("confirmThirdPartyPackageSource returns false when user cancels", async () => { |
| 72 | + Object.defineProperty(process.stdin, "isTTY", { |
| 73 | + value: true, |
| 74 | + configurable: true, |
| 75 | + }); |
| 76 | + Object.defineProperty(process.stdout, "isTTY", { |
| 77 | + value: true, |
| 78 | + configurable: true, |
| 79 | + }); |
| 80 | + |
| 81 | + vi.mocked(confirm).mockResolvedValue(false); |
| 82 | + const stdoutSpy = vi.spyOn(process.stdout, "write").mockImplementation(() => true); |
| 83 | + |
| 84 | + await expect( |
| 85 | + confirmThirdPartyPackageSource( |
| 86 | + "https://downloads.example.com/demo-plugin.jar", |
| 87 | + { |
| 88 | + commandPath: "halo plugin install", |
| 89 | + actionLabel: "installing plugin", |
| 90 | + }, |
| 91 | + {}, |
| 92 | + ), |
| 93 | + ).resolves.toBe(false); |
| 94 | + |
| 95 | + expect(stdoutSpy).toHaveBeenCalledWith( |
| 96 | + "Warning: remote package URL is not hosted on www.halo.run: https://downloads.example.com/demo-plugin.jar\n", |
| 97 | + ); |
| 98 | + expect(stdoutSpy).toHaveBeenCalledWith("Cancelled installing plugin.\n"); |
| 99 | +}); |
0 commit comments