Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, expect, it } from '@effect/vitest'; |
| 2 | import { Effect, Layer, Option } from 'effect'; |
| 3 | import type { BackendMismatch } from 'src/config/backend'; |
| 4 | import { BackendMismatchWarning } from 'src/services/backend-mismatch-warning'; |
| 5 | import { getTerminalCapabilities, TerminalUI } from 'src/services/terminal-ui'; |
| 6 | import { ComposioUserContext } from 'src/services/user-context'; |
| 7 | import { commandIdentity } from 'test/__utils__/command-identity'; |
| 8 | import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test'; |
| 9 | |
| 10 | const MISMATCH: BackendMismatch = { |
| 11 | variable: 'COMPOSIO_BASE_URL', |
| 12 | issuedBy: 'https://backend.composio.dev', |
| 13 | selected: 'https://staging.composio.dev', |
| 14 | }; |
| 15 | |
| 16 | const MESSAGE = |
| 17 | 'COMPOSIO_BASE_URL sends your stored API key to https://staging.composio.dev, but it was issued by https://backend.composio.dev. ' + |
| 18 | 'Unset COMPOSIO_BASE_URL to use the backend you logged in to, or run `composio logout && composio login` to log in to https://staging.composio.dev instead.'; |
| 19 | |
| 20 | const userContext = (backendMismatch: Option.Option<BackendMismatch>) => |
| 21 | Layer.succeed( |
| 22 | ComposioUserContext, |
| 23 | ComposioUserContext.of({ |
| 24 | data: { |
| 25 | apiKey: Option.some('uak_test'), |
| 26 | baseURL: 'https://staging.composio.dev', |
| 27 | webURL: 'https://platform.composio.dev', |
| 28 | orgId: Option.none(), |
| 29 | projectId: Option.none(), |
| 30 | testUserId: Option.none(), |
| 31 | }, |
| 32 | backendMismatch, |
| 33 | isLoggedIn: () => true, |
| 34 | logout: Effect.void, |
| 35 | login: () => Effect.void, |
| 36 | update: () => Effect.void, |
| 37 | }) |
| 38 | ); |
| 39 | |
| 40 | /** A terminal whose stderr is (or is not) a TTY, recording warnings and plain stderr lines. */ |
| 41 | const recordingTerminal = (stderrIsTTY: boolean) => { |
| 42 | const written: Array<{ readonly via: 'log.warn' | 'error'; readonly text: string }> = []; |
| 43 | const layer = Layer.succeed( |
| 44 | TerminalUI, |
| 45 | TerminalUI.of({ |
| 46 | ...terminalUITestImpl, |
| 47 | capabilities: Effect.succeed( |
| 48 | getTerminalCapabilities({ |
| 49 | stdin: { isTTY: false }, |
| 50 | stdout: { isTTY: false }, |
| 51 | stderr: { isTTY: stderrIsTTY }, |
| 52 | }) |
| 53 | ), |
| 54 | error: text => Effect.sync(() => void written.push({ via: 'error', text })), |
| 55 | log: { |
| 56 | ...terminalUITestImpl.log, |
| 57 | warn: text => Effect.sync(() => void written.push({ via: 'log.warn', text })), |
| 58 | }, |
| 59 | }) |
| 60 | ); |
| 61 | return { layer, written }; |
| 62 | }; |
| 63 | |
| 64 | const cases = [Option.none<BackendMismatch>(), Option.some(MISMATCH)].flatMap(backendMismatch => |
| 65 | [true, false].map(stderrIsTTY => ({ |
| 66 | backendMismatch, |
| 67 | situation: Option.isNone(backendMismatch) ? 'no mismatch' : 'a mismatch', |
| 68 | stderrIsTTY, |
| 69 | expected: Option.isNone(backendMismatch) |
| 70 | ? [] |
| 71 | : stderrIsTTY |
| 72 | ? [{ via: 'log.warn', text: MESSAGE }] |
| 73 | : [{ via: 'error', text: `Warning: ${MESSAGE}` }], |
| 74 | })) |
| 75 | ); |
| 76 | |
| 77 | describe('BackendMismatchWarning', () => { |
| 78 | it.effect.each(cases)( |
| 79 | '[Given] $situation, stderr TTY $stderrIsTTY [Then] warns only on a mismatch, in the form stderr supports', |
| 80 | ({ backendMismatch, stderrIsTTY, expected }) => { |
| 81 | const terminal = recordingTerminal(stderrIsTTY); |
| 82 | return Effect.gen(function* () { |
| 83 | const warning = yield* BackendMismatchWarning; |
| 84 | yield* warning.run({ identity: commandIdentity(['whoami']) }); |
| 85 | expect(terminal.written).toEqual(expected); |
| 86 | }).pipe( |
| 87 | Effect.provide( |
| 88 | Layer.provide( |
| 89 | BackendMismatchWarning.Default, |
| 90 | Layer.mergeAll(userContext(backendMismatch), terminal.layer) |
| 91 | ) |
| 92 | ) |
| 93 | ); |
| 94 | } |
| 95 | ); |
| 96 | }); |