From aeb29d04289db3a7eb3496a5499407189d2c0196 Mon Sep 17 00:00:00 2001 From: tech Date: Wed, 4 Mar 2026 21:28:16 +0800 Subject: [PATCH] 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 --- app/api/__tests__/projects.test.ts | 41 ++++++++++++++++++++++++++++++ app/api/projects/route.ts | 7 ++++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 app/api/__tests__/projects.test.ts diff --git a/app/api/__tests__/projects.test.ts b/app/api/__tests__/projects.test.ts new file mode 100644 index 0000000..b931c5f --- /dev/null +++ b/app/api/__tests__/projects.test.ts @@ -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'); + }); +}); diff --git a/app/api/projects/route.ts b/app/api/projects/route.ts index d787e67..affdcd4 100644 --- a/app/api/projects/route.ts +++ b/app/api/projects/route.ts @@ -1,11 +1,16 @@ // app/api/projects/route.ts import { NextResponse } from 'next/server'; import { getProjects } from '@/lib/fs-utils'; +import { getProjectDisplayName } from '@/lib/utils'; export async function GET() { try { const projects = await getProjects(); - return NextResponse.json(projects); + const projectsWithAliases = projects.map(project => ({ + name: project, + displayName: getProjectDisplayName(project), + })); + return NextResponse.json(projectsWithAliases); } catch (error) { return NextResponse.json( { error: 'Failed to fetch projects' },