import { describe, expect, it, vi } from 'vitest'; import { listTags } from '../src/github.js'; // Vitest statically hoists `vi.mock(...)` calls above every static // `import` in the file, so the factory below is registered before // `../src/github.js` is evaluated even though it appears afterwards // textually. vi.mock('@actions/github', () => ({ context: { repo: { owner: 'mock-owner', repo: 'mock-repo' } }, getOctokit: vi.fn().mockReturnValue({ rest: { repos: { listTags: vi.fn().mockImplementation(({ page }: { page: number }) => { if (page === 6) { return { data: [] }; } const res = [...new Array(100).keys()].map((_) => ({ name: `v0.0.${_ + (page - 1) * 100}`, commit: { sha: 'string', url: 'string' }, zipball_url: 'string', tarball_url: 'string', node_id: 'string', })); return { data: res }; }), }, }, }), })); describe('github', () => { it('returns all tags', async () => { const tags = await listTags(true); expect(tags.length).toEqual(500); expect(tags[499]).toEqual({ name: 'v0.0.499', commit: { sha: 'string', url: 'string' }, zipball_url: 'string', tarball_url: 'string', node_id: 'string', }); }); it('returns only the first page when shouldFetchAllTags is false', async () => { const tags = await listTags(false); expect(tags.length).toEqual(100); expect(tags[99]).toEqual({ name: 'v0.0.99', commit: { sha: 'string', url: 'string' }, zipball_url: 'string', tarball_url: 'string', node_id: 'string', }); }); });