| 1 | import * as fs from "fs"; |
| 2 | |
| 3 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | |
| 5 | import { |
| 6 | autoSelectOrganizationAndConfig, |
| 7 | createUpdatedAuthConfig, |
| 8 | } from "./orgSelection.js"; |
| 9 | import type { AuthenticatedConfig } from "./workos.js"; |
| 10 | |
| 11 | // Mock dependencies |
| 12 | vi.mock("fs"); |
| 13 | vi.mock("../config.js"); |
| 14 | vi.mock("../env.js"); |
| 15 | vi.mock("./workos.js"); |
| 16 | |
| 17 | const mockApiClient = { |
| 18 | listOrganizations: vi.fn(), |
| 19 | listAssistants: vi.fn(), |
| 20 | } as any; // Mock API client for testing |
| 21 | |
| 22 | describe("orgSelection", () => { |
| 23 | const mockConfig: AuthenticatedConfig = { |
| 24 | userId: "user123", |
| 25 | userEmail: "user@example.com", |
| 26 | accessToken: "token123", |
| 27 | refreshToken: "refresh123", |
| 28 | expiresAt: Date.now() + 3600000, |
| 29 | organizationId: undefined, |
| 30 | configUri: undefined, |
| 31 | modelName: undefined, |
| 32 | }; |
| 33 | |
| 34 | beforeEach(async () => { |
| 35 | vi.clearAllMocks(); |
| 36 | |
| 37 | // Mock modules using vi.mocked |
| 38 | const configMod = await import("../config.js"); |
| 39 | const envMod = await import("../env.js"); |
| 40 | const workosMod = await import("./workos.js"); |
| 41 | |
| 42 | vi.mocked(configMod.getApiClient).mockReturnValue(mockApiClient); |
| 43 | vi.mocked(envMod.env).continueHome = "/mock/home"; |
| 44 | vi.mocked(workosMod.saveAuthConfig).mockImplementation(() => {}); |
| 45 | |
| 46 | // Default: no local config |
| 47 | vi.mocked(fs.existsSync).mockReturnValue(false); |
| 48 | }); |
| 49 | |
| 50 | describe("createUpdatedAuthConfig", () => { |
| 51 | it("should update organization ID", () => { |
| 52 | const result = createUpdatedAuthConfig(mockConfig, "org123"); |
| 53 | expect(result.organizationId).toBe("org123"); |
| 54 | }); |
| 55 | |
| 56 | it("should update organization ID and config URI", () => { |
| 57 | const result = createUpdatedAuthConfig( |
| 58 | mockConfig, |
| 59 | "org123", |
| 60 | "slug://owner/assistant", |
| 61 | ); |
| 62 | expect(result.organizationId).toBe("org123"); |
| 63 | expect(result!.configUri).toBe("slug://owner/assistant"); |
| 64 | }); |
| 65 | |
| 66 | it("should set personal organization", () => { |
| 67 | const result = createUpdatedAuthConfig(mockConfig, null); |
| 68 | expect(result.organizationId).toBe(null); |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | describe("autoSelectOrganizationAndConfig scenarios", () => { |
| 73 | it("Priority 1: Org with assistants → select first assistant", async () => { |
| 74 | mockApiClient.listOrganizations.mockResolvedValue({ |
| 75 | organizations: [ |
| 76 | { id: "org1", name: "Org 1" }, |
| 77 | { id: "org2", name: "Org 2" }, |
| 78 | ], |
| 79 | }); |
| 80 | |
| 81 | // Mock API calls in order: org1 assistants, org2 assistants |
| 82 | mockApiClient.listAssistants.mockImplementation( |
| 83 | ({ organizationId }: { organizationId?: string }) => { |
| 84 | if (organizationId === "org1") return Promise.resolve([]); |
| 85 | if (organizationId === "org2") |
| 86 | return Promise.resolve([ |
| 87 | { ownerSlug: "owner", packageSlug: "assistant1" }, |
| 88 | ]); |
| 89 | return Promise.resolve([]); |
| 90 | }, |
| 91 | ); |
| 92 | |
| 93 | const result = await autoSelectOrganizationAndConfig(mockConfig); |
| 94 | |
| 95 | expect(result!.organizationId).toBe("org2"); |
| 96 | expect(result!.configUri).toBe("slug://owner/assistant1"); |
| 97 | }); |
| 98 | |
| 99 | it("Priority 2: No org assistants → personal assistants exist", async () => { |
| 100 | mockApiClient.listOrganizations.mockResolvedValue({ |
| 101 | organizations: [{ id: "org1", name: "Org 1" }], |
| 102 | }); |
| 103 | |
| 104 | mockApiClient.listAssistants.mockImplementation( |
| 105 | ({ organizationId }: { organizationId?: string }) => { |
| 106 | if (organizationId === "org1") return Promise.resolve([]); |
| 107 | if (organizationId === undefined) |
| 108 | return Promise.resolve([ |
| 109 | { ownerSlug: "personal", packageSlug: "my-assistant" }, |
| 110 | ]); |
| 111 | return Promise.resolve([]); |
| 112 | }, |
| 113 | ); |
| 114 | |
| 115 | const result = await autoSelectOrganizationAndConfig(mockConfig); |
| 116 | |
| 117 | expect(result!.organizationId).toBe(null); // personal org |
| 118 | expect(result!.configUri).toBe("slug://personal/my-assistant"); |
| 119 | expect(mockApiClient.listAssistants).toHaveBeenCalledWith({ |
| 120 | organizationId: undefined, |
| 121 | }); |
| 122 | }); |
| 123 | |
| 124 | it("Priority 3: No assistants anywhere → local config.yaml exists", async () => { |
| 125 | mockApiClient.listOrganizations.mockResolvedValue({ |
| 126 | organizations: [{ id: "org1", name: "Org 1" }], |
| 127 | }); |
| 128 | |
| 129 | mockApiClient.listAssistants.mockResolvedValue([]); // No assistants anywhere |
| 130 | vi.mocked(fs.existsSync).mockReturnValue(true); // config.yaml exists |
| 131 | |
| 132 | const result = await autoSelectOrganizationAndConfig(mockConfig); |
| 133 | |
| 134 | expect(result!.organizationId).toBe(null); |
| 135 | expect(result!.configUri).toContain("config.yaml"); |
| 136 | }); |
| 137 | |
| 138 | it("Priority 4: No assistants, no config.yaml → fallback", async () => { |
| 139 | mockApiClient.listOrganizations.mockResolvedValue({ |
| 140 | organizations: [{ id: "org1", name: "Org 1" }], |
| 141 | }); |
| 142 | |
| 143 | mockApiClient.listAssistants.mockResolvedValue([]); // No assistants anywhere |
| 144 | vi.mocked(fs.existsSync).mockReturnValue(false); // No config.yaml |
| 145 | |
| 146 | const result = await autoSelectOrganizationAndConfig(mockConfig); |
| 147 | |
| 148 | expect(result!.organizationId).toBe(null); |
| 149 | expect(result!.configUri).toBeUndefined(); |
| 150 | }); |
| 151 | |
| 152 | it("Multiple orgs with assistants → pick first one", async () => { |
| 153 | mockApiClient.listOrganizations.mockResolvedValue({ |
| 154 | organizations: [ |
| 155 | { id: "org1", name: "Org 1" }, |
| 156 | { id: "org2", name: "Org 2" }, |
| 157 | ], |
| 158 | }); |
| 159 | |
| 160 | mockApiClient.listAssistants.mockImplementation( |
| 161 | ({ organizationId }: { organizationId?: string }) => { |
| 162 | if (organizationId === "org1") |
| 163 | return Promise.resolve([ |
| 164 | { ownerSlug: "owner1", packageSlug: "assistant1" }, |
| 165 | ]); |
| 166 | if (organizationId === "org2") |
| 167 | return Promise.resolve([ |
| 168 | { ownerSlug: "owner2", packageSlug: "assistant2" }, |
| 169 | ]); |
| 170 | return Promise.resolve([]); |
| 171 | }, |
| 172 | ); |
| 173 | |
| 174 | const result = await autoSelectOrganizationAndConfig(mockConfig); |
| 175 | |
| 176 | expect(result!.organizationId).toBe("org1"); // First org wins |
| 177 | expect(result!.configUri).toBe("slug://owner1/assistant1"); |
| 178 | }); |
| 179 | |
| 180 | it("Personal assistants API fails → fallback to config.yaml", async () => { |
| 181 | mockApiClient.listOrganizations.mockResolvedValue({ |
| 182 | organizations: [{ id: "org1", name: "Org 1" }], |
| 183 | }); |
| 184 | |
| 185 | mockApiClient.listAssistants |
| 186 | .mockResolvedValueOnce([]) // org1: no assistants |
| 187 | .mockRejectedValueOnce(new Error("Personal API failed")); // personal: API error |
| 188 | |
| 189 | vi.mocked(fs.existsSync).mockReturnValue(true); // config.yaml exists |
| 190 | |
| 191 | const result = await autoSelectOrganizationAndConfig(mockConfig); |
| 192 | |
| 193 | expect(result!.organizationId).toBe(null); |
| 194 | expect(result!.configUri).toContain("config.yaml"); |
| 195 | }); |
| 196 | |
| 197 | it("Empty organizations list → check personal assistants", async () => { |
| 198 | mockApiClient.listOrganizations.mockResolvedValue({ |
| 199 | organizations: [], // No orgs |
| 200 | }); |
| 201 | |
| 202 | mockApiClient.listAssistants.mockResolvedValue([ |
| 203 | { ownerSlug: "personal", packageSlug: "my-assistant" }, |
| 204 | ]); |
| 205 | |
| 206 | const result = await autoSelectOrganizationAndConfig(mockConfig); |
| 207 | |
| 208 | expect(result!.organizationId).toBe(null); |
| 209 | expect(result!.configUri).toBe("slug://personal/my-assistant"); |
| 210 | }); |
| 211 | |
| 212 | it("API error → fallback with error message", async () => { |
| 213 | mockApiClient.listOrganizations.mockRejectedValue( |
| 214 | new Error("Network error"), |
| 215 | ); |
| 216 | |
| 217 | const consoleSpy = vi |
| 218 | .spyOn(console, "error") |
| 219 | .mockImplementation(() => {}); |
| 220 | |
| 221 | const result = await autoSelectOrganizationAndConfig(mockConfig); |
| 222 | |
| 223 | expect(result!.organizationId).toBe(null); |
| 224 | expect(result!.configUri).toBeUndefined(); |
| 225 | expect(consoleSpy).toHaveBeenCalledWith( |
| 226 | expect.stringContaining("Error fetching organizations:"), |
| 227 | "Network error", |
| 228 | ); |
| 229 | |
| 230 | consoleSpy.mockRestore(); |
| 231 | }); |
| 232 | }); |
| 233 | }); |