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 331 lines 14.4 KB
Raw
1 import { describe, expect, test } from 'bun:test';
2 import { Effect } from 'effect';
3 import type { Channel } from '../scripts/sdk-release/channel';
4 import type { Language } from '../scripts/sdk-release/lanes';
5 import {
6 NpmCli,
7 loadOriginalDistributions,
8 npmDistTag,
9 reconcileRegistry as reconcileRegistryEffect,
10 waitForRegistry,
11 type Distribution,
12 } from '../scripts/sdk-release/registry';
13 import { docsPinSpecs } from '../scripts/sdk-release/report';
14 import { CommandError, sha256Hex } from '../scripts/sdk-release/runtime';
15 import { batch, head } from './sdk-release-controller-fixture';
16 import { runOnTestClock, runRelease, type Fetch } from './sdk-release-harness';
17
18 const distribution = async (file: string, language: Language = 'python'): Promise<Distribution> => {
19 const bytes = new TextEncoder().encode(`original ${file}`);
20 return {
21 language,
22 name: language === 'python' ? 'composio' : '@composio/core',
23 version: '1.0.1',
24 file,
25 bytes,
26 sha256: await runRelease(sha256Hex(bytes)),
27 };
28 };
29 const wheel = await distribution('composio-1.0.1-py3-none-any.whl');
30 const sdist = await distribution('composio-1.0.1.tar.gz');
31 const tarball = await distribution('composio-core-1.0.1.tgz', 'typescript');
32 const reconcileRegistry = (files: Distribution[], fetch: Fetch, channel?: Channel) =>
33 runRelease(reconcileRegistryEffect(files, channel), fetch);
34
35 function registry(
36 files: Distribution[],
37 options: { conflict?: boolean; unknownPython?: boolean; newer?: boolean } = {}
38 ) {
39 const reads: string[] = [];
40 return {
41 reads,
42 fetcher: (async (input: string | URL | Request) => {
43 const url = String(input);
44 reads.push(url);
45 const json = (value: unknown, status = 200) =>
46 new Response(JSON.stringify(value), { status });
47 if (
48 url.startsWith('https://files.pythonhosted.org/') ||
49 url.startsWith('https://registry.npmjs.org/files/')
50 ) {
51 const file = files.find(file => url.endsWith(file.file));
52 return new Response(
53 options.conflict ? 'wrong bytes' : file ? new Uint8Array(file.bytes).buffer : undefined,
54 { status: file ? 200 : 404 }
55 );
56 }
57 if (url.startsWith('https://pypi.org/'))
58 return files.some(file => file.language === 'python')
59 ? json({
60 urls: [
61 ...files
62 .filter(file => file.language === 'python')
63 .map(file => ({
64 filename: file.file,
65 url: `https://files.pythonhosted.org/${file.file}`,
66 digests: { sha256: file.sha256 },
67 })),
68 ...(options.unknownPython
69 ? [
70 {
71 filename: 'unreviewed.whl',
72 url: 'https://files.pythonhosted.org/unreviewed.whl',
73 digests: { sha256: 'f'.repeat(64) },
74 },
75 ]
76 : []),
77 ],
78 })
79 : json({}, 404);
80 if (url.endsWith('/latest')) return json({ version: options.newer ? '1.0.2' : '1.0.1' });
81 if (url.startsWith('https://registry.npmjs.org/'))
82 return files.some(file => file.language === 'typescript')
83 ? json({
84 name: tarball.name,
85 version: tarball.version,
86 dist: { tarball: `https://registry.npmjs.org/files/${tarball.file}` },
87 })
88 : json({}, 404);
89 throw new Error(`Unexpected registry fetch ${url}`);
90 }) satisfies Fetch,
91 };
92 }
93
94 describe('registry reconciliation', () => {
95 test('a first run finds every candidate file missing', async () => {
96 const result = await reconcileRegistry([tarball, wheel, sdist], registry([]).fetcher);
97 expect(result.existing).toEqual([]);
98 expect(result.missing.map(file => file.file)).toEqual([tarball.file, wheel.file, sdist.file]);
99 });
100 test('identical published files are accepted and only the missing sdist is left to upload', async () => {
101 const fake = registry([wheel]);
102 const result = await reconcileRegistry([wheel, sdist], fake.fetcher);
103 expect(result.existing.map(file => file.file)).toEqual([wheel.file]);
104 expect(result.missing.map(file => file.file)).toEqual([sdist.file]);
105 expect(result.missing[0].bytes).toEqual(sdist.bytes);
106 expect(fake.reads).toContain(`https://files.pythonhosted.org/${wheel.file}`);
107 });
108 test('a re-run after npm succeeded and PyPI failed leaves only the PyPI files to upload', async () => {
109 const result = await reconcileRegistry([tarball, wheel, sdist], registry([tarball]).fetcher);
110 expect(result.existing.map(file => file.file)).toEqual([tarball.file]);
111 expect(result.missing.map(file => file.file)).toEqual([wheel.file, sdist.file]);
112 expect(result.tagsToRepair).toEqual([]);
113 });
114 test('published bytes that differ from the candidate stop with a conflict naming the file', async () => {
115 await expect(
116 reconcileRegistry([wheel, sdist], registry([wheel], { conflict: true }).fetcher)
117 ).rejects.toThrow(`Registry content conflict: composio@1.0.1/${wheel.file}`);
118 await expect(
119 reconcileRegistry([wheel, sdist], registry([wheel], { unknownPython: true }).fetcher)
120 ).rejects.toThrow('Unreviewed');
121 });
122 test('Python-only operation does not require npm outputs or requests', async () => {
123 const fake = registry([wheel, sdist]);
124 expect((await reconcileRegistry([wheel, sdist], fake.fetcher)).missing).toHaveLength(0);
125 expect(fake.reads.some(url => url.includes('npmjs'))).toBe(false);
126 });
127 test('older retries accept a newer stable tag without requesting a downgrade', async () => {
128 const result = await reconcileRegistry([tarball], registry([tarball], { newer: true }).fetcher);
129 expect(result.tagsToRepair).toEqual([]);
130 });
131 test('a fresh npm upload is polled until visible, then its channel tag is left to move', async () => {
132 const fake = registry([tarball]);
133 let lookups = 0;
134 const lagging: Fetch = async input => {
135 const url = String(input);
136 if (url.endsWith('/latest')) return new Response(JSON.stringify({ version: '1.0.0' }));
137 if (url.startsWith('https://registry.npmjs.org/') && lookups++ === 0)
138 return new Response('{}', { status: 404 });
139 return fake.fetcher(input);
140 };
141 const state = await runOnTestClock(
142 waitForRegistry([tarball], { channelTags: false }),
143 () => lagging
144 );
145 expect(state.missing).toEqual([]);
146 expect(state.tagsToRepair).toEqual([
147 { name: tarball.name, version: tarball.version, tag: 'latest' },
148 ]);
149 });
150 test('a beta moves only npm next forward and never reads or moves latest', async () => {
151 const beta = { ...tarball, version: '1.0.1-beta.8', file: 'composio-core-1.0.1-beta.8.tgz' };
152 const requested: string[] = [];
153 const fetch: Fetch = async input => {
154 const url = String(input);
155 requested.push(url);
156 if (url.endsWith('/next')) return new Response(JSON.stringify({ version: '0.1.36-next.13' }));
157 if (url.endsWith('/1.0.1-beta.8')) return new Response('{}', { status: 404 });
158 throw new Error(`Unexpected registry request: ${url}`);
159 };
160 const state = await reconcileRegistry([beta], fetch, 'beta');
161 expect(state.missing).toEqual([beta]);
162 expect(state.tagsToRepair).toEqual([{ name: beta.name, version: beta.version, tag: 'next' }]);
163 expect(requested.some(url => url.endsWith('/latest'))).toBe(false);
164 });
165 test('a content conflict fails at once instead of waiting for propagation', async () => {
166 const fake = registry([wheel], { conflict: true });
167 await expect(
168 runOnTestClock(waitForRegistry([wheel, sdist]), () => fake.fetcher)
169 ).rejects.toThrow(`Registry content conflict: composio@1.0.1/${wheel.file}`);
170 expect(fake.reads.filter(url => url.startsWith('https://pypi.org/'))).toHaveLength(1);
171 });
172 test('published bytes of the same length but different content are a conflict', async () => {
173 const fake = registry([wheel, sdist]);
174 const sameLength: Fetch = async input => {
175 const response = await fake.fetcher(input);
176 if (!String(input).endsWith(wheel.file)) return response;
177 const bytes = new Uint8Array(await response.arrayBuffer());
178 bytes[0] ^= 0xff;
179 return new Response(bytes);
180 };
181 await expect(reconcileRegistry([wheel, sdist], sameLength)).rejects.toThrow(
182 `Registry content conflict: composio@1.0.1/${wheel.file}`
183 );
184 });
185 });
186
187 describe('waiting out npm’s publish-time scan', () => {
188 const MINUTE = 60_000;
189 const openai = { ...tarball, name: '@composio/openai', file: 'composio-openai-1.0.1.tgz' };
190 const versionUrl = 'https://registry.npmjs.org/%40composio%2Fcore/1.0.1';
191
192 /** npm answers 404 for each version until `served` says it has finished scanning. */
193 const scanning = (served: (reads: number, now: number) => boolean) => {
194 const reads: string[] = [];
195 let clock = () => 0;
196 const fake = registry([tarball]);
197 const fetch = (now: () => number): Fetch => {
198 clock = now;
199 return async input => {
200 const url = String(input);
201 if (url.endsWith('/latest')) return new Response(JSON.stringify({ version: '1.0.0' }));
202 if (url.startsWith('https://registry.npmjs.org/%40')) {
203 reads.push(url);
204 if (!served(reads.filter(read => read === url).length - 1, now()))
205 return new Response('{}', { status: 404 });
206 }
207 return fake.fetcher(input);
208 };
209 };
210 return { reads, fetch, elapsed: () => clock() };
211 };
212
213 test('a version npm serves after 8 polls of 404 is accepted within the deadline', async () => {
214 const npm = scanning(reads => reads >= 8);
215 const state = await runOnTestClock(
216 waitForRegistry([tarball], { channelTags: false }),
217 npm.fetch
218 );
219 expect(state.missing).toEqual([]);
220 expect(npm.reads.filter(url => url === versionUrl)).toHaveLength(9);
221 expect(npm.elapsed()).toBeLessThan(20 * MINUTE);
222 });
223
224 test('a 15-minute scan is waited out', async () => {
225 const npm = scanning((_reads, now) => now >= 15 * MINUTE);
226 const state = await runOnTestClock(
227 waitForRegistry([tarball], { channelTags: false }),
228 npm.fetch
229 );
230 expect(state.missing).toEqual([]);
231 expect(npm.elapsed()).toBeGreaterThanOrEqual(15 * MINUTE);
232 });
233
234 test('a registry error that waiting cannot fix fails at once', async () => {
235 const npm = scanning(() => false);
236 const unauthorized =
237 (now: () => number): Fetch =>
238 async input =>
239 String(input) === versionUrl ? new Response('{}', { status: 401 }) : npm.fetch(now)(input);
240 await expect(
241 runOnTestClock(waitForRegistry([tarball], { channelTags: false }), unauthorized)
242 ).rejects.toThrow(`Registry lookup failed: ${versionUrl} (401)`);
243 expect(npm.elapsed()).toBeLessThan(MINUTE);
244 });
245
246 test('versions still missing at the 20-minute deadline fail naming each one', async () => {
247 const npm = scanning(() => false);
248 await expect(
249 runOnTestClock(waitForRegistry([tarball, openai], { channelTags: false }), npm.fetch)
250 ).rejects.toThrow(
251 `when the registry wait (20 minutes) ended: @composio/core@1.0.1/${tarball.file}, @composio/openai@1.0.1/${openai.file}`
252 );
253 expect(npm.elapsed()).toBeGreaterThanOrEqual(20 * MINUTE);
254 expect(npm.elapsed()).toBeLessThan(22 * MINUTE);
255 });
256 });
257
258 describe('pinned candidate download', () => {
259 const pinned = { run: 41, artifactId: 77, digest: `sha256:${'c'.repeat(64)}` };
260 const listed = (overrides: { digest?: string; run?: number }) => ({
261 id: 77,
262 name: 'sdk-candidate-typescript',
263 digest: overrides.digest ?? pinned.digest,
264 expired: false,
265 created_at: '2026-11-03T00:05:00Z',
266 expires_at: '2099-12-01T00:00:00Z',
267 workflow_run: { id: overrides.run ?? 41, head_sha: head },
268 });
269 test.each([
270 ['a different digest', { digest: `sha256:${'d'.repeat(64)}` }, 'artifact 77 changed'],
271 ['another producer run', { run: 42 }, 'Candidate source or producer does not match'],
272 ])('a pinned artifact with %s is refused before download', async (_name, overrides, message) => {
273 const reads: string[] = [];
274 const fetcher: Fetch = async input => {
275 const path = new URL(String(input)).pathname;
276 reads.push(path);
277 if (path === '/repos/ComposioHQ/composio/actions/artifacts/77')
278 return new Response(JSON.stringify(listed(overrides)));
279 throw new Error(`Unexpected read ${path}`);
280 };
281 await expect(
282 runRelease(
283 loadOriginalDistributions('.', batch, head, { typescript: pinned }, 'typescript'),
284 fetcher
285 )
286 ).rejects.toThrow(message);
287 expect(reads).toEqual(['/repos/ComposioHQ/composio/actions/artifacts/77']);
288 });
289 test('a language without a pinned candidate is refused without any request', async () => {
290 await expect(
291 runRelease(loadOriginalDistributions('.', batch, head, {}, 'typescript'), async () => {
292 throw new Error('no request expected');
293 })
294 ).rejects.toThrow('No approved typescript candidate was pinned');
295 });
296 });
297
298 describe('npm dist-tag moves', () => {
299 // The E403 npm printed when the trusted publisher of @composio/slim lacked the dist-tag permission.
300 const refusal = [
301 'npm failed: npm error code E403',
302 'npm error 403 403 Forbidden - PUT https://registry.npmjs.org/-/package/@composio%2fslim/dist-tags/latest - This trusted publisher cannot manage dist-tags. Ask a package maintainer to enable Allow npm dist-tag.',
303 ].join('\n');
304 test('a dist-tag refusal names the trusted-publisher setting to enable', async () => {
305 const commands: string[][] = [];
306 await expect(
307 runRelease(
308 npmDistTag(['add', '@composio/slim@1.0.1', 'latest'], '@composio/slim').pipe(
309 Effect.provideService(NpmCli, {
310 run: args => {
311 commands.push([...args]);
312 return Effect.fail(new CommandError({ message: refusal, exitCode: 1 }));
313 },
314 })
315 )
316 )
317 ).rejects.toThrow(
318 /@composio\/slim.*Allow npm dist-tag.*Settings → Trusted publishing.*ts\/docs\/internal\/release\.md#npm-trusted-publishing/s
319 );
320 expect(commands).toEqual([['dist-tag', 'add', '@composio/slim@1.0.1', 'latest']]);
321 });
322 });
323
324 test('docs pins move only used packages forward, so an older retry never downgrades them', () => {
325 expect(
326 docsPinSpecs(
327 { '@composio/core': '0.29.0', '@composio/openai': '0.13.2', '@composio/unused': '1.0.0' },
328 { '@composio/core': '^0.28.0', '@composio/openai': '0.13.3', react: '^19' }
329 )
330 ).toEqual(['@composio/core@0.29.0']);
331 });