|
| 1 | +import { GitProfileCommandService } from "../../../src/commands/git-profile/git-profile-command.service"; |
| 2 | +import { loggingTestTransport } from "../../jest.setup"; |
| 3 | +import * as fs from "fs"; |
| 4 | +import * as path from "path"; |
| 5 | +import * as os from "os"; |
| 6 | + |
| 7 | +jest.mock("fs"); |
| 8 | +jest.mock("os"); |
| 9 | + |
| 10 | +describe("Git Profile - List Profiles", () => { |
| 11 | + |
| 12 | + let gitProfileCommandService: GitProfileCommandService; |
| 13 | + const mockHomedir = "/mock/home"; |
| 14 | + const mockProfilePath = path.resolve(mockHomedir, ".celonis-content-cli-git-profiles"); |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + (os.homedir as jest.Mock).mockReturnValue(mockHomedir); |
| 18 | + |
| 19 | + gitProfileCommandService = new GitProfileCommandService(); |
| 20 | + |
| 21 | + jest.spyOn(process, "exit").mockImplementation((code?: number) => { |
| 22 | + return undefined as never; |
| 23 | + }); |
| 24 | + |
| 25 | + loggingTestTransport.logMessages = []; |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + jest.clearAllMocks(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("Should list all Git profiles without a default profile", async () => { |
| 33 | + const mockProfiles = [ |
| 34 | + { name: "profile1.json", isDirectory: () => false }, |
| 35 | + { name: "profile2.json", isDirectory: () => false }, |
| 36 | + { name: "profile3.json", isDirectory: () => false }, |
| 37 | + ]; |
| 38 | + |
| 39 | + (fs.existsSync as jest.Mock).mockImplementation((filePath: string) => { |
| 40 | + if (filePath.includes("config.json")) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + return true; |
| 44 | + }); |
| 45 | + (fs.readdirSync as jest.Mock).mockReturnValue(mockProfiles); |
| 46 | + |
| 47 | + await gitProfileCommandService.listProfiles(); |
| 48 | + |
| 49 | + expect(loggingTestTransport.logMessages).toHaveLength(3); |
| 50 | + expect(loggingTestTransport.logMessages[0].message.trim()).toEqual("profile1"); |
| 51 | + expect(loggingTestTransport.logMessages[1].message.trim()).toEqual("profile2"); |
| 52 | + expect(loggingTestTransport.logMessages[2].message.trim()).toEqual("profile3"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("Should list all Git profiles and mark the default profile", async () => { |
| 56 | + const mockProfiles = [ |
| 57 | + { name: "profile1.json", isDirectory: () => false }, |
| 58 | + { name: "default-profile.json", isDirectory: () => false }, |
| 59 | + { name: "profile3.json", isDirectory: () => false }, |
| 60 | + ]; |
| 61 | + |
| 62 | + (fs.existsSync as jest.Mock).mockImplementation((filePath: string) => { |
| 63 | + return true; |
| 64 | + }); |
| 65 | + (fs.readdirSync as jest.Mock).mockReturnValue(mockProfiles); |
| 66 | + (fs.readFileSync as jest.Mock).mockImplementation((filePath: string) => { |
| 67 | + if (filePath.includes("config.json")) { |
| 68 | + return JSON.stringify({ defaultProfile: "default-profile" }); |
| 69 | + } |
| 70 | + return "{}"; |
| 71 | + }); |
| 72 | + |
| 73 | + await gitProfileCommandService.listProfiles(); |
| 74 | + |
| 75 | + expect(loggingTestTransport.logMessages).toHaveLength(3); |
| 76 | + expect(loggingTestTransport.logMessages[0].message.trim()).toEqual("profile1"); |
| 77 | + expect(loggingTestTransport.logMessages[1].message.trim()).toEqual("default-profile (default)"); |
| 78 | + expect(loggingTestTransport.logMessages[2].message.trim()).toEqual("profile3"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("Should handle when no profiles exist", async () => { |
| 82 | + (fs.existsSync as jest.Mock).mockReturnValue(false); |
| 83 | + |
| 84 | + await gitProfileCommandService.listProfiles(); |
| 85 | + |
| 86 | + expect(loggingTestTransport.logMessages).toHaveLength(0); |
| 87 | + }); |
| 88 | + |
| 89 | + it("Should handle when profile directory exists but is empty", async () => { |
| 90 | + (fs.existsSync as jest.Mock).mockReturnValue(true); |
| 91 | + (fs.readdirSync as jest.Mock).mockReturnValue([]); |
| 92 | + |
| 93 | + await gitProfileCommandService.listProfiles(); |
| 94 | + |
| 95 | + expect(loggingTestTransport.logMessages).toHaveLength(0); |
| 96 | + }); |
| 97 | + |
| 98 | + it("Should filter out config.json from the profile list", async () => { |
| 99 | + const mockProfiles = [ |
| 100 | + { name: "profile1.json", isDirectory: () => false }, |
| 101 | + { name: "config.json", isDirectory: () => false }, |
| 102 | + { name: "profile2.json", isDirectory: () => false }, |
| 103 | + ]; |
| 104 | + |
| 105 | + (fs.existsSync as jest.Mock).mockReturnValue(true); |
| 106 | + (fs.readdirSync as jest.Mock).mockReturnValue(mockProfiles); |
| 107 | + (fs.readFileSync as jest.Mock).mockImplementation((filePath: string) => { |
| 108 | + if (filePath.includes("config.json")) { |
| 109 | + return JSON.stringify({ defaultProfile: "profile1" }); |
| 110 | + } |
| 111 | + return "{}"; |
| 112 | + }); |
| 113 | + |
| 114 | + await gitProfileCommandService.listProfiles(); |
| 115 | + |
| 116 | + expect(loggingTestTransport.logMessages).toHaveLength(2); |
| 117 | + expect(loggingTestTransport.logMessages[0].message.trim()).toEqual("profile1 (default)"); |
| 118 | + expect(loggingTestTransport.logMessages[1].message.trim()).toEqual("profile2"); |
| 119 | + expect(loggingTestTransport.logMessages.some(msg => msg.message.includes("config"))).toBe(false); |
| 120 | + }); |
| 121 | + |
| 122 | + it("Should filter out directories from the profile list", async () => { |
| 123 | + const mockProfiles = [ |
| 124 | + { name: "profile1.json", isDirectory: () => false }, |
| 125 | + { name: "some-directory", isDirectory: () => true }, |
| 126 | + { name: "profile2.json", isDirectory: () => false }, |
| 127 | + ]; |
| 128 | + |
| 129 | + (fs.existsSync as jest.Mock).mockReturnValue(true); |
| 130 | + (fs.readdirSync as jest.Mock).mockReturnValue(mockProfiles); |
| 131 | + (fs.readFileSync as jest.Mock).mockReturnValue("{}"); |
| 132 | + |
| 133 | + await gitProfileCommandService.listProfiles(); |
| 134 | + |
| 135 | + expect(loggingTestTransport.logMessages).toHaveLength(2); |
| 136 | + expect(loggingTestTransport.logMessages[0].message.trim()).toEqual("profile1"); |
| 137 | + expect(loggingTestTransport.logMessages[1].message.trim()).toEqual("profile2"); |
| 138 | + }); |
| 139 | + |
| 140 | + it("Should filter out non-JSON files from the profile list", async () => { |
| 141 | + const mockProfiles = [ |
| 142 | + { name: "profile1.json", isDirectory: () => false }, |
| 143 | + { name: "readme.txt", isDirectory: () => false }, |
| 144 | + { name: "profile2.json", isDirectory: () => false }, |
| 145 | + { name: "notes.md", isDirectory: () => false }, |
| 146 | + ]; |
| 147 | + |
| 148 | + (fs.existsSync as jest.Mock).mockReturnValue(true); |
| 149 | + (fs.readdirSync as jest.Mock).mockReturnValue(mockProfiles); |
| 150 | + (fs.readFileSync as jest.Mock).mockReturnValue("{}"); |
| 151 | + |
| 152 | + await gitProfileCommandService.listProfiles(); |
| 153 | + |
| 154 | + expect(loggingTestTransport.logMessages).toHaveLength(2); |
| 155 | + expect(loggingTestTransport.logMessages[0].message.trim()).toEqual("profile1"); |
| 156 | + expect(loggingTestTransport.logMessages[1].message.trim()).toEqual("profile2"); |
| 157 | + }); |
| 158 | + |
| 159 | + it("Should handle errors when reading profiles gracefully", async () => { |
| 160 | + (fs.existsSync as jest.Mock).mockReturnValue(true); |
| 161 | + (fs.readdirSync as jest.Mock).mockImplementation(() => { |
| 162 | + throw new Error("Permission denied"); |
| 163 | + }); |
| 164 | + |
| 165 | + await gitProfileCommandService.listProfiles(); |
| 166 | + |
| 167 | + expect(loggingTestTransport.logMessages.some(msg => |
| 168 | + msg.level.includes("error") |
| 169 | + )).toBe(true); |
| 170 | + }); |
| 171 | + |
| 172 | + it("Should handle when config.json exists but is empty", async () => { |
| 173 | + const mockProfiles = [ |
| 174 | + { name: "profile1.json", isDirectory: () => false }, |
| 175 | + { name: "profile2.json", isDirectory: () => false }, |
| 176 | + ]; |
| 177 | + |
| 178 | + (fs.existsSync as jest.Mock).mockReturnValue(true); |
| 179 | + (fs.readdirSync as jest.Mock).mockReturnValue(mockProfiles); |
| 180 | + (fs.readFileSync as jest.Mock).mockImplementation((filePath: string) => { |
| 181 | + if (filePath.includes("config.json")) { |
| 182 | + return "{}"; |
| 183 | + } |
| 184 | + return "{}"; |
| 185 | + }); |
| 186 | + |
| 187 | + await gitProfileCommandService.listProfiles(); |
| 188 | + |
| 189 | + expect(loggingTestTransport.logMessages).toHaveLength(2); |
| 190 | + expect(loggingTestTransport.logMessages[0].message.trim()).toEqual("profile1"); |
| 191 | + expect(loggingTestTransport.logMessages[1].message.trim()).toEqual("profile2"); |
| 192 | + }); |
| 193 | + |
| 194 | + it("Should handle when config.json has a default profile that doesn't exist in the list", async () => { |
| 195 | + const mockProfiles = [ |
| 196 | + { name: "profile1.json", isDirectory: () => false }, |
| 197 | + { name: "profile2.json", isDirectory: () => false }, |
| 198 | + ]; |
| 199 | + |
| 200 | + (fs.existsSync as jest.Mock).mockReturnValue(true); |
| 201 | + (fs.readdirSync as jest.Mock).mockReturnValue(mockProfiles); |
| 202 | + (fs.readFileSync as jest.Mock).mockImplementation((filePath: string) => { |
| 203 | + if (filePath.includes("config.json")) { |
| 204 | + return JSON.stringify({ defaultProfile: "non-existent-profile" }); |
| 205 | + } |
| 206 | + return "{}"; |
| 207 | + }); |
| 208 | + |
| 209 | + await gitProfileCommandService.listProfiles(); |
| 210 | + |
| 211 | + expect(loggingTestTransport.logMessages).toHaveLength(2); |
| 212 | + expect(loggingTestTransport.logMessages[0].message.trim()).toEqual("profile1"); |
| 213 | + expect(loggingTestTransport.logMessages[1].message.trim()).toEqual("profile2"); |
| 214 | + expect(loggingTestTransport.logMessages.some(msg => msg.message.includes("(default)"))).toBe(false); |
| 215 | + }); |
| 216 | + |
| 217 | + it("Should list profiles in the order they are read from the filesystem", async () => { |
| 218 | + const mockProfiles = [ |
| 219 | + { name: "zeta.json", isDirectory: () => false }, |
| 220 | + { name: "alpha.json", isDirectory: () => false }, |
| 221 | + { name: "beta.json", isDirectory: () => false }, |
| 222 | + ]; |
| 223 | + |
| 224 | + (fs.existsSync as jest.Mock).mockReturnValue(true); |
| 225 | + (fs.readdirSync as jest.Mock).mockReturnValue(mockProfiles); |
| 226 | + (fs.readFileSync as jest.Mock).mockReturnValue("{}"); |
| 227 | + |
| 228 | + await gitProfileCommandService.listProfiles(); |
| 229 | + |
| 230 | + expect(loggingTestTransport.logMessages).toHaveLength(3); |
| 231 | + expect(loggingTestTransport.logMessages[0].message.trim()).toEqual("zeta"); |
| 232 | + expect(loggingTestTransport.logMessages[1].message.trim()).toEqual("alpha"); |
| 233 | + expect(loggingTestTransport.logMessages[2].message.trim()).toEqual("beta"); |
| 234 | + }); |
| 235 | +}); |
| 236 | + |
0 commit comments