Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { afterEach, describe, expect, it } from '@effect/vitest'; |
| 2 | import { vi } from 'vitest'; |
| 3 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 4 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 5 | import { APIConnectionError } from '@composio/client/effect'; |
| 6 | import { ConfigProvider, Effect, Fiber, Layer, Option } from 'effect'; |
| 7 | import { TestClock } from 'effect/testing'; |
| 8 | import * as tempy from 'tempy'; |
| 9 | import { |
| 10 | ProjectResolutionError, |
| 11 | resolveCommandProject, |
| 12 | resolveDeveloperProjectOrDefault, |
| 13 | } from 'src/services/command-project'; |
| 14 | import { ComposioClientSingleton } from 'src/services/composio-clients'; |
| 15 | import { defaultNodeOs, NodeOs } from 'src/services/node-os'; |
| 16 | import { ProjectContext } from 'src/services/project-context'; |
| 17 | import { ComposioUserContext } from 'src/services/user-context'; |
| 18 | import { makeOrgProject, makeSessionInfo } from 'test/__utils__/models/account'; |
| 19 | |
| 20 | const BASE_URL = 'https://backend.composio.dev'; |
| 21 | |
| 22 | const userContext = ( |
| 23 | orgId: Option.Option<string>, |
| 24 | apiKey: Option.Option<string> = Option.some('uak_test') |
| 25 | ) => |
| 26 | Layer.succeed( |
| 27 | ComposioUserContext, |
| 28 | ComposioUserContext.of({ |
| 29 | data: { |
| 30 | apiKey, |
| 31 | baseURL: BASE_URL, |
| 32 | webURL: 'https://app.composio.dev', |
| 33 | orgId, |
| 34 | projectId: Option.none(), |
| 35 | testUserId: Option.none(), |
| 36 | }, |
| 37 | backendMismatch: Option.none(), |
| 38 | isLoggedIn: () => true, |
| 39 | logout: Effect.void, |
| 40 | login: () => Effect.void, |
| 41 | update: () => Effect.void, |
| 42 | }) |
| 43 | ); |
| 44 | |
| 45 | const noLocalProject = Layer.succeed( |
| 46 | ProjectContext, |
| 47 | ProjectContext.of({ resolve: Effect.succeed(Option.none()) }) |
| 48 | ); |
| 49 | |
| 50 | const singleton = Layer.provide( |
| 51 | ComposioClientSingleton.Default, |
| 52 | Layer.mergeAll( |
| 53 | BunFileSystem.layer, |
| 54 | BunPath.layer, |
| 55 | Layer.succeed(NodeOs, defaultNodeOs({ homedir: tempy.temporaryDirectory() })), |
| 56 | Layer.succeed( |
| 57 | ConfigProvider.ConfigProvider, |
| 58 | ConfigProvider.fromEnv({ |
| 59 | env: { COMPOSIO_USER_API_KEY: 'uak_test', COMPOSIO_BASE_URL: BASE_URL }, |
| 60 | }) |
| 61 | ) |
| 62 | ) |
| 63 | ); |
| 64 | |
| 65 | const testLayer = Layer.mergeAll(singleton, userContext(Option.some('org_1')), noLocalProject); |
| 66 | |
| 67 | const jsonResponse = (body: unknown, status = 200) => |
| 68 | new Response(JSON.stringify(body), { |
| 69 | status, |
| 70 | headers: { 'Content-Type': 'application/json', 'x-should-retry': 'false' }, |
| 71 | }); |
| 72 | |
| 73 | const projectsPage = (names: ReadonlyArray<string>) => |
| 74 | jsonResponse({ |
| 75 | data: names.map((name, index) => makeOrgProject({ id: `p${index + 1}`, name })), |
| 76 | total_pages: 1, |
| 77 | current_page: 1, |
| 78 | total_items: names.length, |
| 79 | }); |
| 80 | |
| 81 | const localProject = (orgId: string) => |
| 82 | Layer.succeed( |
| 83 | ProjectContext, |
| 84 | ProjectContext.of({ |
| 85 | resolve: Effect.succeed( |
| 86 | Option.some({ |
| 87 | orgId, |
| 88 | projectId: 'pr_local', |
| 89 | projectName: Option.some('local-app'), |
| 90 | orgName: Option.none(), |
| 91 | email: Option.none(), |
| 92 | testUserId: Option.none(), |
| 93 | }) |
| 94 | ), |
| 95 | }) |
| 96 | ); |
| 97 | |
| 98 | const orgList = (ids: ReadonlyArray<string>, nextCursor: string | null = null) => |
| 99 | jsonResponse({ |
| 100 | organizations: ids.map(id => ({ |
| 101 | id, |
| 102 | name: id, |
| 103 | created_at: '2024-01-01T00:00:00.000Z', |
| 104 | updated_at: '2024-01-01T00:00:00.000Z', |
| 105 | })), |
| 106 | next_cursor: nextCursor, |
| 107 | total_pages: 1, |
| 108 | current_page: 1, |
| 109 | total_items: ids.length, |
| 110 | }); |
| 111 | |
| 112 | describe('resolveCommandProject', () => { |
| 113 | afterEach(() => { |
| 114 | vi.restoreAllMocks(); |
| 115 | }); |
| 116 | |
| 117 | it.effect('fails with missing_org before any request when no org is configured', () => |
| 118 | Effect.gen(function* () { |
| 119 | const fetchSpy = vi.spyOn(globalThis, 'fetch'); |
| 120 | |
| 121 | const error = yield* resolveCommandProject({ mode: 'consumer' }).pipe(Effect.flip); |
| 122 | |
| 123 | expect(error).toBeInstanceOf(ProjectResolutionError); |
| 124 | expect(error).toMatchObject({ |
| 125 | reason: 'missing_org', |
| 126 | message: 'No current org configured. Run `composio login` or `composio orgs switch`.', |
| 127 | }); |
| 128 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 129 | }).pipe(Effect.provide(Layer.mergeAll(singleton, userContext(Option.none()), noLocalProject))) |
| 130 | ); |
| 131 | |
| 132 | it.effect('fails with missing_developer_project when nothing binds the directory', () => |
| 133 | Effect.gen(function* () { |
| 134 | const error = yield* resolveCommandProject({ mode: 'developer' }).pipe(Effect.flip); |
| 135 | |
| 136 | expect(error).toMatchObject({ |
| 137 | _tag: 'services/ProjectResolutionError', |
| 138 | reason: 'missing_developer_project', |
| 139 | message: 'No developer project configured for this directory. Run `composio dev init`.', |
| 140 | }); |
| 141 | }).pipe(Effect.provide(testLayer)) |
| 142 | ); |
| 143 | |
| 144 | it.effect('refuses a directory project outside the logged-in org without a request', () => |
| 145 | Effect.gen(function* () { |
| 146 | const fetchSpy = vi.spyOn(globalThis, 'fetch'); |
| 147 | |
| 148 | const error = yield* resolveCommandProject({ mode: 'developer' }).pipe(Effect.flip); |
| 149 | |
| 150 | expect(error).toMatchObject({ reason: 'org_mismatch' }); |
| 151 | expect(error.message).toContain('"org_other"'); |
| 152 | expect(error.message).toContain('composio orgs switch'); |
| 153 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 154 | }).pipe( |
| 155 | Effect.provide( |
| 156 | Layer.mergeAll(singleton, userContext(Option.some('org_1')), localProject('org_other')) |
| 157 | ) |
| 158 | ) |
| 159 | ); |
| 160 | |
| 161 | it.effect('resolves a directory project in the logged-in org without a request', () => |
| 162 | Effect.gen(function* () { |
| 163 | const fetchSpy = vi.spyOn(globalThis, 'fetch'); |
| 164 | const project = yield* resolveCommandProject({ mode: 'developer' }); |
| 165 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 166 | |
| 167 | expect(project).toMatchObject({ |
| 168 | orgId: 'org_1', |
| 169 | projectId: 'pr_local', |
| 170 | source: 'developer-local-config', |
| 171 | }); |
| 172 | }).pipe( |
| 173 | Effect.provide( |
| 174 | Layer.mergeAll(singleton, userContext(Option.some('org_1')), localProject('org_1')) |
| 175 | ) |
| 176 | ) |
| 177 | ); |
| 178 | |
| 179 | it.effect( |
| 180 | 'checks a directory project against every page of the key orgs when none is stored', |
| 181 | () => |
| 182 | Effect.gen(function* () { |
| 183 | const fetchSpy = vi |
| 184 | .spyOn(globalThis, 'fetch') |
| 185 | .mockResolvedValueOnce(orgList(['org_a'], 'cursor_2')) |
| 186 | .mockResolvedValueOnce(orgList(['org_2'])); |
| 187 | |
| 188 | const project = yield* resolveCommandProject({ mode: 'developer' }); |
| 189 | |
| 190 | expect(project).toMatchObject({ orgId: 'org_2', projectId: 'pr_local' }); |
| 191 | expect(fetchSpy).toHaveBeenCalledTimes(2); |
| 192 | }).pipe( |
| 193 | Effect.provide(Layer.mergeAll(singleton, userContext(Option.none()), localProject('org_2'))) |
| 194 | ) |
| 195 | ); |
| 196 | |
| 197 | it.effect('refuses a directory project from another org when no org is stored', () => |
| 198 | Effect.gen(function* () { |
| 199 | vi.spyOn(globalThis, 'fetch').mockResolvedValue(orgList(['org_mine'])); |
| 200 | |
| 201 | const error = yield* resolveCommandProject({ mode: 'developer' }).pipe(Effect.flip); |
| 202 | |
| 203 | expect(error).toMatchObject({ reason: 'org_mismatch' }); |
| 204 | expect(error.message).toContain('cannot access'); |
| 205 | }).pipe( |
| 206 | Effect.provide(Layer.mergeAll(singleton, userContext(Option.none()), localProject('org_2'))) |
| 207 | ) |
| 208 | ); |
| 209 | |
| 210 | it.effect('turns an unknown project name into project_not_found with the remedy', () => |
| 211 | Effect.gen(function* () { |
| 212 | vi.spyOn(globalThis, 'fetch').mockResolvedValue(projectsPage(['Other'])); |
| 213 | |
| 214 | const error = yield* resolveCommandProject({ |
| 215 | mode: 'developer', |
| 216 | projectName: 'Missing', |
| 217 | }).pipe(Effect.flip); |
| 218 | |
| 219 | expect(error).toMatchObject({ |
| 220 | _tag: 'services/ProjectResolutionError', |
| 221 | reason: 'project_not_found', |
| 222 | message: |
| 223 | 'Developer project "Missing" was not found in org "org_1". Run `composio dev init` or `composio dev projects list`.', |
| 224 | }); |
| 225 | }).pipe(Effect.provide(testLayer)) |
| 226 | ); |
| 227 | |
| 228 | it.effect('turns a name collision into ambiguous_project_name', () => |
| 229 | Effect.gen(function* () { |
| 230 | vi.spyOn(globalThis, 'fetch').mockResolvedValue(projectsPage(['Shared', 'shared'])); |
| 231 | |
| 232 | const error = yield* resolveCommandProject({ |
| 233 | mode: 'consumer', |
| 234 | projectName: 'SHARED', |
| 235 | }).pipe(Effect.flip); |
| 236 | |
| 237 | expect(error).toMatchObject({ |
| 238 | _tag: 'services/ProjectResolutionError', |
| 239 | reason: 'ambiguous_project_name', |
| 240 | message: |
| 241 | 'Developer project name "SHARED" is ambiguous in org "org_1". Use an exact unique project name.', |
| 242 | }); |
| 243 | }).pipe(Effect.provide(testLayer)) |
| 244 | ); |
| 245 | |
| 246 | it.effect('turns an API error into request_failed carrying the server message', () => |
| 247 | Effect.gen(function* () { |
| 248 | vi.spyOn(globalThis, 'fetch').mockResolvedValue( |
| 249 | jsonResponse( |
| 250 | { error: { slug: 'Forbidden', status: 403, code: 403, message: 'Org access revoked.' } }, |
| 251 | 403 |
| 252 | ) |
| 253 | ); |
| 254 | |
| 255 | const error = yield* resolveCommandProject({ |
| 256 | mode: 'developer', |
| 257 | projectName: 'Any', |
| 258 | }).pipe(Effect.flip); |
| 259 | |
| 260 | expect(error).toBeInstanceOf(ProjectResolutionError); |
| 261 | expect(error).toMatchObject({ reason: 'request_failed', message: 'Org access revoked.' }); |
| 262 | expect((error as ProjectResolutionError).cause).toMatchObject({ status: 403 }); |
| 263 | }).pipe(Effect.provide(testLayer)) |
| 264 | ); |
| 265 | |
| 266 | it.effect('passes a connection failure through with its own type', () => |
| 267 | Effect.gen(function* () { |
| 268 | vi.spyOn(globalThis, 'fetch').mockRejectedValue(new TypeError('fetch failed')); |
| 269 | |
| 270 | // The client retries a connection failure with a backoff on Effect's clock, so run the |
| 271 | // lookup on its own fiber and move the test clock past every retry before joining. The |
| 272 | // first backoff is armed only after client construction and a rejected fetch promise, so |
| 273 | // advance in bounded steps until the fiber has settled rather than assuming one yield |
| 274 | // reaches it. |
| 275 | const fiber = yield* resolveCommandProject({ mode: 'developer', projectName: 'Any' }).pipe( |
| 276 | Effect.flip, |
| 277 | Effect.forkChild |
| 278 | ); |
| 279 | yield* Effect.yieldNow.pipe( |
| 280 | Effect.andThen(TestClock.adjust('1 minute')), |
| 281 | Effect.repeat({ until: () => fiber.pollUnsafe() !== undefined, times: 100 }) |
| 282 | ); |
| 283 | expect(fiber.pollUnsafe()).toBeDefined(); |
| 284 | const error = yield* Fiber.join(fiber); |
| 285 | |
| 286 | expect(error).not.toBeInstanceOf(ProjectResolutionError); |
| 287 | expect(error).toBeInstanceOf(APIConnectionError); |
| 288 | }).pipe(Effect.provide(testLayer)) |
| 289 | ); |
| 290 | }); |
| 291 | |
| 292 | // The backend's default developer project, as session info reports it. |
| 293 | const defaultProjectInfo = (orgId: string) => |
| 294 | jsonResponse(makeSessionInfo({ orgId, projectNanoId: 'pr_newest', projectName: 'Newest App' })); |
| 295 | |
| 296 | const requestOf = (call: ReadonlyArray<unknown> | undefined) => { |
| 297 | const [input, init] = call ?? []; |
| 298 | return { |
| 299 | path: new URL(String(input)).pathname, |
| 300 | headers: new Headers((init as RequestInit | undefined)?.headers), |
| 301 | }; |
| 302 | }; |
| 303 | |
| 304 | const SESSION_INFO_PATH = '/api/v3.1/auth/session/info'; |
| 305 | |
| 306 | describe('resolveDeveloperProjectOrDefault', () => { |
| 307 | afterEach(() => { |
| 308 | vi.restoreAllMocks(); |
| 309 | }); |
| 310 | |
| 311 | it.effect('uses the directory project, with no notice and no request, when it is usable', () => |
| 312 | Effect.gen(function* () { |
| 313 | const fetchSpy = vi.spyOn(globalThis, 'fetch'); |
| 314 | |
| 315 | const { project, notice } = yield* resolveDeveloperProjectOrDefault({}); |
| 316 | |
| 317 | expect(project).toMatchObject({ projectId: 'pr_local', source: 'developer-local-config' }); |
| 318 | expect(notice).toEqual(Option.none()); |
| 319 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 320 | }).pipe( |
| 321 | Effect.provide( |
| 322 | Layer.mergeAll(singleton, userContext(Option.some('org_1')), localProject('org_1')) |
| 323 | ) |
| 324 | ) |
| 325 | ); |
| 326 | |
| 327 | it.effect( |
| 328 | 'falls back to the default developer project of the logged-in org when the directory has none', |
| 329 | () => |
| 330 | Effect.gen(function* () { |
| 331 | const fetchSpy = vi |
| 332 | .spyOn(globalThis, 'fetch') |
| 333 | .mockResolvedValue(defaultProjectInfo('org_1')); |
| 334 | |
| 335 | const { project, notice } = yield* resolveDeveloperProjectOrDefault({}); |
| 336 | |
| 337 | expect(project).toEqual({ |
| 338 | orgId: 'org_1', |
| 339 | projectId: 'pr_newest', |
| 340 | projectName: 'Newest App', |
| 341 | projectType: 'DEVELOPER', |
| 342 | source: 'developer-default', |
| 343 | }); |
| 344 | expect(notice).toEqual( |
| 345 | Option.some( |
| 346 | 'No developer project is set up for this directory; using "Newest App" (pr_newest), the newest developer project in the org you are logged in to, instead. Run `composio dev init` to choose one, or pass --project-name.' |
| 347 | ) |
| 348 | ); |
| 349 | // One session-info read, scoped to the stored org so `composio orgs switch` holds. |
| 350 | expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 351 | const request = requestOf(fetchSpy.mock.calls[0]); |
| 352 | expect(request.path).toBe(SESSION_INFO_PATH); |
| 353 | expect(request.headers.get('x-org-id')).toBe('org_1'); |
| 354 | expect(request.headers.has('x-project-id')).toBe(false); |
| 355 | }).pipe(Effect.provide(testLayer)) |
| 356 | ); |
| 357 | |
| 358 | it.effect('asks for the key’s default developer project when no org is stored', () => |
| 359 | Effect.gen(function* () { |
| 360 | const fetchSpy = vi |
| 361 | .spyOn(globalThis, 'fetch') |
| 362 | .mockResolvedValue(defaultProjectInfo('org_joined_last')); |
| 363 | |
| 364 | const { project, notice } = yield* resolveDeveloperProjectOrDefault({}); |
| 365 | |
| 366 | expect(project).toMatchObject({ |
| 367 | orgId: 'org_joined_last', |
| 368 | projectId: 'pr_newest', |
| 369 | source: 'developer-default', |
| 370 | }); |
| 371 | expect(Option.getOrThrow(notice)).toContain( |
| 372 | 'the newest developer project in the org you joined most recently' |
| 373 | ); |
| 374 | expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 375 | expect(requestOf(fetchSpy.mock.calls[0]).headers.has('x-org-id')).toBe(false); |
| 376 | }).pipe(Effect.provide(Layer.mergeAll(singleton, userContext(Option.none()), noLocalProject))) |
| 377 | ); |
| 378 | |
| 379 | it.effect( |
| 380 | 'ignores a directory project outside the logged-in org and names it in the notice', |
| 381 | () => |
| 382 | Effect.gen(function* () { |
| 383 | const fetchSpy = vi |
| 384 | .spyOn(globalThis, 'fetch') |
| 385 | .mockResolvedValue(defaultProjectInfo('org_1')); |
| 386 | |
| 387 | const { project, notice } = yield* resolveDeveloperProjectOrDefault({}); |
| 388 | |
| 389 | expect(project).toMatchObject({ |
| 390 | orgId: 'org_1', |
| 391 | projectId: 'pr_newest', |
| 392 | source: 'developer-default', |
| 393 | }); |
| 394 | expect(notice).toEqual( |
| 395 | Option.some( |
| 396 | 'This directory\'s developer project is in org "org_other", not the org you are logged in to ("org_1"); using "Newest App" (pr_newest), the newest developer project in the org you are logged in to, instead. Run `composio orgs switch` to use the directory\'s org, or `composio dev init` to set this directory up in yours.' |
| 397 | ) |
| 398 | ); |
| 399 | // No request ever names the directory's project. |
| 400 | expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 401 | expect(requestOf(fetchSpy.mock.calls[0]).headers.get('x-org-id')).toBe('org_1'); |
| 402 | }).pipe( |
| 403 | Effect.provide( |
| 404 | Layer.mergeAll(singleton, userContext(Option.some('org_1')), localProject('org_other')) |
| 405 | ) |
| 406 | ) |
| 407 | ); |
| 408 | |
| 409 | it.effect( |
| 410 | 'ignores a directory project in an org the key cannot access when no org is stored', |
| 411 | () => |
| 412 | Effect.gen(function* () { |
| 413 | const fetchSpy = vi |
| 414 | .spyOn(globalThis, 'fetch') |
| 415 | .mockResolvedValueOnce(orgList(['org_mine'])) |
| 416 | .mockResolvedValueOnce(defaultProjectInfo('org_mine')); |
| 417 | |
| 418 | const { project, notice } = yield* resolveDeveloperProjectOrDefault({}); |
| 419 | |
| 420 | expect(project).toMatchObject({ |
| 421 | orgId: 'org_mine', |
| 422 | projectId: 'pr_newest', |
| 423 | source: 'developer-default', |
| 424 | }); |
| 425 | expect(notice).toEqual( |
| 426 | Option.some( |
| 427 | 'This directory\'s developer project is in org "org_2", which your API key cannot access; using "Newest App" (pr_newest), the newest developer project in the org you joined most recently, instead. Run `composio dev init` to set this directory up in one of your orgs.' |
| 428 | ) |
| 429 | ); |
| 430 | expect(fetchSpy).toHaveBeenCalledTimes(2); |
| 431 | const sessionRequest = requestOf(fetchSpy.mock.calls[1]); |
| 432 | expect(sessionRequest.path).toBe(SESSION_INFO_PATH); |
| 433 | expect(sessionRequest.headers.has('x-org-id')).toBe(false); |
| 434 | }).pipe( |
| 435 | Effect.provide(Layer.mergeAll(singleton, userContext(Option.none()), localProject('org_2'))) |
| 436 | ) |
| 437 | ); |
| 438 | |
| 439 | it.effect.each([ |
| 440 | { |
| 441 | given: 'session info without a project', |
| 442 | response: () => jsonResponse({ ...makeSessionInfo(), project: null }), |
| 443 | }, |
| 444 | { |
| 445 | given: 'an API error', |
| 446 | response: () => |
| 447 | jsonResponse( |
| 448 | { error: { slug: 'NotFound', status: 404, code: 404, message: 'No project found.' } }, |
| 449 | 404 |
| 450 | ), |
| 451 | }, |
| 452 | ])( |
| 453 | '[Given] $given [Then] fails saying no developer project was found, naming `composio dev init`', |
| 454 | ({ response }) => |
| 455 | Effect.gen(function* () { |
| 456 | vi.spyOn(globalThis, 'fetch').mockResolvedValue(response()); |
| 457 | |
| 458 | const error = yield* resolveDeveloperProjectOrDefault({}).pipe(Effect.flip); |
| 459 | |
| 460 | expect(error).toBeInstanceOf(ProjectResolutionError); |
| 461 | expect(error).toMatchObject({ |
| 462 | reason: 'missing_developer_project', |
| 463 | message: |
| 464 | 'No developer project is set up for this directory, and no developer project was found in your org to use instead. Run `composio dev init` to set one up.', |
| 465 | }); |
| 466 | }).pipe(Effect.provide(testLayer)) |
| 467 | ); |
| 468 | |
| 469 | it.effect('names the ignored directory project when no default can be found either', () => |
| 470 | Effect.gen(function* () { |
| 471 | vi.spyOn(globalThis, 'fetch').mockResolvedValue( |
| 472 | jsonResponse({ ...makeSessionInfo(), project: null }) |
| 473 | ); |
| 474 | |
| 475 | const error = yield* resolveDeveloperProjectOrDefault({}).pipe(Effect.flip); |
| 476 | |
| 477 | expect(error).toMatchObject({ |
| 478 | reason: 'missing_developer_project', |
| 479 | message: |
| 480 | 'This directory\'s developer project is in org "org_other", not the org you are logged in to ("org_1"), and no developer project was found in your org to use instead. Run `composio dev init` to set one up.', |
| 481 | }); |
| 482 | }).pipe( |
| 483 | Effect.provide( |
| 484 | Layer.mergeAll(singleton, userContext(Option.some('org_1')), localProject('org_other')) |
| 485 | ) |
| 486 | ) |
| 487 | ); |
| 488 | |
| 489 | it.effect.each([ |
| 490 | { status: 401, message: 'Invalid API key' }, |
| 491 | { status: 403, message: 'Access denied' }, |
| 492 | { status: 500, message: 'Session service unavailable' }, |
| 493 | ])( |
| 494 | 'preserves a $status session lookup failure instead of recommending setup', |
| 495 | ({ status, message }) => |
| 496 | Effect.gen(function* () { |
| 497 | vi.spyOn(globalThis, 'fetch').mockResolvedValue( |
| 498 | jsonResponse({ error: { status, code: status, message } }, status) |
| 499 | ); |
| 500 | |
| 501 | const error = yield* resolveDeveloperProjectOrDefault({}).pipe(Effect.flip); |
| 502 | |
| 503 | expect(error).toMatchObject({ reason: 'request_failed', message, cause: { status } }); |
| 504 | }).pipe(Effect.provide(testLayer)) |
| 505 | ); |
| 506 | |
| 507 | it.effect('resolves --project-name as before, never falling back', () => |
| 508 | Effect.gen(function* () { |
| 509 | const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(projectsPage(['Other'])); |
| 510 | |
| 511 | const error = yield* resolveDeveloperProjectOrDefault({ projectName: 'Missing' }).pipe( |
| 512 | Effect.flip |
| 513 | ); |
| 514 | |
| 515 | expect(error).toMatchObject({ reason: 'project_not_found' }); |
| 516 | expect(fetchSpy).toHaveBeenCalledTimes(1); |
| 517 | expect(requestOf(fetchSpy.mock.calls[0]).path).not.toBe(SESSION_INFO_PATH); |
| 518 | }).pipe(Effect.provide(testLayer)) |
| 519 | ); |
| 520 | |
| 521 | it.effect('fails with missing_org before any request when there is no API key', () => |
| 522 | Effect.gen(function* () { |
| 523 | const fetchSpy = vi.spyOn(globalThis, 'fetch'); |
| 524 | |
| 525 | const error = yield* resolveDeveloperProjectOrDefault({}).pipe(Effect.flip); |
| 526 | |
| 527 | expect(error).toMatchObject({ reason: 'missing_org' }); |
| 528 | expect(fetchSpy).not.toHaveBeenCalled(); |
| 529 | }).pipe( |
| 530 | Effect.provide( |
| 531 | Layer.mergeAll(singleton, userContext(Option.none(), Option.none()), noLocalProject) |
| 532 | ) |
| 533 | ) |
| 534 | ); |
| 535 | }); |