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
| 1 | import { afterEach, describe, expect, test } from 'bun:test'; |
| 2 | import { Effect, Option } from 'effect'; |
| 3 | import { showStatus, stageOutcome } from '../scripts/sdk-release/report'; |
| 4 | import { |
| 5 | DISPLAY, |
| 6 | STAGES, |
| 7 | describe as describeState, |
| 8 | type ReleaseState, |
| 9 | type Stage, |
| 10 | type StateName, |
| 11 | } from '../scripts/sdk-release/state'; |
| 12 | import { spawn } from '../scripts/sdk-release/runtime'; |
| 13 | import { |
| 14 | readText, |
| 15 | resolvePath, |
| 16 | runRelease, |
| 17 | temporaryDirectories, |
| 18 | writeText, |
| 19 | type Fetch, |
| 20 | } from './sdk-release-harness'; |
| 21 | |
| 22 | const roots = temporaryDirectories('sdk-release-status-'); |
| 23 | afterEach(() => roots.cleanup()); |
| 24 | |
| 25 | const pull = 'https://github.com/ComposioHQ/composio/pull/9'; |
| 26 | const run = '77'; |
| 27 | |
| 28 | /** One example of every state, so the table checks below cover each one. */ |
| 29 | const EXAMPLES: { readonly [Name in StateName]: Extract<ReleaseState, { _tag: Name }> } = { |
| 30 | NeedsInput: { _tag: 'NeedsInput', reason: 'Use the SDK release request issue form' }, |
| 31 | Waiting: { _tag: 'Waiting', pull }, |
| 32 | Blocked: { _tag: 'Blocked', release: '20' }, |
| 33 | Empty: { _tag: 'Empty' }, |
| 34 | InReview: { _tag: 'InReview', pull }, |
| 35 | Abandoned: { _tag: 'Abandoned', pull }, |
| 36 | Refused: { _tag: 'Refused', pull, reason: 'Missing green final-head docs check' }, |
| 37 | Tagged: { _tag: 'Tagged', tag: 'sdk-release/8' }, |
| 38 | Published: { _tag: 'Published', versions: [{ name: '`@composio/core`', version: '1.0.1' }] }, |
| 39 | Failed: { |
| 40 | _tag: 'Failed', |
| 41 | stage: 'publish', |
| 42 | jobs: ['publish-pypi'], |
| 43 | run: 'https://github.com/ComposioHQ/composio/actions/runs/77', |
| 44 | }, |
| 45 | }; |
| 46 | const stages = Object.keys(STAGES) as Stage[]; |
| 47 | const ends = (stage: Stage): ReadonlyArray<StateName> => STAGES[stage].to; |
| 48 | |
| 49 | describe('release state table', () => { |
| 50 | test('stages chain: each stage starts where the previous one can end', () => { |
| 51 | expect(stages).toEqual(['request', 'review', 'publish']); |
| 52 | for (const [previous, next] of [ |
| 53 | ['request', 'review'], |
| 54 | ['review', 'publish'], |
| 55 | ] as const) |
| 56 | expect(ends(previous)).toContain(STAGES[next].from); |
| 57 | }); |
| 58 | |
| 59 | test('every state ends exactly one stage, apart from Failed, which ends any stage', () => { |
| 60 | const ended = stages.flatMap(ends); |
| 61 | expect(new Set(ended).size).toBe(ended.length); |
| 62 | expect(new Set([...ended, 'Failed'])).toEqual(new Set(Object.keys(EXAMPLES))); |
| 63 | }); |
| 64 | |
| 65 | test('a release stays open only while it can still advance', () => { |
| 66 | const open = (Object.keys(DISPLAY) as StateName[]).filter(name => DISPLAY[name].close === null); |
| 67 | expect(open.toSorted()).toEqual(['Failed', 'InReview', 'Tagged']); |
| 68 | expect(DISPLAY.Published.close).toBe('completed'); |
| 69 | const labels = Object.values(DISPLAY).map(display => display.label); |
| 70 | expect(new Set(labels).size).toBe(labels.length); |
| 71 | }); |
| 72 | |
| 73 | test.each(Object.values(EXAMPLES))('$_tag has a status text with its facts', state => { |
| 74 | const text = describeState(state); |
| 75 | expect(text.length).toBeGreaterThan(20); |
| 76 | for (const value of Object.values(state).filter(value => typeof value === 'string')) |
| 77 | if (value !== state._tag) expect(text).toContain(value); |
| 78 | }); |
| 79 | }); |
| 80 | |
| 81 | const results = (failed: Record<string, 'failure' | 'cancelled'> = {}) => ({ |
| 82 | plan: 'success' as const, |
| 83 | prepare: 'skipped' as const, |
| 84 | ...failed, |
| 85 | }); |
| 86 | const outcome = ( |
| 87 | stage: Stage, |
| 88 | recorded: ReleaseState | undefined, |
| 89 | failed: Record<string, 'failure' | 'cancelled'> = {} |
| 90 | ) => |
| 91 | runRelease( |
| 92 | stageOutcome( |
| 93 | stage, |
| 94 | results(failed), |
| 95 | Option.fromNullishOr(recorded), |
| 96 | Effect.succeed(EXAMPLES.Published), |
| 97 | run |
| 98 | ) |
| 99 | ); |
| 100 | |
| 101 | describe('stage outcome', () => { |
| 102 | test('a stage ends in the state its last job recorded', async () => { |
| 103 | expect(await outcome('request', EXAMPLES.InReview)).toEqual(EXAMPLES.InReview); |
| 104 | expect(await outcome('review', EXAMPLES.Abandoned)).toEqual(EXAMPLES.Abandoned); |
| 105 | }); |
| 106 | |
| 107 | test('a publish stage whose jobs all passed is published', async () => { |
| 108 | expect(await outcome('publish', undefined)).toEqual(EXAMPLES.Published); |
| 109 | }); |
| 110 | |
| 111 | test('a failed or cancelled job fails the stage, naming only those jobs', async () => { |
| 112 | expect( |
| 113 | await outcome('request', EXAMPLES.InReview, { 'release-pr': 'cancelled', other: 'failure' }) |
| 114 | ).toEqual({ |
| 115 | _tag: 'Failed', |
| 116 | stage: 'request', |
| 117 | jobs: ['release-pr', 'other'], |
| 118 | run: 'https://github.com/ComposioHQ/composio/actions/runs/77', |
| 119 | }); |
| 120 | }); |
| 121 | |
| 122 | test('a state that does not end the stage is refused', async () => { |
| 123 | await expect(outcome('review', EXAMPLES.Waiting)).rejects.toThrow( |
| 124 | 'The review stage cannot end in Waiting' |
| 125 | ); |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | /** A fake request issue with its comments and labels. */ |
| 130 | function requestIssue(comments: { id: number; login: string; body: string }[] = []) { |
| 131 | const labels = new Set(['SDK release']); |
| 132 | const writes: string[] = []; |
| 133 | let state = 'open'; |
| 134 | const fetcher: Fetch = async (input, init) => { |
| 135 | const url = new URL(String(input)); |
| 136 | const path = url.pathname.replace('/repos/ComposioHQ/composio', ''); |
| 137 | const method = init?.method ?? 'GET'; |
| 138 | const body = init?.body ? JSON.parse(String(init.body)) : undefined; |
| 139 | const respond = (data: unknown) => new Response(JSON.stringify(data)); |
| 140 | if (method !== 'GET') writes.push(`${method} ${path}`); |
| 141 | if (method === 'GET' && path === '/issues/8/comments') |
| 142 | return respond(comments.map(({ id, login, body }) => ({ id, user: { login }, body }))); |
| 143 | if (method === 'GET' && path === '/issues/8/labels') |
| 144 | return respond([...labels].map(name => ({ name }))); |
| 145 | if (method === 'POST' && path === '/issues/8/comments') { |
| 146 | comments.push({ id: comments.length + 1, login: 'github-actions[bot]', body: body.body }); |
| 147 | return respond({ id: comments.length }); |
| 148 | } |
| 149 | const edited = /^\/issues\/comments\/(\d+)$/.exec(path); |
| 150 | if (method === 'PATCH' && edited) { |
| 151 | const comment = comments.find(comment => comment.id === Number(edited[1])); |
| 152 | if (comment) comment.body = body.body; |
| 153 | return respond({}); |
| 154 | } |
| 155 | if (method === 'DELETE' && path.startsWith('/issues/8/labels/')) { |
| 156 | labels.delete(decodeURIComponent(path.slice('/issues/8/labels/'.length))); |
| 157 | return respond([]); |
| 158 | } |
| 159 | if (method === 'POST' && path === '/issues/8/labels') { |
| 160 | for (const label of body.labels) labels.add(label); |
| 161 | return respond([]); |
| 162 | } |
| 163 | if (method === 'GET' && path === '/issues/8') return respond({ state }); |
| 164 | if (method === 'PATCH' && path === '/issues/8') { |
| 165 | state = body.state; |
| 166 | return respond({}); |
| 167 | } |
| 168 | throw new Error(`Unexpected request: ${method} ${path}`); |
| 169 | }; |
| 170 | return { fetcher, comments, labels, writes, state: () => state }; |
| 171 | } |
| 172 | |
| 173 | describe('status on the request issue', () => { |
| 174 | test('one status comment and one label follow the release, and a final state closes it', async () => { |
| 175 | const issue = requestIssue(); |
| 176 | await runRelease(showStatus(8, EXAMPLES.InReview, run, 'request'), issue.fetcher); |
| 177 | expect(issue.comments).toHaveLength(1); |
| 178 | expect([...issue.labels]).toEqual(['SDK release', 'sdk-release:in-review']); |
| 179 | expect(issue.state()).toBe('open'); |
| 180 | |
| 181 | await runRelease(showStatus(8, EXAMPLES.Published, run, 'publish'), issue.fetcher); |
| 182 | expect(issue.comments).toHaveLength(1); |
| 183 | expect(issue.comments[0].body).toContain('`@composio/core` 1.0.1'); |
| 184 | expect([...issue.labels]).toEqual(['SDK release', 'sdk-release:published']); |
| 185 | expect(issue.state()).toBe('closed'); |
| 186 | }); |
| 187 | |
| 188 | test('a comment that copies the status marker is never edited unless the workflow wrote it', async () => { |
| 189 | const issue = requestIssue([ |
| 190 | { id: 1, login: 'someone', body: '<!-- sdk-release-status stage=publish -->\nfake status' }, |
| 191 | ]); |
| 192 | await runRelease(showStatus(8, EXAMPLES.Waiting, run, 'request'), issue.fetcher); |
| 193 | expect(issue.comments[0].body).toContain('fake status'); |
| 194 | expect(issue.comments).toHaveLength(2); |
| 195 | expect(issue.writes).toContain('POST /issues/8/comments'); |
| 196 | }); |
| 197 | |
| 198 | test('a release tagged after it was refused reopens its request issue', async () => { |
| 199 | const issue = requestIssue(); |
| 200 | await runRelease(showStatus(8, EXAMPLES.Refused, run, 'review'), issue.fetcher); |
| 201 | expect(issue.state()).toBe('closed'); |
| 202 | await runRelease(showStatus(8, EXAMPLES.Tagged, run, 'review'), issue.fetcher); |
| 203 | expect(issue.state()).toBe('open'); |
| 204 | expect(issue.comments).toEqual([ |
| 205 | expect.objectContaining({ body: expect.stringContaining('Tagged `sdk-release/8`') }), |
| 206 | ]); |
| 207 | expect([...issue.labels]).toEqual(['SDK release', 'sdk-release:publishing']); |
| 208 | }); |
| 209 | |
| 210 | test('a late or re-run earlier stage never replaces a later stage result', async () => { |
| 211 | const issue = requestIssue(); |
| 212 | await runRelease(showStatus(8, EXAMPLES.Published, run, 'publish'), issue.fetcher); |
| 213 | const published = issue.comments[0].body; |
| 214 | await runRelease(showStatus(8, EXAMPLES.Tagged, run, 'review'), issue.fetcher); |
| 215 | expect(issue.comments).toEqual([expect.objectContaining({ body: published })]); |
| 216 | expect([...issue.labels]).toEqual(['SDK release', 'sdk-release:published']); |
| 217 | }); |
| 218 | }); |
| 219 | |
| 220 | /** The status job as the workflow runs it, with the job outputs GitHub passes. */ |
| 221 | const statusRun = async (env: Record<string, string>) => { |
| 222 | const directory = await roots.create(); |
| 223 | const summary = `${directory}/summary`; |
| 224 | await writeText(summary, ''); |
| 225 | const result = await runRelease( |
| 226 | spawn('bun', [await resolvePath('scripts/sdk-release/report.ts'), 'status', 'request'], { |
| 227 | cwd: directory, |
| 228 | env: { GITHUB_TOKEN: 'fixture', GITHUB_RUN_ID: '77', GITHUB_STEP_SUMMARY: summary, ...env }, |
| 229 | }) |
| 230 | ); |
| 231 | return { exitCode: result.exitCode, stderr: result.stderr, summary: await readText(summary) }; |
| 232 | }; |
| 233 | |
| 234 | test('outputs a failed job never wrote arrive empty, and the status still records the failure', async () => { |
| 235 | const run = await statusRun({ RESULTS: '{"plan": "failure"}', OUTCOME: '', ISSUE: '' }); |
| 236 | expect({ exitCode: run.exitCode, stderr: run.stderr }).toMatchObject({ exitCode: 0 }); |
| 237 | expect(run.summary).toContain('The request stage failed in: plan.'); |
| 238 | }); |