adapters.gitlab.spec.ts•2.07 kB
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GitLabAdapter } from '../src/adapters/gitlab.js';
vi.mock('../src/lib/http.js', () => {
return {
requestJson: vi.fn(),
};
});
import { requestJson } from '../src/lib/http.js';
describe('GitLabAdapter', () => {
const adapter = new GitLabAdapter({ url: 'https://gitlab.example', token: 't' });
beforeEach(() => {
vi.clearAllMocks();
});
it('paginates listProjects using x-next-page', async () => {
(requestJson as any).mockImplementation((url: string) => {
if (url.includes('page=1')) return Promise.resolve({ status: 200, headers: { 'x-next-page': '2' }, data: [{ id: 1 }, { id: 2 }] });
if (url.includes('page=2')) return Promise.resolve({ status: 200, headers: { 'x-next-page': '' }, data: [{ id: 3 }] });
throw new Error('unexpected url ' + url);
});
const res = await adapter.listProjects(100);
expect(res).toHaveLength(3);
});
it('paginates listMergeRequests and respects state', async () => {
(requestJson as any).mockImplementation((url: string) => {
expect(url).toContain('state=opened');
if (url.includes('page=1')) return Promise.resolve({ status: 200, headers: { 'x-next-page': '2' }, data: [{ id: 10 }] });
if (url.includes('page=2')) return Promise.resolve({ status: 200, headers: { 'x-next-page': '' }, data: [{ id: 11 }] });
throw new Error('unexpected url ' + url);
});
const res = await adapter.listMergeRequests(123, 'opened', 100);
expect(res).toHaveLength(2);
});
it('paginates listIssues', async () => {
(requestJson as any).mockImplementation((url: string) => {
if (url.includes('page=1')) return Promise.resolve({ status: 200, headers: { 'x-next-page': '2' }, data: [{ id: 201 }] });
if (url.includes('page=2')) return Promise.resolve({ status: 200, headers: { 'x-next-page': '' }, data: [] });
throw new Error('unexpected url ' + url);
});
const res = await adapter.listIssues(999, 100);
expect(res).toHaveLength(1);
});
});