|
| 1 | +/** |
| 2 | + * Login Command Tests |
| 3 | + * |
| 4 | + * Unit tests for the --token authentication path in src/commands/auth/login.ts. |
| 5 | + * Uses spyOn to mock api-client, db/auth, db/user, and interactive-login |
| 6 | + * to cover all branches without real HTTP calls or database access. |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + afterEach, |
| 11 | + beforeEach, |
| 12 | + describe, |
| 13 | + expect, |
| 14 | + mock, |
| 15 | + spyOn, |
| 16 | + test, |
| 17 | +} from "bun:test"; |
| 18 | +import { loginCommand } from "../../../src/commands/auth/login.js"; |
| 19 | +// biome-ignore lint/performance/noNamespaceImport: needed for spyOn mocking |
| 20 | +import * as apiClient from "../../../src/lib/api-client.js"; |
| 21 | +// biome-ignore lint/performance/noNamespaceImport: needed for spyOn mocking |
| 22 | +import * as dbAuth from "../../../src/lib/db/auth.js"; |
| 23 | +// biome-ignore lint/performance/noNamespaceImport: needed for spyOn mocking |
| 24 | +import * as dbUser from "../../../src/lib/db/user.js"; |
| 25 | +import { AuthError } from "../../../src/lib/errors.js"; |
| 26 | +// biome-ignore lint/performance/noNamespaceImport: needed for spyOn mocking |
| 27 | +import * as interactiveLogin from "../../../src/lib/interactive-login.js"; |
| 28 | + |
| 29 | +type LoginFlags = { readonly token?: string; readonly timeout: number }; |
| 30 | + |
| 31 | +/** Command function type extracted from loader result */ |
| 32 | +type LoginFunc = (this: unknown, flags: LoginFlags) => Promise<void>; |
| 33 | + |
| 34 | +const SAMPLE_USER = { |
| 35 | + id: "42", |
| 36 | + name: "Jane Doe", |
| 37 | + username: "janedoe", |
| 38 | + email: "jane@example.com", |
| 39 | +}; |
| 40 | + |
| 41 | +function createContext() { |
| 42 | + const stdoutLines: string[] = []; |
| 43 | + const stderrLines: string[] = []; |
| 44 | + const context = { |
| 45 | + stdout: { |
| 46 | + write: mock((s: string) => { |
| 47 | + stdoutLines.push(s); |
| 48 | + }), |
| 49 | + }, |
| 50 | + stderr: { |
| 51 | + write: mock((s: string) => { |
| 52 | + stderrLines.push(s); |
| 53 | + }), |
| 54 | + }, |
| 55 | + cwd: "/tmp", |
| 56 | + setContext: mock((_k: string, _v: unknown) => { |
| 57 | + /* no-op */ |
| 58 | + }), |
| 59 | + }; |
| 60 | + const getStdout = () => stdoutLines.join(""); |
| 61 | + return { context, getStdout }; |
| 62 | +} |
| 63 | + |
| 64 | +describe("loginCommand.func --token path", () => { |
| 65 | + let isAuthenticatedSpy: ReturnType<typeof spyOn>; |
| 66 | + let setAuthTokenSpy: ReturnType<typeof spyOn>; |
| 67 | + let getUserRegionsSpy: ReturnType<typeof spyOn>; |
| 68 | + let clearAuthSpy: ReturnType<typeof spyOn>; |
| 69 | + let getCurrentUserSpy: ReturnType<typeof spyOn>; |
| 70 | + let setUserInfoSpy: ReturnType<typeof spyOn>; |
| 71 | + let runInteractiveLoginSpy: ReturnType<typeof spyOn>; |
| 72 | + let func: LoginFunc; |
| 73 | + |
| 74 | + beforeEach(async () => { |
| 75 | + isAuthenticatedSpy = spyOn(dbAuth, "isAuthenticated"); |
| 76 | + setAuthTokenSpy = spyOn(dbAuth, "setAuthToken"); |
| 77 | + getUserRegionsSpy = spyOn(apiClient, "getUserRegions"); |
| 78 | + clearAuthSpy = spyOn(dbAuth, "clearAuth"); |
| 79 | + getCurrentUserSpy = spyOn(apiClient, "getCurrentUser"); |
| 80 | + setUserInfoSpy = spyOn(dbUser, "setUserInfo"); |
| 81 | + runInteractiveLoginSpy = spyOn(interactiveLogin, "runInteractiveLogin"); |
| 82 | + func = (await loginCommand.loader()) as unknown as LoginFunc; |
| 83 | + }); |
| 84 | + |
| 85 | + afterEach(() => { |
| 86 | + isAuthenticatedSpy.mockRestore(); |
| 87 | + setAuthTokenSpy.mockRestore(); |
| 88 | + getUserRegionsSpy.mockRestore(); |
| 89 | + clearAuthSpy.mockRestore(); |
| 90 | + getCurrentUserSpy.mockRestore(); |
| 91 | + setUserInfoSpy.mockRestore(); |
| 92 | + runInteractiveLoginSpy.mockRestore(); |
| 93 | + }); |
| 94 | + |
| 95 | + test("already authenticated: prints message and returns early", async () => { |
| 96 | + isAuthenticatedSpy.mockResolvedValue(true); |
| 97 | + |
| 98 | + const { context, getStdout } = createContext(); |
| 99 | + await func.call(context, { timeout: 900 }); |
| 100 | + |
| 101 | + expect(getStdout()).toContain("already authenticated"); |
| 102 | + expect(setAuthTokenSpy).not.toHaveBeenCalled(); |
| 103 | + expect(getCurrentUserSpy).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + test("--token: stores token, fetches user, writes success", async () => { |
| 107 | + isAuthenticatedSpy.mockResolvedValue(false); |
| 108 | + setAuthTokenSpy.mockResolvedValue(undefined); |
| 109 | + getUserRegionsSpy.mockResolvedValue([]); |
| 110 | + getCurrentUserSpy.mockResolvedValue(SAMPLE_USER); |
| 111 | + setUserInfoSpy.mockReturnValue(undefined); |
| 112 | + |
| 113 | + const { context, getStdout } = createContext(); |
| 114 | + await func.call(context, { token: "my-token", timeout: 900 }); |
| 115 | + |
| 116 | + expect(setAuthTokenSpy).toHaveBeenCalledWith("my-token"); |
| 117 | + expect(getCurrentUserSpy).toHaveBeenCalled(); |
| 118 | + expect(setUserInfoSpy).toHaveBeenCalledWith({ |
| 119 | + userId: "42", |
| 120 | + name: "Jane Doe", |
| 121 | + username: "janedoe", |
| 122 | + email: "jane@example.com", |
| 123 | + }); |
| 124 | + const out = getStdout(); |
| 125 | + expect(out).toContain("Authenticated"); |
| 126 | + expect(out).toContain("Jane Doe"); |
| 127 | + }); |
| 128 | + |
| 129 | + test("--token: invalid token clears auth and throws AuthError", async () => { |
| 130 | + isAuthenticatedSpy.mockResolvedValue(false); |
| 131 | + setAuthTokenSpy.mockResolvedValue(undefined); |
| 132 | + getUserRegionsSpy.mockRejectedValue(new Error("401 Unauthorized")); |
| 133 | + clearAuthSpy.mockResolvedValue(undefined); |
| 134 | + |
| 135 | + const { context } = createContext(); |
| 136 | + |
| 137 | + await expect( |
| 138 | + func.call(context, { token: "bad-token", timeout: 900 }) |
| 139 | + ).rejects.toBeInstanceOf(AuthError); |
| 140 | + |
| 141 | + expect(clearAuthSpy).toHaveBeenCalled(); |
| 142 | + expect(getCurrentUserSpy).not.toHaveBeenCalled(); |
| 143 | + }); |
| 144 | + |
| 145 | + test("--token: shows 'Logged in as' when user info fetch succeeds", async () => { |
| 146 | + isAuthenticatedSpy.mockResolvedValue(false); |
| 147 | + setAuthTokenSpy.mockResolvedValue(undefined); |
| 148 | + getUserRegionsSpy.mockResolvedValue([]); |
| 149 | + getCurrentUserSpy.mockResolvedValue({ id: "5", email: "only@email.com" }); |
| 150 | + setUserInfoSpy.mockReturnValue(undefined); |
| 151 | + |
| 152 | + const { context, getStdout } = createContext(); |
| 153 | + await func.call(context, { token: "valid-token", timeout: 900 }); |
| 154 | + |
| 155 | + expect(getStdout()).toContain("Logged in as"); |
| 156 | + expect(getStdout()).toContain("only@email.com"); |
| 157 | + }); |
| 158 | + |
| 159 | + test("--token: login succeeds even when getCurrentUser() fails transiently", async () => { |
| 160 | + isAuthenticatedSpy.mockResolvedValue(false); |
| 161 | + setAuthTokenSpy.mockResolvedValue(undefined); |
| 162 | + getUserRegionsSpy.mockResolvedValue([]); |
| 163 | + getCurrentUserSpy.mockRejectedValue(new Error("Network error")); |
| 164 | + |
| 165 | + const { context, getStdout } = createContext(); |
| 166 | + |
| 167 | + // Must not throw — login should succeed with the stored token |
| 168 | + await func.call(context, { token: "valid-token", timeout: 900 }); |
| 169 | + |
| 170 | + const out = getStdout(); |
| 171 | + expect(out).toContain("Authenticated"); |
| 172 | + // 'Logged in as' is omitted when user info is unavailable |
| 173 | + expect(out).not.toContain("Logged in as"); |
| 174 | + // Token was stored and not cleared |
| 175 | + expect(clearAuthSpy).not.toHaveBeenCalled(); |
| 176 | + expect(setUserInfoSpy).not.toHaveBeenCalled(); |
| 177 | + }); |
| 178 | + |
| 179 | + test("no token: falls through to interactive login", async () => { |
| 180 | + isAuthenticatedSpy.mockResolvedValue(false); |
| 181 | + runInteractiveLoginSpy.mockResolvedValue(true); |
| 182 | + |
| 183 | + const { context } = createContext(); |
| 184 | + await func.call(context, { timeout: 900 }); |
| 185 | + |
| 186 | + expect(runInteractiveLoginSpy).toHaveBeenCalled(); |
| 187 | + expect(setAuthTokenSpy).not.toHaveBeenCalled(); |
| 188 | + }); |
| 189 | +}); |
0 commit comments