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 | /** |
| 2 | * Helpers shared by the operation suites. |
| 3 | */ |
| 4 | |
| 5 | import { join } from 'node:path'; |
| 6 | import { sanitizeOutput } from '@e2e-tests/utils'; |
| 7 | import type { JournalEntry } from '@e2e-tests/cli-fake-backend'; |
| 8 | import type { CliRig, CommandResult } from '@e2e-tests/cli-fake-backend/rig'; |
| 9 | import { z } from 'zod'; |
| 10 | |
| 11 | /** Per-test budget: a command is a few hundred milliseconds, a retried one a few seconds. */ |
| 12 | export const TEST_TIMEOUT_MS = 90_000; |
| 13 | |
| 14 | /** Budget for starting a rig, which may build the CLI image on a cold machine. */ |
| 15 | export const RIG_TIMEOUT_MS = 1_200_000; |
| 16 | |
| 17 | export const SLACK_ARGUMENTS = `'{"channel":"general","text":"hello"}'`; |
| 18 | export const EXECUTE_SLACK = `composio execute SLACK_SEND_MESSAGE -d ${SLACK_ARGUMENTS}`; |
| 19 | export const PLAYGROUND_EXECUTE_SLACK = `composio dev playground-execute SLACK_SEND_MESSAGE -d ${SLACK_ARGUMENTS}`; |
| 20 | export const SEARCH_SLACK = 'composio search "send a slack message"'; |
| 21 | |
| 22 | /** A directory outside the journey's home, initialized by `composio dev init`. */ |
| 23 | export const DEV_DIRECTORY = '/work/app'; |
| 24 | |
| 25 | export const clean = sanitizeOutput; |
| 26 | |
| 27 | /** |
| 28 | * The requests a command made to do its job. The update check and telemetry |
| 29 | * run beside every command and are asserted on their own. |
| 30 | */ |
| 31 | export const commandRequests = (result: CommandResult): ReadonlyArray<JournalEntry> => |
| 32 | result.requests.filter(entry => entry.role === 'backend' || entry.role === 'dashboard'); |
| 33 | |
| 34 | export const routesOf = (result: CommandResult): string[] => |
| 35 | commandRequests(result).map(entry => entry.route ?? `unrouted ${entry.method} ${entry.path}`); |
| 36 | |
| 37 | /** Parses stdout as JSON with `schema`; a stdout that is not pure JSON fails the test. */ |
| 38 | export const stdoutJson = <T>(result: CommandResult, schema: z.ZodType<T>): T => { |
| 39 | const raw = clean(result.stdout); |
| 40 | let parsed: unknown; |
| 41 | try { |
| 42 | parsed = JSON.parse(raw); |
| 43 | } catch (cause) { |
| 44 | throw new Error( |
| 45 | `stdout is not pure JSON (exit ${result.exitCode}).\nstdout: ${raw.slice(0, 400)}\nstderr: ${clean(result.stderr).slice(0, 400)}`, |
| 46 | { cause } |
| 47 | ); |
| 48 | } |
| 49 | return schema.parse(parsed); |
| 50 | }; |
| 51 | |
| 52 | export const ExecutionResult = z.looseObject({ |
| 53 | successful: z.boolean(), |
| 54 | error: z.string().nullable().optional(), |
| 55 | slug: z.string().optional(), |
| 56 | data: z.record(z.string(), z.unknown()).optional(), |
| 57 | retry: z.enum(['safe', 'check-first']).optional(), |
| 58 | }); |
| 59 | |
| 60 | export const SearchResult = z.looseObject({ |
| 61 | results: z.array( |
| 62 | z.looseObject({ use_case: z.string(), primary_tool_slugs: z.array(z.string()) }) |
| 63 | ), |
| 64 | next_steps: z.looseObject({ |
| 65 | steps: z.array(z.looseObject({ action: z.string(), command: z.string() })), |
| 66 | }), |
| 67 | }); |
| 68 | |
| 69 | /** The stored login, as `~/.composio/user_data.json` holds it. */ |
| 70 | export const StoredLogin = z.looseObject({ |
| 71 | api_key: z.string().nullable(), |
| 72 | base_url: z.string().nullable(), |
| 73 | web_url: z.string().nullable(), |
| 74 | org_id: z.string().nullable(), |
| 75 | test_user_id: z.string().nullable(), |
| 76 | }); |
| 77 | |
| 78 | export const readStoredLogin = async (rig: CliRig) => |
| 79 | StoredLogin.parse( |
| 80 | JSON.parse(await rig.journey.readFile(`${rig.journey.home}/.composio/user_data.json`)) |
| 81 | ); |
| 82 | |
| 83 | /** Initializes `DEV_DIRECTORY` for the org's default developer project. */ |
| 84 | export const initDeveloperDirectory = async (rig: CliRig): Promise<void> => { |
| 85 | const result = await rig.run( |
| 86 | `mkdir -p ${DEV_DIRECTORY} && cd ${DEV_DIRECTORY} && composio dev init --yes --no-browser` |
| 87 | ); |
| 88 | if (result.exitCode !== 0) { |
| 89 | throw new Error(`composio dev init failed: ${clean(result.stderr) || clean(result.stdout)}`); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * Runs `command` with a pseudo-terminal on all three streams, then applies the |
| 95 | * redirections written into the command itself. `script` allocates the |
| 96 | * terminal; what the terminal showed lands in the returned `terminal` text. |
| 97 | */ |
| 98 | export const runWithTerminal = async (rig: CliRig, command: string) => { |
| 99 | const transcript = '/tmp/e2e-terminal.txt'; |
| 100 | const quoted = `'${`stty cols 200; ${command}`.replace(/'/g, `'\\''`)}'`; |
| 101 | const result = await rig.run(`script -qec ${quoted} ${transcript} > /dev/null`); |
| 102 | const terminal = clean(await rig.journey.readFile(transcript)) |
| 103 | .split('\n') |
| 104 | .filter(line => !line.startsWith('Script started on') && !line.startsWith('Script done on')) |
| 105 | .join('\n'); |
| 106 | return { ...result, terminal }; |
| 107 | }; |
| 108 | |
| 109 | /** Writes the journey's steps next to the suite, as the other CLI suites do. */ |
| 110 | export const writeDebugLog = (rig: CliRig, importMetaDir: string, name: string): Promise<void> => |
| 111 | rig.journey.writeDebugLog(join(importMetaDir, `DEBUG.${name}.log`)); |