Skip to content

A full-featured download manager.

9.2 KB
Raw Blame
import { mkdtemp, readFile, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { Readable } from 'node:stream'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

const mockRequest = vi.fn()

const { downloadGithubMoext, parseGithubSpec } = await import(
  './github-fetcher'
)

let tmp: string

beforeEach(async () => {
  tmp = await mkdtemp(path.join(tmpdir(), 'motrix-gh-'))
  mockRequest.mockReset()
  vi.stubGlobal('fetch', mockRequest)
})

afterEach(async () => {
  vi.unstubAllGlobals()
  await rm(tmp, { recursive: true, force: true })
})

function jsonResponse(payload: unknown, statusCode = 200) {
  return new Response(JSON.stringify(payload), { status: statusCode })
}

function streamResponse(
  bytes: Buffer,
  statusCode = 200,
  headers: Record<string, string> = {}
) {
  return new Response(Uint8Array.from(bytes), { status: statusCode, headers })
}

describe('parseGithubSpec', () => {
  it('parses owner/repo without tag', () => {
    expect(parseGithubSpec('acme/widget')).toEqual({
      owner: 'acme',
      repo: 'widget',
    })
  })

  it('parses owner/repo@tag', () => {
    expect(parseGithubSpec('acme/[email protected]')).toEqual({
      owner: 'acme',
      repo: 'widget',
      tag: 'v1.2.3',
    })
  })

  it('rejects malformed spec without slash', () => {
    expect(() => parseGithubSpec('no-slash')).toThrow(/invalid_github_spec/)
  })

  it('rejects whitespace in spec', () => {
    expect(() => parseGithubSpec('a b/c')).toThrow(/invalid_github_spec/)
  })
})

describe('downloadGithubMoext', () => {
  it('uses /releases/latest when no tag is provided', async () => {
    mockRequest
      .mockResolvedValueOnce(
        jsonResponse({
          tag_name: 'v0.1.0',
          assets: [
            {
              name: 'widget-0.1.0.moext',
              browser_download_url: 'https://gh/assets/widget.moext',
            },
          ],
        })
      )
      .mockResolvedValueOnce(streamResponse(Buffer.from('zip-bytes-here')))

    const dest = path.join(tmp, 'widget.moext')
    const out = await downloadGithubMoext(
      { owner: 'acme', repo: 'widget' },
      dest
    )

    expect(out).toEqual({ tag: 'v0.1.0', assetName: 'widget-0.1.0.moext' })
    expect(mockRequest.mock.calls[0]?.[0]).toBe(
      'https://api.github.com/repos/acme/widget/releases/latest'
    )
    expect(mockRequest.mock.calls[1]?.[0]).toBe(
      'https://gh/assets/widget.moext'
    )
    expect((await readFile(dest)).toString()).toBe('zip-bytes-here')
  })

  it('uses /releases/tags/<tag> when a tag is provided', async () => {
    mockRequest
      .mockResolvedValueOnce(
        jsonResponse({
          tag_name: 'v1.2.3',
          assets: [
            {
              name: 'widget-1.2.3.moext',
              browser_download_url: 'https://gh/assets/widget-1.2.3.moext',
            },
          ],
        })
      )
      .mockResolvedValueOnce(streamResponse(Buffer.from('bytes')))

    const dest = path.join(tmp, 'tagged.moext')
    await downloadGithubMoext(
      { owner: 'acme', repo: 'widget', tag: 'v1.2.3' },
      dest
    )
    expect(mockRequest.mock.calls[0]?.[0]).toBe(
      'https://api.github.com/repos/acme/widget/releases/tags/v1.2.3'
    )
  })

  it('URL-encodes a tag before putting it in the GitHub API path', async () => {
    mockRequest
      .mockResolvedValueOnce(
        jsonResponse({
          tag_name: 'release/1',
          assets: [
            {
              name: 'widget.moext',
              browser_download_url: 'https://gh/assets/widget.moext',
            },
          ],
        })
      )
      .mockResolvedValueOnce(streamResponse(Buffer.from('bytes')))

    await downloadGithubMoext(
      { owner: 'acme', repo: 'widget', tag: 'release/1' },
      path.join(tmp, 'tagged-slash.moext')
    )
    expect(mockRequest.mock.calls[0]?.[0]).toBe(
      'https://api.github.com/repos/acme/widget/releases/tags/release%2F1'
    )
  })

  it('rejects when GitHub returns non-200', async () => {
    const destroy = vi.fn()
    mockRequest.mockResolvedValueOnce({
      status: 404,
      body: { cancel: async () => destroy() },
    })
    await expect(
      downloadGithubMoext(
        { owner: 'acme', repo: 'widget' },
        path.join(tmp, 'x.moext')
      )
    ).rejects.toMatchObject({
      message: 'plugin.install.gh_release_unavailable: 404',
    })
    expect(destroy).toHaveBeenCalledOnce()
  })

  it('rejects invalid metadata JSON after consuming its bounded body', async () => {
    mockRequest.mockResolvedValueOnce(new Response('invalid JSON'))
    await expect(
      downloadGithubMoext(
        { owner: 'acme', repo: 'widget' },
        path.join(tmp, 'x.moext')
      )
    ).rejects.toBeInstanceOf(SyntaxError)
  })

  it('rejects when no asset ends with .moext', async () => {
    mockRequest.mockResolvedValueOnce(
      jsonResponse({
        tag_name: 'v0.1.0',
        assets: [
          {
            name: 'source.zip',
            browser_download_url: 'https://gh/assets/source.zip',
          },
        ],
      })
    )
    await expect(
      downloadGithubMoext(
        { owner: 'acme', repo: 'widget' },
        path.join(tmp, 'x.moext')
      )
    ).rejects.toMatchObject({ message: 'plugin.install.no_moext_asset' })
  })

  it('rejects when asset download itself fails', async () => {
    const destroy = vi.fn()
    const failedBody = new ReadableStream({ cancel: destroy })
    mockRequest
      .mockResolvedValueOnce(
        jsonResponse({
          tag_name: 'v0.1.0',
          assets: [
            {
              name: 'x.moext',
              browser_download_url: 'https://gh/assets/x.moext',
            },
          ],
        })
      )
      .mockResolvedValueOnce(new Response(failedBody, { status: 500 }))
    await expect(
      downloadGithubMoext(
        { owner: 'acme', repo: 'widget' },
        path.join(tmp, 'x.moext')
      )
    ).rejects.toMatchObject({
      message: 'plugin.install.gh_asset_download_failed: 500',
    })
    expect(destroy).toHaveBeenCalledOnce()
  })

  it('cancels a failing asset stream and removes any destination file', async () => {
    let emitted = false
    const failedBody = new Readable({
      read() {
        if (emitted) return
        emitted = true
        this.push(Buffer.from('partial'))
        this.destroy(new Error('asset stream failed'))
      },
    })
    const destroy = vi.spyOn(failedBody, 'destroy')
    mockRequest
      .mockResolvedValueOnce(
        jsonResponse({
          tag_name: 'v0.1.0',
          assets: [
            {
              name: 'x.moext',
              browser_download_url: 'https://gh/assets/x.moext',
            },
          ],
        })
      )
      .mockResolvedValueOnce(
        new Response(Readable.toWeb(failedBody) as ReadableStream<Uint8Array>)
      )
    const destination = path.join(tmp, 'x.moext')

    await expect(
      downloadGithubMoext({ owner: 'acme', repo: 'widget' }, destination)
    ).rejects.toThrow('asset stream failed')

    expect(destroy).toHaveBeenCalled()
    await expect(readFile(destination)).rejects.toMatchObject({
      code: 'ENOENT',
    })
  })

  it('rejects an insecure GitHub asset URL before downloading it', async () => {
    mockRequest.mockResolvedValueOnce(
      jsonResponse({
        tag_name: 'v1.0.0',
        assets: [
          {
            name: 'x.moext',
            browser_download_url: 'http://assets.example/x.moext',
          },
        ],
      })
    )

    await expect(
      downloadGithubMoext(
        { owner: 'acme', repo: 'widget', tag: 'v1.0.0' },
        path.join(tmp, 'x.moext')
      )
    ).rejects.toMatchObject({
      message: 'plugin.install.gh_asset_insecure_url',
    })
    expect(mockRequest).toHaveBeenCalledOnce()
  })

  it('rejects a GitHub asset redirect that downgrades HTTPS', async () => {
    mockRequest
      .mockResolvedValueOnce(
        jsonResponse({
          tag_name: 'v1.0.0',
          assets: [
            {
              name: 'x.moext',
              browser_download_url: 'https://assets.example/x.moext',
            },
          ],
        })
      )
      .mockResolvedValueOnce(
        streamResponse(Buffer.alloc(0), 302, {
          location: 'http://cdn.example/x.moext',
        })
      )

    await expect(
      downloadGithubMoext(
        { owner: 'acme', repo: 'widget', tag: 'v1.0.0' },
        path.join(tmp, 'x.moext')
      )
    ).rejects.toMatchObject({
      message: 'plugin.install.redirect_protocol_downgrade',
    })
    expect(mockRequest).toHaveBeenCalledTimes(2)
  })

  it('rejects a GitHub asset after five redirects', async () => {
    mockRequest.mockResolvedValueOnce(
      jsonResponse({
        tag_name: 'v1.0.0',
        assets: [
          {
            name: 'x.moext',
            browser_download_url: 'https://assets.example/x.moext',
          },
        ],
      })
    )
    for (let hop = 0; hop < 6; hop += 1) {
      mockRequest.mockResolvedValueOnce(
        streamResponse(Buffer.alloc(0), 302, {
          location: `https://cdn.example/hop-${hop}.moext`,
        })
      )
    }

    await expect(
      downloadGithubMoext(
        { owner: 'acme', repo: 'widget', tag: 'v1.0.0' },
        path.join(tmp, 'x.moext')
      )
    ).rejects.toMatchObject({
      message: 'plugin.install.too_many_redirects',
    })
    expect(mockRequest).toHaveBeenCalledTimes(7)
  })
})