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, Runtime } from 'effect'; |
| 3 | import { printFailure, reportFailure, ReportedFailure } from 'src/effects/report-failure'; |
| 4 | import { getTerminalCapabilities, TerminalUI } from 'src/services/terminal-ui'; |
| 5 | import { exitCodeOf } from 'test/__utils__/exit-code'; |
| 6 | import { redirectedTerminalUI } from 'test/__utils__/services/redirected-terminal-ui'; |
| 7 | import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test'; |
| 8 | |
| 9 | // A TerminalUI whose stderr is a TTY, recording every line with the channel it went to. |
| 10 | const decoratingUI = () => { |
| 11 | const lines: Array<string> = []; |
| 12 | const record = (channel: string) => (message: string) => |
| 13 | Effect.sync(() => { |
| 14 | lines.push(`${channel}: ${message}`); |
| 15 | }); |
| 16 | const ui = TerminalUI.of({ |
| 17 | ...terminalUITestImpl, |
| 18 | capabilities: Effect.succeed( |
| 19 | getTerminalCapabilities({ |
| 20 | stdin: { isTTY: true }, |
| 21 | stdout: { isTTY: true }, |
| 22 | stderr: { isTTY: true }, |
| 23 | }) |
| 24 | ), |
| 25 | error: record('stderr'), |
| 26 | log: { ...terminalUITestImpl.log, error: record('log.error'), step: record('log.step') }, |
| 27 | }); |
| 28 | return { ui, lines }; |
| 29 | }; |
| 30 | |
| 31 | describe('reportFailure', () => { |
| 32 | it.effect( |
| 33 | '[Given] a redirected stderr [Then] prints the message and hint plainly and exits 1', |
| 34 | () => { |
| 35 | const terminal = redirectedTerminalUI(); |
| 36 | return Effect.gen(function* () { |
| 37 | const ui = yield* TerminalUI; |
| 38 | |
| 39 | const exit = yield* Effect.exit( |
| 40 | reportFailure(ui, 'Failed to fetch toolkit "gmail".', 'Browse available toolkits') |
| 41 | ); |
| 42 | |
| 43 | expect(terminal.stderr()).toBe( |
| 44 | 'Failed to fetch toolkit "gmail".\nBrowse available toolkits\n' |
| 45 | ); |
| 46 | expect(terminal.stdout()).toBe(''); |
| 47 | expect(exitCodeOf(exit)).toBe(1); |
| 48 | }).pipe(Effect.provide(terminal.layer)); |
| 49 | } |
| 50 | ); |
| 51 | |
| 52 | it.effect('[Given] no hint [Then] prints only the message', () => { |
| 53 | const terminal = redirectedTerminalUI(); |
| 54 | return Effect.gen(function* () { |
| 55 | const ui = yield* TerminalUI; |
| 56 | |
| 57 | const exit = yield* Effect.exit(reportFailure(ui, 'The link response is incomplete.')); |
| 58 | |
| 59 | expect(terminal.stderr()).toBe('The link response is incomplete.\n'); |
| 60 | expect(exitCodeOf(exit)).toBe(1); |
| 61 | }).pipe(Effect.provide(terminal.layer)); |
| 62 | }); |
| 63 | |
| 64 | it.effect('[Given] a terminal [Then] renders a decorated error and step, once', () => { |
| 65 | const { ui, lines } = decoratingUI(); |
| 66 | return Effect.gen(function* () { |
| 67 | const exit = yield* Effect.exit( |
| 68 | reportFailure(ui, 'Failed to fetch toolkit "gmail".', 'Browse available toolkits') |
| 69 | ); |
| 70 | |
| 71 | expect(lines).toEqual([ |
| 72 | 'log.error: Failed to fetch toolkit "gmail".', |
| 73 | 'log.step: Browse available toolkits', |
| 74 | ]); |
| 75 | expect(exitCodeOf(exit)).toBe(1); |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | // `BunRuntime.runMain` logs a failure unless it carries `[Runtime.errorReported] = false`, so |
| 80 | // the marker is what keeps the report from printing twice. |
| 81 | it.effect('[Given] a report [Then] fails with a ReportedFailure the runtime does not log', () => { |
| 82 | const { ui } = decoratingUI(); |
| 83 | return Effect.gen(function* () { |
| 84 | const failure = yield* Effect.flip(reportFailure(ui, 'Toolkit "gmail" not found.')); |
| 85 | |
| 86 | expect(failure).toBeInstanceOf(ReportedFailure); |
| 87 | expect(failure.message).toBe('Toolkit "gmail" not found.'); |
| 88 | expect(Runtime.getErrorReported(failure)).toBe(false); |
| 89 | expect(Runtime.getErrorExitCode(failure)).toBe(1); |
| 90 | }); |
| 91 | }); |
| 92 | }); |
| 93 | |
| 94 | describe('printFailure', () => { |
| 95 | it.effect('[Given] a failure [Then] prints it like reportFailure and succeeds', () => { |
| 96 | const terminal = redirectedTerminalUI(); |
| 97 | return Effect.gen(function* () { |
| 98 | const ui = yield* TerminalUI; |
| 99 | |
| 100 | yield* printFailure(ui, 'Failed to fetch toolkits.', 'Retry later'); |
| 101 | |
| 102 | expect(terminal.stderr()).toBe('Failed to fetch toolkits.\nRetry later\n'); |
| 103 | }).pipe(Effect.provide(terminal.layer)); |
| 104 | }); |
| 105 | }); |