1
+import { describe, expect, test } from 'bun:test';
2
+import { Effect, Layer, Option } from 'effect';
3
+import { Digest } from 'effect/crypto';
4
+import { Base64 } from 'effect/encoding';
5
+import semver from 'semver';
6
+import { readReleasePull } from '../scripts/sdk-release/approval';
7
+import {
8
+ authorizeSourceTag,
9
+ executeReleaseRequest,
10
+ openReleasePull,
11
+} from '../scripts/sdk-release/batch';
12
+import { NpmCli, publishNpm, type Distribution } from '../scripts/sdk-release/registry';
13
+import { stageOutcome } from '../scripts/sdk-release/report';
14
+import { sha256Hex } from '../scripts/sdk-release/runtime';
15
+import type { ReleaseBatch } from '../scripts/sdk-release/source';
16
+import { STAGES, type ReleaseState, type StateName } from '../scripts/sdk-release/state';
17
+import {
18
+ base,
19
+ batch,
20
+ betaBatch,
21
+ betaFiles,
22
+ fakeController,
23
+ head,
24
+ releaseFiles,
25
+} from './sdk-release-controller-fixture';
26
+import { resolvePath, runRelease, type Fetch } from './sdk-release-harness';
27
+
28
+const NPM = 'https://registry.npmjs.org';
29
+
30
+/**
31
+ * A fake npm registry with its dist-tags, and the npm CLI that writes to it. Uploads record SLSA
32
+ * provenance the way trusted publishing does, from `ref` (the release tag unless a test forges
33
+ * another one).
34
+ */
35
+function npmRegistry(
36
+ tags: Record<string, string>,
37
+ options: { ref?: string; otherBytes?: boolean } = {}
38
+) {
39
+ const versions = new Map<string, Uint8Array>();
40
+ const commands: string[] = [];
41
+ const pending = new Map<string, Distribution>();
42
+ const sha512 = (bytes: Uint8Array) =>
43
+ runRelease(
44
+ Effect.gen(function* () {
45
+ return yield* (yield* Digest.Digest).digestHex(Digest.sha512, bytes);
46
+ })
47
+ );
48
+ const provenance = new Map<string, string>();
49
+ const cli = Layer.succeed(NpmCli, {
50
+ run: (args: ReadonlyArray<string>) =>
51
+ Effect.promise(async () => {
52
+ commands.push(args.join(' '));
53
+ const [command, target] = args;
54
+ if (command === 'publish') {
55
+ const file = [...pending.values()].find(candidate => target.endsWith(candidate.file));
56
+ if (!file) throw new Error(`Unexpected upload ${target}`);
57
+ versions.set(file.version, file.bytes);
58
+ const releaseOnly = args.find(arg => arg.startsWith('--tag='))!.slice('--tag='.length);
59
+ tags[releaseOnly] = file.version;
60
+ const statement = {
61
+ subject: [
62
+ {
63
+ digest: {
64
+ sha512: await sha512(options.otherBytes ? new Uint8Array([1]) : file.bytes),
65
+ },
66
+ },
67
+ ],
68
+ predicate: {
69
+ buildDefinition: {
70
+ externalParameters: {
71
+ workflow: {
72
+ ref:
73
+ options.ref ??
74
+ `refs/tags/sdk-release/${releaseOnly.slice('sdk-release-'.length)}`,
75
+ repository: 'https://github.com/ComposioHQ/composio',
76
+ path: '.github/workflows/sdk.release.yml',
77
+ },
78
+ },
79
+ },
80
+ },
81
+ };
82
+ provenance.set(file.version, Base64.encode(JSON.stringify(statement)));
83
+ }
84
+ if (command === 'dist-tag' && target === 'add') tags[args[3]] = args[2].split('@').at(-1)!;
85
+ if (command === 'dist-tag' && target === 'rm') delete tags[args[3]];
86
+ return '';
87
+ }),
88
+ });
89
+ const fetcher: Fetch = async input => {
90
+ const url = String(input);
91
+ const respond = (data: unknown) => new Response(JSON.stringify(data));
92
+ const notFound = new Response('{}', { status: 404 });
93
+ if (url === `${NPM}/-/package/%40composio%2Fcore/dist-tags`) return respond(tags);
94
+ const attestation = /\/-\/npm\/v1\/attestations\/%40composio%2Fcore@(.+)$/.exec(url);
95
+ if (attestation)
96
+ return provenance.has(attestation[1])
97
+ ? respond({
98
+ attestations: [
99
+ {
100
+ predicateType: 'https://slsa.dev/provenance/v1',
101
+ bundle: { dsseEnvelope: { payload: provenance.get(attestation[1]) } },
102
+ },
103
+ ],
104
+ })
105
+ : notFound;
106
+ const tarball = /\/@composio\/core\/-\/core-(.+)\.tgz$/.exec(url);
107
+ if (tarball) return new Response((versions.get(tarball[1]) ?? new Uint8Array()).slice());
108
+ const document = /^https:\/\/registry\.npmjs\.org\/%40composio%2Fcore\/(.+)$/.exec(url);
109
+ if (document && semver.valid(document[1]))
110
+ return versions.has(document[1])
111
+ ? respond({
112
+ name: '@composio/core',
113
+ version: document[1],
114
+ dist: { tarball: `${NPM}/@composio/core/-/core-${document[1]}.tgz` },
115
+ })
116
+ : notFound;
117
+ if (document) return document[1] in tags ? respond({ version: tags[document[1]] }) : notFound;
118
+ throw new Error(`Unexpected registry request: ${url}`);
119
+ };
120
+ const stage = (file: Distribution) => pending.set(file.file, file);
121
+ return { cli, fetcher, commands, tags, stage };
122
+}
123
+
124
+const tarballOf = async (release: ReleaseBatch): Promise<Distribution> => {
125
+ const version = release.plan.releases[0].newVersion;
126
+ const bytes = new TextEncoder().encode(`reviewed tarball ${version}`);
127
+ return {
128
+ language: 'typescript',
129
+ name: '@composio/core',
130
+ version,
131
+ file: `composio-core-${version}.tgz`,
132
+ bytes,
133
+ sha256: await runRelease(sha256Hex(bytes)),
134
+ };
135
+};
136
+
137
+/** One release through every stage, with the production handlers and fakes at the edges. */
138
+async function rehearse(release: ReleaseBatch, tags: Record<string, string>) {
139
+ const beta = release.plan.choices.channel === 'beta';
140
+ const github = fakeController({ beta });
141
+ const ended: ReleaseState[] = [];
142
+ const cwd = await resolvePath('.');
143
+
144
+ // 1. request: an active member's request plans the batch.
145
+ const planned = await runRelease(
146
+ executeReleaseRequest(
147
+ Effect.succeed({
148
+ channel: release.plan.choices.channel,
149
+ typescript: 'auto',
150
+ python: 'skip',
151
+ }),
152
+ { actor: 'requester', runId: release.id, sourceSha: base, issueNumber: release.issue },
153
+ () => Effect.succeed(release.plan)
154
+ ),
155
+ github.fetcher
156
+ );
157
+ expect(planned).toMatchObject({ _tag: 'Planned', batch: { id: release.id, plan: release.plan } });
158
+ ended.push(
159
+ await runRelease(
160
+ openReleasePull(cwd, release, { files: beta ? betaFiles : releaseFiles }),
161
+ github.fetcher
162
+ )
163
+ );
164
+
165
+ // 2. review: the merged, approved release PR is tagged.
166
+ ended.push(
167
+ await runRelease(
168
+ Effect.flatMap(readReleasePull(9), ({ pull, batch: committed }) =>
169
+ authorizeSourceTag(pull, committed, 'sdk-release-app')
170
+ ),
171
+ github.fetcher
172
+ )
173
+ );
174
+ expect(github.writes.at(-1)).toEqual({
175
+ method: 'POST',
176
+ path: '/git/refs',
177
+ body: { ref: 'refs/tags/sdk-release/8', sha: head },
178
+ });
179
+
180
+ // 3. publish: the reviewed tarball reaches npm, and a re-run uploads nothing.
181
+ const registry = npmRegistry(tags);
182
+ const tarball = await tarballOf(release);
183
+ registry.stage(tarball);
184
+ const publish = publishNpm(cwd, release, [tarball]).pipe(Effect.provide(registry.cli));
185
+ await runRelease(publish, registry.fetcher);
186
+ const firstRun = [...registry.commands];
187
+ await runRelease(publish, registry.fetcher);
188
+ ended.push(
189
+ await runRelease(
190
+ stageOutcome(
191
+ 'publish',
192
+ { candidates: 'success', 'publish-npm': 'success' },
193
+ Option.none(),
194
+ Effect.succeed({ _tag: 'Published', versions: [] } as ReleaseState),
195
+ '77'
196
+ )
197
+ )
198
+ );
199
+ return { ended, firstRun, registry, tarball };
200
+}
201
+
202
+describe('release rehearsal', () => {
203
+ test('a stable release goes from request to npm latest, and a re-run changes nothing', async () => {
204
+ const { ended, firstRun, registry } = await rehearse(batch, { latest: '1.0.0' });
205
+ expect(ended.map(state => state._tag)).toEqual(['InReview', 'Tagged', 'Published']);
206
+ expect(firstRun).toEqual([
207
+ `publish ${await resolvePath('sdk-publication/typescript/composio-core-1.0.1.tgz')} --ignore-scripts --provenance --access=public --tag=sdk-release-8`,
208
+ 'dist-tag add @composio/core@1.0.1 latest',
209
+ 'dist-tag rm @composio/core sdk-release-8',
210
+ ]);
211
+ expect(registry.commands).toEqual(firstRun);
212
+ expect(registry.tags).toEqual({ latest: '1.0.1' });
213
+ });
214
+
215
+ test('a beta reaches npm next and never reads or moves latest', async () => {
216
+ const { ended, registry } = await rehearse(betaBatch, {
217
+ latest: '1.0.0',
218
+ next: '0.1.36-next.13',
219
+ });
220
+ expect(ended.map(state => state._tag)).toEqual(['InReview', 'Tagged', 'Published']);
221
+ expect(registry.tags).toEqual({ latest: '1.0.0', next: '1.0.1-beta.8' });
222
+ expect(registry.commands.some(command => command.includes('latest'))).toBe(false);
223
+ });
224
+
225
+ test('provenance from another ref fails the publication after upload', async () => {
226
+ const registry = npmRegistry({ latest: '1.0.0' }, { ref: 'refs/heads/next' });
227
+ const tarball = await tarballOf(batch);
228
+ registry.stage(tarball);
229
+ await expect(
230
+ runRelease(
231
+ publishNpm(await resolvePath('.'), batch, [tarball]).pipe(Effect.provide(registry.cli)),
232
+ registry.fetcher
233
+ )
234
+ ).rejects.toThrow('was built by https://github.com/ComposioHQ/composio');
235
+ });
236
+
237
+ test('provenance that describes other bytes fails the publication', async () => {
238
+ const registry = npmRegistry({ latest: '1.0.0' }, { otherBytes: true });
239
+ const tarball = await tarballOf(batch);
240
+ registry.stage(tarball);
241
+ await expect(
242
+ runRelease(
243
+ publishNpm(await resolvePath('.'), batch, [tarball]).pipe(Effect.provide(registry.cli)),
244
+ registry.fetcher
245
+ )
246
+ ).rejects.toThrow('provenance describes other bytes than the reviewed tarball');
247
+ });
248
+
249
+ test('every state a stage can end in is reached by a production handler', async () => {
250
+ const reached = new Set<StateName>();
251
+ for (const { ended } of [
252
+ await rehearse(batch, { latest: '1.0.0' }),
253
+ await rehearse(betaBatch, { next: '0.1.36-next.13' }),
254
+ ])
255
+ for (const state of ended) reached.add(state._tag);
256
+ const review = async (options: Parameters<typeof fakeController>[0]) => {
257
+ const { fetcher } = fakeController(options);
258
+ return runRelease(
259
+ Effect.flatMap(readReleasePull(9), ({ pull, batch: committed }) =>
260
+ authorizeSourceTag(pull, committed, 'sdk-release-app')
261
+ ),
262
+ fetcher
263
+ );
264
+ };
265
+ reached.add((await review({ closedUnmerged: true }))._tag);
266
+ reached.add((await review({ reviewer: 'requester' }))._tag);
267
+ const request = async (
268
+ options: Parameters<typeof fakeController>[0],
269
+ sourceSha: string,
270
+ parse: Effect.Effect<never, string> | undefined,
271
+ releases = batch.plan
272
+ ) => {
273
+ const { fetcher } = fakeController(options);
274
+ return runRelease(
275
+ executeReleaseRequest(
276
+ parse ?? Effect.succeed({ channel: 'stable', typescript: 'auto', python: 'skip' }),
277
+ { actor: 'requester', runId: '8', sourceSha, issueNumber: 8 },
278
+ () => Effect.succeed(releases)
279
+ ),
280
+ fetcher
281
+ );
282
+ };
283
+ reached.add((await request({}, base, Effect.fail('Use the form')))._tag as StateName);
284
+ reached.add(
285
+ (await request({ openRelease: 'sdk-release/batch-7' }, base, undefined))._tag as StateName
286
+ );
287
+ reached.add((await request({}, head, undefined))._tag as StateName);
288
+ reached.add(
289
+ (await request({}, base, undefined, { ...batch.plan, releases: [] }))._tag as StateName
290
+ );
291
+ reached.add(
292
+ (
293
+ await runRelease(
294
+ stageOutcome('request', { plan: 'cancelled' }, Option.none(), Effect.never, '77')
295
+ )
296
+ )._tag
297
+ );
298
+ const ends = new Set<StateName>([
299
+ ...Object.values(STAGES).flatMap(stage => stage.to as ReadonlyArray<StateName>),
300
+ 'Failed',
301
+ ]);
302
+ expect([...reached].toSorted()).toEqual([...ends].toSorted());
303
+ });
304
+});