github.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import * as crypto from 'crypto'
  2. import * as fs from 'fs'
  3. import * as http from 'http'
  4. import type { AddressInfo } from 'net'
  5. import * as os from 'os'
  6. import * as path from 'path'
  7. import { afterAll, beforeAll, describe, expect, it } from 'vitest'
  8. import { downloadVerified, selectableReleases, type GithubAsset, type GithubRelease } from './github'
  9. const body = Buffer.from('engine archive bytes '.repeat(500))
  10. const sha256 = crypto.createHash('sha256').update(body).digest('hex')
  11. let server: http.Server
  12. let url: string
  13. const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'wem-github-'))
  14. beforeAll(async () => {
  15. server = http.createServer((_req, res) => {
  16. res.writeHead(200, { 'Content-Length': body.length })
  17. res.end(body)
  18. })
  19. await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
  20. url = `http://127.0.0.1:${(server.address() as AddressInfo).port}/engine.tar.gz`
  21. })
  22. afterAll(() => {
  23. server.close()
  24. fs.rmSync(tmp, { recursive: true, force: true })
  25. })
  26. const asset = (extra: Partial<GithubAsset> = {}): GithubAsset => ({
  27. name: 'engine.tar.gz',
  28. browser_download_url: url,
  29. size: body.length,
  30. digest: `sha256:${sha256}`,
  31. ...extra
  32. })
  33. describe('downloadVerified', () => {
  34. it('keeps a download that matches the release', async () => {
  35. const dest = path.join(tmp, 'good')
  36. await downloadVerified(asset(), dest, () => {})
  37. expect(fs.readFileSync(dest).equals(body)).toBe(true)
  38. })
  39. it('removes a download that does not match its checksum', async () => {
  40. const dest = path.join(tmp, 'tampered')
  41. await expect(downloadVerified(asset({ digest: `sha256:${'0'.repeat(64)}` }), dest, () => {})).rejects.toThrow(/checksum/)
  42. expect(fs.existsSync(dest)).toBe(false)
  43. })
  44. it('removes a download that is not the size the release says', async () => {
  45. const dest = path.join(tmp, 'short')
  46. await expect(downloadVerified(asset({ size: body.length + 1 }), dest, () => {})).rejects.toThrow(/bytes/)
  47. expect(fs.existsSync(dest)).toBe(false)
  48. })
  49. it('accepts an asset GitHub gave no digest for when the size is right', async () => {
  50. const dest = path.join(tmp, 'nodigest')
  51. await downloadVerified(asset({ digest: null }), dest, () => {})
  52. expect(fs.existsSync(dest)).toBe(true)
  53. })
  54. })
  55. const release = (tag_name: string, draft = false) =>
  56. ({ tag_name, draft, html_url: '', body: null, published_at: '', assets: [] }) as GithubRelease
  57. describe('selectableReleases', () => {
  58. it('keeps v-tagged releases and drops helper releases and drafts', () => {
  59. const list = [release('v0.9.8'), release('libnode-arch'), release('libnode (Arch)'), release('v0.9.7-rc1'), release('v0.9.6', true), release('vim')]
  60. expect(selectableReleases(list).map(r => r.tag_name)).toEqual(['v0.9.8', 'v0.9.7-rc1'])
  61. })
  62. })