| 1 | #!/usr/bin/env python3 |
| 2 | """Check native fixture content independently of a revision-store parser.""" |
| 3 | import base64 |
| 4 | from collections import Counter |
| 5 | import hashlib |
| 6 | import json |
| 7 | from pathlib import Path |
| 8 | import xml.etree.ElementTree as ET |
| 9 | from native_xml import ns, texts, pages |
| 10 | |
| 11 | root = Path(__file__).resolve().parent.parent |
| 12 | native = root / 'corpus/native/20260905-05' |
| 13 | |
| 14 | |
| 15 | manifest = json.loads((native / 'manifest.json').read_text(encoding='utf-8-sig')) |
| 16 | assert manifest['status'] == 'completed' |
| 17 | assert manifest['office']['file_version'] == '14.0.7015.1000' |
| 18 | assert len(manifest['fixtures']) == 9 |
| 19 | for 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 | |
| 67 | ink = root / 'corpus/native-ink/cold-ui-ink/read' |
| 68 | ink_pages = pages(ink) |
| 69 | assert len(ink_pages) == 1 |
| 70 | drawings = ink_pages[0].findall('one:InkDrawing', ns) |
| 71 | assert len(drawings) == 2 |
| 72 | for drawing in drawings: |
| 73 | assert len(base64.b64decode(drawing.find('one:Data', ns).text)) > 20 |
| 74 | attachment = list(ink.glob('*.attachment')) |
| 75 | assert len(attachment) == 1 |
| 76 | assert attachment[0].read_bytes() == (native / 'assets/fictitious-attachment.txt').read_bytes() |
| 77 | |
| 78 | encrypted = root / 'corpus/native-encrypted' |
| 79 | provenance = json.loads((encrypted / 'manifest.json').read_text()) |
| 80 | for 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'] |
| 84 | unlocked = pages(encrypted / 'cold-encrypted-02/read') |
| 85 | assert len(unlocked) == 1 |
| 86 | assert texts(unlocked[0]) == [''] + texts(pages(root / 'corpus/native/cold-05-07-table/read')[0]) |
| 87 | assert next((encrypted / 'cold-encrypted-02/read').glob('*.attachment')).read_bytes() == attachment[0].read_bytes() |
| 88 | |
| 89 | deleted = root / 'corpus/native-delete/cold-deletion-05/read' |
| 90 | deleted_pages = pages(deleted) |
| 91 | assert len(deleted_pages) == 2 |
| 92 | marker = [page for page in deleted_pages if texts(page) == ['Recoverable deletion marker.']] |
| 93 | assert len(marker) == 1 |
| 94 | hierarchy = ET.parse(deleted / 'hierarchy.xml') |
| 95 | trash = [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)] |
| 97 | assert marker[0].get('ID') in trash |
| 98 | |
| 99 | private = root / 'corpus/private' |
| 100 | source = json.loads((private / 'source-manifest.json').read_text()) |
| 101 | for item in source['files']: |
| 102 | assert hashlib.sha256((private / 'original' / item['path']).read_bytes()).hexdigest() == item['sha256'] |
| 103 | private_pages = pages(private / 'exact-native/read') |
| 104 | private_ids = {p.get('ID') for p in private_pages} |
| 105 | tree = ET.parse(private / 'exact-native/read/hierarchy.xml') |
| 106 | assert private_ids == {p.get('ID') for p in tree.findall('.//one:Page', ns)} |
| 107 | assert len(private_ids) == 26 |
| 108 | features = Counter(e.tag.rsplit('}', 1)[-1] for page in private_pages for e in page.iter()) |
| 109 | assert features['InkDrawing'] and features['MediaFile'] and features['Image'] and features['Tag'] |
| 110 | print('Passed: 9 generated snapshots, cold ink, deletion and encryption fixtures, attachment bytes, 26 private pages') |