Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions test/scripts/ci/mint-release-token.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { spawnSync } from 'node:child_process'
import { generateKeyPairSync, verify } from 'node:crypto'
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { expect, it } from 'vitest'

const script = fileURLToPath(
new URL('../../../scripts/ci/mint-release-token.mjs', import.meta.url),
)

it('requests a short lived token scoped to the current repository', async () => {
const { privateKey, publicKey } = generateKeyPairSync('rsa', {
modulusLength: 2048,
})
const root = await mkdtemp(path.join(os.tmpdir(), 'release-token-'))
try {
const key = privateKey.export({ type: 'pkcs8', format: 'pem' }).toString()
const preload = path.join(root, 'mock-fetch.mjs')
const callsFile = path.join(root, 'calls.json')
await writeFile(
preload,
`
import { writeFileSync } from 'node:fs'
const calls = []
globalThis.fetch = async (url, options) => {
calls.push({ url: String(url), options })
writeFileSync(process.env.CALLS_FILE, JSON.stringify(calls))
if (calls.length === 1) return Response.json({ id: 42 })
return Response.json({ token: 'scoped-token' })
}
`,
)
const result = spawnSync(process.execPath, ['--import', preload, script], {
encoding: 'utf8',
env: {
...process.env,
APP_PRIVATE_KEY: key,
CLIENT_ID: '12345',
GITHUB_REPOSITORY: 'SocketDev/socket-registry',
CALLS_FILE: callsFile,
},
})
expect(result.status).toBe(0)
expect(result.stdout).toBe('scoped-token')
const calls = JSON.parse(await readFile(callsFile, 'utf8')) as Array<{
url: string
options: { body?: string; headers: Record<string, string> }
}>
expect(calls.map(({ url }) => url)).toEqual([
'https://api.github.com/repos/SocketDev/socket-registry/installation',
'https://api.github.com/app/installations/42/access_tokens',
])
const jwt = calls[0]!.options.headers.authorization!.slice('Bearer '.length)
const [header, claims, signature] = jwt.split('.')
expect(JSON.parse(Buffer.from(header!, 'base64url').toString())).toEqual({
alg: 'RS256',
typ: 'JWT',
})
expect(
verify(
'RSA-SHA256',
Buffer.from(`${header}.${claims}`),
publicKey,
Buffer.from(signature!, 'base64url'),
),
).toBe(true)
const body = JSON.parse(calls[1]!.options.body!)
expect(body).toEqual({
repositories: ['socket-registry'],
permissions: { contents: 'write' },
})
} finally {
await rm(root, { recursive: true, force: true })
}
})

it('rejects repository names outside owner/repository form before making requests', () => {
const result = spawnSync(process.execPath, [script], {
encoding: 'utf8',
env: {
...process.env,
APP_PRIVATE_KEY: 'unused',
CLIENT_ID: '12345',
GITHUB_REPOSITORY: 'SocketDev/socket-registry/extra',
},
})
expect(result.status).not.toBe(0)
expect(result.stderr).toContain(
'Expected GITHUB_REPOSITORY to be owner/repository.',
)
})
133 changes: 133 additions & 0 deletions test/scripts/ci/with-toolchain.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { spawnSync } from 'node:child_process'
import {
chmod,
mkdir,
mkdtemp,
readFile,
rm,
writeFile,
} from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { expect, it } from 'vitest'

const source = fileURLToPath(
new URL('../../../scripts/ci/with-toolchain.sh', import.meta.url),
)

it.skipIf(process.platform === 'win32')(
'uses the cached exact Node version and pinned package manager versions',
async () => {
const root = await mkdtemp(path.join(os.tmpdir(), 'with-toolchain-'))
try {
const bin = path.join(root, 'bin')
const cache = path.join(root, 'cache')
const runnerTemp = path.join(root, 'runner-temp')
await mkdir(bin)
await mkdir(path.join(cache, 'node', '24.13.0', 'x64', 'bin'), {
recursive: true,
})
await mkdir(runnerTemp)
await writeFile(path.join(root, '.node-version'), '24.13.0\n')
await writeFile(
path.join(root, 'package.json'),
JSON.stringify({ packageManager: 'pnpm@12.12.0' }),
)
await writeFile(
path.join(cache, 'node', '24.13.0', 'x64', 'bin', 'node'),
'#!/bin/sh\nif [ "$1" = "--version" ]; then echo v24.13.0; else exec "$REAL_NODE" "$@"; fi\n',
)
await chmod(
path.join(cache, 'node', '24.13.0', 'x64', 'bin', 'node'),
0o755,
)
const log = path.join(root, 'commands.log')
const npm = path.join(bin, 'npm')
await writeFile(
npm,
`#!/bin/sh
printf 'npm %s\\n' "$*" >> "$COMMAND_LOG"
case "$*" in
*'install --global'*)
mkdir -p "$4/lib/node_modules/pnpm/bin" "$4/bin"
cat > "$4/lib/node_modules/pnpm/bin/pnpm.mjs" <<'SCRIPT'
#!/bin/sh
printf 'pnpm %s\\n' "$*" >> "$COMMAND_LOG"
if [ "$1" = "--version" ]; then echo 12.12.0; fi
SCRIPT
;;
'--version') echo 12.2.0 ;;
esac
`,
)
await chmod(npm, 0o755)
const result = spawnSync(
'bash',
[source, 'sh', '-c', 'printf "final command\\n" >> "$COMMAND_LOG"'],
{
cwd: root,
encoding: 'utf8',
env: {
...process.env,
PATH: [bin, process.env.PATH].join(path.delimiter),
RUNNER_TEMP: runnerTemp,
RUNNER_TOOL_CACHE: cache,
COMMAND_LOG: log,
REAL_NODE: process.execPath,
},
},
)
expect(result.status, result.stderr).toBe(0)
const commands = await readFile(log, 'utf8')
expect(commands).toContain('npm install --global')
expect(commands).toContain('npm@12.2.0')
expect(commands).toContain('pnpm@12.12.0')
expect(commands).toContain('pnpm install --frozen-lockfile')
expect(commands).toContain('final command')
} finally {
await rm(root, { recursive: true, force: true })
}
},
)

it.skipIf(process.platform === 'win32')(
'rejects a non-exact package manager pin before installing tools',
async () => {
const root = await mkdtemp(
path.join(os.tmpdir(), 'with-toolchain-invalid-'),
)
try {
const bin = path.join(root, 'bin')
await mkdir(bin)
const fakeNode = path.join(bin, 'node')
await writeFile(
fakeNode,
'#!/bin/sh\nif [ "$1" = "--version" ]; then echo v24.13.0; else exec "$REAL_NODE" "$@"; fi\n',
)
await chmod(fakeNode, 0o755)
await writeFile(path.join(root, '.node-version'), '24.13.0\n')
await writeFile(
path.join(root, 'package.json'),
JSON.stringify({ packageManager: 'pnpm@12.12' }),
)
const result = spawnSync('bash', [source, 'true'], {
cwd: root,
encoding: 'utf8',
env: {
...process.env,
PATH: [bin, process.env.PATH].join(path.delimiter),
REAL_NODE: process.execPath,
RUNNER_TEMP: root,
},
})
expect(result.status).not.toBe(0)
expect(result.stderr).toContain(
'packageManager must pin an exact pnpm version',
)
} finally {
await rm(root, { recursive: true, force: true })
}
},
)