this repo has no description
at main 94 lines 2.9 kB view raw
1import assert from 'node:assert/strict' 2import { describe, test } from 'node:test' 3import { fetchReleaseInfo } from '../src/github.js' 4 5const mockReleaseData = { 6 tag_name: 'v0.20.0', 7 assets: [ 8 { 9 name: 'zola-v0.20.0-x86_64-unknown-linux-gnu.tar.gz', 10 browser_download_url: 11 'https://github.com/getzola/zola/releases/download/v0.20.0/zola-v0.20.0-x86_64-unknown-linux-gnu.tar.gz', 12 size: 15728640, 13 }, 14 { 15 name: 'zola-v0.20.0-x86_64-apple-darwin.tar.gz', 16 browser_download_url: 17 'https://github.com/getzola/zola/releases/download/v0.20.0/zola-v0.20.0-x86_64-apple-darwin.tar.gz', 18 size: 16777216, 19 }, 20 ], 21} 22 23describe('GitHub API Integration', () => { 24 test('fetchReleaseInfo returns release data for valid repo/version', async (t) => { 25 t.mock.method(global, 'fetch', async (url: string) => { 26 if (url === 'https://api.github.com/repos/getzola/zola/releases/tags/v0.20.0') { 27 return { 28 ok: true, 29 json: async () => mockReleaseData, 30 } as Response 31 } 32 throw new Error('Unexpected fetch call') 33 }) 34 35 const release = await fetchReleaseInfo('getzola/zola', 'v0.20.0') 36 37 assert.equal(typeof release.tag_name, 'string') 38 assert.equal(release.tag_name, 'v0.20.0') 39 assert.equal(Array.isArray(release.assets), true) 40 assert.equal(release.assets.length > 0, true) 41 42 // Check asset structure 43 const asset = release.assets[0] 44 assert.equal(typeof asset.name, 'string') 45 assert.equal(typeof asset.download_url, 'string') 46 assert.equal(typeof asset.size, 'number') 47 assert.equal(asset.download_url.startsWith('https://'), true) 48 }) 49 50 test('fetchReleaseInfo throws error for non-existent repo', async (t) => { 51 t.mock.method(global, 'fetch', async () => { 52 return { 53 ok: false, 54 status: 404, 55 statusText: 'Not Found', 56 } as Response 57 }) 58 59 await assert.rejects( 60 async () => await fetchReleaseInfo('nonexistent/repo', 'v1.0.0'), 61 /Release v1.0.0 not found for repository nonexistent\/repo/, 62 ) 63 }) 64 65 test('fetchReleaseInfo throws error for non-existent version', async (t) => { 66 t.mock.method(global, 'fetch', async () => { 67 return { 68 ok: false, 69 status: 404, 70 statusText: 'Not Found', 71 } as Response 72 }) 73 74 await assert.rejects( 75 async () => await fetchReleaseInfo('getzola/zola', 'v999.999.999'), 76 /Release v999.999.999 not found for repository getzola\/zola/, 77 ) 78 }) 79 80 test('fetchReleaseInfo handles malformed repo name', async (t) => { 81 t.mock.method(global, 'fetch', async () => { 82 return { 83 ok: false, 84 status: 404, 85 statusText: 'Not Found', 86 } as Response 87 }) 88 89 await assert.rejects( 90 async () => await fetchReleaseInfo('invalid-repo-name', 'v1.0.0'), 91 /Release v1.0.0 not found for repository invalid-repo-name/, 92 ) 93 }) 94})