Elffuss BraidHub

kikocis/continue public

438b2c9c3ac0 · 5.8 KiBRawPermalink
1import chalk from "chalk";
2import { describe, expect, test, beforeEach, vi } from "vitest";
3
4import { getApiClient } from "../config.js";
5
6import { ensureOrganization } from "./workos.js";
7import type { AuthenticatedConfig, EnvironmentAuthConfig } from "./workos.js";
8
9// Mock dependencies
10vi.mock("../config.js", () => ({
11 getApiClient: vi.fn(() => ({
12 listOrganizations: vi.fn(),
13 })),
14}));
15
16vi.mock("fs", () => ({
17 default: {
18 existsSync: vi.fn(() => false),
19 readFileSync: vi.fn(),
20 writeFileSync: vi.fn(),
21 mkdirSync: vi.fn(),
22 unlinkSync: vi.fn(),
23 },
24}));
25
26describe("ensureOrganization with CLI organization slug", () => {
27 const mockAuthConfig: AuthenticatedConfig = {
28 userId: "user-123",
29 userEmail: "test@example.com",
30 accessToken: "test-token",
31 refreshToken: "refresh-token",
32 expiresAt: Date.now() + 3600000,
33 organizationId: null,
34 };
35
36 const mockEnvAuthConfig: EnvironmentAuthConfig = {
37 accessToken: "env-token",
38 organizationId: null,
39 };
40
41 const mockOrganizations = [
42 { id: "org-1", name: "Organization One", slug: "org-one" },
43 { id: "org-2", name: "Organization Two", slug: "org-two" },
44 { id: "org-3", name: "Test Org", slug: "test-org" },
45 ];
46
47 beforeEach(() => {
48 vi.clearAllMocks();
49 // Suppress console output during tests
50 vi.spyOn(console, "error").mockImplementation(() => {});
51 vi.spyOn(console, "info").mockImplementation(() => {});
52 });
53
54 describe("Organization resolution by slug", () => {
55 test("should resolve organization by exact slug match", async () => {
56 const apiClient = {
57 listOrganizations: vi
58 .fn()
59 .mockResolvedValue({ organizations: mockOrganizations }),
60 };
61 vi.mocked(getApiClient).mockReturnValue(apiClient as any);
62
63 const result = await ensureOrganization(mockAuthConfig, true, "org-one");
64
65 expect(result).toMatchObject({
66 organizationId: "org-1",
67 });
68 expect(apiClient.listOrganizations).toHaveBeenCalled();
69 });
70
71 test("should show available organizations when slug not found", async () => {
72 const apiClient = {
73 listOrganizations: vi
74 .fn()
75 .mockResolvedValue({ organizations: mockOrganizations }),
76 };
77 vi.mocked(getApiClient).mockReturnValue(apiClient as any);
78
79 const consoleInfoSpy = vi.spyOn(console, "info");
80
81 await expect(
82 ensureOrganization(mockAuthConfig, true, "invalid-org"),
83 ).rejects.toThrow('Organization "invalid-org" not found');
84
85 expect(consoleInfoSpy).toHaveBeenCalledWith(
86 chalk.yellow("Available organizations:"),
87 expect.stringContaining("personal"),
88 );
89 });
90 });
91
92 describe("Mode restriction", () => {
93 test("should reject --org flag in interactive mode", async () => {
94 const consoleErrorSpy = vi.spyOn(console, "error");
95 const processExitSpy = vi
96 .spyOn(process, "exit")
97 .mockImplementation(() => {
98 throw new Error("Process exit");
99 });
100
101 await expect(
102 ensureOrganization(mockAuthConfig, false, "org-one"),
103 ).rejects.toThrow("Process exit");
104
105 expect(consoleErrorSpy).toHaveBeenCalledWith(
106 chalk.red(
107 "The --org flag is only supported in headless mode (with -p/--print flag)",
108 ),
109 );
110 expect(processExitSpy).toHaveBeenCalledWith(1);
111
112 processExitSpy.mockRestore();
113 });
114 });
115
116 describe("Auth type handling", () => {
117 test("should work with authenticated config and return updated organizationId", async () => {
118 const apiClient = {
119 listOrganizations: vi
120 .fn()
121 .mockResolvedValue({ organizations: mockOrganizations }),
122 };
123 vi.mocked(getApiClient).mockReturnValue(apiClient as any);
124
125 const result = await ensureOrganization(mockAuthConfig, true, "test-org");
126
127 expect(result).toMatchObject({
128 userId: "user-123",
129 organizationId: "org-3",
130 });
131 });
132
133 test("should work with environment auth config and return updated organizationId", async () => {
134 const apiClient = {
135 listOrganizations: vi
136 .fn()
137 .mockResolvedValue({ organizations: mockOrganizations }),
138 };
139 vi.mocked(getApiClient).mockReturnValue(apiClient as any);
140
141 const result = await ensureOrganization(
142 mockEnvAuthConfig,
143 true,
144 "org-two",
145 );
146
147 expect(result).toMatchObject({
148 accessToken: "env-token",
149 organizationId: "org-2",
150 });
151 });
152
153 test("should override existing organization with CLI slug", async () => {
154 const authWithOrg = {
155 ...mockAuthConfig,
156 organizationId: "existing-org",
157 };
158
159 const apiClient = {
160 listOrganizations: vi
161 .fn()
162 .mockResolvedValue({ organizations: mockOrganizations }),
163 };
164 vi.mocked(getApiClient).mockReturnValue(apiClient as any);
165
166 const result = await ensureOrganization(authWithOrg, true, "org-one");
167
168 expect(result).toMatchObject({
169 organizationId: "org-1",
170 });
171 });
172
173 test("should keep existing organization when no slug provided", async () => {
174 const authWithOrg = {
175 ...mockAuthConfig,
176 organizationId: "existing-org",
177 };
178
179 const result = await ensureOrganization(authWithOrg, true);
180
181 expect(result).toMatchObject({
182 organizationId: "existing-org",
183 });
184 });
185 });
186
187 describe("Error handling", () => {
188 test("should handle API errors during organization fetch", async () => {
189 const apiClient = {
190 listOrganizations: vi.fn().mockRejectedValue(new Error("API Error")),
191 };
192 vi.mocked(getApiClient).mockReturnValue(apiClient as any);
193
194 await expect(
195 ensureOrganization(mockAuthConfig, true, "org-one"),
196 ).rejects.toThrow("API Error");
197 });
198 });
199});