Open History to see the last commit
1import { existsSync, readFileSync, rmSync } from 'node:fs'
2import { join, resolve, sep } from 'node:path'
3
4import { describe, expect, it } from 'vitest'
5
6import {
7 assertPublishableManifest,
8 assertVersionsMatchTag,
9 listPublicPackages,
10 main,
11 packAll,
12 packedEntryNames,
13 publishOrder,
14 readPackedManifest,
15 tarballFileName,
16 versionFromTag,
17} from './npm-pack.ts'
18
19const EXPECTED_NAMES = [
20 '@modelschemas/client',
21 '@modelschemas/codegen',
22 '@modelschemas/rate-card',
23 '@modelschemas/vite',
24 'modelschemas',
25] as const
26
27function sharedVersion(): string {
28 const pkgs = listPublicPackages()
29 const versions = new Set(pkgs.map((pkg) => pkg.version))
30 expect(versions.size).toBe(1)
31 const version = pkgs[0]?.version
32 if (version === undefined) throw new Error('no public packages')
33 return version
34}
35
36describe('listPublicPackages', () => {
37 it('returns the five public workspace packages at one shared version', () => {
38 const pkgs = listPublicPackages()
39 expect(pkgs.map((pkg) => pkg.name).sort()).toEqual([...EXPECTED_NAMES])
40 expect(new Set(pkgs.map((pkg) => pkg.version)).size).toBe(1)
41 })
42})
43
44describe('publishOrder', () => {
45 it('puts packages with no workspace deps before their dependents', () => {
46 const ordered = publishOrder(listPublicPackages()).map((pkg) => pkg.name)
47 expect(ordered.indexOf('@modelschemas/client')).toBeLessThan(
48 ordered.indexOf('modelschemas'),
49 )
50 expect(ordered.indexOf('@modelschemas/codegen')).toBeLessThan(
51 ordered.indexOf('modelschemas'),
52 )
53 expect(ordered.indexOf('@modelschemas/codegen')).toBeLessThan(
54 ordered.indexOf('@modelschemas/vite'),
55 )
56 })
57
58 it('throws on a workspace dependency cycle', () => {
59 expect(() =>
60 publishOrder([
61 {
62 name: 'a',
63 version: '1.0.0',
64 dir: '/tmp/a',
65 dependencies: { b: 'workspace:*' },
66 },
67 {
68 name: 'b',
69 version: '1.0.0',
70 dir: '/tmp/b',
71 dependencies: { a: 'workspace:*' },
72 },
73 ]),
74 ).toThrow(/workspace dependency cycle/)
75 })
76})
77
78describe('tarballFileName / versionFromTag', () => {
79 it('turns scoped names into slash-free tarball filenames', () => {
80 expect(tarballFileName('@modelschemas/client', '0.1.0')).toBe(
81 'modelschemas-client-0.1.0.tgz',
82 )
83 expect(tarballFileName('modelschemas', '0.1.0')).toBe(
84 'modelschemas-0.1.0.tgz',
85 )
86 })
87
88 it('parses v-prefixed semver tags only', () => {
89 expect(versionFromTag('v0.1.0')).toBe('0.1.0')
90 expect(versionFromTag('refs/tags/v1.2.3-rc.1')).toBe('1.2.3-rc.1')
91 expect(versionFromTag('0.1.0')).toBeUndefined()
92 expect(versionFromTag('vnext')).toBeUndefined()
93 })
94
95 it('rejects a tag that does not match package versions', () => {
96 const pkgs = listPublicPackages()
97 const version = sharedVersion()
98 expect(() => assertVersionsMatchTag(pkgs, 'v9.9.9')).toThrow(
99 /does not match/,
100 )
101 expect(() => assertVersionsMatchTag(pkgs, `v${version}`)).not.toThrow()
102 expect(() =>
103 assertVersionsMatchTag(
104 [
105 { name: 'ok', version: '0.2.0', dir: '/tmp', dependencies: {} },
106 {
107 name: 'modelschemas',
108 version: '0.1.0',
109 dir: '/tmp',
110 dependencies: {},
111 },
112 ],
113 'v0.2.0',
114 ),
115 ).toThrow(/modelschemas@0.1.0/)
116 })
117})
118
119describe('main', () => {
120 it('refuses --tag / RELEASE_TAG that do not match manifests', () => {
121 expect(() => main(['--tag', 'v9.9.9'])).toThrow(/does not match/)
122 const prev = process.env.RELEASE_TAG
123 process.env.RELEASE_TAG = 'v9.9.9'
124 try {
125 expect(() => main([])).toThrow(/does not match/)
126 } finally {
127 if (prev === undefined) delete process.env.RELEASE_TAG
128 else process.env.RELEASE_TAG = prev
129 }
130 })
131})
132
133describe('assertPublishableManifest', () => {
134 it('rejects workspace: and catalog: specs', () => {
135 expect(() =>
136 assertPublishableManifest(
137 {
138 name: 'x',
139 version: '1.0.0',
140 dependencies: { '@modelschemas/client': 'workspace:*' },
141 },
142 'test',
143 ),
144 ).toThrow(/workspace:\*/)
145 })
146})
147
148describe('packAll', () => {
149 it('rewrites workspace protocol, omits tests, and resolves relative dest from cwd', () => {
150 const dest = join('dist', 'npm-pack-test')
151 const abs = resolve(dest)
152 try {
153 const packed = packAll(publishOrder(listPublicPackages()), dest)
154 expect(packed.map((row) => row.pkg.name)).toEqual(
155 publishOrder(listPublicPackages()).map((pkg) => pkg.name),
156 )
157 const version = sharedVersion()
158 for (const { pkg, tarball } of packed) {
159 expect(tarball.startsWith(`${abs}${sep}`)).toBe(true)
160 expect(tarball.includes(`${sep}packages${sep}`)).toBe(false)
161 expect(existsSync(tarball)).toBe(true)
162 const manifest = readPackedManifest(tarball)
163 assertPublishableManifest(manifest, tarball)
164 const json = JSON.stringify(manifest)
165 expect(json).not.toContain('workspace:')
166 expect(json).not.toContain('catalog:')
167 if (pkg.name === 'modelschemas') {
168 expect(json).toContain(`"@modelschemas/client":"${version}"`)
169 expect(json).toContain(`"@modelschemas/codegen":"${version}"`)
170 }
171 if (pkg.name === '@modelschemas/vite') {
172 expect(json).toContain(`"@modelschemas/codegen":"${version}"`)
173 }
174 const entries = packedEntryNames(tarball)
175 expect(entries.some((entry) => entry.endsWith('.test.ts'))).toBe(false)
176 expect(entries).toContain('package/package.json')
177 expect(entries).toContain('package/LICENSE')
178 expect(entries).toContain('package/src/index.ts')
179 }
180 } finally {
181 rmSync(abs, { recursive: true, force: true })
182 }
183 })
184})
185
186describe('publish.yml', () => {
187 it('is the trusted-publisher workflow: OIDC, stage-only, no npm token', () => {
188 const yaml = readFileSync(
189 join(import.meta.dirname, '..', '.github', 'workflows', 'publish.yml'),
190 'utf8',
191 )
192 const withoutComments = yaml.replace(/^\s*#.*$/gm, '')
193 expect(withoutComments).toMatch(/id-token:\s*write/)
194 expect(withoutComments).toMatch(/environment:\s*npm/)
195 expect(withoutComments).toContain('npm stage publish')
196 expect(withoutComments.replaceAll('npm stage publish', '')).not.toContain(
197 'npm publish',
198 )
199 expect(withoutComments).not.toContain('NPM_TOKEN')
200 expect(withoutComments).not.toContain('NODE_AUTH_TOKEN')
201 expect(withoutComments).not.toContain('bun publish')
202 expect(withoutComments).toContain('RELEASE_TAG')
203 expect(withoutComments).toContain('scripts/npm-pack.ts')
204 expect(withoutComments).toMatch(/!inputs\.dry_run/)
205 expect(withoutComments).toMatch(/default:\s*true/)
206 expect(withoutComments).toContain('npm@11.19.0')
207 expect(withoutComments).not.toContain('npm@latest')
208 expect(withoutComments).toContain('persist-credentials: false')
209 expect(withoutComments).toMatch(/permissions:\s*\{\}/)
210 })
211})