Files
pnpm/agent/server/test/integration.ts
Marvin Hagemeister 49e6074644 test: replace @pnpm/registry-mock with an in-repo in-process registry (#11927)
Replace the external `@pnpm/registry-mock` (Verdaccio) test dependency with an in-repo, in-process registry that serves package fixtures to **both** the pacquet Rust tests and the pnpm CLI (Jest) tests. No separately managed registry process is needed.

### How it works

- **Fixtures** live at `registry/.fixtures/packages/<name>/<version>/…`, moved verbatim from [`pnpm/registry-mock`](https://github.com/pnpm/registry-mock) (keyed by each `package.json`'s `name`+`version`).
- **`pnpm-registry-fixtures`** builds verdaccio-shaped storage from those fixtures; the in-tree **`pnpm-registry`** crate serves it.
  - Files whose names differ only by case (`@pnpm.e2e/with-same-file-in-different-cases`) and `bundleDependencies` trees are composed **in memory** by the builder, since neither can be committed to the working tree.
- **pacquet**: `pacquet-testing-utils`' `TestRegistry` starts the server lazily (once per process) in proxy mode, serving `@pnpm.e2e` fixtures locally and falling through to the npm uplink for real packages (`is-positive`, `is-negative`, …) — matching how registry-mock behaved.
- **pnpm CLI**: the `with-registry` Jest `globalSetup` builds storage from the fixtures via the new `pnpm-registry-prepare` binary (built from source in the Test CI job) and serves it with `pnpm-registry`. `REGISTRY_MOCK_PORT` / `REGISTRY_MOCK_CREDENTIALS` / `getIntegrity` now come from `@pnpm/testing.registry-mock`.

### Result

`@pnpm/registry-mock` is removed from every manifest, the catalog, and `packageExtensions`; `cargo test` / `cargo nextest run` / `just test` and the pnpm CLI Jest suites all run registry-backed tests without launching Verdaccio.
2026-05-29 14:35:45 +02:00

190 lines
6.4 KiB
TypeScript

import { promises as fs } from 'node:fs'
import http from 'node:http'
import os from 'node:os'
import path from 'node:path'
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals'
// First run downloads packages from registry-mock — slow on Windows CI
jest.setTimeout(600_000)
import { fetchFromPnpmRegistry } from '@pnpm/agent.client'
import { StoreIndex } from '@pnpm/store.index'
import { REGISTRY_MOCK_PORT } from '@pnpm/testing.registry-mock'
import type { DepPath, ProjectId } from '@pnpm/types'
import { createRegistryServer } from 'pnpm-agent'
const REGISTRY = `http://localhost:${REGISTRY_MOCK_PORT}/`
describe('pnpm-agent integration', () => {
let server: http.Server
let serverPort: number
let serverStoreDir: string
let serverCacheDir: string
beforeAll(async () => {
// Create server store in a temp directory
const tmpBase = await fs.mkdtemp(path.join(os.tmpdir(), 'pnpm-agent-test-server-'))
serverStoreDir = path.join(tmpBase, 'store')
serverCacheDir = path.join(tmpBase, 'cache')
server = await createRegistryServer({
storeDir: serverStoreDir,
cacheDir: serverCacheDir,
registries: { default: REGISTRY },
})
// Listen on random port
await new Promise<void>((resolve) => {
server.listen(0, resolve)
})
serverPort = (server.address() as any).port // eslint-disable-line @typescript-eslint/no-explicit-any
})
afterAll(async () => {
const { finishWorkers } = await import('../../../worker/src/index.js')
await finishWorkers()
await new Promise<void>((resolve, reject) => {
server.close((err) => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
await fs.rm(path.dirname(serverStoreDir), { recursive: true, force: true })
})
it('returns a lockfile with importers keyed by "."', async () => {
const tmpClient = await fs.mkdtemp(path.join(os.tmpdir(), 'pnpm-agent-test-importers-'))
const clientStoreDir = path.join(tmpClient, 'store')
await fs.mkdir(clientStoreDir, { recursive: true })
const clientStoreIndex = new StoreIndex(clientStoreDir)
try {
const result = await fetchFromPnpmRegistry({
registryUrl: `http://localhost:${serverPort}`,
storeDir: clientStoreDir,
storeIndex: clientStoreIndex,
dependencies: {
'is-positive': '1.0.0',
},
})
// The lockfile must have importers keyed by "." (not by the server's temp dir)
const importerKeys = Object.keys(result.lockfile.importers)
expect(importerKeys).toEqual(['.'])
// The "." importer must have specifiers and dependencies
const rootImporter = result.lockfile.importers['.' as ProjectId]
expect(rootImporter).toBeTruthy()
expect(rootImporter.specifiers).toBeTruthy()
expect(rootImporter.dependencies).toBeTruthy()
expect(rootImporter.dependencies?.['is-positive']).toBeTruthy()
await result.fileDownloads
} finally {
clientStoreIndex.close()
await fs.rm(tmpClient, { recursive: true, force: true })
}
})
it('resolves a single dependency and returns lockfile + files', async () => {
// Create a client store in a temp directory
const tmpClient = await fs.mkdtemp(path.join(os.tmpdir(), 'pnpm-agent-test-client-'))
const clientStoreDir = path.join(tmpClient, 'store')
await fs.mkdir(clientStoreDir, { recursive: true })
const clientStoreIndex = new StoreIndex(clientStoreDir)
try {
const result = await fetchFromPnpmRegistry({
registryUrl: `http://localhost:${serverPort}`,
storeDir: clientStoreDir,
storeIndex: clientStoreIndex,
dependencies: {
'is-positive': '1.0.0',
},
})
// Verify lockfile was returned
expect(result.lockfile).toBeTruthy()
expect(result.lockfile.lockfileVersion).toBeTruthy()
// Verify packages were resolved
const packages = result.lockfile.packages ?? {}
const depPaths = Object.keys(packages)
expect(depPaths.length).toBeGreaterThanOrEqual(1)
// Verify at least one package has a resolution with integrity
const hasIntegrity = depPaths.some(dp => {
const pkg = packages[dp as DepPath]
return pkg?.resolution && typeof pkg.resolution === 'object' && 'integrity' in pkg.resolution
})
expect(hasIntegrity).toBe(true)
// Verify stats
expect(result.stats.totalPackages).toBeGreaterThanOrEqual(1)
await result.fileDownloads
} finally {
clientStoreIndex.close()
await fs.rm(tmpClient, { recursive: true, force: true })
}
})
it('returns consistent lockfile on repeated requests', async () => {
const tmpClient = await fs.mkdtemp(path.join(os.tmpdir(), 'pnpm-agent-test-client2-'))
const clientStoreDir = path.join(tmpClient, 'store')
await fs.mkdir(clientStoreDir, { recursive: true })
const clientStoreIndex = new StoreIndex(clientStoreDir)
try {
const result1 = await fetchFromPnpmRegistry({
registryUrl: `http://localhost:${serverPort}`,
storeDir: clientStoreDir,
storeIndex: clientStoreIndex,
dependencies: {
'is-positive': '1.0.0',
},
})
const result2 = await fetchFromPnpmRegistry({
registryUrl: `http://localhost:${serverPort}`,
storeDir: clientStoreDir,
storeIndex: clientStoreIndex,
dependencies: {
'is-positive': '1.0.0',
},
})
// Same dependency → same lockfile
expect(Object.keys(result1.lockfile.packages ?? {})).toEqual(
Object.keys(result2.lockfile.packages ?? {})
)
// Wait for file downloads to complete before cleanup
await result1.fileDownloads
await result2.fileDownloads
} finally {
clientStoreIndex.close()
await fs.rm(tmpClient, { recursive: true, force: true })
}
})
it('returns 404 for unknown endpoints', async () => {
const result = await new Promise<{ statusCode: number, body: string }>((resolve, reject) => {
const req = http.request(`http://localhost:${serverPort}/v1/unknown`, {
method: 'POST',
}, (res) => {
const chunks: Buffer[] = []
res.on('data', (chunk: Buffer) => chunks.push(chunk))
res.on('end', () => resolve({
statusCode: res.statusCode!,
body: Buffer.concat(chunks).toString(),
}))
})
req.on('error', reject)
req.end()
})
expect(result.statusCode).toBe(404)
})
})