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
33 changes: 20 additions & 13 deletions packages/ui-vite/src/lib/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { api } from './api';
import { APIError, adaptProject, adaptSpec, adaptSpecDetail, api } from './api';

// Mock fetch
const mockFetch = vi.fn();
Expand All @@ -13,16 +13,17 @@ describe('API Client', () => {
describe('getProjects', () => {
it('should fetch projects successfully', async () => {
const mockResponse = {
current: { id: 'proj1', name: 'Project 1', path: '/path/1' },
current: { id: 'proj1', name: 'Project 1', displayName: 'Project 1', path: '/path/1', specsDir: '/path/1' },
available: [
{ id: 'proj1', name: 'Project 1', path: '/path/1' },
{ id: 'proj2', name: 'Project 2', path: '/path/2' },
{ id: 'proj1', name: 'Project 1', displayName: 'Project 1', path: '/path/1', specsDir: '/path/1' },
{ id: 'proj2', name: 'Project 2', displayName: 'Project 2', path: '/path/2', specsDir: '/path/2' },
],
};

mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => mockResponse,
status: 200,
text: async () => JSON.stringify(mockResponse),
});

const result = await api.getProjects();
Expand Down Expand Up @@ -101,7 +102,7 @@ describe('API Client', () => {
text: async () => 'Server error',
});

await expect(api.getProjects()).rejects.toThrow();
await expect(api.getProjects()).rejects.toBeInstanceOf(APIError);
});
});

Expand All @@ -112,18 +113,20 @@ describe('API Client', () => {
id: '123-feature',
specName: '123-feature',
specNumber: 123,
specName: '123-feature',
title: 'Test Spec',
status: 'planned' as const,
priority: 'high' as const,
tags: ['ui'],
createdAt: '2025-01-01T00:00:00Z',
updatedAt: '2025-01-02T00:00:00Z',
createdAtAt: '2025-01-01T00:00:00Z',
updatedAtAt: '2025-01-02T00:00:00Z',
},
];

mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => ({ specs: mockSpecs }),
status: 200,
text: async () => JSON.stringify({ specs: mockSpecs }),
});

const result = await api.getSpecs();
Expand Down Expand Up @@ -172,7 +175,8 @@ describe('API Client', () => {

mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => ({}),
status: 204,
text: async () => '',
});

await api.updateSpec('spec-001', updates);
Expand Down Expand Up @@ -207,7 +211,8 @@ describe('API Client', () => {

mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => mockStats,
status: 200,
text: async () => JSON.stringify(mockStats),
});

const result = await api.getStats();
Expand All @@ -229,7 +234,8 @@ describe('API Client', () => {

mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => ({ graph: mockGraph }),
status: 200,
text: async () => JSON.stringify({ graph: mockGraph }),
});

const result = await api.getDependencies();
Expand All @@ -244,7 +250,8 @@ describe('API Client', () => {

mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => ({ graph: mockGraph }),
status: 200,
text: async () => JSON.stringify({ graph: mockGraph }),
});

await api.getDependencies('spec-001');
Expand Down
Loading
Loading