Skip to content

Commit bb94a94

Browse files
Copilotstreamich
andcommitted
feat: add comprehensive templates for TemplateJson covering README examples and real-life scenarios
Co-authored-by: streamich <[email protected]>
1 parent 0fdfe89 commit bb94a94

File tree

3 files changed

+946
-1
lines changed

3 files changed

+946
-1
lines changed

src/__demos__/templates-demo.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {TemplateJson} from '../structured/TemplateJson';
2+
import * as templates from '../structured/templates';
3+
4+
console.log('🎲 JSON Random Template Examples\n');
5+
6+
console.log('📧 Email addresses:');
7+
for (let i = 0; i < 3; i++) {
8+
const email = TemplateJson.gen(['str', templates.tokenEmail]);
9+
console.log(` ${email}`);
10+
}
11+
12+
console.log('\n📞 Phone numbers:');
13+
for (let i = 0; i < 3; i++) {
14+
const phone = TemplateJson.gen(['str', templates.tokenPhone]);
15+
console.log(` ${phone}`);
16+
}
17+
18+
console.log('\n🏷️ Product codes:');
19+
for (let i = 0; i < 3; i++) {
20+
const code = TemplateJson.gen(['str', templates.tokenProductCode]);
21+
console.log(` ${code}`);
22+
}
23+
24+
console.log('\n👤 User profile:');
25+
const user = TemplateJson.gen(templates.userProfile);
26+
console.log(JSON.stringify(user, null, 2));
27+
28+
console.log('\n🛒 E-commerce product:');
29+
const product = TemplateJson.gen(templates.product);
30+
console.log(JSON.stringify(product, null, 2));
31+
32+
console.log('\n📋 Order:');
33+
const order = TemplateJson.gen(templates.order);
34+
console.log(JSON.stringify(order, null, 2));
35+
36+
console.log('\n🌐 API Response:');
37+
const apiResponse = TemplateJson.gen(templates.apiResponse);
38+
console.log(JSON.stringify(apiResponse, null, 2));
39+
40+
console.log('\n🏥 Patient record:');
41+
const patient = TemplateJson.gen(templates.patient);
42+
console.log(JSON.stringify(patient, null, 2));
43+
44+
console.log('\n📊 IoT Sensor reading:');
45+
const sensor = TemplateJson.gen(templates.sensorReading);
46+
console.log(JSON.stringify(sensor, null, 2));
47+
48+
console.log('\n🌳 Tree structure (recursive):');
49+
const tree = TemplateJson.gen(templates.tree());
50+
console.log(JSON.stringify(tree, null, 2));
51+
52+
console.log('\n🔀 Mixed types (or template):');
53+
for (let i = 0; i < 5; i++) {
54+
const mixed = TemplateJson.gen(templates.mixedTypes);
55+
console.log(` ${typeof mixed}: ${JSON.stringify(mixed)}`);
56+
}
57+
58+
console.log('\n🎯 Edge cases:');
59+
const empty = TemplateJson.gen(templates.emptyStructures);
60+
console.log(JSON.stringify(empty, null, 2));
61+
62+
console.log('\n📏 Large numbers:');
63+
const large = TemplateJson.gen(templates.largeNumbers);
64+
console.log(JSON.stringify(large, null, 2));
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
import {resetMathRandom} from '../../__tests__/setup';
2+
import {deterministic} from '../../util';
3+
import {TemplateJson} from '../TemplateJson';
4+
import * as templates from '../templates';
5+
6+
describe('Template Examples', () => {
7+
describe('String Pattern Templates', () => {
8+
test('generates email addresses', () => {
9+
deterministic(123, () => {
10+
const email = TemplateJson.gen(['str', templates.tokenEmail]) as string;
11+
expect(typeof email).toBe('string');
12+
expect(email).toContain('@');
13+
expect(email.length).toBeGreaterThan(5);
14+
});
15+
});
16+
17+
test('generates phone numbers', () => {
18+
deterministic(456, () => {
19+
const phone = TemplateJson.gen(['str', templates.tokenPhone]) as string;
20+
expect(typeof phone).toBe('string');
21+
expect(phone).toMatch(/^\+1-\d{3}-\d{3}-\d{4}$/);
22+
});
23+
});
24+
25+
test('generates product codes', () => {
26+
deterministic(789, () => {
27+
const code = TemplateJson.gen(['str', templates.tokenProductCode]) as string;
28+
expect(typeof code).toBe('string');
29+
expect(code).toMatch(/^(PRD|ITM|SKU)-[A-Z]{2}\d{6}$/);
30+
});
31+
});
32+
33+
test('generates URLs', () => {
34+
deterministic(101, () => {
35+
const url = TemplateJson.gen(['str', templates.tokenUrl]) as string;
36+
expect(typeof url).toBe('string');
37+
expect(url).toMatch(/^https:\/\/.*\.(com|org|net|io)/);
38+
});
39+
});
40+
});
41+
42+
describe('User Profile Templates', () => {
43+
test('generates user profile with all required fields', () => {
44+
deterministic(202, () => {
45+
const user = TemplateJson.gen(templates.userProfile) as any;
46+
expect(user).toHaveProperty('id');
47+
expect(user).toHaveProperty('username');
48+
expect(user).toHaveProperty('email');
49+
expect(user).toHaveProperty('age');
50+
expect(user).toHaveProperty('isActive');
51+
expect(user).toHaveProperty('profile');
52+
expect(typeof user.id).toBe('number');
53+
expect(typeof user.username).toBe('string');
54+
expect(typeof user.email).toBe('string');
55+
expect(typeof user.age).toBe('number');
56+
expect(typeof user.isActive).toBe('boolean');
57+
expect(user.age).toBeGreaterThanOrEqual(18);
58+
expect(user.age).toBeLessThanOrEqual(120);
59+
});
60+
});
61+
62+
test('generates basic user with required fields', () => {
63+
deterministic(303, () => {
64+
const user = TemplateJson.gen(templates.userBasic) as any;
65+
expect(user).toHaveProperty('id');
66+
expect(user).toHaveProperty('name');
67+
expect(user).toHaveProperty('active');
68+
expect(typeof user.id).toBe('number');
69+
expect(typeof user.name).toBe('string');
70+
expect(typeof user.active).toBe('boolean');
71+
expect(user.name).toContain(' '); // Should have first and last name
72+
});
73+
});
74+
});
75+
76+
describe('API Response Templates', () => {
77+
test('generates API response with correct structure', () => {
78+
deterministic(404, () => {
79+
const response = TemplateJson.gen(templates.apiResponse) as any;
80+
expect(response).toHaveProperty('status');
81+
expect(response).toHaveProperty('timestamp');
82+
expect(response).toHaveProperty('data');
83+
expect(['success', 'error']).toContain(response.status);
84+
expect(typeof response.timestamp).toBe('number');
85+
expect(Array.isArray(response.data)).toBe(true);
86+
});
87+
});
88+
});
89+
90+
describe('E-commerce Templates', () => {
91+
test('generates product with all fields', () => {
92+
deterministic(505, () => {
93+
const product = TemplateJson.gen(templates.product) as any;
94+
expect(product).toHaveProperty('id');
95+
expect(product).toHaveProperty('name');
96+
expect(product).toHaveProperty('price');
97+
expect(product).toHaveProperty('currency');
98+
expect(product).toHaveProperty('category');
99+
expect(product).toHaveProperty('tags');
100+
expect(product).toHaveProperty('inventory');
101+
expect(product).toHaveProperty('rating');
102+
expect(product).toHaveProperty('reviews');
103+
104+
expect(typeof product.id).toBe('string');
105+
expect(product.id).toMatch(/^prod_\d{8}$/);
106+
expect(typeof product.price).toBe('number');
107+
expect(product.price).toBeGreaterThanOrEqual(9.99);
108+
expect(Array.isArray(product.tags)).toBe(true);
109+
expect(product.inventory).toHaveProperty('stock');
110+
expect(product.rating).toBeGreaterThanOrEqual(1.0);
111+
expect(product.rating).toBeLessThanOrEqual(5.0);
112+
});
113+
});
114+
115+
test('generates order with items', () => {
116+
deterministic(606, () => {
117+
const order = TemplateJson.gen(templates.order) as any;
118+
expect(order).toHaveProperty('orderId');
119+
expect(order).toHaveProperty('customerId');
120+
expect(order).toHaveProperty('items');
121+
expect(order).toHaveProperty('total');
122+
expect(order).toHaveProperty('status');
123+
expect(order).toHaveProperty('shippingAddress');
124+
125+
expect(order.orderId).toMatch(/^ORD-\d{10}$/);
126+
expect(order.customerId).toMatch(/^CUST-[A-Z]{3}\d{6}$/);
127+
expect(Array.isArray(order.items)).toBe(true);
128+
expect(order.items.length).toBeGreaterThan(0);
129+
expect(order.shippingAddress).toHaveProperty('street');
130+
expect(order.shippingAddress).toHaveProperty('city');
131+
expect(order.shippingAddress).toHaveProperty('state');
132+
expect(order.shippingAddress).toHaveProperty('zipCode');
133+
});
134+
});
135+
});
136+
137+
describe('Recursive Templates', () => {
138+
test('generates tree structure', () => {
139+
deterministic(707, () => {
140+
const tree = TemplateJson.gen(templates.tree()) as any;
141+
expect(tree).toHaveProperty('value');
142+
expect(typeof tree.value).toBe('number');
143+
// Tree may or may not have left/right children due to probability
144+
});
145+
});
146+
147+
test('generates comment thread', () => {
148+
deterministic(808, () => {
149+
const comment = TemplateJson.gen(templates.comment()) as any;
150+
expect(comment).toHaveProperty('id');
151+
expect(comment).toHaveProperty('text');
152+
expect(comment).toHaveProperty('author');
153+
expect(typeof comment.id).toBe('number');
154+
expect(typeof comment.text).toBe('string');
155+
expect(typeof comment.author).toBe('string');
156+
// Replies may or may not exist due to probability
157+
});
158+
});
159+
});
160+
161+
describe('IoT & Sensor Templates', () => {
162+
test('generates sensor reading', () => {
163+
deterministic(909, () => {
164+
const reading = TemplateJson.gen(templates.sensorReading) as any;
165+
expect(reading).toHaveProperty('sensorId');
166+
expect(reading).toHaveProperty('deviceType');
167+
expect(reading).toHaveProperty('value');
168+
expect(reading).toHaveProperty('unit');
169+
expect(reading).toHaveProperty('timestamp');
170+
expect(reading).toHaveProperty('location');
171+
expect(reading).toHaveProperty('status');
172+
173+
expect(reading.sensorId).toMatch(/^sensor_[A-Z]{2}\d{6}$/);
174+
expect(typeof reading.value).toBe('number');
175+
expect(reading.value).toBeGreaterThanOrEqual(-50);
176+
expect(reading.value).toBeLessThanOrEqual(150);
177+
expect(reading.location).toHaveProperty('room');
178+
expect(reading.location).toHaveProperty('floor');
179+
});
180+
});
181+
});
182+
183+
describe('Medical Templates', () => {
184+
test('generates patient record', () => {
185+
deterministic(1010, () => {
186+
const patient = TemplateJson.gen(templates.patient) as any;
187+
expect(patient).toHaveProperty('patientId');
188+
expect(patient).toHaveProperty('firstName');
189+
expect(patient).toHaveProperty('lastName');
190+
expect(patient).toHaveProperty('dateOfBirth');
191+
expect(patient).toHaveProperty('gender');
192+
expect(patient).toHaveProperty('bloodType');
193+
expect(patient).toHaveProperty('allergies');
194+
expect(patient).toHaveProperty('emergencyContact');
195+
196+
expect(patient.patientId).toMatch(/^PAT-\d{8}$/);
197+
expect(typeof patient.firstName).toBe('string');
198+
expect(typeof patient.lastName).toBe('string');
199+
expect(Array.isArray(patient.allergies)).toBe(true);
200+
expect(patient.emergencyContact).toHaveProperty('name');
201+
expect(patient.emergencyContact).toHaveProperty('relationship');
202+
expect(patient.emergencyContact).toHaveProperty('phone');
203+
});
204+
});
205+
});
206+
207+
describe('Edge Case Templates', () => {
208+
test('generates empty structures', () => {
209+
const empty = TemplateJson.gen(templates.emptyStructures) as any;
210+
expect(empty).toHaveProperty('emptyObject');
211+
expect(empty).toHaveProperty('emptyArray');
212+
expect(empty).toHaveProperty('emptyString');
213+
expect(empty).toHaveProperty('nullValue');
214+
expect(empty).toHaveProperty('zeroNumber');
215+
expect(empty).toHaveProperty('falseBool');
216+
217+
expect(empty.emptyObject).toEqual({});
218+
expect(empty.emptyArray).toEqual([]);
219+
expect(empty.emptyString).toBe('');
220+
expect(empty.nullValue).toBeNull();
221+
expect(empty.zeroNumber).toBe(0);
222+
expect(empty.falseBool).toBe(false);
223+
});
224+
225+
test('generates unicode text', () => {
226+
deterministic(1111, () => {
227+
const unicode = TemplateJson.gen(templates.unicodeText) as any;
228+
expect(unicode).toHaveProperty('ascii');
229+
expect(unicode).toHaveProperty('latin');
230+
expect(unicode).toHaveProperty('emoji');
231+
expect(unicode).toHaveProperty('chinese');
232+
expect(unicode).toHaveProperty('arabic');
233+
expect(unicode).toHaveProperty('mixed');
234+
235+
expect(typeof unicode.ascii).toBe('string');
236+
expect(typeof unicode.emoji).toBe('string');
237+
expect(typeof unicode.mixed).toBe('string');
238+
});
239+
});
240+
241+
test('generates large numbers', () => {
242+
const large = TemplateJson.gen(templates.largeNumbers) as any;
243+
expect(large).toHaveProperty('maxSafeInteger');
244+
expect(large).toHaveProperty('minSafeInteger');
245+
expect(large).toHaveProperty('largeFloat');
246+
expect(large).toHaveProperty('smallFloat');
247+
expect(large).toHaveProperty('preciseDecimal');
248+
expect(large).toHaveProperty('scientificNotation');
249+
250+
expect(large.maxSafeInteger).toBe(Number.MAX_SAFE_INTEGER);
251+
expect(large.minSafeInteger).toBe(Number.MIN_SAFE_INTEGER);
252+
expect(typeof large.largeFloat).toBe('number');
253+
expect(typeof large.smallFloat).toBe('number');
254+
expect(large.scientificNotation).toBe(1.23e-45);
255+
});
256+
257+
test('generates mixed types with or template', () => {
258+
deterministic(1212, () => {
259+
// Test multiple times to see different types
260+
const values = [];
261+
for (let i = 0; i < 10; i++) {
262+
values.push(TemplateJson.gen(templates.mixedTypes));
263+
}
264+
265+
// Should have generated different types
266+
const types = new Set(values.map(v => typeof v));
267+
expect(types.size).toBeGreaterThan(1);
268+
});
269+
});
270+
});
271+
});

0 commit comments

Comments
 (0)