| 1 | import json |
| 2 | from pathlib import Path |
| 3 | import subprocess |
| 4 | from tempfile import TemporaryDirectory |
| 5 | import unittest |
| 6 | |
| 7 | from document_model import EXPORTER, ordered_pages, walk |
| 8 | from verify_page_creation import compare |
| 9 | |
| 10 | FIXTURE = Path(__file__).resolve().parent.parent / 'corpus/page-lifecycle/creation' |
| 11 | |
| 12 | |
| 13 | class PageCreationTest(unittest.TestCase): |
| 14 | def test_offline_created_pages_and_dependent_bodies_survive_native_reopen(self): |
| 15 | fixture = FIXTURE.parent / 'offline-creation' |
| 16 | compare(fixture / 'candidate', fixture / 'cold') |
| 17 | |
| 18 | def test_created_pages_and_both_writers_survive_native_cold_reopens(self): |
| 19 | for source, capture in [('candidate', 'cold'), ('native/notebook', 'native/cold')]: |
| 20 | with self.subTest(source=source): |
| 21 | compare(FIXTURE / source, FIXTURE / capture) |
| 22 | # OneNote refreshes the renamed titles' stale section metadata copies. |
| 23 | self.assertEqual(compare(FIXTURE / 'followup', FIXTURE / 'followup/cold', refresh_metadata=True), 2) |
| 24 | |
| 25 | def test_duplicate_titles_keep_distinct_pages_through_native_and_rust_edits(self): |
| 26 | with TemporaryDirectory() as temporary: |
| 27 | identities = None |
| 28 | for ordinal, path in enumerate(('candidate', 'native/cold/notebook', 'followup/cold/notebook')): |
| 29 | output = Path(temporary) / str(ordinal) |
| 30 | subprocess.run([EXPORTER, FIXTURE / path / 'Lifecycle.one', output], check=True) |
| 31 | document = json.loads((output / 'document.json').read_text()) |
| 32 | pages = list(ordered_pages(document)) |
| 33 | self.assertEqual(len(pages), 13) |
| 34 | current = [(sid, page) for sid, _, _, page in pages] |
| 35 | self.assertEqual(len(set(current)), 13) |
| 36 | if identities is None: |
| 37 | identities = current |
| 38 | self.assertEqual(identities, current) |
| 39 | for index, (_, _, view, page) in enumerate(pages[-2:]): |
| 40 | title = ('Reviewed ' if ordinal == 2 else '') + 'New 🦋 é' |
| 41 | self.assertEqual(view['nodes'][view['roots']['2']]['kind']['title'], title) |
| 42 | expected = [title] |
| 43 | if ordinal: |
| 44 | expected.append(('Rust + ' if ordinal == 2 else '') + f'Native body {index} after Rust page creation.') |
| 45 | self.assertEqual([node['kind']['text'] for _, node in walk(view, page) |
| 46 | if node['kind']['type'] == 'RichText'], expected) |
| 47 | for at, titled in [(0, True), (10, False)]: |
| 48 | _, _, view, page = pages[at] |
| 49 | self.assertEqual(bool(view['nodes'][page]['structure']), titled) |
| 50 | self.assertEqual(view['nodes'][page]['children'], []) |
| 51 | |
| 52 | |
| 53 | if __name__ == '__main__': |
| 54 | unittest.main() |