1#!/usr/bin/env python3
2"""Check native fixture content independently of a revision-store parser."""
3import base64
4from collections import Counter
5import hashlib
6import json
7from pathlib import Path
8import xml.etree.ElementTree as ET
9from native_xml import ns, texts, pages
10
11root = Path(__file__).resolve().parent.parent
12native = root / 'corpus/native/20260905-05'
13
14
15manifest = json.loads((native / 'manifest.json').read_text(encoding='utf-8-sig'))
16assert manifest['status'] == 'completed'
17assert manifest['office']['file_version'] == '14.0.7015.1000'
18assert len(manifest['fixtures']) == 9
19for fixture in manifest['fixtures']:
20 name = fixture['name']
21 assert fixture['status'] == 'captured', name
22 snapshot = native / 'snapshots' / name
23 recorded = json.loads((snapshot / 'snapshot.json').read_text(encoding='utf-8-sig'))
24 for item in recorded['files']:
25 data = (snapshot / 'notebook' / item['path'].replace('\\', '/')).read_bytes()
26 assert len(data) == item['bytes'], name
27 assert hashlib.sha256(data).hexdigest() == item['sha256'], name
28 warm = pages(native / 'evidence' / name / 'pages')
29 cold = pages(root / 'corpus/native' / ('cold-05-' + name) / 'read')
30 number = int(name[:2])
31 assert len(warm) == len(cold) == (0 if number == 0 else 1), name
32 if not cold:
33 continue
34 page = cold[0]
35 assert texts(page) == texts(warm[0]), name
36 expected = []
37 if number == 2:
38 expected = ['Fictitious plain text.']
39 elif number >= 3:
40 expected = ['Fictitious: café, 東京, مرحبا']
41 if number >= 4:
42 expected += ['Fictitious positioned outline.']
43 if number >= 7:
44 expected += ['Left cell', 'Right cell']
45 assert texts(page) == expected, name
46 if number >= 3:
47 assert 'font-weight:bold' in ''.join(n.text or '' for n in page.findall('.//one:T', ns)), name
48 assert any('color:#1F4E79' in n.get('style', '') for n in page.findall('.//one:OE', ns)), name
49 if number >= 4:
50 positioned = [o for o in page.findall('one:Outline', ns) if texts(o) == ['Fictitious positioned outline.']]
51 assert len(positioned) == 1, name
52 position = positioned[0].find('one:Position', ns)
53 assert float(position.get('x')) == 144 and float(position.get('y')) == 96, name
54 images = page.findall('.//one:Image/one:Data', ns)
55 assert len(images) == int(number >= 5), name
56 for data in images:
57 assert base64.b64decode(data.text) == (native / 'assets/fictitious-image.png').read_bytes(), name
58 attachments = page.findall('.//one:InsertedFile', ns)
59 assert len(attachments) == int(number >= 6), name
60 if attachments:
61 assert attachments[0].get('preferredName') == 'fictitious-attachment.txt', name
62 tables = page.findall('.//one:Table', ns)
63 assert len(tables) == int(number >= 7), name
64 if tables:
65 assert [texts(cell) for cell in tables[0].findall('one:Row/one:Cell', ns)] == [['Left cell'], ['Right cell']], name
66
67ink = root / 'corpus/native-ink/cold-ui-ink/read'
68ink_pages = pages(ink)
69assert len(ink_pages) == 1
70drawings = ink_pages[0].findall('one:InkDrawing', ns)
71assert len(drawings) == 2
72for drawing in drawings:
73 assert len(base64.b64decode(drawing.find('one:Data', ns).text)) > 20
74attachment = list(ink.glob('*.attachment'))
75assert len(attachment) == 1
76assert attachment[0].read_bytes() == (native / 'assets/fictitious-attachment.txt').read_bytes()
77
78encrypted = root / 'corpus/native-encrypted'
79provenance = json.loads((encrypted / 'manifest.json').read_text())
80for item in provenance['files']:
81 data = (encrypted / item['path']).read_bytes()
82 assert len(data) == item['bytes']
83 assert hashlib.sha256(data).hexdigest() == item['sha256']
84unlocked = pages(encrypted / 'cold-encrypted-02/read')
85assert len(unlocked) == 1
86assert texts(unlocked[0]) == [''] + texts(pages(root / 'corpus/native/cold-05-07-table/read')[0])
87assert next((encrypted / 'cold-encrypted-02/read').glob('*.attachment')).read_bytes() == attachment[0].read_bytes()
88
89deleted = root / 'corpus/native-delete/cold-deletion-05/read'
90deleted_pages = pages(deleted)
91assert len(deleted_pages) == 2
92marker = [page for page in deleted_pages if texts(page) == ['Recoverable deletion marker.']]
93assert len(marker) == 1
94hierarchy = ET.parse(deleted / 'hierarchy.xml')
95trash = [p.get('ID') for section in hierarchy.findall('.//one:Section', ns)
96 if section.get('isInRecycleBin') == 'true' for p in section.findall('one:Page', ns)]
97assert marker[0].get('ID') in trash
98
99private = root / 'corpus/private'
100source = json.loads((private / 'source-manifest.json').read_text())
101for item in source['files']:
102 assert hashlib.sha256((private / 'original' / item['path']).read_bytes()).hexdigest() == item['sha256']
103private_pages = pages(private / 'exact-native/read')
104private_ids = {p.get('ID') for p in private_pages}
105tree = ET.parse(private / 'exact-native/read/hierarchy.xml')
106assert private_ids == {p.get('ID') for p in tree.findall('.//one:Page', ns)}
107assert len(private_ids) == 26
108features = Counter(e.tag.rsplit('}', 1)[-1] for page in private_pages for e in page.iter())
109assert features['InkDrawing'] and features['MediaFile'] and features['Image'] and features['Tag']
110print('Passed: 9 generated snapshots, cold ink, deletion and encryption fixtures, attachment bytes, 26 private pages')