@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 232 lines 8.74 KB
Raw
1 import { describe, expect, test } from 'bun:test';
2 import { Effect, Option } from 'effect';
3 import { executeReleaseRequest, type ReleaseRequestContext } from '../scripts/sdk-release/batch';
4 import type { SdkReleasePlan } from '../scripts/sdk-release/plan';
5 import { parseReleaseRequest, type ReleaseRequest } from '../scripts/sdk-release/request';
6 import { runRelease, type Fetch } from './sdk-release-harness';
7
8 const sha = 'a'.repeat(40);
9 const context = {
10 actor: 'member',
11 runId: '21',
12 sourceSha: sha,
13 issueNumber: 7,
14 appSlug: 'sdk-release-app',
15 };
16 const plan: SdkReleasePlan = {
17 schemaVersion: 1,
18 inputFingerprint: 'frozen',
19 choices: { typescript: 'auto', python: 'auto', channel: 'stable' },
20 recommended: [],
21 changesets: [],
22 retainedChangesets: [],
23 releases: [
24 {
25 name: '@composio/python-sdk',
26 oldVersion: '0.31.0',
27 newVersion: '0.31.1',
28 type: 'patch',
29 changesets: [],
30 language: 'python',
31 },
32 ],
33 };
34 const request: ReleaseRequest = { channel: 'stable', typescript: 'auto', python: 'auto' };
35 const planned = (value: SdkReleasePlan) => () => Effect.succeed(value);
36 const failing = (message: string) => () =>
37 Effect.sync((): SdkReleasePlan => {
38 throw new Error(message);
39 });
40 const execute = (
41 fetch: Fetch,
42 requestContext: ReleaseRequestContext,
43 plan: () => Effect.Effect<SdkReleasePlan>,
44 parse: Effect.Effect<ReleaseRequest, string> = Effect.succeed(request)
45 ) =>
46 runRelease(
47 Effect.map(executeReleaseRequest(parse, requestContext, plan), result => result.outcome),
48 fetch
49 );
50
51 const openRelease = {
52 number: 9,
53 html_url: 'https://github.com/ComposioHQ/composio/pull/9',
54 state: 'open',
55 user: { login: 'sdk-release-app[bot]' },
56 head: { sha, ref: 'sdk-release/batch-8', repo: { full_name: 'ComposioHQ/composio' } },
57 base: { ref: 'next' },
58 };
59
60 function client(
61 options: {
62 membership?: 'active' | 'pending' | 'absent' | 'error';
63 /** Head branch of a release PR already open into next. */
64 open?: string;
65 /** The release plan committed on next, and whether its tag exists. */
66 committed?: { id: string; tagged: boolean };
67 } = {}
68 ) {
69 const calls: { method: string; path: string; body?: unknown }[] = [];
70 const fetcher: Fetch = async (input, init) => {
71 const url = new URL(String(input));
72 const method = init?.method ?? 'GET';
73 const path = url.pathname + url.search;
74 calls.push({ method, path, ...(init?.body ? { body: JSON.parse(String(init.body)) } : {}) });
75 const respond = (data: unknown, status = 200) => new Response(JSON.stringify(data), { status });
76 if (path === '/orgs/ComposioHQ/memberships/member') {
77 if (options.membership === 'absent') return respond({}, 404);
78 if (options.membership === 'error') return respond({}, 503);
79 return respond({ state: options.membership ?? 'active' });
80 }
81 if (path.includes('/pulls?'))
82 return respond(
83 options.open ? [{ ...openRelease, head: { ...openRelease.head, ref: options.open } }] : []
84 );
85 if (path.startsWith('/repos/ComposioHQ/composio/contents/.github/sdk-release-plan.json'))
86 return options.committed
87 ? new Response(JSON.stringify({ id: options.committed.id }))
88 : respond({}, 404);
89 if (path === `/repos/ComposioHQ/composio/git/ref/tags/sdk-release/${options.committed?.id}`)
90 return options.committed?.tagged
91 ? respond({ object: { type: 'commit', sha } })
92 : respond({}, 404);
93 if (method === 'POST' && path === '/repos/ComposioHQ/composio/issues')
94 return respond({ number: 31 }, 201);
95 if (path.endsWith('/issues/7/comments')) return respond({ id: 99 });
96 throw new Error(`Unexpected request: ${method} ${path}`);
97 };
98 const writes = () => calls.filter(call => call.method !== 'GET');
99 return { fetcher, writes };
100 }
101
102 describe('release request parsing', () => {
103 test('the issue form and a dispatch read into the same request', () => {
104 const issue = {
105 issue: { body: '### Channel\n\nbeta\n\n### TypeScript\n\nskip\n\n### Python\n\npatch\n' },
106 };
107 expect(parseReleaseRequest('issues', issue)).toEqual({
108 channel: 'beta',
109 typescript: 'skip',
110 python: 'patch',
111 });
112 expect(
113 parseReleaseRequest('workflow_dispatch', { inputs: { typescript: 'skip', python: 'patch' } })
114 ).toEqual({ channel: 'stable', typescript: 'skip', python: 'patch' });
115 expect(parseReleaseRequest('workflow_dispatch', { inputs: {} })).toEqual(request);
116 });
117 test('a hand-written issue body and an unknown bump are refused', () => {
118 expect(() => parseReleaseRequest('issues', { issue: { body: 'please release' } })).toThrow(
119 'Use the SDK release request issue form'
120 );
121 expect(() =>
122 parseReleaseRequest('workflow_dispatch', { inputs: { typescript: 'huge' } })
123 ).toThrow();
124 });
125 });
126
127 describe('SDK release member request', () => {
128 for (const membership of ['pending', 'absent', 'error'] as const) {
129 test(`${membership} membership fails before planning or any write`, async () => {
130 const { fetcher, writes } = client({ membership });
131 let planCalls = 0;
132 await expect(
133 execute(fetcher, context, () =>
134 Effect.sync(() => {
135 planCalls += 1;
136 return plan;
137 })
138 )
139 ).rejects.toThrow();
140 expect(planCalls).toBe(0);
141 expect(writes()).toEqual([]);
142 });
143 }
144
145 test('an active member gets a batch frozen at the source, and nothing is posted', async () => {
146 const { fetcher, writes } = client();
147 const outcome = await execute(fetcher, context, planned(plan));
148 expect(outcome).toMatchObject({
149 _tag: 'Planned',
150 batch: { id: '21', inputBase: sha, requestedBy: 'member', issue: 7, plan },
151 });
152 expect(writes()).toEqual([]);
153 });
154
155 test('a planning failure fails the job without posting; the status job reports it', async () => {
156 const { fetcher, writes } = client();
157 await expect(execute(fetcher, context, failing('Invalid Changesets config'))).rejects.toThrow(
158 'Invalid Changesets config'
159 );
160 expect(writes()).toEqual([]);
161 });
162
163 test("a member's unreadable form needs input and plans nothing", async () => {
164 const { fetcher } = client();
165 expect(
166 await execute(fetcher, context, failing('must not plan'), Effect.fail('Use the form'))
167 ).toEqual({ _tag: 'NeedsInput', reason: 'Use the form' });
168 });
169
170 test('another open release PR makes the request wait, before planning', async () => {
171 const { fetcher, writes } = client({ open: 'sdk-release/batch-8' });
172 expect(await execute(fetcher, context, failing('must not plan'))).toEqual({
173 _tag: 'Waiting',
174 pull: openRelease.html_url,
175 });
176 expect(writes()).toEqual([]);
177 });
178
179 test('a re-run that finds its own release PR resumes it instead of waiting', async () => {
180 const { fetcher } = client({ open: 'sdk-release/batch-21' });
181 expect(await execute(fetcher, context, failing('must not plan'))).toEqual({
182 _tag: 'InReview',
183 pull: openRelease.html_url,
184 });
185 });
186
187 test('a merged release plan on next without its tag blocks new planning', async () => {
188 const blocked = client({ committed: { id: '20', tagged: false } });
189 expect(await execute(blocked.fetcher, context, failing('must not plan'))).toEqual({
190 _tag: 'Blocked',
191 release: '20',
192 });
193 const tagged = client({ committed: { id: '20', tagged: true } });
194 expect(await execute(tagged.fetcher, context, planned(plan))).toMatchObject({
195 _tag: 'Planned',
196 });
197 });
198
199 test('an empty plan ends the request with nothing to release', async () => {
200 const { fetcher, writes } = client();
201 expect(await execute(fetcher, context, planned({ ...plan, releases: [] }))).toEqual({
202 _tag: 'Empty',
203 });
204 expect(writes()).toEqual([]);
205 });
206
207 test('the request issue travels with every outcome, so the status job can show it', async () => {
208 const waiting = client({ open: 'sdk-release/batch-8' });
209 const result = await runRelease(
210 executeReleaseRequest(Effect.succeed(request), context, failing('must not plan')),
211 waiting.fetcher
212 );
213 expect(result.issue).toEqual(Option.some(7));
214 const dispatched = client();
215 const planned = await runRelease(
216 executeReleaseRequest(Effect.succeed(request), { ...context, issueNumber: undefined }, () =>
217 Effect.succeed(plan)
218 ),
219 dispatched.fetcher
220 );
221 expect(planned.issue).toEqual(Option.some(31));
222 });
223
224 test('a dispatch-started release opens its tracking issue', async () => {
225 const { fetcher, writes } = client();
226 const outcome = await execute(fetcher, { ...context, issueNumber: undefined }, planned(plan));
227 expect(outcome).toMatchObject({ _tag: 'Planned', batch: { issue: 31 } });
228 expect(writes().map(call => `${call.method} ${call.path}`)).toEqual([
229 'POST /repos/ComposioHQ/composio/issues',
230 ]);
231 });
232 });