Skip to content

Commit 2dd9bf6

Browse files
author
Daniel Duong
committed
test(autorag): add missing AutoRAG unit tests and refactor existing ones
1 parent f5c2c10 commit 2dd9bf6

17 files changed

Lines changed: 6352 additions & 1233 deletions

TESTING_ANALYSIS.md

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
# AutoRAG/AutoML Testing Analysis
2+
## RHOAIENG-58993
3+
4+
Date: 2026-04-21
5+
Analyst: Claude Code
6+
7+
---
8+
9+
## Executive Summary
10+
11+
Analysis of `packages/autorag/contract-tests/__tests__/testAutoRagContract.test.ts` reveals:
12+
- **Total tests**: 111
13+
- **Proper contract tests** (using `toMatchContract()`): 45 (~40%)
14+
- **Business logic/manual validation tests**: 66 (~60%)
15+
16+
The contract test file is **misnamed** and contains a significant amount of business logic testing that belongs in dedicated unit tests.
17+
18+
---
19+
20+
## Phase 1 Analysis: Contract Tests Categorization
21+
22+
### KEEP (Proper Contract Tests) - ~45 tests
23+
24+
These tests properly validate API responses against OpenAPI schemas using `toMatchContract()`:
25+
26+
#### Health Check (1 test)
27+
- Lines 27-30: Basic health check (though missing schema validation)
28+
29+
#### User Endpoint (1 test)
30+
- Lines 34-40: GET /api/v1/user with schema validation
31+
32+
#### Namespaces (1 test)
33+
- Lines 44-47: GET /api/v1/namespaces (minimal validation, should add schema)
34+
35+
#### LSD Models (2 tests)
36+
- Lines 52-60: GET /api/v1/lsd/models - proper schema validation
37+
- Error cases with schema validation
38+
39+
#### LSD Vector Store Providers (2 tests)
40+
- Lines 128-136: GET /api/v1/lsd/vector-store-providers - proper schema validation
41+
- Error cases with schema validation
42+
43+
#### Secrets Endpoint (3 tests)
44+
- Lines 253-261: GET /api/v1/secrets (all secrets)
45+
- Lines 263-271: GET /api/v1/secrets?type=storage
46+
- Lines 273-282: GET /api/v1/secrets?type=lls
47+
48+
#### S3 File Endpoint (2 tests)
49+
- Lines 567-576: GET /api/v1/s3/file - successful download with schema validation
50+
- Lines 698-702: Bucket parameter fallback with schema validation
51+
52+
#### S3 Files Endpoint (multiple tests)
53+
- Success cases with schema validation for listing files
54+
55+
#### Pipeline Runs (multiple tests)
56+
- Lines 1072-1078: GET /api/v1/pipeline-runs with schema validation
57+
- Lines 1122-1130: GET /api/v1/pipeline-runs/:id with schema validation
58+
- Lines 1144-1159: POST /api/v1/pipeline-runs with schema validation
59+
- Lines 1257-1265: POST /api/v1/pipeline-runs/:id/terminate with schema validation
60+
- Lines 1289-1297: POST /api/v1/pipeline-runs/:id/retry with schema validation
61+
62+
#### S3 Upload (1 test)
63+
- Lines 1506-1518: POST /api/v1/s3/file with schema validation
64+
65+
### MOVE TO UNIT TESTS (Business Logic) - ~66 tests
66+
67+
These tests validate business rules, data transformations, and BFF behavior. They should be moved to appropriate unit test locations:
68+
69+
#### 1. Manual Structure Validation (REDUNDANT) - ~15 tests
70+
**Location**: Remove or consolidate into unit tests
71+
**Examples**:
72+
- Lines 62-81: "should return models with expected data structure" - manually checks model object structure
73+
- Lines 138-164: "should return vector store providers with expected data structure" - redundant manual validation
74+
- Lines 168-181: "should return a valid array in vector_store_providers field" - redundant array validation
75+
- Lines 389-417: "Field Type Validation" - manually validates field types (redundant with schema validation)
76+
- Lines 512-525: "should return data as object, not array" - redundant type validation
77+
- Lines 1007-1016: "should return response with expected S3 list objects structure" - manual structure validation
78+
79+
**Action**: **DELETE** these tests - if `toMatchContract()` works correctly, manual validation is unnecessary.
80+
81+
#### 2. Secret Sanitization Logic (BUSINESS LOGIC) - ~4 tests
82+
**Move to**: `packages/autorag/bff/internal/api/__tests__/secrets_sanitization_test.go`
83+
**Lines 420-526**: "Secret Value Sanitization" tests
84+
- Lines 421-462: "should return actual values only for AWS_S3_BUCKET keys"
85+
- Lines 464-489: "should sanitize all secret values except AWS_S3_BUCKET"
86+
- Lines 491-510: "should only allow uppercase AWS_S3_BUCKET key"
87+
88+
**Rationale**: These test the BFF's secret sanitization business rules (AWS_S3_BUCKET redaction logic), not the API contract.
89+
90+
#### 3. Parameter Fallback Behavior (BFF LOGIC) - ~4 tests
91+
**Move to**: `packages/autorag/bff/internal/api/__tests__/s3_parameter_fallback_test.go`
92+
**Examples**:
93+
- Lines 692-714: S3 File - Bucket parameter fallback from secret's AWS_S3_BUCKET
94+
- Lines 704-713: S3 File - Bucket query parameter overrides secret's AWS_S3_BUCKET
95+
- Lines 864-883: S3 Files - Similar bucket parameter fallback tests
96+
97+
**Rationale**: These test complex BFF parameter resolution logic, not the API contract.
98+
99+
#### 4. Display Name Length Limits (BUSINESS RULE) - ~2 tests
100+
**Move to**: `packages/autorag/bff/internal/api/__tests__/pipeline_run_validation_test.go`
101+
**Lines 1183-1214**:
102+
- Line 1183: "display_name 250 chars accepted"
103+
- Line 1200: "display_name 251 chars rejected"
104+
105+
**Rationale**: These test business rule validation (max length constraints), not the OpenAPI schema compliance.
106+
107+
#### 5. Error Cases (PARTIALLY KEEP) - ~40 tests
108+
**Status**: Most error case tests are VALID contract tests and should KEEP using `toMatchContract()` with error response schemas.
109+
**Examples of VALID contract tests**:
110+
- Lines 85-124: Missing/invalid query parameters return 400
111+
- Lines 184-223: Missing/invalid parameters for vector stores
112+
- Lines 529-561: Error cases for secrets endpoint
113+
- Lines 578-689: Error cases for S3 file operations
114+
115+
**Action**: KEEP these but ensure they all use `toMatchContract()` with the error response schema reference.
116+
117+
#### 6. Optional Field Handling (SCHEMA VALIDATION) - ~6 tests
118+
**Status**: KEEP in contract tests
119+
**Lines 284-385**: Optional Fields tests
120+
- Lines 285-302: displayName from annotation
121+
- Lines 304-321: description from annotation
122+
- Lines 323-339: omit displayName/description when annotations absent
123+
- Lines 342-361: include type field when recognized
124+
- Lines 363-385: omit type field when not recognized
125+
126+
**Action**: These are valid contract tests - they validate that optional fields behave according to the OpenAPI schema.
127+
128+
---
129+
130+
## Phase 2-4: Missing Unit Tests Identified
131+
132+
### Frontend Hooks - Missing 3 Tests
133+
**Location**: `packages/autorag/frontend/src/app/hooks/__tests__/`
134+
135+
**Missing**:
136+
1. `useAutoragResults.spec.ts` - test AutoRAG pattern evaluation results fetching
137+
2. `usePatternEvaluationResults.spec.ts` - test pattern-specific evaluation data
138+
3. `usePreferredNamespaceRedirect.spec.ts` - test namespace navigation logic
139+
140+
**Existing** (good patterns to follow):
141+
- `useNotification.spec.tsx`
142+
- `usePipelineDefinitions.spec.ts`
143+
- `usePipelineRuns.spec.ts`
144+
145+
### Frontend API Layer - Missing 3 Test Files
146+
**Location**: `packages/autorag/frontend/src/app/api/__tests__/` (directory needs creation)
147+
148+
**Missing**:
149+
1. `pipelines.spec.ts` - test pipeline creation, retrieval, deletion
150+
2. `s3.spec.ts` - test S3 file upload, download, listing
151+
3. `k8s.spec.ts` - test Kubernetes secret management
152+
153+
### Frontend Utilities - Missing 2 Tests
154+
**Location**: `packages/autorag/frontend/src/app/utilities/__tests__/`
155+
156+
**Missing**:
157+
1. `schema.spec.ts` - test schema transformation utilities
158+
2. `secretValidation.spec.ts` - test secret validation logic (CRITICAL for security)
159+
160+
**Existing** (good patterns to follow):
161+
- `pipelineServerEmptyState.spec.ts`
162+
- `utils.spec.ts`
163+
164+
### Frontend Component - Missing 1 Test
165+
**Location**: `packages/autorag/frontend/src/app/components/configure/__tests__/` (directory needs creation)
166+
167+
**Missing**:
168+
1. `EvaluationTemplateModal.spec.tsx` - test evaluation template selection modal
169+
170+
---
171+
172+
## Phase 5: BFF Test Issues
173+
174+
### Missing Critical Test
175+
**Location**: `packages/autorag/bff/internal/api/`
176+
177+
**CRITICAL SECURITY GAP**:
178+
- `s3_upload_limit.go` (1659 bytes, lines) - **NO TEST EXISTS**
179+
- This file implements S3 upload size limits (security-critical)
180+
- **MUST** create `s3_upload_limit_test.go`
181+
182+
### Poorly Structured Test to Refactor
183+
**File**: `packages/autorag/bff/internal/api/secrets_handler_test.go`
184+
**Lines**: 1402-1612 (210 lines, ends at EOF)
185+
**Function**: `TestGetSecretsHandler_DoesNotLogCredentials`
186+
187+
**Problem**: Combines 4 different test scenarios in one 210-line test:
188+
1. Successful request logging (should not log credentials)
189+
2. K8s error logging (should not log credentials)
190+
3. Forbidden error logging (should not log credentials)
191+
4. Response body sanitization (credentials redacted in response)
192+
193+
**Impact**:
194+
- Violates single responsibility principle
195+
- Makes failures ambiguous (which scenario failed?)
196+
- Complex nested setup duplicated for each sub-test
197+
- Difficult to maintain
198+
199+
**Required Refactoring**: Split into 4 separate focused tests:
200+
```go
201+
func TestGetSecretsHandler_DoesNotLogCredentials_SuccessfulRequest(t *testing.T) { ... }
202+
func TestGetSecretsHandler_DoesNotLogCredentials_K8sError(t *testing.T) { ... }
203+
func TestGetSecretsHandler_DoesNotLogCredentials_ForbiddenError(t *testing.T) { ... }
204+
func TestGetSecretsHandler_SanitizesResponseBodyCredentials(t *testing.T) { ... }
205+
```
206+
207+
---
208+
209+
## Recommendations Summary
210+
211+
### Phase 1: Refactor Contract Tests
212+
1. **DELETE** redundant manual structure validation tests (~15 tests)
213+
2. **MOVE** business logic tests to unit tests:
214+
- Secret sanitization → `secrets_sanitization_test.go`
215+
- Parameter fallback → `s3_parameter_fallback_test.go`
216+
- Display name limits → `pipeline_run_validation_test.go`
217+
3. **KEEP** proper contract tests that use `toMatchContract()` (~45 tests)
218+
4. **ENSURE** all error cases use `toMatchContract()` with error response schemas
219+
220+
**Expected Result**: Contract test file reduced from 1519 lines to ~600-700 lines, focused solely on OpenAPI schema validation.
221+
222+
### Phase 2-4: Add Missing Frontend Tests
223+
1. Add 3 hook tests following existing patterns
224+
2. Add 3 API layer tests (create new `__tests__` directory)
225+
3. Add 2 utility tests (schema, secretValidation)
226+
4. Add 1 component test for EvaluationTemplateModal
227+
228+
**Expected Result**: Comprehensive frontend test coverage following project standards.
229+
230+
### Phase 5: Fix BFF Tests
231+
1. **CREATE** `s3_upload_limit_test.go` (CRITICAL SECURITY)
232+
2. **REFACTOR** `TestGetSecretsHandler_DoesNotLogCredentials` into 4 focused tests
233+
3. Follow Go testing best practices: one test, one assertion
234+
235+
**Expected Result**: Complete BFF test coverage with clear, maintainable tests.
236+
237+
### Phase 6: Apply to AutoML
238+
Apply same analysis and refactoring patterns to `packages/automl/` package.
239+
240+
---
241+
242+
## Testing Standards References
243+
244+
All new tests must follow:
245+
- `.claude/rules/unit-tests.md` - Unit test patterns
246+
- `.claude/rules/testing-standards.md` - Testing strategy
247+
- `.claude/rules/contract-tests.md` - Contract test guidelines
248+
- `.claude/rules/bff-go.md` - Go BFF testing patterns
249+
250+
---
251+
252+
## File Size Impact Estimation
253+
254+
### Contract Test File
255+
- **Current**: 1519 lines
256+
- **After Phase 1**: ~600-700 lines (~53% reduction)
257+
- **Rationale**: Removing ~66 business logic tests and redundant validations
258+
259+
### New Files Created
260+
- Frontend: ~8 new test files
261+
- BFF: ~4 new test files (3 moved from contract tests + 1 new s3_upload_limit_test.go)
262+
- BFF refactor: 1 file split into 4 focused tests
263+
264+
---
265+
266+
## AutoML Package Notes
267+
268+
The Jira states: "Examples in this ticket are from AutoRAG, but the same issues and patterns apply to AutoML."
269+
270+
**Action Required**: After completing AutoRAG, perform identical analysis and refactoring for `packages/automl/`:
271+
1. Analyze `packages/automl/contract-tests/__tests__/testAutoMlContract.test.ts`
272+
2. Apply same categorization (KEEP/MOVE/DELETE)
273+
3. Create missing unit tests for hooks, API, utilities, components
274+
4. Fix BFF tests following same patterns
275+
276+
Expected to find similar issues:
277+
- Contract tests containing business logic
278+
- Missing hook/API/utility tests
279+
- Missing s3_upload_limit test (if AutoML has same file)
280+
- Poorly structured BFF tests
281+
282+
---
283+
284+
## Next Steps
285+
286+
1.**COMPLETE**: Analysis documented
287+
2. **TODO**: Begin Phase 1 - Move business logic tests to unit tests
288+
3. **TODO**: Begin Phase 2-4 - Create missing frontend tests
289+
4. **TODO**: Begin Phase 5 - Create/refactor BFF tests
290+
5. **TODO**: Apply to AutoML package
291+
6. **TODO**: Run all tests and validate coverage improvements

0 commit comments

Comments
 (0)