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 96 lines 3.68 KB
Raw
1 import { describe, expect, test } from 'bun:test';
2 import { verifyPypiProvenance } from '../scripts/sdk-release/provenance';
3 import { verifyPypiFiles, type Distribution } from '../scripts/sdk-release/registry';
4 import { runOnTestClock, type Fetch } from './sdk-release-harness';
5
6 const wheel: Distribution = {
7 language: 'python',
8 name: 'composio',
9 version: '0.31.1b8',
10 file: 'composio-0.31.1b8-py3-none-any.whl',
11 bytes: new Uint8Array(),
12 sha256: '',
13 };
14 const fileUrl = 'https://files.pythonhosted.org/packages/ab/cd/composio-0.31.1b8-py3-none-any.whl';
15 const releasePublisher = {
16 kind: 'GitHub',
17 repository: 'ComposioHQ/composio',
18 workflow: 'sdk.release.yml',
19 environment: 'pypi-release',
20 };
21
22 /**
23 * PyPI's integrity and version metadata endpoints for one wheel, with `publisher` attesting it.
24 * The provenance endpoint first answers each of `before`, then serves the attestation.
25 */
26 const pypi =
27 (publisher: Record<string, string> | undefined, before: ReadonlyArray<number> = []) =>
28 (): Fetch => {
29 const pending = [...before];
30 return async input => {
31 const url = String(input);
32 if (url === `https://pypi.org/integrity/composio/0.31.1b8/${wheel.file}/provenance`) {
33 const status = pending.shift();
34 if (status !== undefined) return new Response('{}', { status });
35 return publisher
36 ? Response.json({ version: 1, attestation_bundles: [{ publisher, attestations: [] }] })
37 : new Response('{}', { status: 404 });
38 }
39 if (url === 'https://pypi.org/pypi/composio/0.31.1b8/json')
40 return Response.json({
41 urls: [{ filename: wheel.file, url: fileUrl, digests: { sha256: '' } }],
42 });
43 throw new Error(`Unexpected PyPI request: ${url}`);
44 };
45 };
46
47 describe('PyPI attestations', () => {
48 test('a file attested by the release workflow in pypi-release is accepted', async () => {
49 await runOnTestClock(verifyPypiProvenance(wheel), pypi(releasePublisher));
50 });
51
52 test.each([
53 ['404 for 10 polls', Array.from({ length: 10 }, () => 404)],
54 ['429', [429]],
55 ])('provenance that answers %s and is then present is accepted', async (_name, before) => {
56 await runOnTestClock(verifyPypiProvenance(wheel), pypi(releasePublisher, before));
57 });
58
59 test('a provenance lookup that stalls is retried, then accepted', async () => {
60 let stalled = false;
61 const stallOnce = (): Fetch => {
62 const served = pypi(releasePublisher)();
63 return async (input, init) => {
64 if (!stalled && String(input).endsWith('/provenance')) {
65 stalled = true;
66 return new Promise<Response>(() => {});
67 }
68 return served(input, init);
69 };
70 };
71 await runOnTestClock(verifyPypiProvenance(wheel), stallOnce);
72 expect(stalled).toBe(true);
73 });
74
75 test.each([
76 ['another repository', { repository: 'someone/composio' }],
77 ['another workflow', { workflow: 'py.release.yml' }],
78 ['another environment', { environment: 'pypi' }],
79 ])('a file attested by %s is refused', async (_name, change) => {
80 await expect(
81 runOnTestClock(verifyPypiProvenance(wheel), pypi({ ...releasePublisher, ...change }))
82 ).rejects.toThrow('is not attested by ComposioHQ/composio sdk.release.yml in pypi-release');
83 });
84
85 test('a file without provenance is refused once the wait runs out', async () => {
86 await expect(runOnTestClock(verifyPypiProvenance(wheel), pypi(undefined))).rejects.toThrow(
87 'No provenance is published at https://pypi.org/integrity/composio'
88 );
89 });
90
91 test('verified files return the URLs PyPI serves, for the signature check', async () => {
92 expect(await runOnTestClock(verifyPypiFiles([wheel]), pypi(releasePublisher))).toEqual([
93 fileUrl,
94 ]);
95 });
96 });