Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | import { PassThrough } from 'node:stream'; |
| 3 | import { readStdinToEnd } from 'src/services/stdin'; |
| 4 | |
| 5 | const SILENCE_TIMEOUT_MS = 1000; |
| 6 | |
| 7 | /** Marks a read still waiting, so a test can tell a pending read from a resolved one. */ |
| 8 | const PENDING = Symbol('pending'); |
| 9 | |
| 10 | const settledValue = async (read: Promise<string | undefined>) => { |
| 11 | await vi.advanceTimersByTimeAsync(0); |
| 12 | return Promise.race([read, Promise.resolve(PENDING)]); |
| 13 | }; |
| 14 | |
| 15 | describe('readStdinToEnd', () => { |
| 16 | beforeEach(() => { |
| 17 | vi.useFakeTimers(); |
| 18 | }); |
| 19 | |
| 20 | afterEach(() => { |
| 21 | vi.useRealTimers(); |
| 22 | }); |
| 23 | |
| 24 | it('[Given] a pipe that never sends data or closes [Then] resolves undefined once the silence timeout passes', async () => { |
| 25 | const input = new PassThrough(); |
| 26 | const read = readStdinToEnd(input, SILENCE_TIMEOUT_MS); |
| 27 | |
| 28 | await vi.advanceTimersByTimeAsync(SILENCE_TIMEOUT_MS - 1); |
| 29 | expect(await settledValue(read)).toBe(PENDING); |
| 30 | |
| 31 | await vi.advanceTimersByTimeAsync(1); |
| 32 | expect(await settledValue(read)).toBeUndefined(); |
| 33 | expect(input.listenerCount('data')).toBe(0); |
| 34 | expect(input.isPaused()).toBe(true); |
| 35 | }); |
| 36 | |
| 37 | it('[Given] stdin that closes without data [Then] resolves an empty string', async () => { |
| 38 | const input = new PassThrough(); |
| 39 | const read = readStdinToEnd(input, SILENCE_TIMEOUT_MS); |
| 40 | input.end(); |
| 41 | |
| 42 | expect(await settledValue(read)).toBe(''); |
| 43 | }); |
| 44 | |
| 45 | it('[Given] data before the silence timeout and the end of input after it [Then] reads everything', async () => { |
| 46 | const input = new PassThrough(); |
| 47 | const read = readStdinToEnd(input, SILENCE_TIMEOUT_MS); |
| 48 | input.write('{"owner":'); |
| 49 | |
| 50 | await vi.advanceTimersByTimeAsync(SILENCE_TIMEOUT_MS * 3); |
| 51 | expect(await settledValue(read)).toBe(PENDING); |
| 52 | |
| 53 | input.end('"composio"}'); |
| 54 | expect(await settledValue(read)).toBe('{"owner":"composio"}'); |
| 55 | }); |
| 56 | |
| 57 | it('[Given] the first byte arrives after the silence timeout [Then] resolves undefined and leaves the data unread', async () => { |
| 58 | const input = new PassThrough(); |
| 59 | const read = readStdinToEnd(input, SILENCE_TIMEOUT_MS); |
| 60 | |
| 61 | await vi.advanceTimersByTimeAsync(SILENCE_TIMEOUT_MS); |
| 62 | input.end('{"owner":"x"}'); |
| 63 | |
| 64 | expect(await settledValue(read)).toBeUndefined(); |
| 65 | expect(input.read()?.toString()).toBe('{"owner":"x"}'); |
| 66 | }); |
| 67 | |
| 68 | it('[Given] no silence timeout [Then] waits for the end of input however late it arrives', async () => { |
| 69 | const input = new PassThrough(); |
| 70 | const read = readStdinToEnd(input); |
| 71 | |
| 72 | await vi.advanceTimersByTimeAsync(SILENCE_TIMEOUT_MS * 60); |
| 73 | expect(await settledValue(read)).toBe(PENDING); |
| 74 | |
| 75 | input.end('late'); |
| 76 | expect(await settledValue(read)).toBe('late'); |
| 77 | }); |
| 78 | }); |