Elffuss BraidHub

kikocis/continue public

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