1
+import * as fs from 'node:fs';
2
+import { describe, expect, layer } from '@effect/vitest';
3
+import { vi } from 'vitest';
4
+import { ConfigProvider, Effect } from 'effect';
5
+import { extendConfigProvider } from 'src/services/config';
6
+import { cli, TestLive, MockConsole } from 'test/__utils__';
7
+import { liveEnvConfigProvider } from 'test/__utils__/live-env-config-provider';
8
+
9
+// The encoder companion is missing: the loader fails the way a damaged install
10
+// whose repair download also failed does. Every other companion loads as usual.
11
+vi.mock('src/services/run-companion-modules', async importOriginal => {
12
+ const actual = await importOriginal<typeof import('src/services/run-companion-modules')>();
13
+ const { Effect } = await import('effect');
14
+ return {
15
+ ...actual,
16
+ loadInstalledCompanionModule: (baseName: string) =>
17
+ baseName === 'execute-output-encoder-runtime'
18
+ ? Effect.fail(
19
+ new actual.RunCompanionRepairError({
20
+ message: "Unable to restore the CLI's bundled support files.",
21
+ })
22
+ )
23
+ : actual.loadInstalledCompanionModule(baseName),
24
+ };
25
+});
26
+
27
+vi.hoisted(() => {
28
+ delete process.env.CI;
29
+});
30
+
31
+const testArtifactConfigProvider = liveEnvConfigProvider.pipe(
32
+ ConfigProvider.mapInput(configPath =>
33
+ configPath.map(segment =>
34
+ segment === 'SESSION_DIR'
35
+ ? 'CACHE_DIR'
36
+ : typeof segment === 'string'
37
+ ? `UNSET_${segment}`
38
+ : segment
39
+ )
40
+ )
41
+);
42
+
43
+const largeOutputConfigProvider = ConfigProvider.fromEnvRecord({
44
+ COMPOSIO_USER_API_KEY: 'test_api_key',
45
+}).pipe(ConfigProvider.orElse(testArtifactConfigProvider), extendConfigProvider);
46
+
47
+const parseLastJson = (lines: ReadonlyArray<string>): Record<string, unknown> => {
48
+ for (let i = lines.length - 1; i >= 0; i -= 1) {
49
+ const line = lines[i];
50
+ if (!line) continue;
51
+ try {
52
+ return JSON.parse(line) as Record<string, unknown>;
53
+ } catch {
54
+ // keep searching for the last JSON line
55
+ }
56
+ }
57
+ throw new Error('Expected JSON output but none found');
58
+};
59
+
60
+describe('CLI: composio execute without the encoder companion', () => {
61
+ layer(
62
+ TestLive({
63
+ baseConfigProvider: largeOutputConfigProvider,
64
+ fixture: 'global-test-user-id',
65
+ stdin: { isTTY: true, data: '' },
66
+ toolsExecutor: {
67
+ respondWith: {
68
+ data: { content: 'token '.repeat(20_000) },
69
+ error: null,
70
+ successful: true,
71
+ logId: 'log_encoder_missing',
72
+ },
73
+ },
74
+ })
75
+ )('[Given] a large response [Then] it estimates the token count instead of failing', it => {
76
+ it.effect('stores the payload with a byte-based token estimate', () =>
77
+ Effect.gen(function* () {
78
+ yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
79
+ const lines = yield* MockConsole.getLines({ stripAnsi: true });
80
+ const output = parseLastJson(lines) as {
81
+ successful: boolean;
82
+ storedInFile: boolean;
83
+ tokenCount: number;
84
+ outputFilePath: string;
85
+ };
86
+
87
+ expect(output.successful).toBe(true);
88
+ expect(output.storedInFile).toBe(true);
89
+ const storedJson = fs.readFileSync(output.outputFilePath, 'utf8');
90
+ expect(output.tokenCount).toBe(Math.ceil(Buffer.byteLength(storedJson, 'utf8') / 4));
91
+
92
+ fs.rmSync(output.outputFilePath.slice(0, output.outputFilePath.lastIndexOf('/')), {
93
+ recursive: true,
94
+ force: true,
95
+ });
96
+ })
97
+ );
98
+ });
99
+});