|
| 1 | +import { it } from "vitest"; |
| 2 | +import { beforeEach } from "vitest"; |
| 3 | +import { describe } from "vitest"; |
| 4 | +import { expect } from "vitest"; |
| 5 | +import { vi } from "vitest"; |
| 6 | + |
| 7 | +const mockConfig = { |
| 8 | + get: vi.fn(), |
| 9 | + set: vi.fn(), |
| 10 | + clear: vi.fn(), |
| 11 | + delete: vi.fn(), |
| 12 | +}; |
| 13 | + |
| 14 | +vi.mock('conf', () => { |
| 15 | + const Conf = vi.fn(() => mockConfig); |
| 16 | + return { default: Conf }; |
| 17 | +}); |
| 18 | + |
| 19 | +vi.mock('../src/commons.js', () => ({ |
| 20 | + BASE_URL: 'https://puter.com', |
| 21 | + NULL_UUID: '00000000-0000-0000-0000-000000000000', |
| 22 | + PROJECT_NAME: 'puter-cli', |
| 23 | + getHeaders: vi.fn(() => ({ 'Content-Type': 'application/json' })), |
| 24 | + reconfigureURLs: vi.fn(), |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock('./PuterModule.js', () => ({ |
| 28 | + initPuterModule: vi.fn(), |
| 29 | +})); |
| 30 | + |
| 31 | +let initProfileModule; |
| 32 | +let getProfileModule; |
| 33 | + |
| 34 | +beforeEach(async () => { |
| 35 | + vi.resetModules(); |
| 36 | + vi.clearAllMocks(); |
| 37 | + mockConfig.get.mockReset(); |
| 38 | + mockConfig.set.mockReset(); |
| 39 | + mockConfig.delete.mockReset(); |
| 40 | + const module = await import('../src/modules/ProfileModule'); |
| 41 | + initProfileModule = module.initProfileModule; |
| 42 | + getProfileModule = module.getProfileModule; |
| 43 | +}); |
| 44 | + |
| 45 | +describe("initProfileModule", () => { |
| 46 | + it("should initialize profile module", () => { |
| 47 | + initProfileModule(); |
| 48 | + const profileModule = getProfileModule(); |
| 49 | + expect(profileModule).toBeDefined(); |
| 50 | + }) |
| 51 | +}) |
| 52 | + |
| 53 | +describe('getProfileModule', () => { |
| 54 | + it('should return profile module if initialized', () => { |
| 55 | + initProfileModule(); |
| 56 | + const result = getProfileModule(); |
| 57 | + expect(result).toBeDefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should throw error if not initialized', () => { |
| 61 | + expect(() => getProfileModule()).toThrow('Call initprofileModule() first'); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('ProfileModule.getProfiles', () => { |
| 66 | + it('should return profiles from config', () => { |
| 67 | + const mockProfiles = [ |
| 68 | + { uuid: '1', username: 'user1', host: 'https://puter.com' }, |
| 69 | + { uuid: '2', username: 'user2', host: 'https://puter.com' }, |
| 70 | + ]; |
| 71 | + mockConfig.get.mockReturnValue(mockProfiles); |
| 72 | + |
| 73 | + initProfileModule(); |
| 74 | + const profileModule = getProfileModule(); |
| 75 | + const profiles = profileModule.getProfiles(); |
| 76 | + |
| 77 | + expect(profiles).toEqual(mockProfiles); |
| 78 | + expect(mockConfig.get).toHaveBeenCalledWith('profiles'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should return empty array if no profiles exist', () => { |
| 82 | + mockConfig.get.mockReturnValue(undefined); |
| 83 | + |
| 84 | + initProfileModule(); |
| 85 | + const profileModule = getProfileModule(); |
| 86 | + const profiles = profileModule.getProfiles(); |
| 87 | + |
| 88 | + expect(profiles).toEqual([]); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe('ProfileModule.addProfile', () => { |
| 93 | + it('should add a new profile to existing profiles', () => { |
| 94 | + const existingProfiles = [ |
| 95 | + { uuid: '1', username: 'user1', host: 'https://puter.com' }, |
| 96 | + ]; |
| 97 | + const newProfile = { uuid: '2', username: 'user2', host: 'https://puter.com' }; |
| 98 | + mockConfig.get.mockReturnValue(existingProfiles); |
| 99 | + |
| 100 | + initProfileModule(); |
| 101 | + const profileModule = getProfileModule(); |
| 102 | + profileModule.addProfile(newProfile); |
| 103 | + |
| 104 | + expect(mockConfig.set).toHaveBeenCalledWith('profiles', [...existingProfiles, newProfile]); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should filter out transient profiles when adding', () => { |
| 108 | + const existingProfiles = [ |
| 109 | + { uuid: '1', username: 'user1', host: 'https://puter.com', transient: true }, |
| 110 | + { uuid: '2', username: 'user2', host: 'https://puter.com' }, |
| 111 | + ]; |
| 112 | + const newProfile = { uuid: '3', username: 'user3', host: 'https://puter.com' }; |
| 113 | + mockConfig.get.mockReturnValue(existingProfiles); |
| 114 | + |
| 115 | + initProfileModule(); |
| 116 | + const profileModule = getProfileModule(); |
| 117 | + profileModule.addProfile(newProfile); |
| 118 | + |
| 119 | + expect(mockConfig.set).toHaveBeenCalledWith('profiles', [ |
| 120 | + { uuid: '2', username: 'user2', host: 'https://puter.com' }, |
| 121 | + newProfile, |
| 122 | + ]); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('ProfileModule.selectProfile', () => { |
| 127 | + it('should set selected profile in config', () => { |
| 128 | + const profile = { uuid: 'test-uuid', username: 'testuser', host: 'https://puter.com' }; |
| 129 | + mockConfig.get.mockImplementation((key) => { |
| 130 | + if (key === 'profiles') return [profile]; |
| 131 | + if (key === 'selected_profile') return 'test-uuid'; |
| 132 | + return undefined; |
| 133 | + }); |
| 134 | + |
| 135 | + initProfileModule(); |
| 136 | + const profileModule = getProfileModule(); |
| 137 | + profileModule.selectProfile(profile); |
| 138 | + |
| 139 | + expect(mockConfig.set).toHaveBeenCalledWith('selected_profile', 'test-uuid'); |
| 140 | + expect(mockConfig.set).toHaveBeenCalledWith('username', 'testuser'); |
| 141 | + expect(mockConfig.set).toHaveBeenCalledWith('cwd', '/testuser'); |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
| 145 | +describe('ProfileModule.getCurrentProfile', () => { |
| 146 | + it('should return the currently selected profile', () => { |
| 147 | + const profiles = [ |
| 148 | + { uuid: '1', username: 'user1', host: 'https://puter.com' }, |
| 149 | + { uuid: '2', username: 'user2', host: 'https://puter.com' }, |
| 150 | + ]; |
| 151 | + mockConfig.get.mockImplementation((key) => { |
| 152 | + if (key === 'profiles') return profiles; |
| 153 | + if (key === 'selected_profile') return '2'; |
| 154 | + return undefined; |
| 155 | + }); |
| 156 | + |
| 157 | + initProfileModule(); |
| 158 | + const profileModule = getProfileModule(); |
| 159 | + const currentProfile = profileModule.getCurrentProfile(); |
| 160 | + |
| 161 | + expect(currentProfile).toEqual(profiles[1]); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should return undefined if no profile matches', () => { |
| 165 | + const profiles = [ |
| 166 | + { uuid: '1', username: 'user1', host: 'https://puter.com' }, |
| 167 | + ]; |
| 168 | + mockConfig.get.mockImplementation((key) => { |
| 169 | + if (key === 'profiles') return profiles; |
| 170 | + if (key === 'selected_profile') return 'non-existent'; |
| 171 | + return undefined; |
| 172 | + }); |
| 173 | + |
| 174 | + initProfileModule(); |
| 175 | + const profileModule = getProfileModule(); |
| 176 | + const currentProfile = profileModule.getCurrentProfile(); |
| 177 | + |
| 178 | + expect(currentProfile).toBeUndefined(); |
| 179 | + }); |
| 180 | +}); |
| 181 | + |
| 182 | +describe('ProfileModule.getAuthToken', () => { |
| 183 | + it('should return auth token for selected profile', () => { |
| 184 | + const profiles = [ |
| 185 | + { uuid: '1', username: 'user1', host: 'https://puter.com', token: 'token1' }, |
| 186 | + { uuid: '2', username: 'user2', host: 'https://puter.com', token: 'token2' }, |
| 187 | + ]; |
| 188 | + mockConfig.get.mockImplementation((key) => { |
| 189 | + if (key === 'profiles') return profiles; |
| 190 | + if (key === 'selected_profile') return '2'; |
| 191 | + return undefined; |
| 192 | + }); |
| 193 | + |
| 194 | + initProfileModule(); |
| 195 | + const profileModule = getProfileModule(); |
| 196 | + const token = profileModule.getAuthToken(); |
| 197 | + |
| 198 | + expect(token).toBe('token2'); |
| 199 | + }); |
| 200 | + |
| 201 | + it('should return undefined if no profile is selected', () => { |
| 202 | + const profiles = [ |
| 203 | + { uuid: '1', username: 'user1', host: 'https://puter.com', token: 'token1' }, |
| 204 | + ]; |
| 205 | + mockConfig.get.mockImplementation((key) => { |
| 206 | + if (key === 'profiles') return profiles; |
| 207 | + if (key === 'selected_profile') return undefined; |
| 208 | + return undefined; |
| 209 | + }); |
| 210 | + |
| 211 | + initProfileModule(); |
| 212 | + const profileModule = getProfileModule(); |
| 213 | + const token = profileModule.getAuthToken(); |
| 214 | + |
| 215 | + expect(token).toBeUndefined(); |
| 216 | + }); |
| 217 | +}); |
| 218 | + |
| 219 | +describe('ProfileModule.getDefaultProfile', () => { |
| 220 | + it('should return default profile if auth_token exists', () => { |
| 221 | + mockConfig.get.mockImplementation((key) => { |
| 222 | + if (key === 'auth_token') return 'legacy-token'; |
| 223 | + if (key === 'username') return 'legacyuser'; |
| 224 | + return undefined; |
| 225 | + }); |
| 226 | + |
| 227 | + initProfileModule(); |
| 228 | + const profileModule = getProfileModule(); |
| 229 | + const defaultProfile = profileModule.getDefaultProfile(); |
| 230 | + |
| 231 | + expect(defaultProfile).toEqual({ |
| 232 | + host: 'puter.com', |
| 233 | + username: 'legacyuser', |
| 234 | + token: 'legacy-token', |
| 235 | + }); |
| 236 | + }); |
| 237 | + |
| 238 | + it('should return undefined if no auth_token exists', () => { |
| 239 | + mockConfig.get.mockReturnValue(undefined); |
| 240 | + |
| 241 | + initProfileModule(); |
| 242 | + const profileModule = getProfileModule(); |
| 243 | + const defaultProfile = profileModule.getDefaultProfile(); |
| 244 | + |
| 245 | + expect(defaultProfile).toBeUndefined(); |
| 246 | + }); |
| 247 | +}); |
| 248 | + |
| 249 | +describe('ProfileModule.migrateLegacyConfig', () => { |
| 250 | + it('should migrate legacy config to profile format', () => { |
| 251 | + mockConfig.get.mockImplementation((key) => { |
| 252 | + if (key === 'auth_token') return 'legacy-token'; |
| 253 | + if (key === 'username') return 'legacyuser'; |
| 254 | + if (key === 'profiles') return []; |
| 255 | + return undefined; |
| 256 | + }); |
| 257 | + |
| 258 | + initProfileModule(); |
| 259 | + const profileModule = getProfileModule(); |
| 260 | + profileModule.migrateLegacyConfig(); |
| 261 | + |
| 262 | + expect(mockConfig.set).toHaveBeenCalledWith('profiles', [ |
| 263 | + { |
| 264 | + host: 'https://puter.com', |
| 265 | + username: 'legacyuser', |
| 266 | + cwd: '/legacyuser', |
| 267 | + token: 'legacy-token', |
| 268 | + uuid: '00000000-0000-0000-0000-000000000000', |
| 269 | + }, |
| 270 | + ]); |
| 271 | + expect(mockConfig.delete).toHaveBeenCalledWith('auth_token'); |
| 272 | + expect(mockConfig.delete).toHaveBeenCalledWith('username'); |
| 273 | + }); |
| 274 | +}); |
0 commit comments