Files
ftdl/app/api/__tests__/projects.test.ts
tech edc4cb770f fix: separate client and server utilities to fix build error
Split lib/utils.ts into:
- lib/utils.ts: Client-safe utilities only (no Node.js imports)
- lib/project-utils.ts: Server-only utilities with Node.js imports

This fixes the Next.js build error where client components were importing
modules that contained Node.js dependencies (fs, path).

All tests pass and build succeeds.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-04 22:17:31 +08:00

42 lines
1.1 KiB
TypeScript

/**
* @jest-environment node
*/
import { GET } from '../projects/route';
import { getProjects } from '@/lib/fs-utils';
jest.mock('@/lib/fs-utils');
jest.mock('@/lib/project-utils', () => ({
getProjectDisplayName: jest.fn((name) => name === 'FT' ? '外放构建' : name),
}));
describe('/api/projects', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should return projects with display names', async () => {
(getProjects as jest.Mock).mockResolvedValue(['FT', 'FT_DEV', 'UNKNOWN']);
const response = await GET();
const data = await response.json();
expect(response.status).toBe(200);
expect(data).toEqual([
{ name: 'FT', displayName: '外放构建' },
{ name: 'FT_DEV', displayName: 'FT_DEV' },
{ name: 'UNKNOWN', displayName: 'UNKNOWN' },
]);
});
it('should handle errors gracefully', async () => {
(getProjects as jest.Mock).mockRejectedValue(new Error('File system error'));
const response = await GET();
const data = await response.json();
expect(response.status).toBe(500);
expect(data.error).toBe('Failed to fetch projects');
});
});