vomebook commited on
Commit ·
a5c30a5
1
Parent(s): 6db4908
Repair isolated release tests for download and chapter progress
Browse filesFiles changed (3)
423 unmodified lines4244254264274284294304314324334344354364374384394404414424434444454464474284294304484494504514334344354364374384524394534544558 unmodified lines464465466454467468469470423 unmodified lines root = Path(directory) for name in ('backend', 'quality', 'scripts/coverage-anchors'): shutil.copytree(live / name, root / name) # Unlike ordinary no-debt destinations, the audited app must match # the exact successor even when the historical proof uses frozen bytes. app = root / 'backend/app.py' original = app.read_bytes() marker = b'from backend.search.domain.ordering import ' data = original.replace(marker, b'from backend.search.domain.ordering import ') self.assertNotIn(marker, data) app.write_bytes(b'# Live comment shifts line numbers.\n' + data) self.assertTrue(transport_layout(root)) # The path map still identifies the layout. for validate in (lambda: transport_baseline(root, debt), lambda: reviewed_source(root, 'backend/app.py'), lambda: validate_migration(root)): with self.assertRaisesRegex(ValueError, r'exact current source: backend/app\.py'): validate() app.unlink() with self.assertRaises(FileNotFoundError) as error: transport_baseline(root, debt) self.assertEqual(error.exception.filename, str(app)) app.write_bytes(original)
# Exercise both path-map and transition-only overlaps, plus a plain destination. changed = ('backend/app.py', 'backend/search/domain/ordering.py', 'backend/search/application/requests.py', 'backend/search/http/schemas.py', 'backend/catalog/application/metadata.py') changed = ('backend/search/domain/ordering.py', 'backend/search/application/requests.py', 'backend/search/http/schemas.py', 'backend/catalog/application/metadata.py') for name in changed: source = root / name data = source.read_bytes() if name == 'backend/app.py': data = data.replace(b'from backend.search.domain.ordering import ', b'from backend.search.domain.ordering import ') self.assertNotIn(b'from backend.search.domain.ordering import ', data) source.write_bytes(b'# Live comment shifts line numbers.\n' + data) source.write_bytes(b'# Live comment shifts line numbers.\n' + source.read_bytes()) self.assertTrue(transport_layout(root)) self.assertEqual(transport_baseline(root, debt), expected) for name in changed[:-1]: with self.subTest(strict_source=name), self.assertRaisesRegex(ValueError, 'exact current source'):8 unmodified lines with tempfile.TemporaryDirectory() as fixture_directory: history = build_coverage_history(root, Path(fixture_directory)) frozen = frozen_refactor_sources(live) for name in changed[1:]: for name in changed: self.assertEqual((history / name).read_bytes(), frozen[name]) self.assertEqual((history / 'backend/app.py').read_bytes(), (live / 'scripts/coverage-anchors/transport-parent-app.py.snapshot').read_bytes())61 unmodified lines626364656667686970717273747576777879803368 unmodified lines3449345034513439344034413442344334443445345234533454345553 unmodified lines3509351035113505350635073508350935103511351235133514351539 unmodified lines3555355635573557355835593560356135623563355835593560356150 unmodified lines3612361336143620362136223623362436253626361536163617361842 unmodified lines3661366236633675367636773678367936803681366436653666366735 unmodified lines3703370437053723372437253726372737283729370637073708370965 unmodified lines3775377637773801380238033804380538063807377837793780378161 unmodified lines (self.root / "scripts/check_python_static.py").write_bytes((ROOT / "scripts/check_python_static.py").read_bytes()) (self.root / "scripts/sample.py").write_text("print('ok')\n")
def materialize_reader_exact_project(self, project): # Chapter progress imports ChapterLoader, whose declarations reach the # search core's SearchResult through reader-chapter-search-strict.d.ts. # Copy this transitive dependency without changing the config's root files. dependency = "static/reader/search/reader-chapter-search-core.mjs" for name in [*project["files"], dependency]: target = self.root / name target.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(ROOT / name, target) config = self.root / "jsconfig.reader-exact.json" config.write_text(json.dumps(project)) return config
def run_check(self, *command, timeout=30, tap_stdout=False): # Replay only stdout: a Node warning on stderr is not TAP input. return subprocess.run(command, cwd=self.root, text=True, stdout=subprocess.PIPE,3368 unmodified lines self.assert_check(self.run_check(*check), False, "TS2769")
def test_reader_chapter_exact_gate_checks_original_bounds_invariant(self): config = self.root / "jsconfig.reader-exact.json" original = load_type_config("jsconfig.reader-exact.json") config.write_text(json.dumps(original)) for name in original["files"]: target = self.root / name target.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(ROOT / name, target) config = self.materialize_reader_exact_project(load_type_config("jsconfig.reader-exact.json")) check = ["node", ROOT / "node_modules/typescript/bin/tsc", "--project", config] self.assert_check(self.run_check(*check), True, "") module = self.root / "static/reader/formats/reader-chapter-repository.js"53 unmodified lines adapter = self.root / "static/reader/pdf/pdf-first-page-worker-adapter.mjs" adapter.parent.mkdir(parents=True, exist_ok=True) original = (ROOT / "static/reader/pdf/pdf-first-page-worker-adapter.mjs").read_text() local_config = self.root / "jsconfig.reader-exact.json" project = load_type_config("jsconfig.reader-exact.json") for name in project["files"]: target = self.root / name target.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(ROOT / name, target) local_config.write_text(json.dumps(project)) local_config = self.materialize_reader_exact_project(load_type_config("jsconfig.reader-exact.json")) production_check = ["node", ROOT / "node_modules/typescript/bin/tsc", "--project", local_config] self.assert_check(self.run_check(*production_check), True, "") for annotation in [39 unmodified lines with self.subTest(expression=expression): source.write_text(prefix + expression + '\n') self.assert_check(self.run_check(*check), False, diagnostic) project = load_type_config('jsconfig.reader-exact.json') for name in project['files']: target = self.root / name target.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(ROOT / name, target) local_config = self.root / 'jsconfig.reader-exact.json' local_config.write_text(json.dumps(project)) local_config = self.materialize_reader_exact_project(load_type_config('jsconfig.reader-exact.json')) production_check = ['node', ROOT / 'node_modules/typescript/bin/tsc', '--project', local_config] self.assert_check(self.run_check(*production_check), True, '') local = self.root / 'static/reader/search/reader-pdf-book-search.mjs'50 unmodified lines with self.subTest(expression=expression): source.write_text(prefix + expression + '\n') self.assert_check(self.run_check(*check), False, diagnostic) project = load_type_config('jsconfig.reader-exact.json') for name in project['files']: target = self.root / name target.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(ROOT / name, target) local_config = self.root / 'jsconfig.reader-exact.json' local_config.write_text(json.dumps(project)) local_config = self.materialize_reader_exact_project(load_type_config('jsconfig.reader-exact.json')) production_check = ['node', ROOT / 'node_modules/typescript/bin/tsc', '--project', local_config] self.assert_check(self.run_check(*production_check), True, '') local = self.root / 'static/reader/formats/reader-text-format.mjs'42 unmodified lines with self.subTest(expression=expression): source.write_text(prefix + expression + '\n') self.assert_check(self.run_check(*check), False, diagnostic) project = load_type_config('jsconfig.reader-exact.json') for name in project['files']: target = self.root / name target.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(ROOT / name, target) local_config = self.root / 'jsconfig.reader-exact.json' local_config.write_text(json.dumps(project)) local_config = self.materialize_reader_exact_project(load_type_config('jsconfig.reader-exact.json')) production_check = ['node', ROOT / 'node_modules/typescript/bin/tsc', '--project', local_config] self.assert_check(self.run_check(*production_check), True, '') local = self.root / 'static/reader/formats/reader-media-format.mjs'35 unmodified lines with self.subTest(expression=expression): source.write_text(prefix + expression + "\n") self.assert_check(self.run_check(*check), False, diagnostic) local_config = self.root / "jsconfig.reader-exact.json" project = load_type_config("jsconfig.reader-exact.json") for name in project["files"]: target = self.root / name target.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(ROOT / name, target) local_config.write_text(json.dumps(project)) local_config = self.materialize_reader_exact_project(load_type_config("jsconfig.reader-exact.json")) production_check = ["node", ROOT / "node_modules/typescript/bin/tsc", "--project", local_config] self.assert_check(self.run_check(*production_check), True, "") lifecycle = self.root / "static/reader/core/reader-lifecycle.mjs"65 unmodified lines self.assert_check(self.run_check(*check), False, "TS2375") configure({**options, "exactOptionalPropertyTypes": False}) self.assert_check(self.run_check(*check), True, "") local_config = self.root / "jsconfig.reader-exact.json" local_config.write_text(json.dumps(original)) for name in [*original["files"], "typings/reader-security-strict.d.ts", "typings/reader-store-strict.d.ts", "typings/reader-contract-strict.d.ts", "typings/reader-toc-strict.d.ts"]: target = self.root / name target.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(ROOT / name, target) local_config = self.materialize_reader_exact_project(original) production_check = ["node", ROOT / "node_modules/typescript/bin/tsc", "--project", local_config] self.assert_check(self.run_check(*production_check), True, "") # Block scope keeps the probes independent even in classic scripts.1088 unmodified lines108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711041105110611381139114011411142114311444 unmodified lines114911501151115211531154115511561121115711581159116011611162116311642 unmodified lines11671168116911701171117211331134113511731174117511761177117811791180118111821183118411852 unmodified lines118811891190119111921193119414 unmodified lines120912101211121212131214121512161 unmodified line121812191220122112221223122411781179122512261227122812291 unmodified line123112321233123412351236123712381239119111921193124012411242124312441245124612471088 unmodified lines }''') self.assertTrue(result)
def _watchdog_history_probe(self): # Chromium can cache a stale renderer history.length after opening the # iframe, even after it loads. Check the browser's actual entry IDs, # URLs and position; Firefox uses length plus the real traversals. context = self.fixture.context cdp = context.new_cdp_session(self.page) if self.fixture.browser.browser_type.name == 'chromium' else None
def snapshot(): if cdp is None: return {'length': self.page.evaluate('history.length')} history = cdp.send('Page.getNavigationHistory') return {'length': len(history['entries']), 'index': history['currentIndex'], 'entries': [(entry['id'], entry['url']) for entry in history['entries']]}
return snapshot
def _assert_reader_added_one_entry(self, search, reader, reader_url): self.assertEqual(reader['length'], search['length'] + 1) if 'entries' in reader: self.assertEqual(reader['index'], search['index'] + 1) self.assertEqual(reader['entries'][:-1], search['entries']) self.assertEqual(reader['entries'][reader['index']][1], reader_url)
def _assert_reader_back_history(self, snapshot, search, reader): expected = dict(reader) if 'index' in search: expected['index'] = search['index'] self.assertEqual(snapshot(), expected)
def _assert_reader_frame_identity(self, url): self.assertEqual(self.page.evaluate('[readerHost.overlay.src, readerHost.overlay.contentDocument.URL]'), [url, url])
def test_watchdog_late_back_preserves_reader_forward_and_foreign_state(self): history_snapshot = self._watchdog_history_probe() self.page.locator('#search-input').focus() before = self.page.evaluate('''() => { history.replaceState({...history.state, foreignSearch:{keep:1}}, '', location.href); return {url:location.href, state:history.state, rows:STATE.results.map(getResultStableId)}; }''') search_history = history_snapshot() self.page.evaluate('''() => { const path=new URL(document.querySelector('script[src*="search/reader-host/application/navigation.js"]').src) .pathname.replace('search/reader-host/application/navigation.js','reader.html'); navigateToReader(path+'?ext=txt&title=Delayed'); history.replaceState({...history.state, foreignReader:{keep:2}}, '', location.href); }''') self.page.frame_locator('.reader-overlay').locator('body').wait_for() # The iframe's initial navigation can itself change the joint history length. opened = self.page.evaluate('({url:location.href,state:history.state,length:history.length})') self.page.frame_locator('.reader-overlay').locator('body', has_text='reader').wait_for() opened = self.page.evaluate('({url:location.href,state:history.state})') reader_history = history_snapshot() self._assert_reader_added_one_entry(search_history, reader_history, opened['url']) self.page.evaluate('''() => { const back=history.back.bind(history); window.__releaseBack=()=>{history.back=back;back();};4 unmodified lines history[name]=(...args)=>{__historyWrites++;return original(...args);}; } window.__returnSnapshot=readerReturnSnapshot; window.__closingFrame=readerHost.overlay; handleReaderMessage({origin:location.origin,source:readerHost.overlay.contentWindow,data:{type:'voice-reader-close'}}); }''') recovery = self.page.get_by_role('link', name='直接打开搜索页') recovery.wait_for(state='visible', timeout=3000) self.assertEqual(self.page.evaluate('({url:location.href,state:history.state,length:history.length})'), opened) self.assertEqual(self.page.evaluate('({url:location.href,state:history.state})'), opened) self.assertEqual(history_snapshot(), reader_history) self.assertEqual(self.page.evaluate('[__backCalls,__historyWrites]'), [1, 0]) self.assertTrue(self.page.evaluate('readerClosing && readerReturnSnapshot === __returnSnapshot')) self.assertTrue(self.page.evaluate('readerHost.overlay === __closingFrame && __closingFrame.isConnected')) # Release the sole requested traversal only after the watchdog has run. self.page.evaluate('__releaseBack()') self.page.locator('.reader-overlay').wait_for(state='detached')2 unmodified lines self.assertEqual(self.page.evaluate('STATE.results.map(getResultStableId)'), before['rows']) self.assertEqual(self.page.evaluate('document.activeElement.id'), 'search-input') self.assertEqual(self.page.locator('.reader-overlay-opening').count(), 0) self._assert_reader_back_history(history_snapshot, search_history, reader_history) writes_before_forward = self.page.evaluate('__historyWrites') self.page.evaluate('history.forward()') self.page.frame_locator('.reader-overlay').locator('body').wait_for() self.assertEqual(self.page.evaluate('({url:location.href,state:history.state,length:history.length})'), opened) self.assertEqual(self.page.evaluate('readerHost.overlay.src'), opened['state']['readerUrl']) self.page.frame_locator('.reader-overlay').locator('body', has_text='reader').wait_for() self.assertEqual(self.page.evaluate('({url:location.href,state:history.state})'), opened) self.assertEqual(history_snapshot(), reader_history) self.assertEqual(self.page.evaluate('__historyWrites'), writes_before_forward) self._assert_reader_frame_identity(opened['state']['readerUrl']) self.assertFalse(self.page.evaluate('readerClosing')) self.assertEqual(self.page.locator('.reader-overlay-opening a').count(), 0)
def test_pending_watchdog_late_frame_and_back_preserve_history(self): history_snapshot = self._watchdog_history_probe() held = [] self.page.unroute('**/static/reader.html?**') self.page.route('**/static/reader.html?**', lambda route: held.append(route))2 unmodified lines history.replaceState({...history.state, foreignSearch:{keep:1}}, '', location.href); return {url:location.href, state:history.state, rows:STATE.results.map(getResultStableId)}; }''') search_history = history_snapshot() with self.page.expect_request('**/static/reader.html?**'): self.page.evaluate('''() => { const path=new URL(document.querySelector('script[src*="search/reader-host/application/navigation.js"]').src)14 unmodified lines opening.wait_for(state='visible') self.assertEqual(len(held), 1) pending = self.page.evaluate('({url:location.href,state:history.state})') reader_history = history_snapshot() self._assert_reader_added_one_entry(search_history, reader_history, pending['url']) opening.locator('button').click() recovery = self.page.get_by_role('link', name='直接打开搜索页') recovery.wait_for(state='visible', timeout=3000)1 unmodified line self.assertEqual(self.page.evaluate('[__backCalls,__historyWrites]'), [1, 0]) self.assertTrue(self.page.evaluate('readerClosing && readerReturnSnapshot === __returnSnapshot')) self.assertEqual(self.page.evaluate('({url:location.href,state:history.state})'), pending) self.assertEqual(history_snapshot(), reader_history) self._deliver_late_reader_shell(held.pop(), recovery) self.assertEqual(self.page.evaluate('[__backCalls,__historyWrites]'), [1, 0]) self.assertTrue(self.page.evaluate('readerClosing && readerReturnSnapshot === __returnSnapshot')) opened = self.page.evaluate('({url:location.href,state:history.state,length:history.length})') self.assertEqual({k: opened[k] for k in pending}, pending) self.assertEqual(self.page.evaluate('({url:location.href,state:history.state})'), pending) self.assertEqual(history_snapshot(), reader_history) self.page.evaluate('__releaseBack()') self.page.locator('.reader-overlay').wait_for(state='detached') self.assertEqual(self.page.url, before['url'])1 unmodified line self.assertEqual(self.page.evaluate('STATE.results.map(getResultStableId)'), before['rows']) self.assertEqual(self.page.evaluate('document.activeElement.id'), 'search-input') self.assertEqual(self.page.locator('.reader-overlay-opening').count(), 0) self._assert_reader_back_history(history_snapshot, search_history, reader_history) self.page.unroute('**/static/reader.html?**') self.page.route('**/static/reader.html?**', lambda route: route.fulfill( content_type='text/html', body='<body>reader</body>')) writes_before_forward = self.page.evaluate('__historyWrites') self.page.evaluate('history.forward()') self.page.frame_locator('.reader-overlay').locator('body').wait_for() self.assertEqual(self.page.evaluate('({url:location.href,state:history.state,length:history.length})'), opened) self.assertEqual(self.page.evaluate('readerHost.overlay.src'), opened['state']['readerUrl']) self.page.frame_locator('.reader-overlay').locator('body', has_text='reader').wait_for() self.assertEqual(self.page.evaluate('({url:location.href,state:history.state})'), pending) self.assertEqual(history_snapshot(), reader_history) self.assertEqual(self.page.evaluate('__historyWrites'), writes_before_forward) self._assert_reader_frame_identity(pending['state']['readerUrl']) self.assertFalse(self.page.evaluate('readerClosing')) self.assertEqual(self.page.locator('.reader-overlay-opening a').count(), 0)
tests/gates/test_lifespan_coverage_slice.py+24-11
tests/gates/test_static_checks.py+20-49
tests/reader/browser/test_reader_navigation_host.py+63-12