@devries-cameron20 / composio Mirror · read-only
Fork 0

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, 03:37 UTC

next
ts 231 lines 8.14 KB
Raw
1 import { afterAll, expect, test } from 'bun:test';
2 import { adoptApprovedCandidate } from '../scripts/sdk-release/candidates';
3 import { formatJson, run, sha256Hex } from '../scripts/sdk-release/runtime';
4 import {
5 json,
6 readText,
7 runRelease,
8 temporaryDirectories,
9 writeText,
10 type Fetch,
11 } from './sdk-release-harness';
12
13 const directories = temporaryDirectories('sdk-approved-candidate-');
14 afterAll(() => directories.cleanup());
15
16 const head = 'a'.repeat(40);
17 const wheel = 'composio-1.0.1-py3-none-any.whl';
18 const wheelText = 'approved wheel bytes';
19
20 /** A real candidate archive as the release-PR run uploaded it; `listed` is the manifest's text. */
21 const approvedArchive = async (
22 listed = wheelText,
23 identity: Record<string, unknown> = { producerRun: 41 }
24 ) => {
25 const directory = await directories.create();
26 const listedBytes = new TextEncoder().encode(listed);
27 await writeText(`${directory}/${wheel}`, wheelText);
28 await writeText(
29 `${directory}/candidate.json`,
30 formatJson({
31 sourceSha: head,
32 language: 'python',
33 ...identity,
34 files: [
35 { name: wheel, sha256: await runRelease(sha256Hex(listedBytes)), size: listedBytes.length },
36 ],
37 })
38 );
39 const zip = `${directory}/candidate.zip`;
40 await runRelease(
41 run('zip', ['-j', '-q', zip, `${directory}/${wheel}`, `${directory}/candidate.json`])
42 );
43 const bytes = new Uint8Array(await Bun.file(zip).arrayBuffer());
44 return { bytes, digest: `sha256:${await runRelease(sha256Hex(bytes))}` };
45 };
46
47 const releaseRun = (id: number, conclusion = 'success') => ({
48 id,
49 path: '.github/workflows/sdk.release.candidates.yml',
50 head_branch: 'sdk-release/batch-8',
51 head_sha: head,
52 event: 'pull_request',
53 repository: { full_name: 'ComposioHQ/composio' },
54 status: 'completed',
55 conclusion,
56 created_at: '2026-11-03T00:00:00Z',
57 run_attempt: 1,
58 html_url: `https://github.com/ComposioHQ/composio/actions/runs/${id}`,
59 });
60
61 const fakeGitHub =
62 (
63 archive: { bytes: Uint8Array<ArrayBuffer>; digest: string },
64 runs: unknown[],
65 expired = false,
66 build: {
67 createdAt?: string;
68 conclusion?: string;
69 completedAt?: string;
70 smokeOnlyRerun?: boolean;
71 } = {}
72 ): Fetch =>
73 async input => {
74 const url = new URL(String(input));
75 if (url.hostname === 'artifacts.test') return new Response(archive.bytes);
76 const path = url.pathname.replace('/repos/ComposioHQ/composio', '');
77 if (path === '/actions/workflows/sdk.release.candidates.yml/runs')
78 return json({ workflow_runs: runs });
79 if (path === '/actions/runs/41/jobs') {
80 const buildJob = {
81 name: 'SDK candidate (python)',
82 status: 'completed',
83 conclusion: build.conclusion ?? 'success',
84 started_at: '2026-11-03T00:01:00Z',
85 completed_at: build.completedAt ?? '2026-11-03T00:10:00Z',
86 steps: [
87 {
88 name: 'Retain candidate bytes for publication',
89 conclusion: 'success',
90 started_at: '2026-11-03T00:04:00Z',
91 completed_at: '2026-11-03T00:06:00Z',
92 },
93 ],
94 };
95 const smoke = (conclusion: string, startedAt: string, completedAt: string) => ({
96 name: 'SDK candidate smoke (python)',
97 status: 'completed',
98 conclusion,
99 started_at: startedAt,
100 completed_at: completedAt,
101 });
102 if (!build.smokeOnlyRerun)
103 return json({
104 jobs: [buildJob, smoke('success', '2026-11-03T00:11:00Z', '2026-11-03T00:20:00Z')],
105 });
106 // "Re-run failed jobs" on the smoke job: the latest attempt holds only the smoke job.
107 const latest = [smoke('success', '2026-11-03T01:00:00Z', '2026-11-03T01:10:00Z')];
108 return json({
109 jobs:
110 url.searchParams.get('filter') === 'latest'
111 ? latest
112 : [
113 buildJob,
114 smoke('failure', '2026-11-03T00:11:00Z', '2026-11-03T00:20:00Z'),
115 ...latest,
116 ],
117 });
118 }
119 if (path === '/actions/runs/41/artifacts')
120 return json({
121 artifacts: [
122 {
123 id: 77,
124 name: 'sdk-candidate-python',
125 digest: archive.digest,
126 expired,
127 created_at: build.createdAt ?? '2026-11-03T00:05:00Z',
128 expires_at: '2099-12-01T00:00:00Z',
129 workflow_run: { id: 41, head_sha: head },
130 },
131 ],
132 });
133 if (path === '/actions/artifacts/77/zip')
134 return new Response(null, {
135 status: 302,
136 headers: { location: 'https://artifacts.test/77.zip' },
137 });
138 throw new Error(`Unexpected read ${path}`);
139 };
140
141 test('a source-tag run copies the newest approved release-PR candidate byte for byte', async () => {
142 const archive = await approvedArchive();
143 const target = `${await directories.create()}/python`;
144 const approved = await runRelease(
145 adoptApprovedCandidate(head, 'python', target),
146 fakeGitHub(archive, [releaseRun(40), releaseRun(41), releaseRun(42, 'failure')])
147 );
148 expect(approved).toEqual({ run: 41, artifactId: 77, digest: archive.digest });
149 expect(await readText(`${target}/${wheel}`)).toBe(wheelText);
150 expect(await Bun.file(`${target}/candidate.json`).exists()).toBe(false);
151 });
152
153 test('approved files that differ from their recorded checksums are not adopted', async () => {
154 const archive = await approvedArchive('different wheel bytes');
155 const target = `${await directories.create()}/python`;
156 await expect(
157 runRelease(
158 adoptApprovedCandidate(head, 'python', target),
159 fakeGitHub(archive, [releaseRun(41)])
160 )
161 ).rejects.toThrow(`Candidate checksum mismatch: ${wheel}`);
162 });
163
164 test.each<
165 [string, Parameters<typeof fakeGitHub>[1], boolean, Parameters<typeof fakeGitHub>[3], string]
166 >([
167 [
168 'no successful release-PR run',
169 [releaseRun(41, 'failure')],
170 false,
171 {},
172 'No successful release-PR run built the approved python candidate',
173 ],
174 ['an expired artifact', [releaseRun(41)], true, {}, 'Original candidate unavailable'],
175 [
176 'an artifact created after the upload step',
177 [releaseRun(41)],
178 false,
179 { createdAt: '2026-11-03T00:08:00Z' },
180 'sdk-candidate-python was created at 2026-11-03T00:08:00Z, outside its upload step',
181 ],
182 [
183 'an artifact created two seconds after its upload step ended',
184 [releaseRun(41)],
185 false,
186 { createdAt: '2026-11-03T00:06:02Z' },
187 'sdk-candidate-python was created at 2026-11-03T00:06:02Z, outside its upload step',
188 ],
189 [
190 'a build job that did not succeed',
191 [releaseRun(41)],
192 false,
193 { conclusion: 'skipped' },
194 'The SDK candidate (python) job did not upload a candidate',
195 ],
196 ])('refuses to adopt the candidate of %s', async (_name, runs, expired, build, message) => {
197 const archive = await approvedArchive();
198 const target = `${await directories.create()}/python`;
199 await expect(
200 runRelease(
201 adoptApprovedCandidate(head, 'python', target),
202 fakeGitHub(archive, runs, expired, build)
203 )
204 ).rejects.toThrow(message);
205 expect(await Bun.file(`${target}/${wheel}`).exists()).toBe(false);
206 });
207
208 // GitHub reports both times to the second, so a real artifact can land on either boundary second.
209 test.each(['2026-11-03T00:03:59Z', '2026-11-03T00:06:00Z', '2026-11-03T00:06:01Z'])(
210 'adopts an artifact created at %s, within a second of its 00:04:00–00:06:00 upload step',
211 async createdAt => {
212 const archive = await approvedArchive();
213 const target = `${await directories.create()}/python`;
214 const approved = await runRelease(
215 adoptApprovedCandidate(head, 'python', target),
216 fakeGitHub(archive, [releaseRun(41)], false, { createdAt })
217 );
218 expect(approved).toEqual({ run: 41, artifactId: 77, digest: archive.digest });
219 }
220 );
221
222 test('a smoke-only re-run attempt still adopts the candidate its first attempt built', async () => {
223 const archive = await approvedArchive();
224 const target = `${await directories.create()}/python`;
225 const approved = await runRelease(
226 adoptApprovedCandidate(head, 'python', target),
227 fakeGitHub(archive, [{ ...releaseRun(41), run_attempt: 2 }], false, { smokeOnlyRerun: true })
228 );
229 expect(approved).toEqual({ run: 41, artifactId: 77, digest: archive.digest });
230 expect(await readText(`${target}/${wheel}`)).toBe(wheelText);
231 });