- Replace spread operator with explicit field selection to avoid duplicate commit field from getApks() and parseApkFilename() - Add test case for commitId parameter filtering to ensure the filter parameter works correctly with new metadata extraction Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
122 lines
3.5 KiB
TypeScript
122 lines
3.5 KiB
TypeScript
/**
|
|
* @jest-environment node
|
|
*/
|
|
|
|
import { GET } from '../apks/route';
|
|
import { getApks } from '@/lib/fs-utils';
|
|
import { parseApkFilename } from '@/lib/apk-parser';
|
|
|
|
jest.mock('@/lib/fs-utils');
|
|
jest.mock('@/lib/apk-parser');
|
|
|
|
describe('/api/apks', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should return APKs with environment metadata', async () => {
|
|
const mockApks = [
|
|
{
|
|
name: 'ft_timeshift_0_42_57ef3a60d.apk',
|
|
size: 15728640,
|
|
modifiedAt: new Date('2026-03-04T14:30:00Z'),
|
|
},
|
|
{
|
|
name: 'ft_0_42_ff9ff3441.apk',
|
|
size: 15728640,
|
|
modifiedAt: new Date('2026-03-04T14:30:00Z'),
|
|
},
|
|
];
|
|
|
|
(getApks as jest.Mock).mockResolvedValue(mockApks);
|
|
(parseApkFilename as jest.Mock)
|
|
.mockReturnValueOnce({ environment: 'dev', commit: '57ef3a60d', isValid: true })
|
|
.mockReturnValueOnce({ environment: 'product', commit: 'ff9ff3441', isValid: true });
|
|
|
|
const request = new Request('http://localhost:3000/api/apks?project=FT&version=0_42');
|
|
const response = await GET(request);
|
|
const data = await response.json();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(data).toHaveLength(2);
|
|
expect(data[0]).toMatchObject({
|
|
name: 'ft_timeshift_0_42_57ef3a60d.apk',
|
|
environment: 'dev',
|
|
commit: '57ef3a60d',
|
|
});
|
|
expect(data[1]).toMatchObject({
|
|
name: 'ft_0_42_ff9ff3441.apk',
|
|
environment: 'product',
|
|
commit: 'ff9ff3441',
|
|
});
|
|
});
|
|
|
|
it('should require project and version parameters', async () => {
|
|
const request = new Request('http://localhost:3000/api/apks');
|
|
const response = await GET(request);
|
|
const data = await response.json();
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(data.error).toBe('Project and version parameters are required');
|
|
});
|
|
|
|
it('should handle errors gracefully', async () => {
|
|
(getApks as jest.Mock).mockRejectedValue(new Error('File system error'));
|
|
|
|
const request = new Request('http://localhost:3000/api/apks?project=FT&version=0_42');
|
|
const response = await GET(request);
|
|
const data = await response.json();
|
|
|
|
expect(response.status).toBe(500);
|
|
expect(data.error).toBe('Failed to fetch APK files');
|
|
});
|
|
|
|
it('should filter APKs by commitId parameter', async () => {
|
|
const mockApks = [
|
|
{
|
|
name: 'ft_timeshift_0_42_57ef3a60d.apk',
|
|
size: 15728640,
|
|
modifiedAt: new Date('2026-03-04T14:30:00Z'),
|
|
},
|
|
{
|
|
name: 'ft_0_42_ff9ff3441.apk',
|
|
size: 15728640,
|
|
modifiedAt: new Date('2026-03-04T14:30:00Z'),
|
|
},
|
|
{
|
|
name: 'ft_0_42_abc1234.apk',
|
|
size: 15728640,
|
|
modifiedAt: new Date('2026-03-04T14:30:00Z'),
|
|
},
|
|
];
|
|
|
|
const filteredApks = [
|
|
mockApks[1],
|
|
];
|
|
|
|
(getApks as jest.Mock).mockImplementation((project, version, commitId) => {
|
|
if (commitId === 'ff9ff3441') {
|
|
return Promise.resolve(filteredApks);
|
|
}
|
|
return Promise.resolve(mockApks);
|
|
});
|
|
|
|
(parseApkFilename as jest.Mock)
|
|
.mockReturnValue({ environment: 'product', commit: 'ff9ff3441', isValid: true });
|
|
|
|
const request = new Request('http://localhost:3000/api/apks?project=FT&version=0_42&commit=ff9ff3441');
|
|
const response = await GET(request);
|
|
const data = await response.json();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(data).toHaveLength(1);
|
|
expect(data[0]).toMatchObject({
|
|
name: 'ft_0_42_ff9ff3441.apk',
|
|
environment: 'product',
|
|
commit: 'ff9ff3441',
|
|
});
|
|
|
|
expect(getApks).toHaveBeenCalledWith('FT', '0_42', 'ff9ff3441');
|
|
});
|
|
});
|