Skip to content
Open
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
33 changes: 22 additions & 11 deletions dashboard/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions dashboard/src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,26 @@ import {
addProxyKey,
deleteProxyKey,
fetchProxyHealth,
setAdminToken,
clearAdminToken,
} from './api';

// Mock fetch
const mockFetch = vi.fn();
vi.stubGlobal('fetch', mockFetch);

const TEST_TOKEN = 'test-admin-token';
const AUTH_HEADER = { Authorization: `Bearer ${TEST_TOKEN}` };

describe('API Client', () => {
beforeEach(() => {
mockFetch.mockReset();
setAdminToken(TEST_TOKEN);
});

afterEach(() => {
vi.clearAllMocks();
clearAdminToken();
});

describe('fetchBots', () => {
Expand All @@ -44,7 +51,7 @@ describe('API Client', () => {
const result = await fetchBots();

expect(result).toEqual(bots);
expect(mockFetch).toHaveBeenCalledWith('/api/bots');
expect(mockFetch).toHaveBeenCalledWith('/api/bots', { headers: AUTH_HEADER });
});

it('should throw on error response', async () => {
Expand Down Expand Up @@ -80,7 +87,7 @@ describe('API Client', () => {
const result = await fetchBot('bot-1');

expect(result).toEqual(bot);
expect(mockFetch).toHaveBeenCalledWith('/api/bots/bot-1');
expect(mockFetch).toHaveBeenCalledWith('/api/bots/bot-1', { headers: AUTH_HEADER });
});
});

Expand Down Expand Up @@ -108,7 +115,7 @@ describe('API Client', () => {
expect(result).toEqual(createdBot);
expect(mockFetch).toHaveBeenCalledWith('/api/bots', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: { 'Content-Type': 'application/json', ...AUTH_HEADER },
body: JSON.stringify(input),
});
});
Expand All @@ -124,6 +131,7 @@ describe('API Client', () => {
await expect(deleteBot('bot-1')).resolves.toBeUndefined();
expect(mockFetch).toHaveBeenCalledWith('/api/bots/bot-1', {
method: 'DELETE',
headers: AUTH_HEADER,
});
});
});
Expand All @@ -138,6 +146,7 @@ describe('API Client', () => {
await expect(startBot('bot-1')).resolves.toBeUndefined();
expect(mockFetch).toHaveBeenCalledWith('/api/bots/bot-1/start', {
method: 'POST',
headers: AUTH_HEADER,
});
});
});
Expand All @@ -152,6 +161,7 @@ describe('API Client', () => {
await expect(stopBot('bot-1')).resolves.toBeUndefined();
expect(mockFetch).toHaveBeenCalledWith('/api/bots/bot-1/stop', {
method: 'POST',
headers: AUTH_HEADER,
});
});
});
Expand All @@ -170,7 +180,7 @@ describe('API Client', () => {
const result = await fetchContainerStats();

expect(result).toEqual(stats);
expect(mockFetch).toHaveBeenCalledWith('/api/stats');
expect(mockFetch).toHaveBeenCalledWith('/api/stats', { headers: AUTH_HEADER });
});
});

Expand All @@ -191,7 +201,7 @@ describe('API Client', () => {
const result = await fetchOrphans();

expect(result).toEqual(orphans);
expect(mockFetch).toHaveBeenCalledWith('/api/admin/orphans');
expect(mockFetch).toHaveBeenCalledWith('/api/admin/orphans', { headers: AUTH_HEADER });
});
});

Expand All @@ -214,6 +224,7 @@ describe('API Client', () => {
expect(result).toEqual(report);
expect(mockFetch).toHaveBeenCalledWith('/api/admin/cleanup', {
method: 'POST',
headers: AUTH_HEADER,
});
});
});
Expand Down Expand Up @@ -248,7 +259,7 @@ describe('API Client', () => {
const result = await fetchProxyKeys();

expect(result).toEqual(keys);
expect(mockFetch).toHaveBeenCalledWith('/api/proxy/keys');
expect(mockFetch).toHaveBeenCalledWith('/api/proxy/keys', { headers: AUTH_HEADER });
});
});

Expand All @@ -268,7 +279,7 @@ describe('API Client', () => {
expect(result.id).toBe('new-key-id');
expect(mockFetch).toHaveBeenCalledWith('/api/proxy/keys', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
headers: { 'Content-Type': 'application/json', ...AUTH_HEADER },
body: JSON.stringify({
vendor: 'openai',
secret: 'sk-test',
Expand All @@ -288,6 +299,7 @@ describe('API Client', () => {
await expect(deleteProxyKey('key-123')).resolves.toBeUndefined();
expect(mockFetch).toHaveBeenCalledWith('/api/proxy/keys/key-123', {
method: 'DELETE',
headers: AUTH_HEADER,
});
});
});
Expand All @@ -309,7 +321,7 @@ describe('API Client', () => {
const result = await fetchProxyHealth();

expect(result).toEqual(health);
expect(mockFetch).toHaveBeenCalledWith('/api/proxy/health');
expect(mockFetch).toHaveBeenCalledWith('/api/proxy/health', { headers: AUTH_HEADER });
});
});
});
Loading