1
import { describe, expect, layer } from '@effect/vitest';
2
import { ConfigProvider, Effect, Fiber, Latch, Layer } from 'effect';
3
+import * as FileSystem from 'effect/FileSystem';
4
+import * as Path from 'effect/Path';
5
import { TestClock } from 'effect/testing';
6
import { ListenCommandError } from 'src/commands/listen/listen.errors';
7
import type { ConnectedAccountItem } from 'src/models/connected-accounts';
43
})
44
);
45
46
+// Formatting fixtures: standalone controls (BEL, DEL, NEL), CR/LF, a tab, a literal backslash,
47
+// combining and astral Unicode, in a value, a nested key and a top-level key.
48
+const formattingEvent = {
49
+ id: 'evt_formatting_1',
50
+ type: 'composio.connected_account.expired',
51
+ timestamp: '2026-04-06T18:00:00.000Z',
52
+ 'extra\u0085key': true,
53
+ metadata: {
54
+ project_id: 'consumer_project_id_test',
55
+ org_id: 'org_test',
56
+ 'label\u007f': 'value\u0085',
57
+ },
58
+ data: {
59
+ id: 'con_expired_1',
60
+ note: 'line one\r\nline two\ttabbed\u0007 C:\\temp e\u0301 \u{1f600}',
61
+ },
62
+};
63
+
64
+/** Any C0, DEL or C1 code point but the line feed that ends a record. */
65
+const dataControl = /[\u0000-\u0009\u000b-\u001f\u007f-\u009f]/u;
66
+/** SGR styling, the only escape sequence the CLI writes on its own. */
67
+const sgr = /\u001b\[[0-9;]*m/gu;
68
+
69
+/** The payload file the `event:` record names, and the `events.jsonl` index next to it. */
70
+const readSavedEvent = (stdoutLines: ReadonlyArray<string>) =>
71
+ Effect.gen(function* () {
72
+ const fs = yield* FileSystem.FileSystem;
73
+ const path = yield* Path.Path;
74
+ const eventLine = stdoutLines.find(line => line.startsWith('event: '));
75
+ expect(eventLine).toBeDefined();
76
+ const payloadPath = (eventLine ?? '').slice('event: '.length);
77
+ const payload: unknown = JSON.parse(yield* fs.readFileString(payloadPath));
78
+ const index = (yield* fs.readFileString(path.join(path.dirname(payloadPath), 'events.jsonl')))
79
+ .trim()
80
+ .split('\n')
81
+ .map(line => JSON.parse(line) as { readonly file_path: string });
82
+ return { payloadPath, payload, index };
83
+ });
84
+
85
describe('CLI: composio listen', () => {
86
layer(TestLive({ baseConfigProvider: testConfigProvider, fixture: 'global-test-user-id' }))(
87
'[Given] the realtime subscription fails',
227
);
228
});
229
230
+ layer(
231
+ TestLive({
232
+ baseConfigProvider: testConfigProvider,
233
+ fixture: 'global-test-user-id',
234
+ realtimeData: { events: [formattingEvent] },
235
+ })
236
+ )('[Given] an event whose fields carry controls and line breaks', it => {
237
+ it.effect(
238
+ '[When] a value is projected with --debug [Then] each record is one escaped line and the payload file keeps the event',
239
+ () =>
240
+ Effect.gen(function* () {
241
+ yield* enableListen;
242
+ const terminal = redirectedTerminalUI();
243
+ yield* cli([
244
+ 'listen',
245
+ 'composio.connected_account.expired',
246
+ '--max-events',
247
+ '1',
248
+ '--debug',
249
+ '--stream',
250
+ '.data.note',
251
+ ]).pipe(Effect.provide(terminal.layer));
252
+
253
+ const stdout = terminal.stdout();
254
+ expect(stdout.endsWith('\n')).toBe(true);
255
+ const lines = stdout.slice(0, -1).split('\n');
256
+ expect(stdout).not.toMatch(dataControl);
257
+ expect(lines.map(line => line.split(/[ :]/u)[0])).toEqual([
258
+ 'listening',
259
+ '[debug]',
260
+ '[debug]',
261
+ '[debug]',
262
+ '[debug]',
263
+ 'event',
264
+ 'stream',
265
+ ]);
266
+ expect(lines).toContain(
267
+ '[debug] raw event keys: id, type, timestamp, extra\\u0085key, metadata, data'
268
+ );
269
+ expect(lines.at(-1)).toBe(
270
+ 'stream: line one\\u000d\\u000aline two\\u0009tabbed\\u0007 C:\\temp e\u0301 \u{1f600}'
271
+ );
272
+
273
+ // The preview escapes DEL and C1 in nested keys and values and still decodes to the
274
+ // event.
275
+ const previewPrefix = '[debug] raw event (truncated): ';
276
+ const preview = lines.find(line => line.startsWith(previewPrefix)) ?? '';
277
+ expect(preview).toContain('"label\\u007f":"value\\u0085"');
278
+ expect(JSON.parse(preview.slice(previewPrefix.length))).toEqual(formattingEvent);
279
+
280
+ const { payloadPath, payload, index } = yield* readSavedEvent(lines);
281
+ expect(payload).toEqual(formattingEvent);
282
+ // The index is appended across runs; this run's entry is the last one.
283
+ expect(index.at(-1)?.file_path).toBe(payloadPath);
284
+ // stderr is redirected: no decoration, so no copy of the records.
285
+ expect(terminal.stderr()).toBe('');
286
+ })
287
+ );
288
+
289
+ it.effect(
290
+ '[When] stderr is a terminal and the whole event streams [Then] both sinks get the same escaped record',
291
+ () =>
292
+ Effect.gen(function* () {
293
+ yield* enableListen;
294
+ const terminal = redirectedTerminalUI({ decoratedStderr: true });
295
+ yield* cli([
296
+ 'listen',
297
+ 'composio.connected_account.expired',
298
+ '--max-events',
299
+ '1',
300
+ '--stream',
301
+ ]).pipe(Effect.provide(terminal.layer));
302
+
303
+ const lines = terminal.stdout().slice(0, -1).split('\n');
304
+ const streamLines = lines.filter(line => line.startsWith('stream: '));
305
+ expect(streamLines).toHaveLength(1);
306
+ const record = streamLines[0] ?? '';
307
+ expect(record).not.toMatch(dataControl);
308
+ expect(JSON.parse(record.slice('stream: '.length))).toEqual(formattingEvent);
309
+
310
+ // Raw capture: the decoration renders, carries the same record on one line, and
311
+ // writes no escape sequence other than styling.
312
+ const stderr = terminal.stderr();
313
+ expect(stderr).toContain('│');
314
+ expect(stderr.replace(sgr, '')).not.toMatch(dataControl);
315
+ expect(stderr.replace(sgr, '')).not.toContain('\u001b');
316
+ expect(
317
+ stderr
318
+ .replace(sgr, '')
319
+ .split('\n')
320
+ .filter(line => line.includes(record))
321
+ ).toHaveLength(1);
322
+ expect(stderr).toContain('Stopped after receiving 1 event.');
323
+
324
+ const { payload } = yield* readSavedEvent(lines);
325
+ expect(payload).toEqual(formattingEvent);
326
+ })
327
+ );
328
+ });
329
+
330
layer(
331
TestLive({
332
baseConfigProvider: testConfigProvider,