primary-runtime-preparation.spec.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { createHash } from 'node:crypto'
  2. import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { expect, it } from 'vitest'
  6. import { downloadPrimaryRuntimeAsset, unpackPrimaryRuntimeWheel } from '../scripts/prepare-primary-runtime.ts'
  7. const libraryWheel = Buffer.from('UEsDBAoAAAAAAASeLl0sYMPjDAAAAAwAAAAJAAAAc2FtcGxlLnB5c2FtcGxlID0gNDIKUEsBAh4DCgAAAAAABJ4uXSxgw+MMAAAADAAAAAkAAAAAAAAAAQAAAKSBAAAAAHNhbXBsZS5weVBLBQYAAAAAAQABADcAAAAzAAAAAAA=', 'base64')
  8. const relocatedWheel = Buffer.from('UEsDBAoAAAAAAASeLl3x0Nj9FAAAABQAAAAeAAAAc2FtcGxlLTEuMC5kYXRhL3NjcmlwdHMvc2FtcGxlcmVxdWlyZXMgcmVsb2NhdGlvbgpQSwECHgMKAAAAAAAEni5d8dDY/RQAAAAUAAAAHgAAAAAAAAABAAAApIEAAAAAc2FtcGxlLTEuMC5kYXRhL3NjcmlwdHMvc2FtcGxlUEsFBgAAAAABAAEATAAAAFAAAAAAAA==', 'base64')
  9. it('extracts a hash-verified cached library without a Python installer or network request', async () => {
  10. const root = await mkdtemp(join(tmpdir(), 'desktop-wheel-'))
  11. try {
  12. const hash = createHash('sha256').update(libraryWheel).digest('hex')
  13. const archive = join(root, hash)
  14. await writeFile(archive, libraryWheel)
  15. expect(await downloadPrimaryRuntimeAsset('https://unused.invalid/library.whl', hash, root)).toBe(archive)
  16. await unpackPrimaryRuntimeWheel(archive, join(root, 'site-packages'))
  17. expect(await readFile(join(root, 'site-packages/sample.py'), 'utf8')).toBe('sample = 42\n')
  18. await writeFile(archive, 'corrupt archive')
  19. await expect(downloadPrimaryRuntimeAsset('https://unused.invalid/library.whl', hash, root)).rejects.toThrow('checksum mismatch')
  20. } finally {
  21. await rm(root, { recursive: true, force: true })
  22. }
  23. })
  24. it('rejects wheels that need installation outside site-packages', async () => {
  25. const root = await mkdtemp(join(tmpdir(), 'desktop-wheel-'))
  26. try {
  27. const archive = join(root, 'relocated.whl')
  28. await writeFile(archive, relocatedWheel)
  29. await expect(unpackPrimaryRuntimeWheel(archive, join(root, 'site-packages'))).rejects.toThrow('unsupported installation paths')
  30. } finally {
  31. await rm(root, { recursive: true, force: true })
  32. }
  33. })