|
| 1 | +import { OsvOfflineDb } from '@renovatebot/osv-offline-db'; |
| 2 | +import fs from 'fs-extra'; |
| 3 | +import got from 'got'; |
| 4 | +import path from 'path'; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | +import { tryDownloadDb } from './download'; |
| 7 | + |
| 8 | +vi.mock('got', () => ({ |
| 9 | + default: { |
| 10 | + stream: vi.fn(), |
| 11 | + }, |
| 12 | +})); |
| 13 | + |
| 14 | +describe('packages/osv-offline/src/lib/download.unit', () => { |
| 15 | + describe('tryDownloadDb', () => { |
| 16 | + beforeEach(async () => { |
| 17 | + delete process.env.OSV_OFFLINE_DISABLE_DOWNLOAD; |
| 18 | + delete process.env.OSV_OFFLINE_DATABASE_URL; |
| 19 | + const zipFilePath = path.join( |
| 20 | + OsvOfflineDb.rootDirectory, |
| 21 | + 'osv-offline.zip' |
| 22 | + ); |
| 23 | + await fs.rm(zipFilePath, { force: true }); |
| 24 | + vi.clearAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + vi.restoreAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('uses default URL when OSV_OFFLINE_DATABASE_URL is not set', async () => { |
| 32 | + vi.mocked(got.stream).mockImplementationOnce(() => { |
| 33 | + throw new Error('intentional stream error'); |
| 34 | + }); |
| 35 | + |
| 36 | + const result = await tryDownloadDb(); |
| 37 | + |
| 38 | + expect(result.success).toBe(false); |
| 39 | + expect(vi.mocked(got.stream)).toHaveBeenCalledWith( |
| 40 | + 'https://github.com/renovatebot/osv-offline/releases/latest/download/osv-offline.zip' |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('uses OSV_OFFLINE_DATABASE_URL when set', async () => { |
| 45 | + const customUrl = 'https://example.com/custom-db.zip'; |
| 46 | + process.env.OSV_OFFLINE_DATABASE_URL = customUrl; |
| 47 | + |
| 48 | + vi.mocked(got.stream).mockImplementationOnce(() => { |
| 49 | + throw new Error('intentional stream error'); |
| 50 | + }); |
| 51 | + |
| 52 | + const result = await tryDownloadDb(); |
| 53 | + |
| 54 | + expect(result.success).toBe(false); |
| 55 | + expect(vi.mocked(got.stream)).toHaveBeenCalledWith(customUrl); |
| 56 | + }); |
| 57 | + }); |
| 58 | +}); |
0 commit comments