|
1 | | -import { describe, expect, it } from "vitest"; |
| 1 | +import { describe, expect, it, vi, beforeEach } from "vitest"; |
2 | 2 | import { directoryChoice, fileChoice, repoChoice } from "./common.js"; |
3 | | -import { getSelectRuleChoices } from "./init-prompts.js"; |
| 3 | +import { askUserToSelectRepos, getSelectRuleChoices } from "./init-prompts.js"; |
| 4 | +import { input } from "@inquirer/prompts"; |
| 5 | +import { ok, error, userInputError } from "../utils/result.js"; |
| 6 | + |
| 7 | +// Mock the input prompt |
| 8 | +vi.mock("@inquirer/prompts", () => ({ |
| 9 | + input: vi.fn(), |
| 10 | + checkbox: vi.fn(), |
| 11 | +})); |
4 | 12 |
|
5 | 13 | describe("getSelectRuleChoices", () => { |
6 | 14 | it("should return a list of choices", () => { |
@@ -95,3 +103,40 @@ describe("getSelectRuleChoices", () => { |
95 | 103 | ]); |
96 | 104 | }); |
97 | 105 | }); |
| 106 | + |
| 107 | +describe("askUserToSelectRepos", () => { |
| 108 | + beforeEach(() => { |
| 109 | + vi.clearAllMocks(); |
| 110 | + }); |
| 111 | + |
| 112 | + it("should return a success result with repository URLs when valid repositories are provided", async () => { |
| 113 | + const repoUrl1 = "git@github.com:my-org/my-repo.git"; |
| 114 | + const repoUrl2 = "git@github.com:my-other-org/my-other-repo.git"; |
| 115 | + |
| 116 | + // Mock input to first return two repo URLs and then an empty string to finish |
| 117 | + vi.mocked(input).mockResolvedValueOnce(repoUrl1); |
| 118 | + vi.mocked(input).mockResolvedValueOnce(repoUrl2); |
| 119 | + vi.mocked(input).mockResolvedValueOnce(""); |
| 120 | + |
| 121 | + const result = await askUserToSelectRepos(); |
| 122 | + |
| 123 | + expect(result).toEqual(ok([repoUrl1, repoUrl2])); |
| 124 | + expect(input).toHaveBeenCalledTimes(3); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should return an error result when no repositories are provided", async () => { |
| 128 | + // Mock input to immediately return an empty string (user doesn't add any repos) |
| 129 | + vi.mocked(input).mockResolvedValueOnce(""); |
| 130 | + |
| 131 | + const result = await askUserToSelectRepos(); |
| 132 | + |
| 133 | + expect(result).toEqual( |
| 134 | + error( |
| 135 | + userInputError( |
| 136 | + "No repositories were added. Please try again and enter at least one repository URL." |
| 137 | + ) |
| 138 | + ) |
| 139 | + ); |
| 140 | + expect(input).toHaveBeenCalledTimes(1); |
| 141 | + }); |
| 142 | +}); |
0 commit comments