mirror of
https://github.com/pnpm/pnpm.git
synced 2026-04-10 10:08:15 -04:00
fix: update client protocol tests for slim response format
This commit is contained in:
@@ -4,7 +4,6 @@ import type { DepPath } from '@pnpm/types'
|
||||
|
||||
describe('protocol decoding', () => {
|
||||
it('decodes a response with metadata and files', async () => {
|
||||
// Build a response buffer manually matching the binary protocol spec
|
||||
const metadata: ResponseMetadata = {
|
||||
lockfile: {
|
||||
lockfileVersion: '9.0',
|
||||
@@ -20,16 +19,9 @@ describe('protocol decoding', () => {
|
||||
},
|
||||
},
|
||||
} as any, // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
packageFiles: {
|
||||
'/is-positive/1.0.0': {
|
||||
integrity: 'sha512-test123',
|
||||
algo: 'sha512',
|
||||
files: {
|
||||
'index.js': { digest: 'a'.repeat(128), size: 11, mode: 0o644 },
|
||||
},
|
||||
},
|
||||
},
|
||||
missingDigests: ['a'.repeat(128)],
|
||||
missingFiles: [
|
||||
{ digest: 'a'.repeat(128), size: 11, executable: false },
|
||||
],
|
||||
stats: {
|
||||
totalPackages: 1,
|
||||
alreadyInStore: 0,
|
||||
@@ -44,46 +36,39 @@ describe('protocol decoding', () => {
|
||||
const jsonBuf = Buffer.from(JSON.stringify(metadata), 'utf-8')
|
||||
const parts: Buffer[] = []
|
||||
|
||||
// 4-byte JSON length
|
||||
const lenBuf = Buffer.alloc(4)
|
||||
lenBuf.writeUInt32BE(jsonBuf.length, 0)
|
||||
parts.push(lenBuf)
|
||||
|
||||
// JSON metadata
|
||||
parts.push(jsonBuf)
|
||||
|
||||
// File entry: 64-byte digest + 4-byte size + 1-byte mode + content
|
||||
const fileContent = Buffer.from('hello world')
|
||||
const digestBuf = Buffer.from('a'.repeat(128), 'hex') // 64 bytes
|
||||
const digestBuf = Buffer.from('a'.repeat(128), 'hex')
|
||||
const sizeBuf = Buffer.alloc(4)
|
||||
sizeBuf.writeUInt32BE(fileContent.length, 0)
|
||||
const modeBuf = Buffer.alloc(1, 0x00) // non-executable
|
||||
const modeBuf = Buffer.alloc(1, 0x00)
|
||||
|
||||
parts.push(digestBuf)
|
||||
parts.push(sizeBuf)
|
||||
parts.push(modeBuf)
|
||||
parts.push(fileContent)
|
||||
|
||||
// End marker: 64 zero bytes
|
||||
parts.push(Buffer.alloc(64, 0))
|
||||
|
||||
const responseBuf = Buffer.concat(parts)
|
||||
|
||||
// Decode
|
||||
async function * toStream (): AsyncIterable<Buffer> {
|
||||
yield responseBuf
|
||||
}
|
||||
|
||||
const { metadata: decoded, files } = await decodeResponse(toStream())
|
||||
|
||||
// Verify metadata
|
||||
expect(decoded.stats.totalPackages).toBe(1)
|
||||
expect(decoded.stats.filesToDownload).toBe(1)
|
||||
expect(decoded.missingDigests).toEqual(['a'.repeat(128)])
|
||||
expect(decoded.packageFiles['/is-positive/1.0.0'].integrity).toBe('sha512-test123')
|
||||
expect(decoded.missingFiles).toHaveLength(1)
|
||||
expect(decoded.missingFiles[0].digest).toBe('a'.repeat(128))
|
||||
expect(decoded.lockfile.packages?.['/is-positive/1.0.0' as DepPath]).toBeTruthy()
|
||||
|
||||
// Verify file
|
||||
expect(files).toHaveLength(1)
|
||||
expect(files[0].digest).toBe('a'.repeat(128))
|
||||
expect(files[0].size).toBe(11)
|
||||
@@ -94,8 +79,9 @@ describe('protocol decoding', () => {
|
||||
it('decodes a response with executable files', async () => {
|
||||
const metadata: ResponseMetadata = {
|
||||
lockfile: { lockfileVersion: '9.0', importers: {} } as any, // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
packageFiles: {},
|
||||
missingDigests: ['b'.repeat(128)],
|
||||
missingFiles: [
|
||||
{ digest: 'b'.repeat(128), size: 10, executable: true },
|
||||
],
|
||||
stats: {
|
||||
totalPackages: 0,
|
||||
alreadyInStore: 0,
|
||||
@@ -103,7 +89,7 @@ describe('protocol decoding', () => {
|
||||
filesInNewPackages: 0,
|
||||
filesAlreadyInCafs: 0,
|
||||
filesToDownload: 1,
|
||||
downloadBytes: 4,
|
||||
downloadBytes: 10,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -115,17 +101,16 @@ describe('protocol decoding', () => {
|
||||
parts.push(lenBuf)
|
||||
parts.push(jsonBuf)
|
||||
|
||||
// Executable file
|
||||
const digestBuf = Buffer.from('b'.repeat(128), 'hex')
|
||||
const sizeBuf = Buffer.alloc(4)
|
||||
const content = Buffer.from('#!/bin/sh\n')
|
||||
const sizeBuf = Buffer.alloc(4)
|
||||
sizeBuf.writeUInt32BE(content.length, 0)
|
||||
parts.push(digestBuf)
|
||||
parts.push(sizeBuf)
|
||||
parts.push(Buffer.from([0x01])) // executable
|
||||
parts.push(content)
|
||||
|
||||
parts.push(Buffer.alloc(64, 0)) // end marker
|
||||
parts.push(Buffer.alloc(64, 0))
|
||||
|
||||
async function * toStream (): AsyncIterable<Buffer> {
|
||||
yield Buffer.concat(parts)
|
||||
@@ -141,8 +126,7 @@ describe('protocol decoding', () => {
|
||||
it('decodes a response with no files', async () => {
|
||||
const metadata: ResponseMetadata = {
|
||||
lockfile: { lockfileVersion: '9.0', importers: {} } as any, // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||
packageFiles: {},
|
||||
missingDigests: [],
|
||||
missingFiles: [],
|
||||
stats: {
|
||||
totalPackages: 5,
|
||||
alreadyInStore: 5,
|
||||
@@ -161,7 +145,7 @@ describe('protocol decoding', () => {
|
||||
lenBuf.writeUInt32BE(jsonBuf.length, 0)
|
||||
parts.push(lenBuf)
|
||||
parts.push(jsonBuf)
|
||||
parts.push(Buffer.alloc(64, 0)) // end marker immediately
|
||||
parts.push(Buffer.alloc(64, 0))
|
||||
|
||||
async function * toStream (): AsyncIterable<Buffer> {
|
||||
yield Buffer.concat(parts)
|
||||
|
||||
Reference in New Issue
Block a user