Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 185 lines 6.74 KB
Raw
1 /**
2 * Upgrade faults: `composio upgrade` against a fake GitHub that publishes
3 * releases with something wrong. The invariant in every row is that a failed
4 * upgrade leaves the installed executable untouched and runnable.
5 *
6 * The successful replacement itself is covered by `../upgrade`, which hands
7 * the CLI a local bundle; here the release metadata and the download are real
8 * HTTP, and only the archive bytes are not a real build.
9 */
10
11 import { afterAll, afterEach, beforeAll, describe, expect, it } from 'bun:test';
12 import type { Release } from '@e2e-tests/cli-fake-backend';
13 import { startCliRig, type CliRig, type CommandResult } from '@e2e-tests/cli-fake-backend/rig';
14 import { clean, RIG_TIMEOUT_MS, TEST_TIMEOUT_MS, writeDebugLog } from './support';
15
16 const EXECUTABLE = '/usr/local/bin/composio';
17 const TAG = '@composio/cli@2.0.0';
18 const NOT_A_ZIP = new TextEncoder().encode('not a zip archive\n');
19 const sha256 = (bytes: Uint8Array) => new Bun.CryptoHasher('sha256').update(bytes).digest('hex');
20 const text = (value: string) => new TextEncoder().encode(value);
21
22 describe('upgrade faults', () => {
23 let rig: CliRig;
24 let archive: string;
25 let fingerprintBefore: string;
26
27 /** Inode, size, and mtime: any replacement or rewrite of the executable changes one of them. */
28 const fingerprint = async () => (await rig.run(`stat -c '%i %s %Y' ${EXECUTABLE}`)).stdout.trim();
29
30 const publish = (...releases: Release[]) => {
31 rig.fake.world.releases.length = 0;
32 rig.fake.world.releases.push(...releases);
33 };
34
35 const expectUntouched = async (result: CommandResult) => {
36 expect(result.stdout).toBe('');
37 expect(await fingerprint()).toBe(fingerprintBefore);
38 const version = await rig.run('composio version');
39 expect(version.exitCode).toBe(0);
40 expect(version.stdout.trim()).toMatch(/^\d+\.\d+\.\d+/);
41 };
42
43 const downloads = (result: CommandResult) =>
44 result.requests.filter(entry => entry.route === 'github.releases.download');
45
46 beforeAll(async () => {
47 rig = await startCliRig({ label: 'upgrade' });
48 const machine = (await rig.run('uname -m')).stdout.trim();
49 archive = `composio-linux-${machine === 'aarch64' ? 'aarch64' : 'x64'}.zip`;
50 fingerprintBefore = await fingerprint();
51 }, RIG_TIMEOUT_MS);
52
53 afterEach(() => rig.fake.faults.clear());
54
55 afterAll(async () => {
56 await writeDebugLog(rig, import.meta.dir, 'upgrade');
57 await rig.stop();
58 });
59
60 it(
61 'no asset for this platform: fails before downloading anything',
62 async () => {
63 publish({
64 tag: TAG,
65 prerelease: false,
66 assets: [{ name: 'composio-darwin-aarch64.zip', read: () => NOT_A_ZIP }],
67 });
68 const result = await rig.run('composio upgrade');
69 expect(result.exitCode).toBe(1);
70 expect(clean(result.stderr)).toContain(`No stable CLI releases found with ${archive}`);
71 expect(downloads(result)).toEqual([]);
72 await expectUntouched(result);
73 },
74 TEST_TIMEOUT_MS
75 );
76
77 it(
78 'checksum mismatch: the archive is rejected before extraction',
79 async () => {
80 publish({
81 tag: TAG,
82 prerelease: false,
83 assets: [
84 { name: archive, read: () => NOT_A_ZIP },
85 { name: 'checksums.txt', read: () => text(`${'0'.repeat(64)} ${archive}\n`) },
86 ],
87 });
88 const result = await rig.run('composio upgrade');
89 expect(result.exitCode).toBe(1);
90 const stderr = clean(result.stderr);
91 expect(stderr).toContain(`Checksum mismatch for ${archive}`);
92 expect(stderr).toContain(sha256(NOT_A_ZIP));
93 expect(stderr).not.toContain('Failed to extract');
94 await expectUntouched(result);
95 },
96 TEST_TIMEOUT_MS
97 );
98
99 it(
100 'a corrupt archive with a matching checksum: fails at extraction',
101 async () => {
102 publish({
103 tag: TAG,
104 prerelease: false,
105 assets: [
106 { name: archive, read: () => NOT_A_ZIP },
107 { name: 'checksums.txt', read: () => text(`${sha256(NOT_A_ZIP)} ${archive}\n`) },
108 ],
109 });
110 const result = await rig.run('composio upgrade');
111 expect(result.exitCode).toBe(1);
112 expect(clean(result.stderr)).toContain('Failed to extract zip archive');
113 await expectUntouched(result);
114 },
115 TEST_TIMEOUT_MS
116 );
117
118 it(
119 'a download cut off mid-body: fails without a second attempt',
120 async () => {
121 publish({ tag: TAG, prerelease: false, assets: [{ name: archive, read: () => NOT_A_ZIP }] });
122 rig.fake.faults.inject({
123 route: 'github.releases.download',
124 fault: { kind: 'reset-mid-body' },
125 });
126 const result = await rig.run('composio upgrade');
127 expect(result.exitCode).toBe(1);
128 expect(clean(result.stderr)).toContain('Failed to read downloaded binary');
129 expect(downloads(result)).toHaveLength(1);
130 await expectUntouched(result);
131 },
132 TEST_TIMEOUT_MS
133 );
134
135 it(
136 'a pinned version that was never released: names the tag it looked for',
137 async () => {
138 publish({ tag: TAG, prerelease: false, assets: [{ name: archive, read: () => NOT_A_ZIP }] });
139 const result = await rig.run('composio upgrade 9.9.9');
140 expect(result.exitCode).toBe(1);
141 expect(clean(result.stderr)).toContain(
142 'Failed to fetch tags/@composio/cli@9.9.9 release from GitHub'
143 );
144 expect(downloads(result)).toEqual([]);
145 await expectUntouched(result);
146 },
147 TEST_TIMEOUT_MS
148 );
149
150 it(
151 'a pinned version: downloads that release, not the latest one',
152 async () => {
153 const older = '@composio/cli@1.5.0';
154 publish(
155 { tag: TAG, prerelease: false, assets: [{ name: archive, read: () => NOT_A_ZIP }] },
156 { tag: older, prerelease: false, assets: [{ name: archive, read: () => NOT_A_ZIP }] }
157 );
158 const result = await rig.run('composio upgrade 1.5.0');
159 // The archive is not a real build, so the upgrade still fails; which release it fetched is the point.
160 expect(result.exitCode).toBe(1);
161 expect(downloads(result).map(entry => decodeURIComponent(entry.path))).toEqual([
162 `/ComposioHQ/composio/releases/download/${older}/${archive}`,
163 ]);
164 await expectUntouched(result);
165 },
166 TEST_TIMEOUT_MS
167 );
168
169 it(
170 'already on the latest release: succeeds without downloading',
171 async () => {
172 publish({ tag: TAG, prerelease: false, assets: [{ name: archive, read: () => NOT_A_ZIP }] });
173 // A packaged install records its release next to the executable.
174 await rig.journey.writeFile('/usr/local/bin/release-tag.txt', `${TAG}\n`);
175 const result = await rig.run('composio upgrade');
176 await rig.run('rm -f /usr/local/bin/release-tag.txt');
177
178 expect(result.exitCode).toBe(0);
179 expect(result.stdout).toBe('');
180 expect(downloads(result)).toEqual([]);
181 expect(await fingerprint()).toBe(fingerprintBefore);
182 },
183 TEST_TIMEOUT_MS
184 );
185 });