Skip to content

Commit 24aa88b

Browse files
committed
test: added 10 unit tests for config and basic functionality
1 parent 8133639 commit 24aa88b

File tree

9 files changed

+744
-3
lines changed

9 files changed

+744
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"preversion": "npm run test",
3939
"version": "npm run compile && git add -A .",
4040
"watch": "tsc --build --watch",
41-
"gen-comp": "cd ./types/codegen/ && npm run generate && cd ./generated && npm run build && cd ../../../ && npm run compile"
41+
"gen-comp": "cd ./types/codegen/ && npm run generate && cd ./generated/typescript && npm run build && cd ../../../ && npm run compile"
4242
},
4343
"devDependencies": {
4444
"@commitlint/cli": "^19.8.1",

types/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
LICENSE
22
NOTICE
3-
SECURITY.md
3+
SECURITY.md
4+
codegen/scripts/**/*
5+
codegen/generated/**/*
6+
codegen/tests/fixtures/**/*

types/codegen/README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,45 @@ The validation script provides detailed reporting:
196196
- **Missing models**: Models that should be generated but aren't
197197
- **Extra models**: Intermediate models created by OpenAPI generator
198198

199+
## Testing
200+
201+
The `tests/` directory contains comprehensive tests for the OpenAPI code generation pipeline. These tests validate that your `openapitools.json` configuration and custom templates work correctly.
202+
203+
### Running Tests
204+
205+
```bash
206+
# Run all tests
207+
npm run test:unit
208+
209+
# Inspect generated files for debugging
210+
KEEP_TEST_OUTPUT=1 npm run test:unit
211+
212+
# Run specific test suites
213+
npm run test:unit -- --testPathPattern="field-addition"
214+
npm run test:unit -- --testPathPattern="openapi-normalizer"
215+
```
216+
217+
### Test Coverage
218+
219+
- **Field Addition Tests (8 tests)**: Validates TypeScript interfaces and Java records for optional/required fields
220+
- **Configuration Tests**: Ensures camelCase naming, ES6 exports, Java 21 compatibility, and custom templates
221+
- **OpenAPI Normalizer Tests (2 tests)**: Tests inheritance vs property flattening based on configuration
222+
223+
The tests use your actual `openapitools.json` configuration (not mocked) to ensure production validation and regression protection.
224+
225+
**Key Features:**
226+
- Real configuration testing using your actual `openapitools.json`
227+
- Custom template validation (Java records vs classes)
228+
- Language-specific behavior testing (TypeScript inheritance, Java flattening)
229+
- Configuration-aware tests that adapt to your settings
230+
231+
See [tests/README.md](tests/README.md) for detailed test documentation.
232+
199233
## Development Workflow
200234

201235
1. **Add/modify schemas** in `schema/chatTypes.json`
202236
2. **Run generation**: `npm run generate`
203237
3. **Review validation**: Check for missing or extra models
204238
4. **Test integration**: Use generated types in your application
205-
5. **Commit changes**: Only commit schema files, not generated files
239+
5. **Run tests**: `npm run test:unit` to validate configuration and templates
240+
6. **Commit changes**: Only commit schema files, not generated files

types/codegen/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
"generate:no-post": "npm run generate-schema && openapi-generator-cli generate",
1010
"test": "node scripts/post-test.js",
1111
"test:verbose": "node scripts/post-test.js --verbose",
12+
"test:unit": "ts-mocha -b 'tests/unit/**/*.test.js' --bail false",
13+
"test:all": "npm run test && npm run test:unit",
1214
"validate": "npm run test"
1315
},
1416
"devDependencies": {
1517
"@openapitools/openapi-generator-cli": "^2.21.4",
18+
"@types/mocha": "^10.0.9",
1619
"@types/node": "^24.0.4",
20+
"ts-mocha": "^11.1.0",
1721
"ts-node": "^10.9.2",
1822
"typescript": "^5.8.3"
1923
}

types/codegen/tests/README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# OpenAPI Code Generation Tests
2+
3+
Comprehensive test suite validating OpenAPI code generation pipeline using your actual `openapitools.json` configuration and custom templates.
4+
5+
## Test Structure
6+
7+
```
8+
tests/
9+
├── utils/test-helpers.js # Shared utilities
10+
└── unit/
11+
├── field-addition.test.js # Field scenarios & configuration validation
12+
└── openapi-normalizer.test.js # OpenAPI normalizer features
13+
```
14+
15+
## Running Tests
16+
17+
```bash
18+
# Normal test run
19+
npm run test:unit
20+
21+
# Inspect generated files
22+
KEEP_TEST_OUTPUT=1 npm run test:unit
23+
24+
# Run specific tests
25+
npm run test:unit -- --testPathPattern="field-addition"
26+
```
27+
28+
## Test Coverage
29+
30+
### Field Addition Tests (8 tests)
31+
Validates field addition scenarios and configuration compliance:
32+
33+
- **Field Testing**: Base schema, optional fields (`field?: string`), required fields (`field: string`)
34+
- **Configuration Validation**:
35+
- TypeScript camelCase naming (`displayName` vs `display_name`)
36+
- ES6 exports (`export interface` vs `module.exports`)
37+
- Java 21 compatibility (no deprecated features)
38+
- Model filtering (`global-property models=TestModel`)
39+
- Custom templates (Java records vs classes)
40+
41+
### OpenAPI Normalizer Tests (2 tests)
42+
Validates OpenAPI normalizer features with language-specific behavior:
43+
44+
- **REF_AS_PARENT_IN_ALLOF**: Configuration-aware testing
45+
- TypeScript: Tests inheritance (`extends BaseModel`) vs flattening
46+
- Java: Always flattens properties (records don't support inheritance)
47+
- **Custom Template Compatibility**: Ensures normalizer works with custom templates
48+
49+
## Key Features
50+
51+
### Real Configuration Testing
52+
- Uses your actual `openapitools.json` (not mocked)
53+
- Tests adapt behavior based on your configuration
54+
- Validates custom templates are actually used
55+
56+
### Language-Specific Validation
57+
- TypeScript: Interfaces, optional fields, ES6 exports, inheritance
58+
- Java: Records, annotations (`@Nonnull`, `@Nullable`), property flattening
59+
60+
### Robust Design
61+
- Shared utilities prevent code duplication
62+
- Configuration-aware tests adapt to changes
63+
- Clean test isolation with conditional cleanup
64+
- Clear failure points with specific test names
65+
66+
## Generated Validation Examples
67+
68+
**TypeScript**:
69+
```typescript
70+
export interface TestModel {
71+
id: string;
72+
displayName: string; // camelCase naming
73+
optionalField?: string; // Optional field syntax
74+
}
75+
```
76+
77+
**Java**:
78+
```java
79+
public record TestModel( // Custom template (record vs class)
80+
@JsonProperty("id") @Nonnull String id,
81+
@JsonProperty("displayName") @Nonnull String displayName
82+
) {}
83+
```
84+
85+
## Adding New Tests
86+
87+
**Extend existing files** for simple checks fitting current themes.
88+
89+
**Create new files** for complex features deserving dedicated test suites:
90+
91+
```javascript
92+
const { generateCodeWithConfig, createTestDirectory } = require('../utils/test-helpers')
93+
94+
describe('New Feature Tests', () => {
95+
// Use shared utilities for consistency
96+
})
97+
```
98+
99+
## Benefits
100+
101+
- **Production validation**: Tests your actual build pipeline
102+
- **Regression protection**: Catches configuration and template breaks
103+
- **Maintainable**: Shared utilities, configuration-aware testing
104+
- **Developer-friendly**: Clear test names, easy debugging with `KEEP_TEST_OUTPUT`

0 commit comments

Comments
 (0)