Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

release-http-fixture.test.mjs

243 lines8.7 KB

1// Ordinary fixture controls. These check the release receiver's process and
2// protocol mechanics; product tag judgments remain in the shared oracle.
3import assert from 'node:assert/strict'
4import { execFileSync, spawnSync } from 'node:child_process'
5import {
6 mkdirSync,
7 mkdtempSync,
8 readFileSync,
9 realpathSync,
10 rmSync,
11 writeFileSync,
12} from 'node:fs'
13import { tmpdir } from 'node:os'
14import { delimiter, join } from 'node:path'
15import { test } from 'node:test'
16import { setTimeout as delay } from 'node:timers/promises'
17import { withReleaseWorkspace } from './release-fixture.js'
18import {
19 runReleaseToolTree,
20 withHttpPublishReceiver,
21} from './release-http-fixture.js'
22​
23async function stopped(pid) {
24 for (let attempt = 0; attempt < 100; attempt++) {
25 try {
26 process.kill(pid, 0)
27 } catch (error) {
28 if (error.code === 'ESRCH') return
29 throw error
30 }
31 // A reparented zombie has terminated but can await its host's reaper.
32 try {
33 const state = execFileSync('ps', ['-p', String(pid), '-o', 'stat='], {
34 encoding: 'utf8',
35 }).trim()
36 if (state.startsWith('Z')) return
37 } catch (error) {
38 if (error.status === 1) return
39 throw error
40 }
41 await delay(10)
42 }
43 assert.fail(`release descendant ${pid} still runs after group cleanup`)
45​
46for (const outcome of ['fulfilled', 'timed-out'])
47 test(`release tool ${outcome} terminates its descendant`, async () => {
48 const root = mkdtempSync(join(tmpdir(), 'release-tool-control-'))
49 const receipt = join(root, 'descendant.json')
50 let pid
51 try {
52 const descendant = `require('node:fs').writeFileSync(${JSON.stringify(receipt)}, JSON.stringify({ pid: process.pid, group: process.ppid })); setInterval(() => {}, 1000)`
53 const parent = `
54 const fs = require('node:fs')
55 require('node:child_process').spawn(process.execPath, ['-e', ${JSON.stringify(descendant)}], { stdio: 'ignore' })
56 const timer = setInterval(() => {
57 if (!fs.existsSync(${JSON.stringify(receipt)})) return
58 process.stdout.write('descendant-started\\n')
59 clearInterval(timer)
60 ${outcome === 'fulfilled' ? 'process.exit(0)' : 'setInterval(() => {}, 1000)'}
61 }, 10)
62 `
63 const result = await runReleaseToolTree(
64 process.execPath,
65 ['-e', parent],
66 {
67 cwd: root,
68 env: { PATH: process.env.PATH },
69 timeout: 1500,
70 },
71 )
72 pid = JSON.parse(readFileSync(receipt, 'utf8')).pid
73 assert.match(result.stdout, /descendant-started/)
74 if (outcome === 'fulfilled') {
75 assert.equal(result.status, 0)
76 assert.equal(result.error, undefined)
77 } else {
78 assert.match(result.error?.message ?? '', /timed out/)
79 assert.equal(result.signal, 'SIGKILL')
80 }
81 await stopped(pid)
82 } finally {
83 if (pid !== undefined) {
84 try {
85 process.kill(pid, 'SIGKILL')
86 } catch (error) {
87 if (error.code !== 'ESRCH') throw error
88 }
89 }
90 rmSync(root, { recursive: true, force: true })
91 }
92 })
93​
94test('release tool preserves executable startup failure', async () => {
95 const result = await runReleaseToolTree('/no-such-release-oracle-tool', [], {
96 env: { PATH: process.env.PATH },
97 timeout: 1500,
98 })
99 assert.equal(result.error?.code, 'ENOENT')
100 assert.equal(result.stdout, '')
101 assert.equal(result.stderr, '')
102})
103​
104test('release tool rejects output beyond its bound without replacing it with timeout', async () => {
105 const result = await runReleaseToolTree(
106 process.execPath,
107 [
108 '-e',
109 "process.stdout.write('x'.repeat(17 * 1024 * 1024)); setInterval(() => {}, 1000)",
110 ],
111 { env: { PATH: process.env.PATH }, timeout: 3000 },
112 )
113 assert.match(result.error?.message ?? '', /output exceeds fixture bound/)
114 assert(result.stdout.length <= 16 * 1024 * 1024)
115})
116​
117test('release tool retains timeout and group termination failure separately', () => {
118 // The isolated process replaces only its own group signal. A direct-child
119 // signal remains available, so this fault cannot leave our test child alive.
120 const helper = new URL('./release-http-fixture.js', import.meta.url).href
121 const probe = `
122 const { runReleaseToolTree } = await import(${JSON.stringify(helper)})
123 const realKill = process.kill
124 process.kill = function(pid, signal) {
125 if (pid < 0) throw Object.assign(new Error('termination sentinel'), { code: 'EPERM' })
126 return realKill.call(process, pid, signal)
127 }
128 const result = await runReleaseToolTree(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], {
129 env: { PATH: process.env.PATH }, timeout: 75,
130 })
131 console.log(JSON.stringify({
132 primary: result.error?.cause?.message,
133 secondary: result.error?.errors?.map((error) => error?.message),
134 signal: result.signal,
135 }))
136 `
137 const result = spawnSync(
138 process.execPath,
139 ['--input-type=module', '-e', probe],
140 {
141 encoding: 'utf8',
142 timeout: 5000,
143 env: { ...process.env },
144 },
145 )
146 assert.equal(result.error, undefined)
147 assert.equal(result.status, 0, result.stderr)
148 const observed = JSON.parse(result.stdout)
149 assert.equal(observed.primary, 'Real release command timed out')
150 assert(observed.secondary.includes('termination sentinel'))
151 assert.equal(observed.signal, 'SIGKILL')
152})
153​
154const history = {
155 candidate: '1.0.0-rc.1.10',
156 registry: 'stable-history',
157 failure: 'none',
158 tagShape: 'ordinary',
160​
161test('HTTP receiver executes the PATH-selected relative pnpm launcher at its original location', () => {
162 const root = mkdtempSync(join(tmpdir(), "release pnpm's launcher-"))
163 try {
164 const native = realpathSync(
165 execFileSync('which', ['pnpm'], { encoding: 'utf8' }).trim(),
166 )
167 const bin = join(root, 'bin')
168 const tool = join(root, 'global', 'v11', 'pnpm')
169 mkdirSync(bin)
170 mkdirSync(join(root, 'global', 'v11'), { recursive: true })
171 const quote = (value) => `'${value.replaceAll("'", "'\\''")}'`
172 writeFileSync(tool, `#!/bin/sh\nexec ${quote(native)} "$@"\n`, {
173 mode: 0o755,
174 })
175 const launcher = join(bin, 'pnpm')
176 writeFileSync(
177 launcher,
178 '#!/bin/sh\nbasedir=$(dirname "$0")\nexec "$basedir/../global/v11/pnpm" "$@"\n',
179 { mode: 0o755 },
180 )
181 const receiver = new URL('./release-http-fixture.js', import.meta.url).href
182 const workspace = new URL('./release-fixture.js', import.meta.url).href
183 const probe = `
184 const { withHttpPublishReceiver } = await import(${JSON.stringify(receiver)})
185 const { withReleaseWorkspace } = await import(${JSON.stringify(workspace)})
186 await withReleaseWorkspace(async ({ root }) => withHttpPublishReceiver(root, ${JSON.stringify(history)}, async (receiver) => {
187 await receiver.run()
188 console.log(JSON.stringify(receiver.tools))
189 }))
190 `
191 const probePath = join(root, 'probe.mjs')
192 writeFileSync(probePath, probe)
193 const result = spawnSync(process.execPath, [probePath], {
194 encoding: 'utf8',
195 timeout: 45_000,
196 env: { ...process.env, PATH: `${bin}${delimiter}${process.env.PATH}` },
197 })
198 assert.equal(result.error, undefined)
199 assert.equal(result.status, 0, result.stderr)
200 const tools = JSON.parse(result.stdout)
201 assert.equal(tools.paths.pnpm, realpathSync(launcher))
202 assert.equal(
203 tools.pnpm,
204 execFileSync(native, ['--version'], { encoding: 'utf8' }).trim(),
205 )
206 } finally {
207 rmSync(root, { recursive: true, force: true })
208 }
209})
210for (const outcome of ['error', 'undefined'])
211 test(`HTTP receiver preserves callback throwing ${outcome}`, async () => {
212 const primary =
213 outcome === 'undefined' ? undefined : new Error('comparison sentinel')
214 let rejected = false
215 try {
216 await withReleaseWorkspace(async ({ root }) =>
217 withHttpPublishReceiver(root, history, () => {
218 throw primary
219 }),
220 )
221 } catch (error) {
222 rejected = true
223 assert.equal(error, primary)
224 }
225 assert.equal(rejected, true)
226 })
227​
228test('HTTP receiver rejects and records unsupported scoped routes', async () => {
229 await withReleaseWorkspace(async ({ root }) =>
230 withHttpPublishReceiver(root, history, async (receiver) => {
231 const path = '/%40tanstack%2Fnot-a-fixture-package'
232 const response = await fetch(receiver.origin + path)
233 assert.equal(response.status, 422)
234 const state = receiver.readState()
235 assert.equal(state.requests.length, 1)
236 assert.equal(state.requests[0].type, 'protocol-error')
237 assert.equal(state.requests[0].path, path)
238 assert.equal(state.requests[0].registry, receiver.origin)
239 assert.match(state.requests[0].error, /known public package route/)
240 assert.deepEqual(state.packages, receiver.initialRegistry)
241 }),
242 )
243})