Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 147 additions & 1 deletion tests/api/client.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';
import { afterEach, beforeEach, describe, it, mock } from 'node:test';
import { createApiClient, DEFAULT_API_URL } from '../../src/api/client.js';

describe('api/client', () => {
Expand Down Expand Up @@ -108,4 +108,150 @@ describe('api/client', () => {
assert.ok(userAgent.includes('(api)'));
});
});

describe('request', () => {
let originalFetch;
let mockFetch;

beforeEach(() => {
originalFetch = globalThis.fetch;
mockFetch = mock.fn();
globalThis.fetch = mockFetch;
});

afterEach(() => {
globalThis.fetch = originalFetch;
});

it('makes request to correct URL', async () => {
let client = createApiClient({
token: 'test-token',
baseUrl: 'https://api.test',
});

mockFetch.mock.mockImplementation(async () => ({
ok: true,
json: async () => ({ data: 'result' }),
}));

let result = await client.request('/api/builds');

assert.deepStrictEqual(result, { data: 'result' });
assert.strictEqual(mockFetch.mock.calls.length, 1);

let [url, options] = mockFetch.mock.calls[0].arguments;
assert.strictEqual(url, 'https://api.test/api/builds');
assert.ok(options.headers.Authorization.includes('test-token'));
});

it('passes through fetch options', async () => {
let client = createApiClient({
token: 'test-token',
baseUrl: 'https://api.test',
});

mockFetch.mock.mockImplementation(async () => ({
ok: true,
json: async () => ({}),
}));

await client.request('/api/builds', {
method: 'POST',
body: JSON.stringify({ name: 'test' }),
headers: { 'Content-Type': 'application/json' },
});

let [, options] = mockFetch.mock.calls[0].arguments;
assert.strictEqual(options.method, 'POST');
assert.strictEqual(options.headers['Content-Type'], 'application/json');
});

it('throws AuthError for 401 response', async () => {
let client = createApiClient({
token: 'test-token',
baseUrl: 'https://api.test',
});

mockFetch.mock.mockImplementation(async () => ({
ok: false,
status: 401,
headers: new Map(),
text: async () => 'Unauthorized',
}));

await assert.rejects(
() => client.request('/api/protected'),
error => {
assert.strictEqual(error.name, 'AuthError');
return true;
}
);
});

it('throws VizzlyError for server errors', async () => {
let client = createApiClient({
token: 'test-token',
baseUrl: 'https://api.test',
});

mockFetch.mock.mockImplementation(async () => ({
ok: false,
status: 500,
headers: new Map(),
text: async () => 'Internal Server Error',
}));

await assert.rejects(
() => client.request('/api/test'),
error => {
assert.strictEqual(error.name, 'VizzlyError');
return true;
}
);
});

it('throws VizzlyError for 403 forbidden', async () => {
let client = createApiClient({
token: 'test-token',
baseUrl: 'https://api.test',
});

mockFetch.mock.mockImplementation(async () => ({
ok: false,
status: 403,
headers: new Map(),
text: async () => 'Forbidden',
}));

await assert.rejects(
() => client.request('/api/admin'),
error => {
assert.strictEqual(error.name, 'VizzlyError');
return true;
}
);
});

it('throws VizzlyError for 404 not found', async () => {
let client = createApiClient({
token: 'test-token',
baseUrl: 'https://api.test',
});

mockFetch.mock.mockImplementation(async () => ({
ok: false,
status: 404,
headers: new Map(),
text: async () => 'Not Found',
}));

await assert.rejects(
() => client.request('/api/missing'),
error => {
assert.strictEqual(error.name, 'VizzlyError');
return true;
}
);
});
});
});
Loading