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"""Unit tests for remediation models (PageFeatures, DocumentManifest)."""
import json
from datetime import UTC, datetime
import pytest
from pydantic import ValidationError
from src.shared.models.remediation import DocumentManifest, PageFeatures
class TestPageFeatures:
"""Tests for PageFeatures model."""
def test_minimal_valid_page_features(self) -> None:
"""Test creating PageFeatures with only required fields."""
features = PageFeatures(page_num=1)
assert features.page_num == 1
assert features.has_images is False
assert features.image_count == 0
assert features.has_tables is False
assert features.table_count == 0
assert features.has_lists is False
assert features.has_code_blocks is False
assert features.has_math is False
assert features.layout_type == "single_column"
assert features.has_headers_footers is False
assert features.complexity_score == 0.5
assert features.complexity_factors == []
def test_full_page_features(self) -> None:
"""Test creating PageFeatures with all fields."""
features = PageFeatures(
page_num=5,
has_images=True,
image_count=3,
has_tables=True,
table_count=2,
has_lists=True,
has_code_blocks=True,
has_math=True,
layout_type="two_column",
has_headers_footers=True,
complexity_score=0.85,
complexity_factors=["dense tables", "code blocks", "math notation"]
)
assert features.page_num == 5
assert features.has_images is True
assert features.image_count == 3
assert features.has_tables is True
assert features.table_count == 2
assert features.has_lists is True
assert features.has_code_blocks is True
assert features.has_math is True
assert features.layout_type == "two_column"
assert features.has_headers_footers is True
assert features.complexity_score == 0.85
assert len(features.complexity_factors) == 3
def test_page_num_validation(self) -> None:
"""Test page_num must be >= 1."""
with pytest.raises(ValidationError) as exc_info:
PageFeatures(page_num=0)
assert "page_num" in str(exc_info.value)
def test_negative_page_num_rejected(self) -> None:
"""Test negative page numbers are rejected."""
with pytest.raises(ValidationError):
PageFeatures(page_num=-1)
def test_image_count_validation(self) -> None:
"""Test image_count must be >= 0."""
with pytest.raises(ValidationError):
PageFeatures(page_num=1, image_count=-1)
def test_complexity_score_bounds(self) -> None:
"""Test complexity_score must be between 0.0 and 1.0."""
# Valid at boundaries
features_min = PageFeatures(page_num=1, complexity_score=0.0)
features_max = PageFeatures(page_num=1, complexity_score=1.0)
assert features_min.complexity_score == 0.0
assert features_max.complexity_score == 1.0
# Invalid below minimum
with pytest.raises(ValidationError):
PageFeatures(page_num=1, complexity_score=-0.1)
# Invalid above maximum
with pytest.raises(ValidationError):
PageFeatures(page_num=1, complexity_score=1.1)
def test_layout_type_validation(self) -> None:
"""Test layout_type accepts only valid values."""
# Valid values
for layout in ["single_column", "two_column", "mixed"]:
features = PageFeatures(page_num=1, layout_type=layout) # type: ignore[arg-type]
assert features.layout_type == layout
# Invalid value
with pytest.raises(ValidationError):
PageFeatures(page_num=1, layout_type="three_column") # type: ignore[arg-type]
def test_json_serialization(self) -> None:
"""Test PageFeatures serializes to JSON correctly."""
features = PageFeatures(
page_num=1,
has_images=True,
image_count=2,
complexity_factors=["images", "complex layout"]
)
json_str = features.model_dump_json()
data = json.loads(json_str)
assert data["page_num"] == 1
assert data["has_images"] is True
assert data["image_count"] == 2
assert data["complexity_factors"] == ["images", "complex layout"]
def test_json_deserialization(self) -> None:
"""Test PageFeatures deserializes from JSON correctly."""
data = {
"page_num": 3,
"has_images": True,
"image_count": 1,
"has_tables": False,
"table_count": 0,
"has_lists": True,
"has_code_blocks": False,
"has_math": False,
"layout_type": "single_column",
"has_headers_footers": True,
"complexity_score": 0.6,
"complexity_factors": ["nested lists"]
}
features = PageFeatures(**data)
assert features.page_num == 3
assert features.has_images is True
assert features.has_lists is True
# =========================================================================
# Validator Tests (consistency enforcement)
# =========================================================================
def test_image_consistency_auto_fix_count(self) -> None:
"""Test that has_images=True with image_count=0 is auto-fixed."""
features = PageFeatures(
page_num=1,
has_images=True,
image_count=0, # Inconsistent - should be auto-fixed to 1
)
# Validator should auto-fix to at least 1
assert features.has_images is True
assert features.image_count == 1
def test_image_consistency_auto_fix_flag(self) -> None:
"""Test that has_images=False with image_count>0 is auto-fixed."""
features = PageFeatures(
page_num=1,
has_images=False,
image_count=3, # Inconsistent - should be auto-fixed to 0
)
# Validator should auto-fix count to 0
assert features.has_images is False
assert features.image_count == 0
def test_image_consistency_valid_state(self) -> None:
"""Test valid image states pass through unchanged."""
# has_images=True with count > 0
features1 = PageFeatures(page_num=1, has_images=True, image_count=3)
assert features1.has_images is True
assert features1.image_count == 3
# has_images=False with count == 0
features2 = PageFeatures(page_num=1, has_images=False, image_count=0)
assert features2.has_images is False
assert features2.image_count == 0
def test_table_consistency_auto_fix_count(self) -> None:
"""Test that has_tables=True with table_count=0 is auto-fixed."""
features = PageFeatures(
page_num=1,
has_tables=True,
table_count=0, # Inconsistent - should be auto-fixed to 1
)
assert features.has_tables is True
assert features.table_count == 1
def test_table_consistency_auto_fix_flag(self) -> None:
"""Test that has_tables=False with table_count>0 is auto-fixed."""
features = PageFeatures(
page_num=1,
has_tables=False,
table_count=2, # Inconsistent - should be auto-fixed to 0
)
assert features.has_tables is False
assert features.table_count == 0
def test_complexity_factors_cleared_for_low_score(self) -> None:
"""Test that complexity_factors is cleared when score <= 0.3."""
features = PageFeatures(
page_num=1,
complexity_score=0.2, # Low complexity
complexity_factors=["should", "be", "cleared"],
)
# Validator should clear factors for low complexity
assert features.complexity_score == 0.2
assert features.complexity_factors == []
def test_complexity_factors_kept_for_high_score(self) -> None:
"""Test that complexity_factors is kept when score > 0.3."""
features = PageFeatures(
page_num=1,
complexity_score=0.7, # High complexity
complexity_factors=["dense tables", "nested lists"],
)
# Factors should be preserved
assert features.complexity_score == 0.7
assert features.complexity_factors == ["dense tables", "nested lists"]
def test_complexity_factors_boundary_case(self) -> None:
"""Test boundary case at complexity_score = 0.3."""
# At exactly 0.3, factors should be cleared (score <= 0.3)
features = PageFeatures(
page_num=1,
complexity_score=0.3,
complexity_factors=["some factor"],
)
assert features.complexity_factors == []
# Just above 0.3, factors should be kept
features2 = PageFeatures(
page_num=1,
complexity_score=0.31,
complexity_factors=["some factor"],
)
assert features2.complexity_factors == ["some factor"]
class TestDocumentManifest:
"""Tests for DocumentManifest model."""
def test_minimal_valid_manifest(self) -> None:
"""Test creating DocumentManifest with only required fields."""
manifest = DocumentManifest(
job_id="550e8400-e29b-41d4-a716-446655440000",
total_pages=10,
heading_tree_json='{"sections": []}'
)
assert manifest.job_id == "550e8400-e29b-41d4-a716-446655440000"
assert manifest.total_pages == 10
assert manifest.heading_tree_json == '{"sections": []}'
assert manifest.document_title == "Untitled"
assert manifest.document_type == "unknown"
assert manifest.page_features == []
assert manifest.required_agents == []
assert manifest.skip_agents == []
assert manifest.analysis_confidence == 0.8
assert manifest.analysis_notes == ""
assert manifest.analysis_model == "claude-sonnet-4-5"
def test_full_manifest(self) -> None:
"""Test creating DocumentManifest with all fields."""
page_features = [
PageFeatures(page_num=1, has_images=True, image_count=2),
PageFeatures(page_num=2, has_tables=True, table_count=1),
]
manifest = DocumentManifest(
job_id="job-123",
document_title="CS 101 Syllabus",
document_type="syllabus",
total_pages=10,
heading_tree_json='{"document_title": "CS 101", "sections": []}',
page_features=page_features,
required_agents=["figures", "tables"],
skip_agents=["typography"],
analysis_confidence=0.92,
analysis_notes="Clear structure detected",
analysis_model="claude-sonnet-4-5"
)
assert manifest.document_title == "CS 101 Syllabus"
assert manifest.document_type == "syllabus"
assert len(manifest.page_features) == 2
assert manifest.required_agents == ["figures", "tables"]
assert manifest.skip_agents == ["typography"]
assert manifest.analysis_confidence == 0.92
def test_total_pages_validation(self) -> None:
"""Test total_pages must be >= 1."""
with pytest.raises(ValidationError):
DocumentManifest(
job_id="job-123",
total_pages=0,
heading_tree_json='{}'
)
def test_analysis_confidence_bounds(self) -> None:
"""Test analysis_confidence must be between 0.0 and 1.0."""
# Valid at boundaries
manifest_min = DocumentManifest(
job_id="job-123",
total_pages=1,
heading_tree_json='{}',
analysis_confidence=0.0
)
manifest_max = DocumentManifest(
job_id="job-123",
total_pages=1,
heading_tree_json='{}',
analysis_confidence=1.0
)
assert manifest_min.analysis_confidence == 0.0
assert manifest_max.analysis_confidence == 1.0
# Invalid values
with pytest.raises(ValidationError):
DocumentManifest(
job_id="job-123",
total_pages=1,
heading_tree_json='{}',
analysis_confidence=1.5
)
def test_created_at_default(self) -> None:
"""Test created_at is set to current UTC time by default."""
before = datetime.now(UTC)
manifest = DocumentManifest(
job_id="job-123",
total_pages=1,
heading_tree_json='{}'
)
after = datetime.now(UTC)
assert before <= manifest.created_at <= after
def test_json_serialization(self) -> None:
"""Test DocumentManifest serializes to JSON correctly."""
manifest = DocumentManifest(
job_id="job-123",
document_title="Test Doc",
total_pages=5,
heading_tree_json='{"sections": []}',
required_agents=["figures"],
page_features=[PageFeatures(page_num=1)]
)
json_str = manifest.model_dump_json()
data = json.loads(json_str)
assert data["job_id"] == "job-123"
assert data["document_title"] == "Test Doc"
assert data["total_pages"] == 5
assert data["required_agents"] == ["figures"]
assert len(data["page_features"]) == 1
def test_json_round_trip(self) -> None:
"""Test DocumentManifest survives JSON round-trip."""
original = DocumentManifest(
job_id="job-123",
document_title="Round Trip Test",
total_pages=3,
heading_tree_json='{"test": true}',
page_features=[
PageFeatures(page_num=1, has_images=True),
PageFeatures(page_num=2, has_tables=True),
],
required_agents=["figures", "tables"],
analysis_confidence=0.88
)
json_str = original.model_dump_json()
restored = DocumentManifest.model_validate_json(json_str)
assert restored.job_id == original.job_id
assert restored.document_title == original.document_title
assert restored.total_pages == original.total_pages
assert len(restored.page_features) == len(original.page_features)
assert restored.required_agents == original.required_agents
assert restored.analysis_confidence == original.analysis_confidence
def test_nested_page_features(self) -> None:
"""Test page_features list contains valid PageFeatures objects."""
manifest = DocumentManifest(
job_id="job-123",
total_pages=2,
heading_tree_json='{}',
page_features=[
PageFeatures(page_num=1),
PageFeatures(page_num=2, has_images=True)
]
)
assert len(manifest.page_features) == 2
assert manifest.page_features[0].page_num == 1
assert manifest.page_features[1].has_images is True
def test_schema_example(self) -> None:
"""Test the JSON schema example is valid."""
schema = DocumentManifest.model_json_schema()
example = schema.get("examples", [{}])[0] if "examples" in schema else {}
# The example should be in json_schema_extra
config_extra = DocumentManifest.model_config.get("json_schema_extra", {})
if "example" in config_extra:
example = config_extra["example"]
# Validate it creates a valid model
manifest = DocumentManifest(**example)
assert manifest.job_id == example["job_id"]