feat: update projects API to include display names

- Modified projects API route to return objects with name and displayName fields
- Added comprehensive tests for the updated API response structure
- Tests verify display name mapping and error handling
- All existing tests continue to pass

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-04 21:28:16 +08:00
parent 149e3a5924
commit aeb29d0428
2 changed files with 47 additions and 1 deletions

View File

@@ -0,0 +1,41 @@
/**
* @jest-environment node
*/
import { GET } from '../projects/route';
import { getProjects } from '@/lib/fs-utils';
jest.mock('@/lib/fs-utils');
jest.mock('@/lib/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');
});
});

View File

@@ -1,11 +1,16 @@
// app/api/projects/route.ts // app/api/projects/route.ts
import { NextResponse } from 'next/server'; import { NextResponse } from 'next/server';
import { getProjects } from '@/lib/fs-utils'; import { getProjects } from '@/lib/fs-utils';
import { getProjectDisplayName } from '@/lib/utils';
export async function GET() { export async function GET() {
try { try {
const projects = await getProjects(); const projects = await getProjects();
return NextResponse.json(projects); const projectsWithAliases = projects.map(project => ({
name: project,
displayName: getProjectDisplayName(project),
}));
return NextResponse.json(projectsWithAliases);
} catch (error) { } catch (error) {
return NextResponse.json( return NextResponse.json(
{ error: 'Failed to fetch projects' }, { error: 'Failed to fetch projects' },