|
| 1 | +/** |
| 2 | + * Edge case and robustness tests |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect, beforeAll } from 'vitest'; |
| 6 | +import { apiClient } from '../client'; |
| 7 | +import { |
| 8 | + ErrorResponseSchema, |
| 9 | + ProjectsListResponseSchema, |
| 10 | + ListSpecsResponseSchema, |
| 11 | + type ProjectMutationResponse, |
| 12 | + type ProjectsListResponse, |
| 13 | + type ListSpecsResponse, |
| 14 | +} from '../schemas'; |
| 15 | +import { validateSchema, createSchemaErrorMessage } from '../utils/validation'; |
| 16 | +import { createTestProject, type TestProject, type TestSpecFixture } from '../fixtures'; |
| 17 | + |
| 18 | +const LARGE_SPEC_COUNT = 120; |
| 19 | + |
| 20 | +type Mode = 'single-project' | 'multi-project'; |
| 21 | + |
| 22 | +describe('Edge Cases', () => { |
| 23 | + let mode: Mode = 'single-project'; |
| 24 | + let baseProjectId: string | null = null; |
| 25 | + |
| 26 | + beforeAll(async () => { |
| 27 | + const projectsResponse = await apiClient.get<ProjectsListResponse>('/api/projects'); |
| 28 | + const validation = validateSchema(ProjectsListResponseSchema, projectsResponse.data); |
| 29 | + if (!validation.success) { |
| 30 | + throw new Error(createSchemaErrorMessage('GET /api/projects', validation.errors || [])); |
| 31 | + } |
| 32 | + |
| 33 | + mode = validation.data?.mode ?? 'single-project'; |
| 34 | + baseProjectId = validation.data?.projects[0]?.id ?? null; |
| 35 | + }); |
| 36 | + |
| 37 | + it('rejects invalid status filter without a 5xx', async () => { |
| 38 | + if (!baseProjectId) { |
| 39 | + throw new Error('No project id available for invalid-params test'); |
| 40 | + } |
| 41 | + |
| 42 | + const response = await apiClient.get(`/api/projects/${baseProjectId}/specs`, { |
| 43 | + status: 'definitely-invalid-status', |
| 44 | + }); |
| 45 | + |
| 46 | + expect([400, 422]).toContain(response.status); |
| 47 | + |
| 48 | + const validation = validateSchema(ErrorResponseSchema, response.data); |
| 49 | + expect(validation.success || response.data === null).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns structured error for malformed JSON search payload (no 5xx)', async () => { |
| 53 | + const url = new URL('/api/search', apiClient.baseUrl); |
| 54 | + const res = await fetch(url.toString(), { |
| 55 | + method: 'POST', |
| 56 | + headers: { 'Content-Type': 'application/json' }, |
| 57 | + body: '{"query": "oops"', // intentionally malformed JSON |
| 58 | + }); |
| 59 | + |
| 60 | + expect([400, 422]).toContain(res.status); |
| 61 | + |
| 62 | + const data = await res.json().catch(() => null); |
| 63 | + if (data) { |
| 64 | + const validation = validateSchema(ErrorResponseSchema, data); |
| 65 | + if (!validation.success) { |
| 66 | + throw new Error(createSchemaErrorMessage('POST /api/search (malformed)', validation.errors || [])); |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + it('handles empty projects without 5xx and returns zero specs', async () => { |
| 72 | + if (mode !== 'multi-project') { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const emptyProject = await createTestProject({ name: 'edge-empty-project', specs: [] }); |
| 77 | + let addedProjectId: string | null = null; |
| 78 | + try { |
| 79 | + const addResponse = await apiClient.post<ProjectMutationResponse>('/api/projects', { |
| 80 | + path: emptyProject.path, |
| 81 | + }); |
| 82 | + expect(addResponse.status).toBe(200); |
| 83 | + addedProjectId = addResponse.data?.project?.id ?? null; |
| 84 | + |
| 85 | + if (!addedProjectId) { |
| 86 | + throw new Error('Project creation did not return an id'); |
| 87 | + } |
| 88 | + |
| 89 | + const listResponse = await apiClient.get(`/api/projects/${addedProjectId}/specs`); |
| 90 | + expect(listResponse.status).toBe(200); |
| 91 | + |
| 92 | + const validation = validateSchema(ListSpecsResponseSchema, listResponse.data); |
| 93 | + if (!validation.success) { |
| 94 | + throw new Error( |
| 95 | + createSchemaErrorMessage( |
| 96 | + `/api/projects/${addedProjectId}/specs (empty)`, |
| 97 | + validation.errors || [] |
| 98 | + ) |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + expect((validation.data as ListSpecsResponse).specs.length).toBe(0); |
| 103 | + } finally { |
| 104 | + if (addedProjectId) { |
| 105 | + await apiClient.delete(`/api/projects/${addedProjectId}`).catch(() => undefined); |
| 106 | + } |
| 107 | + await emptyProject.cleanup(); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + it('handles large spec sets (>=100 specs) within response expectations', async () => { |
| 112 | + if (mode !== 'multi-project') { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const largeFixtures: TestSpecFixture[] = Array.from({ length: LARGE_SPEC_COUNT }, (_, i) => ({ |
| 117 | + name: `large-spec-${i + 1}`, |
| 118 | + title: `Large Spec ${i + 1}`, |
| 119 | + status: 'planned', |
| 120 | + priority: (['low', 'medium', 'high', 'critical'] as const)[i % 4], |
| 121 | + tags: ['large'], |
| 122 | + })); |
| 123 | + |
| 124 | + const largeProject = await createTestProject({ name: 'edge-large-project', specs: largeFixtures }); |
| 125 | + let addedProjectId: string | null = null; |
| 126 | + try { |
| 127 | + const addResponse = await apiClient.post<ProjectMutationResponse>('/api/projects', { |
| 128 | + path: largeProject.path, |
| 129 | + }); |
| 130 | + expect(addResponse.status).toBe(200); |
| 131 | + addedProjectId = addResponse.data?.project?.id ?? null; |
| 132 | + |
| 133 | + if (!addedProjectId) { |
| 134 | + throw new Error('Project creation did not return an id'); |
| 135 | + } |
| 136 | + |
| 137 | + const start = Date.now(); |
| 138 | + const listResponse = await apiClient.get(`/api/projects/${addedProjectId}/specs`); |
| 139 | + const duration = Date.now() - start; |
| 140 | + |
| 141 | + expect(listResponse.status).toBe(200); |
| 142 | + |
| 143 | + const validation = validateSchema(ListSpecsResponseSchema, listResponse.data); |
| 144 | + if (!validation.success) { |
| 145 | + throw new Error( |
| 146 | + createSchemaErrorMessage( |
| 147 | + `/api/projects/${addedProjectId}/specs (large)`, |
| 148 | + validation.errors || [] |
| 149 | + ) |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + expect((validation.data as ListSpecsResponse).specs.length).toBeGreaterThanOrEqual(LARGE_SPEC_COUNT); |
| 154 | + expect(duration).toBeLessThan(3000); |
| 155 | + } finally { |
| 156 | + if (addedProjectId) { |
| 157 | + await apiClient.delete(`/api/projects/${addedProjectId}`).catch(() => undefined); |
| 158 | + } |
| 159 | + await largeProject.cleanup(); |
| 160 | + } |
| 161 | + }); |
| 162 | +}); |
0 commit comments