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 | // Adapted from `effect/testing`'s `TestConsole` (vendored at |
| 2 | // ts/vendor/effect/packages/effect/src/testing/TestConsole.ts). |
| 3 | // |
| 4 | // `TestConsole.make` records every `Console` method invocation as a |
| 5 | // `{ method, parameters }` entry, then derives two views from that list: |
| 6 | // `logLines` (entries where `method === 'log'`) and `errorLines` (entries |
| 7 | // where `method === 'error'`) — verified against the vendored source, that |
| 8 | // literal mapping is *all* TestConsole implements; `info`/`warn`/`debug`/etc. |
| 9 | // are captured in the entry list but bucketed by neither accessor. |
| 10 | // |
| 11 | // This mock reuses TestConsole's entry-recording design (tag every call by |
| 12 | // method, derive views by filtering) but widens the two views to fit how |
| 13 | // `test/__utils__/services/terminal-ui-test.ts`'s `TerminalUITest` actually |
| 14 | // spreads Composio's decoration surface across more than two `Console` |
| 15 | // methods: `stdout` covers `log`/`info`/`debug` (the real-data channel, |
| 16 | // `ui.output()`, plus anything log-adjacent) and `stderr` covers |
| 17 | // `error`/`warn` (all decoration, matching this CLI's own output convention — |
| 18 | // see `ts/packages/cli/AGENTS.md`'s "Output Conventions" section). A single |
| 19 | // chronological buffer of every recorded call, regardless of method, still |
| 20 | // backs the pre-existing merged view that ~40 suites already assert against |
| 21 | // via `MockConsole.getLines()` with no `stream`. |
| 22 | |
| 23 | import * as Console from 'effect/Console'; |
| 24 | import * as Context from 'effect/Context'; |
| 25 | import * as Effect from 'effect/Effect'; |
| 26 | |
| 27 | export interface MockConsole extends Console.Console { |
| 28 | readonly getLines: ( |
| 29 | params?: Partial<{ |
| 30 | readonly stripAnsi: boolean; |
| 31 | /** |
| 32 | * Restricts the returned lines to one channel: `stdout` covers |
| 33 | * `log`/`info`/`debug` calls (the real-data channel, `ui.output()`), |
| 34 | * `stderr` covers `error`/`warn` calls (all decoration). Omit to get |
| 35 | * every recorded call in chronological order, regardless of method — |
| 36 | * the original, backward-compatible merged view. |
| 37 | */ |
| 38 | readonly stream: 'stdout' | 'stderr'; |
| 39 | }> |
| 40 | ) => Effect.Effect<ReadonlyArray<string>>; |
| 41 | } |
| 42 | |
| 43 | // `Console.Console` is a `Context.Reference` in v4 (it replaces v3's FiberRef-backed |
| 44 | // console). Reusing its `.key` aliases this service into the same context slot, so |
| 45 | // providing `MockConsole` overrides the ambient console for anything that reads it. |
| 46 | export const MockConsole = Context.Service<Console.Console, MockConsole>(Console.Console.key); |
| 47 | |
| 48 | type Method = keyof Console.Console; |
| 49 | |
| 50 | const pattern = new RegExp( |
| 51 | [ |
| 52 | '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', |
| 53 | '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))', |
| 54 | ].join('|'), |
| 55 | 'g' |
| 56 | ); |
| 57 | |
| 58 | const stripAnsi = (str: string) => str.replace(pattern, ''); |
| 59 | |
| 60 | export const make = Effect.sync(() => { |
| 61 | // v4's `Console.Console` interface methods are plain synchronous `void`-returning |
| 62 | // calls (the effectful wrappers live at the `Console` module level), so the mock |
| 63 | // buffers into local mutable arrays instead of an `Effect`-hosted `Ref`. |
| 64 | |
| 65 | // Every recorded call, tagged with the method that produced it — mirrors |
| 66 | // `TestConsole.make`'s `entries` array and backs the `stdout`/`stderr` views. |
| 67 | const entries: Array<{ readonly method: Method; readonly parameters: ReadonlyArray<unknown> }> = |
| 68 | []; |
| 69 | // Every recorded call's arguments, flattened in call order regardless of |
| 70 | // method — backs the original merged view (`getLines()` with no `stream`). |
| 71 | const allLines: Array<unknown> = []; |
| 72 | |
| 73 | const record = |
| 74 | (method: Method) => |
| 75 | (...parameters: ReadonlyArray<unknown>): void => { |
| 76 | entries.push({ method, parameters }); |
| 77 | allLines.push(...parameters); |
| 78 | }; |
| 79 | |
| 80 | // Every CLI writer hands the console strings, so this only formats the odd |
| 81 | // non-string argument the way `console.log` would when joined. |
| 82 | const asLine = (value: unknown): string => (typeof value === 'string' ? value : String(value)); |
| 83 | |
| 84 | const linesForMethods = (methods: ReadonlySet<Method>) => |
| 85 | entries.filter(entry => methods.has(entry.method)).flatMap(entry => entry.parameters); |
| 86 | |
| 87 | const stdoutMethods = new Set<Method>(['log', 'info', 'debug']); |
| 88 | const stderrMethods = new Set<Method>(['error', 'warn']); |
| 89 | |
| 90 | const getLines: MockConsole['getLines'] = (params = {}) => |
| 91 | Effect.sync(() => { |
| 92 | const source = |
| 93 | params.stream === 'stdout' |
| 94 | ? linesForMethods(stdoutMethods) |
| 95 | : params.stream === 'stderr' |
| 96 | ? linesForMethods(stderrMethods) |
| 97 | : allLines; |
| 98 | const lines = source.map(asLine); |
| 99 | return params.stripAnsi || false ? lines.map(stripAnsi) : lines; |
| 100 | }); |
| 101 | |
| 102 | return MockConsole.of({ |
| 103 | getLines, |
| 104 | clear: record('clear'), |
| 105 | log: record('log'), |
| 106 | info: record('info'), |
| 107 | warn: record('warn'), |
| 108 | error: record('error'), |
| 109 | assert: record('assert'), |
| 110 | count: record('count'), |
| 111 | countReset: record('countReset'), |
| 112 | debug: record('debug'), |
| 113 | dir: record('dir'), |
| 114 | dirxml: record('dirxml'), |
| 115 | group: record('group'), |
| 116 | groupCollapsed: record('groupCollapsed'), |
| 117 | groupEnd: record('groupEnd'), |
| 118 | table: record('table'), |
| 119 | time: record('time'), |
| 120 | timeEnd: record('timeEnd'), |
| 121 | timeLog: record('timeLog'), |
| 122 | trace: record('trace'), |
| 123 | }); |
| 124 | }); |
| 125 | |
| 126 | export const getLines = ( |
| 127 | params?: Partial<{ |
| 128 | readonly stripAnsi?: boolean; |
| 129 | readonly stream?: 'stdout' | 'stderr'; |
| 130 | }> |
| 131 | ): Effect.Effect<ReadonlyArray<string>> => |
| 132 | Console.consoleWith(console => (console as MockConsole).getLines(params)); |