| 123456789101112131415161718192021222324252627282930313233343536 |
- import { createHash } from 'node:crypto'
- import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
- import { tmpdir } from 'node:os'
- import { join } from 'node:path'
- import { expect, it } from 'vitest'
- import { downloadPrimaryRuntimeAsset, unpackPrimaryRuntimeWheel } from '../scripts/prepare-primary-runtime.ts'
- const libraryWheel = Buffer.from('UEsDBAoAAAAAAASeLl0sYMPjDAAAAAwAAAAJAAAAc2FtcGxlLnB5c2FtcGxlID0gNDIKUEsBAh4DCgAAAAAABJ4uXSxgw+MMAAAADAAAAAkAAAAAAAAAAQAAAKSBAAAAAHNhbXBsZS5weVBLBQYAAAAAAQABADcAAAAzAAAAAAA=', 'base64')
- const relocatedWheel = Buffer.from('UEsDBAoAAAAAAASeLl3x0Nj9FAAAABQAAAAeAAAAc2FtcGxlLTEuMC5kYXRhL3NjcmlwdHMvc2FtcGxlcmVxdWlyZXMgcmVsb2NhdGlvbgpQSwECHgMKAAAAAAAEni5d8dDY/RQAAAAUAAAAHgAAAAAAAAABAAAApIEAAAAAc2FtcGxlLTEuMC5kYXRhL3NjcmlwdHMvc2FtcGxlUEsFBgAAAAABAAEATAAAAFAAAAAAAA==', 'base64')
- it('extracts a hash-verified cached library without a Python installer or network request', async () => {
- const root = await mkdtemp(join(tmpdir(), 'desktop-wheel-'))
- try {
- const hash = createHash('sha256').update(libraryWheel).digest('hex')
- const archive = join(root, hash)
- await writeFile(archive, libraryWheel)
- expect(await downloadPrimaryRuntimeAsset('https://unused.invalid/library.whl', hash, root)).toBe(archive)
- await unpackPrimaryRuntimeWheel(archive, join(root, 'site-packages'))
- expect(await readFile(join(root, 'site-packages/sample.py'), 'utf8')).toBe('sample = 42\n')
- await writeFile(archive, 'corrupt archive')
- await expect(downloadPrimaryRuntimeAsset('https://unused.invalid/library.whl', hash, root)).rejects.toThrow('checksum mismatch')
- } finally {
- await rm(root, { recursive: true, force: true })
- }
- })
- it('rejects wheels that need installation outside site-packages', async () => {
- const root = await mkdtemp(join(tmpdir(), 'desktop-wheel-'))
- try {
- const archive = join(root, 'relocated.whl')
- await writeFile(archive, relocatedWheel)
- await expect(unpackPrimaryRuntimeWheel(archive, join(root, 'site-packages'))).rejects.toThrow('unsupported installation paths')
- } finally {
- await rm(root, { recursive: true, force: true })
- }
- })
|