-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_quality.py
More file actions
482 lines (368 loc) · 18.8 KB
/
Copy pathtest_quality.py
File metadata and controls
482 lines (368 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
from __future__ import annotations
import unittest
from pathlib import Path
from typing import Iterable
from tests.support import MemoryFileSystem, PROJECT_ROOT, profile_toml
from mockapi_runtime.diagnostics import Diagnostic
from mockapi_runtime.quality import check_generated_quality, format_quality_result
PACKAGE_ROOT = PROJECT_ROOT / "mock-server"
def diagnostic_ids(diagnostics: Iterable[Diagnostic]) -> set[str]:
return {diagnostic["id"] for diagnostic in diagnostics}
def diagnostic_paths(diagnostics: Iterable[Diagnostic], diagnostic_id: str) -> set[str]:
paths: set[str] = set()
for diagnostic in diagnostics:
if diagnostic["id"] == diagnostic_id and "path" in diagnostic:
paths.add(diagnostic["path"])
return paths
class GeneratedQualityTests(unittest.TestCase):
def setUp(self) -> None:
self.fs = MemoryFileSystem()
def check(self, profile_path: Path | None = None):
return check_generated_quality(
self.fs,
PACKAGE_ROOT,
profile_path=profile_path,
)
def add_package_file(self, relative_path: str, content: str) -> None:
self.fs.write_text(PACKAGE_ROOT / relative_path, content)
def add_profile(self, profile: str) -> None:
self.fs.write_text(PROJECT_ROOT / ".mockapi/profile.toml", profile)
def test_errors_when_allocator_used_without_id_counters(self) -> None:
self.add_package_file(
"src/features/workspaces/service.ts",
"const ids = newIdAllocator(state.getSlice('idCounters'))\n",
)
result = self.check()
self.assertFalse(result.ok)
self.assertIn("quality.idCounters.missingState", diagnostic_ids(result.errors))
def test_accepts_allocator_when_generated_admin_state_declares_id_counters(self) -> None:
self.fs.write_text(PACKAGE_ROOT / "openapi/admin.yaml", "components:\n schemas:\n MockState:\n properties:\n idCounters: {}\n")
self.add_package_file(
"src/features/workspaces/service.ts",
"const ids = newIdAllocator(state.getSlice('idCounters'))\n",
)
result = self.check()
self.assertNotIn("quality.idCounters.missingState", diagnostic_ids(result.errors))
def test_errors_for_hardcoded_base_path_when_option_exists(self) -> None:
self.add_package_file(
"src/app.ts",
"""type Options = { basePath?: string }
export const newApp = ({ basePath = "/api/v1" }: Options = {}) => {
registerRoutes(app, controllers, { basePath: "/api/v1" })
}
""",
)
result = self.check()
self.assertFalse(result.ok)
self.assertIn("quality.basePath.ignoredOption", diagnostic_ids(result.errors))
def test_forgives_malformed_profile_and_still_checks_files(self) -> None:
profile_path = PROJECT_ROOT / ".mockapi/profile.toml"
self.fs.write_text(profile_path, "schemaVersion =")
self.add_package_file(
"src/app.ts",
"""type Options = { basePath?: string }
export const newApp = ({ basePath = "/api/v1" }: Options = {}) => {
registerRoutes(app, controllers, { basePath: "/api/v1" })
}
""",
)
result = self.check(profile_path)
self.assertFalse(result.ok)
self.assertIn("quality.basePath.ignoredOption", diagnostic_ids(result.errors))
def test_errors_when_final_admin_openapi_contains_external_refs(self) -> None:
self.fs.write_text(
PACKAGE_ROOT / "openapi/admin.yaml",
"""openapi: 3.1.0
components:
schemas:
WorkspaceRecord:
$ref: "../../openapi.yaml#/components/schemas/Workspace"
""",
)
result = self.check()
self.assertFalse(result.ok)
self.assertIn("quality.adminOpenapi.externalRefs", diagnostic_ids(result.errors))
def test_allows_external_refs_in_admin_openapi_source(self) -> None:
self.fs.write_text(
PACKAGE_ROOT / "openapi/admin.source.yaml",
"""openapi: 3.1.0
components:
schemas:
WorkspaceRecord:
$ref: "../../openapi.yaml#/components/schemas/Workspace"
""",
)
result = self.check()
self.assertNotIn("quality.adminOpenapi.externalRefs", diagnostic_ids(result.errors))
def test_errors_for_remaining_feature_todo(self) -> None:
self.add_package_file(
"src/features/workspaces/controllers/createWorkspace.ts",
"throw new Error('TODO mockapi: implement createWorkspace')\n",
)
result = self.check()
self.assertFalse(result.ok)
self.assertIn("quality.todo.remaining", diagnostic_ids(result.errors))
def test_reports_incomplete_implementation_phase_first_for_scaffold(self) -> None:
self.add_package_file(
"src/features/workspaces/controllers/createWorkspace.ts",
"throw new Error('TODO mockapi: implement createWorkspace')\n",
)
self.add_package_file(
"src/features/workspaces/seed.ts",
"""import type { MockState } from '../../generated/mock-admin/contract/index.ts'
export const seedWorkspaces = (): Pick<MockState, 'workspaces'> => ({ workspaces: [] })
""",
)
result = self.check()
self.assertFalse(result.ok)
self.assertEqual(result.errors[0]["id"], "quality.phase.incompleteImplementation")
self.assertIn("quality.todo.remaining", diagnostic_ids(result.errors))
self.assertIn("quality.seed.emptyProductSeed", diagnostic_ids(result.errors))
def test_errors_when_stateful_completed_feature_has_no_repository(self) -> None:
self.add_package_file(
"src/features/workspaces/seed.ts",
"""import type { MockState } from '../../generated/mock-admin/contract/index.ts'
export const seedWorkspaces = (): Pick<MockState, 'workspaces'> => ({ workspaces: [] })
""",
)
self.add_package_file("src/features/workspaces/service.ts", "export class WorkspaceService {}\n")
result = self.check()
self.assertFalse(result.ok)
self.assertIn("quality.repository.missingFeatureRepository", diagnostic_ids(result.errors))
def test_accepts_id_counter_only_shared_seed_without_repository(self) -> None:
self.add_package_file(
"src/features/shared/seed.ts",
"""import type { MockState } from '../../generated/mock-admin/contract/index.ts'
export const seedShared = (): Pick<MockState, "idCounters"> => ({ idCounters: {} })
""",
)
self.add_package_file("src/features/shared/service.ts", "export class SharedService {}\n")
result = self.check()
self.assertNotIn("quality.repository.missingFeatureRepository", diagnostic_ids(result.errors))
def test_errors_for_direct_product_slice_access_in_feature_behavior(self) -> None:
self.add_package_file(
"src/features/workspaces/service.ts",
"const workspaces = stateRepository.getSlice('workspaces')\nstateRepository.setSlice('activeWorkspace', {})\n",
)
result = self.check()
self.assertFalse(result.ok)
self.assertIn("quality.stateAccess.directSliceAccess", diagnostic_ids(result.errors))
def test_errors_for_direct_entity_store_access_in_feature_behavior(self) -> None:
self.add_package_file(
"src/features/workspaces/service.ts",
"const workspaces = stateStore.findEntities('workspaces')\nawait stateStore.createEntity('workspaces', workspace)\n",
)
result = self.check()
self.assertFalse(result.ok)
self.assertIn("quality.stateAccess.directSliceAccess", diagnostic_ids(result.errors))
def test_accepts_direct_id_counter_slice_access_in_feature_behavior(self) -> None:
self.fs.write_text(PACKAGE_ROOT / "openapi/admin.yaml", "idCounters: {}\n")
self.add_package_file(
"src/features/workspaces/service.ts",
"const ids = newIdAllocator(stateRepository.getSlice('idCounters'))\n",
)
result = self.check()
self.assertNotIn("quality.stateAccess.directSliceAccess", diagnostic_ids(result.errors))
self.assertNotIn("quality.idCounters.missingState", diagnostic_ids(result.errors))
def test_accepts_clear_style_service_and_repository(self) -> None:
self.fs.write_text(PACKAGE_ROOT / "openapi/admin.yaml", "idCounters: {}\n")
self.add_package_file(
"src/features/workspaces/seed.ts",
"""import type { MockState } from '../../generated/mock-admin/contract/index.ts'
export const seedWorkspaces = (): Pick<MockState, 'workspaces'> => ({
workspaces: [{ id: 'workspace-1', title: 'Default Workspace' }],
})
""",
)
self.add_package_file(
"src/features/workspaces/repository.ts",
"""export class WorkspaceRepository {
constructor(private readonly stateStore: MockStateStore) {}
visible() { return this.stateStore.findEntities('workspaces') }
create(workspace: WorkspaceRecord) { return this.stateStore.createEntity('workspaces', workspace, { prepend: true }) }
}
""",
)
self.add_package_file(
"src/features/workspaces/service.ts",
"""export class WorkspaceService {
constructor(private readonly stateStore: MockStateStore, private readonly workspaces: WorkspaceRepository) {}
create(draft: WorkspaceDraft) {
return this.stateStore.transaction(async () => {
const ids = newIdAllocator(this.stateStore.getSlice('idCounters'))
await this.workspaces.create({ ...draft, id: ids.next('workspace'), updatedAt: this.stateStore.now() })
})
}
}
""",
)
result = self.check()
ids = diagnostic_ids(result.errors)
self.assertNotIn("quality.repository.missingFeatureRepository", ids)
self.assertNotIn("quality.stateAccess.directSliceAccess", ids)
self.assertNotIn("quality.idCounters.missingState", ids)
def test_errors_when_seed_enabled_and_product_seed_is_empty(self) -> None:
self.add_package_file(
"src/features/workspaces/seed.ts",
"""import type { MockState } from '../../generated/mock-admin/contract/index.ts'
export const seedWorkspaces = (): Pick<MockState, 'workspaces'> => ({
workspaces: [
// generated stub left empty
],
})
""",
)
result = self.check()
self.assertIn("quality.seed.emptyProductSeed", diagnostic_ids(result.errors))
def test_accepts_empty_product_seed_when_profile_disables_seed(self) -> None:
self.add_profile(profile_toml().replace("[state]\nschemaVersion = 1", "[state]\nschemaVersion = 1\nseed = false"))
self.add_package_file(
"src/features/workspaces/seed.ts",
"""import type { MockState } from '../../generated/mock-admin/contract/index.ts'
export const seedWorkspaces = (): Pick<MockState, 'workspaces'> => ({ workspaces: [] })
""",
)
result = self.check()
self.assertNotIn("quality.seed.emptyProductSeed", diagnostic_ids(result.errors))
def test_accepts_non_literal_product_seed_initializer(self) -> None:
self.add_package_file(
"src/features/workspaces/seed.ts",
"""import type { MockState } from '../../generated/mock-admin/contract/index.ts'
const seededWorkspaces = () => []
export const seedWorkspaces = (): Pick<MockState, 'workspaces'> => ({
workspaces: seededWorkspaces(),
})
""",
)
result = self.check()
self.assertNotIn("quality.seed.emptyProductSeed", diagnostic_ids(result.errors))
def test_accepts_nested_product_seed_literal(self) -> None:
self.add_package_file(
"src/features/workspaces/seed.ts",
"""import type { MockState } from '../../generated/mock-admin/contract/index.ts'
export const seedWorkspaces = (): Pick<MockState, 'workspaces'> => ({
workspaces: [
{
id: 'workspace-1',
folders: [],
},
],
})
""",
)
result = self.check()
self.assertNotIn("quality.seed.emptyProductSeed", diagnostic_ids(result.errors))
def test_accepts_empty_infrastructure_seed(self) -> None:
self.add_package_file(
"src/features/shared/seed.ts",
"""import type { MockState } from '../../generated/mock-admin/contract/index.ts'
export const seedShared = (): Pick<MockState, 'idCounters'> => ({ idCounters: {} })
""",
)
result = self.check()
self.assertNotIn("quality.seed.emptyProductSeed", diagnostic_ids(result.errors))
def test_warns_for_slug_helper_code(self) -> None:
self.add_package_file("src/lib/domain.ts", "export const slugify = (value: string) => value\n")
result = self.check()
self.assertTrue(result.ok)
self.assertIn("quality.slugIdHelper.present", diagnostic_ids(result.warnings))
def test_warns_for_oversized_domain_module(self) -> None:
self.add_package_file("src/lib/domain.ts", "\n".join(f"export const value{i} = {i}" for i in range(251)))
result = self.check()
self.assertIn("quality.domainModule.oversized", diagnostic_ids(result.warnings))
def test_warns_for_snapshot_set_all_and_as_any(self) -> None:
self.add_package_file(
"src/features/workspaces/service.ts",
"const state = repo.snapshot()\nrepo.setAll(state.workspaces)\nconst value = input as any\n",
)
result = self.check()
ids = diagnostic_ids(result.warnings)
self.assertIn("quality.stateAccess.snapshotSetAll", ids)
self.assertIn("quality.unsafeCast.asAny", ids)
def test_warns_when_no_smoke_tests_exist(self) -> None:
result = self.check()
warning = next(diagnostic for diagnostic in result.warnings if diagnostic["id"] == "quality.tests.missingSmoke")
self.assertEqual(warning["path"], "src/app.test.ts")
self.assertIn("src/app.test.ts", warning["message"])
def test_warns_when_tests_miss_base_path_smoke(self) -> None:
self.add_package_file("src/app.test.ts", "test('health', () => {})\n")
result = self.check()
warning = next(diagnostic for diagnostic in result.warnings if diagnostic["id"] == "quality.tests.missingBasePathSmoke")
self.assertEqual(warning["path"], "src/app.test.ts")
self.assertIn("basePath", warning["message"])
def test_accepts_base_path_smoke_coverage(self) -> None:
self.add_package_file("src/app.test.ts", "test('custom basePath', () => {})\n")
result = self.check()
self.assertNotIn("quality.tests.missingBasePathSmoke", diagnostic_ids(result.warnings))
def test_warns_when_feature_source_has_no_adjacent_unit_test(self) -> None:
self.add_package_file("src/app.test.ts", "test('custom basePath', () => {})\n")
self.add_package_file("src/features/workspaces/service.ts", "export class WorkspaceService {}\n")
result = self.check()
warning = next(
diagnostic
for diagnostic in result.warnings
if diagnostic["id"] == "quality.tests.missingFeatureUnit"
)
self.assertEqual(warning["path"], "src/features/workspaces/service.test.ts")
self.assertIn("adjacent unit test", warning["message"])
def test_accepts_adjacent_feature_unit_test(self) -> None:
self.add_package_file("src/app.test.ts", "test('custom basePath', () => {})\n")
self.add_package_file("src/features/workspaces/service.ts", "export class WorkspaceService {}\n")
self.add_package_file("src/features/workspaces/service.test.ts", "test('service behavior', () => {})\n")
result = self.check()
self.assertNotIn("quality.tests.missingFeatureUnit", diagnostic_ids(result.warnings))
def test_accepts_adjacent_feature_spec_test(self) -> None:
self.add_package_file("src/app.test.ts", "test('custom basePath', () => {})\n")
self.add_package_file("src/features/workspaces/service.ts", "export class WorkspaceService {}\n")
self.add_package_file("src/features/workspaces/service.spec.ts", "test('service behavior', () => {})\n")
result = self.check()
self.assertNotIn("quality.tests.missingFeatureUnit", diagnostic_ids(result.warnings))
def test_warns_when_feature_repository_has_no_adjacent_unit_test(self) -> None:
self.add_package_file("src/app.test.ts", "test('custom basePath', () => {})\n")
self.add_package_file("src/features/workspaces/repository.ts", "export class WorkspacesRepository {}\n")
result = self.check()
self.assertIn(
"src/features/workspaces/repository.test.ts",
diagnostic_paths(result.warnings, "quality.tests.missingFeatureUnit"),
)
def test_warns_when_feature_helper_has_no_adjacent_unit_test(self) -> None:
self.add_package_file("src/app.test.ts", "test('custom basePath', () => {})\n")
self.add_package_file("src/features/workspaces/sortRules.ts", "export const byName = () => 0\n")
result = self.check()
self.assertIn(
"src/features/workspaces/sortRules.test.ts",
diagnostic_paths(result.warnings, "quality.tests.missingFeatureUnit"),
)
def test_feature_unit_warning_ignores_generated_adapters_and_seed(self) -> None:
self.add_package_file("src/app.test.ts", "test('custom basePath', () => {})\n")
self.add_package_file("src/features/workspaces/seed.ts", "export const seedWorkspaces = () => ({})\n")
self.add_package_file("src/features/workspaces/controllers/listWorkspaces.ts", "export const listWorkspaces = () => ({})\n")
result = self.check()
self.assertNotIn("quality.tests.missingFeatureUnit", diagnostic_ids(result.warnings))
def test_feature_unit_warning_ignores_metadata_and_incomplete_sources(self) -> None:
self.add_package_file("src/app.test.ts", "test('custom basePath', () => {})\n")
self.add_package_file("src/features/workspaces/index.ts", "export * from './service.ts'\n")
self.add_package_file("src/features/workspaces/types.ts", "export type WorkspaceId = string\n")
self.add_package_file("src/features/workspaces/model.d.ts", "export type Workspace = { id: string }\n")
self.add_package_file(
"src/features/workspaces/service.ts",
"throw new Error('TODO mockapi: implement service')\n",
)
result = self.check()
self.assertNotIn("quality.tests.missingFeatureUnit", diagnostic_ids(result.warnings))
def test_formats_every_error_and_warning(self) -> None:
self.add_package_file(
"src/app.ts",
"""type Options = { basePath?: string }
export const newApp = ({ basePath = "/api/v1" }: Options = {}) => {
registerRoutes(app, controllers, { basePath: "/api/v1" })
}
""",
)
output = format_quality_result(self.check())
self.assertIn("Errors:", output)
self.assertIn("- quality.basePath.ignoredOption src/app.ts:", output)
self.assertIn("Warnings:", output)
self.assertIn("- quality.tests.missingSmoke", output)
if __name__ == "__main__":
unittest.main()