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 * as path from 'node:path'; |
| 2 | import * as fs from 'node:fs'; |
| 3 | import * as os from 'node:os'; |
| 4 | import { inspect } from 'node:util'; |
| 5 | import { Predicate } from 'effect'; |
| 6 | import { isBlockedSensitiveFileUploadPath } from '@composio/core'; |
| 7 | import { afterEach, assert, describe, expect, it, vi } from 'vitest'; |
| 8 | import { installRunHelpers, parseJson } from 'src/services/run-helpers-runtime'; |
| 9 | import { ToolInputRequiredError } from 'src/utils/tool-input-required'; |
| 10 | |
| 11 | const installedGlobalNames = [ |
| 12 | 'z', |
| 13 | 'zod', |
| 14 | 'search', |
| 15 | 'execute', |
| 16 | 'experimental_subAgent', |
| 17 | 'invokeAgent', |
| 18 | 'proxy', |
| 19 | '__composioRunContext', |
| 20 | '__composioConsumerContext', |
| 21 | ]; |
| 22 | const originalGlobalDescriptors = new Map( |
| 23 | installedGlobalNames.map(name => [name, Object.getOwnPropertyDescriptor(globalThis, name)]) |
| 24 | ); |
| 25 | |
| 26 | const readInstalledFunction = (name: string): ((...args: ReadonlyArray<unknown>) => unknown) => { |
| 27 | const value: unknown = Reflect.get(globalThis, name); |
| 28 | assert(typeof value === 'function', `installRunHelpers() did not install ${name}().`); |
| 29 | return (...args) => Reflect.apply(value, undefined, [...args]); |
| 30 | }; |
| 31 | |
| 32 | describe('parseJson', () => { |
| 33 | it('[Given] blank text [Then] returns undefined', () => { |
| 34 | expect(parseJson(' ')).toBeUndefined(); |
| 35 | }); |
| 36 | |
| 37 | it('[Given] valid JSON [Then] returns the parsed value', () => { |
| 38 | expect(parseJson(' {"a": 1} ')).toEqual({ a: 1 }); |
| 39 | expect(parseJson('[1, 2, 3]')).toEqual([1, 2, 3]); |
| 40 | expect(parseJson('null')).toBeNull(); |
| 41 | }); |
| 42 | |
| 43 | it('[Given] non-JSON text [Then] returns the trimmed text verbatim', () => { |
| 44 | expect(parseJson(' not json at all ')).toBe('not json at all'); |
| 45 | }); |
| 46 | }); |
| 47 | |
| 48 | describe('run-helpers-runtime', () => { |
| 49 | afterEach(() => { |
| 50 | vi.unstubAllGlobals(); |
| 51 | installedGlobalNames.forEach(name => { |
| 52 | const descriptor = originalGlobalDescriptors.get(name); |
| 53 | if (descriptor) { |
| 54 | Object.defineProperty(globalThis, name, descriptor); |
| 55 | } else { |
| 56 | Reflect.deleteProperty(globalThis, name); |
| 57 | } |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | it('[Given] installed helpers [Then] every documented global is defined', async () => { |
| 62 | await installRunHelpers({ cliPrefix: ['composio'] }); |
| 63 | |
| 64 | const helperNames = ['execute', 'search', 'proxy', 'experimental_subAgent', 'invokeAgent']; |
| 65 | expect(helperNames.map(name => typeof Reflect.get(globalThis, name))).toEqual( |
| 66 | helperNames.map(() => 'function') |
| 67 | ); |
| 68 | expect(Reflect.get(globalThis, 'z')).toBeDefined(); |
| 69 | expect(Reflect.get(globalThis, 'zod')).toBe(Reflect.get(globalThis, 'z')); |
| 70 | }); |
| 71 | |
| 72 | it('[Given] experimental_subAgent or invokeAgent [Then] both reject with the same removal message and alternative', async () => { |
| 73 | await installRunHelpers({ cliPrefix: ['composio'] }); |
| 74 | |
| 75 | const [subAgent, invokeAgent] = await Promise.allSettled([ |
| 76 | readInstalledFunction('experimental_subAgent')('p'), |
| 77 | readInstalledFunction('invokeAgent')('p'), |
| 78 | ]); |
| 79 | |
| 80 | assert(subAgent?.status === 'rejected' && subAgent.reason instanceof Error); |
| 81 | expect(subAgent.reason.message).toContain('removed'); |
| 82 | expect(subAgent.reason.message).toContain('automatically approved permission requests'); |
| 83 | expect(subAgent.reason.message).toContain('let the calling agent summarize'); |
| 84 | expect(invokeAgent).toMatchObject({ |
| 85 | status: 'rejected', |
| 86 | reason: { message: subAgent.reason.message }, |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | it.each([ |
| 91 | { name: 'no arguments', args: [] }, |
| 92 | { name: 'a prompt and options', args: ['p', { target: 'codex' }] }, |
| 93 | { name: 'arbitrary extra arguments', args: [42, null, 'extra'] }, |
| 94 | ])('[Given] $name [Then] experimental_subAgent rejects instead of throwing', async ({ args }) => { |
| 95 | await installRunHelpers({ cliPrefix: ['composio'] }); |
| 96 | |
| 97 | const returned = readInstalledFunction('experimental_subAgent')(...args); |
| 98 | |
| 99 | expect(returned).toBeInstanceOf(Promise); |
| 100 | await expect(returned).rejects.toThrow(/removed/); |
| 101 | }); |
| 102 | |
| 103 | it('[Given] a malformed proxy session response [Then] it rejects the HTTP boundary payload', async () => { |
| 104 | const fetchMock = vi.fn().mockResolvedValue( |
| 105 | new Response(JSON.stringify({ session_id: 42 }), { |
| 106 | status: 200, |
| 107 | headers: { 'content-type': 'application/json' }, |
| 108 | }) |
| 109 | ); |
| 110 | vi.stubGlobal('fetch', fetchMock); |
| 111 | |
| 112 | await installRunHelpers({ |
| 113 | cliPrefix: ['composio'], |
| 114 | helperContext: { |
| 115 | apiKey: 'test-key', |
| 116 | orgId: 'test-org', |
| 117 | consumerProjectId: 'test-project', |
| 118 | consumerUserId: 'test-user', |
| 119 | }, |
| 120 | }); |
| 121 | |
| 122 | const installedGlobals: unknown = globalThis; |
| 123 | assert( |
| 124 | Predicate.hasProperty(installedGlobals, 'proxy') && |
| 125 | typeof installedGlobals.proxy === 'function', |
| 126 | 'installRunHelpers() did not install proxy().' |
| 127 | ); |
| 128 | |
| 129 | await expect(installedGlobals.proxy('github')).rejects.toThrow(/session_id/); |
| 130 | expect(fetchMock).toHaveBeenCalledOnce(); |
| 131 | }); |
| 132 | |
| 133 | it('[Given] a proxy call that needs user input [Then] it rejects instead of returning an empty 200', async () => { |
| 134 | const fetchMock = vi |
| 135 | .fn() |
| 136 | .mockResolvedValueOnce( |
| 137 | new Response(JSON.stringify({ session_id: 'session-1' }), { |
| 138 | status: 200, |
| 139 | headers: { 'content-type': 'application/json' }, |
| 140 | }) |
| 141 | ) |
| 142 | .mockResolvedValueOnce( |
| 143 | new Response( |
| 144 | JSON.stringify({ |
| 145 | result_type: 'input_required', |
| 146 | input_requests: { |
| 147 | approval_1: { |
| 148 | type: 'elicitation', |
| 149 | mode: 'form', |
| 150 | message: 'Allow this request to GitHub?', |
| 151 | requested_schema: { type: 'object' }, |
| 152 | }, |
| 153 | }, |
| 154 | request_state: 'opaque-state-token', |
| 155 | }), |
| 156 | { |
| 157 | status: 200, |
| 158 | headers: { 'content-type': 'application/json' }, |
| 159 | } |
| 160 | ) |
| 161 | ); |
| 162 | vi.stubGlobal('fetch', fetchMock); |
| 163 | |
| 164 | await installRunHelpers({ |
| 165 | cliPrefix: ['composio'], |
| 166 | helperContext: { |
| 167 | apiKey: 'test-key', |
| 168 | orgId: 'test-org', |
| 169 | consumerProjectId: 'test-project', |
| 170 | consumerUserId: 'test-user', |
| 171 | }, |
| 172 | }); |
| 173 | |
| 174 | const installedGlobals: unknown = globalThis; |
| 175 | assert( |
| 176 | Predicate.hasProperty(installedGlobals, 'proxy') && |
| 177 | typeof installedGlobals.proxy === 'function', |
| 178 | 'installRunHelpers() did not install proxy().' |
| 179 | ); |
| 180 | |
| 181 | const proxyFetch = await installedGlobals.proxy('github'); |
| 182 | const error: unknown = await proxyFetch('https://api.github.com/user/repos', { |
| 183 | method: 'POST', |
| 184 | }).catch((caught: unknown) => caught); |
| 185 | |
| 186 | assert(error instanceof ToolInputRequiredError); |
| 187 | expect(error.message).toBe( |
| 188 | 'POST proxy call via "github" requires user input before it can run (1 input request) and was not executed. The CLI cannot answer input requests yet.' |
| 189 | ); |
| 190 | // The script is likely to log the error whole, so the continuation state |
| 191 | // is not on it. |
| 192 | expect(inspect(error, { depth: null })).not.toContain('opaque-state-token'); |
| 193 | expect(JSON.stringify(error)).not.toContain('opaque-state-token'); |
| 194 | expect(Object.keys(error.inputRequests)).toEqual(['approval_1']); |
| 195 | expect(fetchMock).toHaveBeenCalledTimes(2); |
| 196 | }); |
| 197 | |
| 198 | it.each([ |
| 199 | { |
| 200 | name: 'a completed answer', |
| 201 | body: { result_type: 'completed', status: 201, data: { id: 1 } }, |
| 202 | }, |
| 203 | { name: 'an answer with no result_type', body: { status: 201, data: { id: 1 } } }, |
| 204 | ])('[Given] $name from the proxy [Then] it returns the proxied response', async ({ body }) => { |
| 205 | const fetchMock = vi |
| 206 | .fn() |
| 207 | .mockResolvedValueOnce( |
| 208 | new Response(JSON.stringify({ session_id: 'session-1' }), { |
| 209 | status: 200, |
| 210 | headers: { 'content-type': 'application/json' }, |
| 211 | }) |
| 212 | ) |
| 213 | .mockResolvedValueOnce( |
| 214 | new Response(JSON.stringify(body), { |
| 215 | status: 200, |
| 216 | headers: { 'content-type': 'application/json' }, |
| 217 | }) |
| 218 | ); |
| 219 | vi.stubGlobal('fetch', fetchMock); |
| 220 | |
| 221 | await installRunHelpers({ |
| 222 | cliPrefix: ['composio'], |
| 223 | helperContext: { |
| 224 | apiKey: 'test-key', |
| 225 | orgId: 'test-org', |
| 226 | consumerProjectId: 'test-project', |
| 227 | consumerUserId: 'test-user', |
| 228 | }, |
| 229 | }); |
| 230 | |
| 231 | const installedGlobals: unknown = globalThis; |
| 232 | assert( |
| 233 | Predicate.hasProperty(installedGlobals, 'proxy') && |
| 234 | typeof installedGlobals.proxy === 'function', |
| 235 | 'installRunHelpers() did not install proxy().' |
| 236 | ); |
| 237 | |
| 238 | const proxyFetch = await installedGlobals.proxy('github'); |
| 239 | const response: Response = await proxyFetch('https://api.github.com/user'); |
| 240 | |
| 241 | expect(response.status).toBe(201); |
| 242 | expect(response.ok).toBe(true); |
| 243 | await expect(response.json()).resolves.toEqual({ id: 1 }); |
| 244 | }); |
| 245 | |
| 246 | it('[Given] a proxy binary URL targets cloud metadata [Then] it blocks the download', async () => { |
| 247 | const fetchMock = vi |
| 248 | .fn() |
| 249 | .mockResolvedValueOnce( |
| 250 | new Response(JSON.stringify({ session_id: 'session-1' }), { |
| 251 | status: 200, |
| 252 | headers: { 'content-type': 'application/json' }, |
| 253 | }) |
| 254 | ) |
| 255 | .mockResolvedValueOnce( |
| 256 | new Response( |
| 257 | JSON.stringify({ |
| 258 | binary_data: { |
| 259 | url: 'http://169.254.169.254/latest/meta-data/iam/security-credentials/', |
| 260 | }, |
| 261 | status: 200, |
| 262 | }), |
| 263 | { |
| 264 | status: 200, |
| 265 | headers: { 'content-type': 'application/json' }, |
| 266 | } |
| 267 | ) |
| 268 | ) |
| 269 | .mockResolvedValueOnce(new Response('stolen cloud credentials', { status: 200 })); |
| 270 | vi.stubGlobal('fetch', fetchMock); |
| 271 | |
| 272 | await installRunHelpers({ |
| 273 | cliPrefix: ['composio'], |
| 274 | helperContext: { |
| 275 | apiKey: 'test-key', |
| 276 | orgId: 'test-org', |
| 277 | consumerProjectId: 'test-project', |
| 278 | consumerUserId: 'test-user', |
| 279 | }, |
| 280 | }); |
| 281 | |
| 282 | const installedGlobals: unknown = globalThis; |
| 283 | assert( |
| 284 | Predicate.hasProperty(installedGlobals, 'proxy') && |
| 285 | typeof installedGlobals.proxy === 'function', |
| 286 | 'installRunHelpers() did not install proxy().' |
| 287 | ); |
| 288 | |
| 289 | const proxyFetch = await installedGlobals.proxy('github'); |
| 290 | await expect(proxyFetch('https://api.github.com/user')).rejects.toMatchObject({ |
| 291 | code: 'TS-SDK::BLOCKED_INTERNAL_URL', |
| 292 | name: 'ComposioBlockedInternalUrlError', |
| 293 | }); |
| 294 | expect(fetchMock).toHaveBeenCalledTimes(2); |
| 295 | }); |
| 296 | |
| 297 | it('[Given] a configured proxy prevents pinning [Then] it blocks the binary download', async () => { |
| 298 | vi.stubEnv('NODE_USE_ENV_PROXY', '1'); |
| 299 | vi.stubEnv('HTTPS_PROXY', 'http://proxy.example:3128'); |
| 300 | vi.stubEnv('NO_PROXY', ''); |
| 301 | const fetchMock = vi |
| 302 | .fn() |
| 303 | .mockResolvedValueOnce( |
| 304 | new Response(JSON.stringify({ session_id: 'session-1' }), { |
| 305 | status: 200, |
| 306 | headers: { 'content-type': 'application/json' }, |
| 307 | }) |
| 308 | ) |
| 309 | .mockResolvedValueOnce( |
| 310 | new Response( |
| 311 | JSON.stringify({ |
| 312 | binary_data: { url: 'https://1.1.1.1/proxy-response.bin' }, |
| 313 | status: 200, |
| 314 | }), |
| 315 | { |
| 316 | status: 200, |
| 317 | headers: { 'content-type': 'application/json' }, |
| 318 | } |
| 319 | ) |
| 320 | ) |
| 321 | .mockResolvedValueOnce(new Response('untrusted proxied response', { status: 200 })); |
| 322 | vi.stubGlobal('fetch', fetchMock); |
| 323 | |
| 324 | await installRunHelpers({ |
| 325 | cliPrefix: ['composio'], |
| 326 | helperContext: { |
| 327 | apiKey: 'test-key', |
| 328 | orgId: 'test-org', |
| 329 | consumerProjectId: 'test-project', |
| 330 | consumerUserId: 'test-user', |
| 331 | }, |
| 332 | }); |
| 333 | |
| 334 | const installedGlobals: unknown = globalThis; |
| 335 | assert( |
| 336 | Predicate.hasProperty(installedGlobals, 'proxy') && |
| 337 | typeof installedGlobals.proxy === 'function', |
| 338 | 'installRunHelpers() did not install proxy().' |
| 339 | ); |
| 340 | |
| 341 | const proxyFetch = await installedGlobals.proxy('github'); |
| 342 | await expect(proxyFetch('https://api.github.com/user')).rejects.toMatchObject({ |
| 343 | code: 'TS-SDK::BLOCKED_INTERNAL_URL', |
| 344 | name: 'ComposioBlockedInternalUrlError', |
| 345 | }); |
| 346 | expect(fetchMock).toHaveBeenCalledTimes(2); |
| 347 | }); |
| 348 | |
| 349 | it('[Given] `composio execute` exits 1 with a failed result [Then] execute() rejects with the result error', async () => { |
| 350 | const childScript = [ |
| 351 | 'console.log(JSON.stringify({ successful: false, data: {}, error: "User not found" }));', |
| 352 | 'process.exit(1);', |
| 353 | ].join('\n'); |
| 354 | await installRunHelpers({ cliPrefix: [process.execPath, '-e', childScript] }); |
| 355 | |
| 356 | const returned = readInstalledFunction('execute')('HACKERNEWS_GET_USER', { |
| 357 | username: 'nobody', |
| 358 | }); |
| 359 | |
| 360 | await expect(returned).rejects.toBeInstanceOf(Error); |
| 361 | await expect(returned).rejects.toMatchObject({ |
| 362 | message: 'User not found', |
| 363 | slug: 'HACKERNEWS_GET_USER', |
| 364 | result: { successful: false, error: 'User not found' }, |
| 365 | }); |
| 366 | }); |
| 367 | |
| 368 | it('[Given] file labels larger than the Linux environment limit [Then] the child reads all labels from a temporary manifest', async () => { |
| 369 | const outputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-batch-')); |
| 370 | const childScript = [ |
| 371 | 'const fs = require("node:fs");', |
| 372 | 'const manifestPath = process.env.COMPOSIO_RUN_STAGED_FILES_FILE;', |
| 373 | 'const manifest = fs.readFileSync(manifestPath, "utf8");', |
| 374 | 'const labels = JSON.parse(manifest);', |
| 375 | 'const input = process.argv[process.argv.indexOf("--data") + 1];', |
| 376 | 'const data = JSON.parse(fs.readFileSync(input.slice(1), "utf8"));', |
| 377 | 'console.log(JSON.stringify({ successful: true, data: { manifestPath, manifestBytes: Buffer.byteLength(manifest), mode: fs.statSync(manifestPath).mode & 0o777, count: Object.keys(labels).length, dataCount: data.files.length, first: labels[data.files[0]], last: labels[data.files.at(-1)] } }));', |
| 378 | ].join('\n'); |
| 379 | const mimeType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; |
| 380 | const fileName = (i: number) => `${i}-${'a'.repeat(200)}.pptx`; |
| 381 | try { |
| 382 | await installRunHelpers({ |
| 383 | cliPrefix: [process.execPath, '-e', childScript], |
| 384 | helperContext: { runOutputDir: outputDir }, |
| 385 | }); |
| 386 | const result = await readInstalledFunction('execute')('TEST_UPLOAD', { |
| 387 | files: Array.from( |
| 388 | { length: 512 }, |
| 389 | (_, i) => new File(['PK'], fileName(i), { type: mimeType }) |
| 390 | ), |
| 391 | }); |
| 392 | assert( |
| 393 | Predicate.hasProperty(result, 'data') && |
| 394 | Predicate.hasProperty(result.data, 'manifestPath') && |
| 395 | typeof result.data.manifestPath === 'string' |
| 396 | ); |
| 397 | expect(result.data).toMatchObject({ |
| 398 | count: 512, |
| 399 | dataCount: 512, |
| 400 | mode: 0o600, |
| 401 | first: { name: fileName(0), mimetype: mimeType }, |
| 402 | last: { name: fileName(511), mimetype: mimeType }, |
| 403 | }); |
| 404 | assert( |
| 405 | Predicate.hasProperty(result.data, 'manifestBytes') && |
| 406 | typeof result.data.manifestBytes === 'number' |
| 407 | ); |
| 408 | expect(result.data.manifestBytes).toBeGreaterThan(128 * 1024); |
| 409 | expect(Buffer.byteLength(result.data.manifestPath)).toBeLessThan(1024); |
| 410 | expect(fs.existsSync(result.data.manifestPath)).toBe(false); |
| 411 | } finally { |
| 412 | fs.rmSync(outputDir, { recursive: true, force: true }); |
| 413 | } |
| 414 | }); |
| 415 | |
| 416 | it.each([true, false])( |
| 417 | '[Given] a child fails with JSON result=%s [Then] its metadata is removed', |
| 418 | async reportsResult => { |
| 419 | const outputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-failure-')); |
| 420 | const observedPath = path.join(outputDir, 'observed-manifest'); |
| 421 | const childScript = [ |
| 422 | 'const fs = require("node:fs");', |
| 423 | `fs.writeFileSync(${JSON.stringify(observedPath)}, process.env.COMPOSIO_RUN_STAGED_FILES_FILE);`, |
| 424 | ...(reportsResult |
| 425 | ? ['console.log(JSON.stringify({ successful: false, error: "refused" }));'] |
| 426 | : []), |
| 427 | 'process.exit(1);', |
| 428 | ].join('\n'); |
| 429 | try { |
| 430 | await installRunHelpers({ cliPrefix: [process.execPath, '-e', childScript] }); |
| 431 | await expect( |
| 432 | readInstalledFunction('execute')('TEST_UPLOAD', { file: new File(['x'], 'x.txt') }) |
| 433 | ).rejects.toBeInstanceOf(Error); |
| 434 | const manifestPath = fs.readFileSync(observedPath, 'utf8'); |
| 435 | expect(fs.existsSync(path.dirname(manifestPath))).toBe(false); |
| 436 | } finally { |
| 437 | fs.rmSync(outputDir, { recursive: true, force: true }); |
| 438 | } |
| 439 | } |
| 440 | ); |
| 441 | |
| 442 | it('[Given] a stale inherited metadata path and no Files [Then] the child receives no metadata path', async () => { |
| 443 | vi.stubEnv('COMPOSIO_RUN_STAGED_FILES_FILE', '/stale/metadata.json'); |
| 444 | const childScript = |
| 445 | 'console.log(JSON.stringify({ successful: true, data: { manifestPath: process.env.COMPOSIO_RUN_STAGED_FILES_FILE } }));'; |
| 446 | await installRunHelpers({ cliPrefix: [process.execPath, '-e', childScript] }); |
| 447 | expect(await readInstalledFunction('execute')('TEST_UPLOAD', { text: 'plain' })).toMatchObject({ |
| 448 | data: { manifestPath: '' }, |
| 449 | }); |
| 450 | }); |
| 451 | |
| 452 | it("[Given] File and Blob arguments [Then] the spawned `composio execute` gets generated paths, and each one's own name and type beside them", async () => { |
| 453 | // Echoes the `--data` JSON and the staged files it was spawned with, and |
| 454 | // whether each staged file exists. |
| 455 | const childScript = [ |
| 456 | 'const fs = require("node:fs");', |
| 457 | 'const data = JSON.parse(process.argv[process.argv.indexOf("--data") + 1]);', |
| 458 | 'const stagedFiles = JSON.parse(fs.readFileSync(process.env.COMPOSIO_RUN_STAGED_FILES_FILE, "utf8"));', |
| 459 | 'const exists = Object.fromEntries(Object.entries(data).map(([k, v]) => [k, fs.existsSync(v)]));', |
| 460 | 'console.log(JSON.stringify({ successful: true, data: { data, stagedFiles, exists } }));', |
| 461 | ].join('\n'); |
| 462 | // A value the run process inherited must not reach the child. |
| 463 | vi.stubEnv('COMPOSIO_RUN_STAGED_FILES_FILE', '/stale/metadata.json'); |
| 464 | await installRunHelpers({ cliPrefix: [process.execPath, '-e', childScript] }); |
| 465 | const PPTX = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; |
| 466 | |
| 467 | const result = await readInstalledFunction('execute')('CANVA_CREATE_DESIGN_IMPORT_JOB', { |
| 468 | named: new File(['PK'], 'deck.pptx', { type: PPTX }), |
| 469 | sameName: new File(['PK'], 'deck.pptx', { type: PPTX }), |
| 470 | extensionless: new File(['PK'], 'deck', { type: PPTX }), |
| 471 | traversal: new File(['PK'], '../escape.pptx', { type: PPTX }), |
| 472 | untyped: new File(['hi'], 'notes.txt'), |
| 473 | envExample: new File(['EXAMPLE=value'], '.env.example', { type: 'text/plain' }), |
| 474 | privateKeyName: new File(['not a key'], 'keys/id_rsa'), |
| 475 | blob: new Blob(['PK'], { type: PPTX }), |
| 476 | }); |
| 477 | |
| 478 | assert( |
| 479 | Predicate.hasProperty(result, 'data') && |
| 480 | Predicate.hasProperty(result.data, 'data') && |
| 481 | Predicate.isObject(result.data.data) && |
| 482 | Predicate.hasProperty(result.data, 'stagedFiles') |
| 483 | ); |
| 484 | const staged = result.data.data; |
| 485 | const stagedPaths = Object.values(staged).map(String); |
| 486 | // Every File is staged under a generated name, with its extension when it has one… |
| 487 | expect(stagedPaths.map(stagedPath => path.basename(stagedPath))).toEqual([ |
| 488 | expect.stringMatching(/^file-[0-9a-f]{8}\.pptx$/), |
| 489 | expect.stringMatching(/^file-[0-9a-f]{8}\.pptx$/), |
| 490 | expect.stringMatching(/^file-[0-9a-f]{8}$/), |
| 491 | expect.stringMatching(/^file-[0-9a-f]{8}\.pptx$/), |
| 492 | expect.stringMatching(/^file-[0-9a-f]{8}\.txt$/), |
| 493 | expect.stringMatching(/^file-[0-9a-f]{8}\.example$/), |
| 494 | expect.stringMatching(/^file-[0-9a-f]{8}$/), |
| 495 | expect.stringMatching(/^file-[0-9a-f]{8}\.bin$/), |
| 496 | ]); |
| 497 | // …that the child's denylist does not refuse, and that two Files with one name do not share. |
| 498 | expect(stagedPaths.filter(stagedPath => isBlockedSensitiveFileUploadPath(stagedPath))).toEqual( |
| 499 | [] |
| 500 | ); |
| 501 | expect(new Set(stagedPaths).size).toBe(stagedPaths.length); |
| 502 | expect(result.data).toMatchObject({ |
| 503 | exists: Object.fromEntries(Object.keys(staged).map(key => [key, true])), |
| 504 | }); |
| 505 | // The child uploads each one under its own name and type. |
| 506 | expect(result.data.stagedFiles).toEqual({ |
| 507 | [String(staged.named)]: { name: 'deck.pptx', mimetype: PPTX }, |
| 508 | [String(staged.sameName)]: { name: 'deck.pptx', mimetype: PPTX }, |
| 509 | [String(staged.extensionless)]: { name: 'deck', mimetype: PPTX }, |
| 510 | [String(staged.traversal)]: { name: 'escape.pptx', mimetype: PPTX }, |
| 511 | [String(staged.untyped)]: { name: 'notes.txt' }, |
| 512 | [String(staged.envExample)]: { name: '.env.example', mimetype: 'text/plain' }, |
| 513 | [String(staged.privateKeyName)]: { name: 'id_rsa' }, |
| 514 | [String(staged.blob)]: { |
| 515 | name: expect.stringMatching(/^file-[0-9a-f]{8}\.bin$/), |
| 516 | mimetype: PPTX, |
| 517 | }, |
| 518 | }); |
| 519 | }); |
| 520 | |
| 521 | it('[Given] a CLI child exits non-zero without a failed result [Then] the helper rejects with the exit code', async () => { |
| 522 | const childScript = ['console.error("boom");', 'process.exit(2);'].join('\n'); |
| 523 | await installRunHelpers({ cliPrefix: [process.execPath, '-e', childScript] }); |
| 524 | |
| 525 | await expect(readInstalledFunction('search')('test')).rejects.toMatchObject({ |
| 526 | message: expect.stringContaining('failed with exit code 2'), |
| 527 | exitCode: 2, |
| 528 | stderr: 'boom', |
| 529 | }); |
| 530 | }); |
| 531 | |
| 532 | it('[Given] normalized false flags [Then] nested CLI children receive false explicitly', async () => { |
| 533 | vi.stubEnv('COMPOSIO_RUN_ENV_SENTINEL', 'forwarded'); |
| 534 | vi.stubEnv('COMPOSIO_PERF_DEBUG', '1'); |
| 535 | vi.stubEnv('COMPOSIO_TOOL_DEBUG', '1'); |
| 536 | vi.stubEnv('BUN_BE_BUN', '1'); |
| 537 | |
| 538 | const childScript = [ |
| 539 | 'console.log(JSON.stringify({', |
| 540 | ' successful: true,', |
| 541 | ' data: {', |
| 542 | ' sentinel: process.env.COMPOSIO_RUN_ENV_SENTINEL,', |
| 543 | ' perfDebug: process.env.COMPOSIO_PERF_DEBUG,', |
| 544 | ' toolDebug: process.env.COMPOSIO_TOOL_DEBUG,', |
| 545 | ' bunBeBun: process.env.BUN_BE_BUN,', |
| 546 | ' },', |
| 547 | '}));', |
| 548 | ].join('\n'); |
| 549 | |
| 550 | await installRunHelpers({ |
| 551 | cliPrefix: [process.execPath, '-e', childScript], |
| 552 | helperContext: { perfDebug: false, toolDebug: false }, |
| 553 | }); |
| 554 | |
| 555 | const installedGlobals: unknown = globalThis; |
| 556 | assert( |
| 557 | Predicate.hasProperty(installedGlobals, 'search') && |
| 558 | typeof installedGlobals.search === 'function', |
| 559 | 'installRunHelpers() did not install search().' |
| 560 | ); |
| 561 | |
| 562 | await expect(installedGlobals.search('test')).resolves.toMatchObject({ |
| 563 | successful: true, |
| 564 | data: { |
| 565 | sentinel: 'forwarded', |
| 566 | perfDebug: '0', |
| 567 | toolDebug: '0', |
| 568 | bunBeBun: '', |
| 569 | }, |
| 570 | }); |
| 571 | }); |
| 572 | }); |