verify_demo.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. """Browser-check the local authentic-dialogue display, not business facts."""
  2. from pathlib import Path
  3. import base64
  4. import functools
  5. import hashlib
  6. import http.server
  7. import json
  8. import subprocess
  9. import tempfile
  10. import threading
  11. import time
  12. from urllib.parse import quote
  13. from urllib.request import urlopen
  14. import websocket
  15. WORK = Path(__file__).resolve().parent
  16. OUT = WORK / 'browser'
  17. OUT.mkdir(exist_ok=True)
  18. class Handler(http.server.SimpleHTTPRequestHandler):
  19. def log_message(self, *args):
  20. pass
  21. def list_directory(self, path):
  22. self.send_error(404)
  23. return None
  24. server = http.server.ThreadingHTTPServer(('127.0.0.1', 0), functools.partial(Handler, directory=str(WORK)))
  25. threading.Thread(target=server.serve_forever, daemon=True).start()
  26. base = 'http://127.0.0.1:' + str(server.server_port)
  27. profile = Path(tempfile.mkdtemp(prefix='ksf-live-case-browser-'))
  28. edge = 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe'
  29. proc = subprocess.Popen([edge, '--headless=new', '--disable-gpu', '--remote-debugging-port=0', '--disable-background-networking', '--disable-component-update', '--disable-sync', '--disable-extensions', '--no-first-run', '--no-default-browser-check', '--user-data-dir=' + str(profile), 'about:blank'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=subprocess.CREATE_NO_WINDOW)
  30. seq = 0
  31. ws = None
  32. errors, requests, checks = [], [], []
  33. try:
  34. deadline = time.monotonic() + 20
  35. while not (profile / 'DevToolsActivePort').exists():
  36. assert time.monotonic() < deadline and proc.poll() is None
  37. time.sleep(.1)
  38. port = (profile / 'DevToolsActivePort').read_text().splitlines()[0]
  39. page = next(p for p in json.load(urlopen('http://127.0.0.1:' + port + '/json/list', timeout=5)) if p['type'] == 'page')
  40. ws = websocket.create_connection(page['webSocketDebuggerUrl'], timeout=20, suppress_origin=True, http_no_proxy=['127.0.0.1', 'localhost'])
  41. def call(method, params=None):
  42. global seq
  43. seq += 1
  44. rid = seq
  45. ws.send(json.dumps({'id': rid, 'method': method, 'params': params or {}}))
  46. while True:
  47. msg = json.loads(ws.recv())
  48. if msg.get('id') == rid:
  49. assert 'error' not in msg, msg
  50. return msg.get('result', {})
  51. if msg.get('method') == 'Runtime.exceptionThrown':
  52. errors.append(msg['params'])
  53. if msg.get('method') == 'Network.requestWillBeSent':
  54. requests.append(msg['params']['request']['url'])
  55. def ev(code, promise=False):
  56. result = call('Runtime.evaluate', {'expression': code, 'returnByValue': True, 'awaitPromise': promise})
  57. assert 'exceptionDetails' not in result, result
  58. return result['result'].get('value')
  59. def settle():
  60. ev('new Promise(r=>requestAnimationFrame(()=>requestAnimationFrame(r)))', True)
  61. call('Page.enable')
  62. call('Runtime.enable')
  63. call('Network.enable')
  64. call('Network.setBlockedURLs', {'urls': ['https://*']})
  65. call('Page.navigate', {'url': base + '/' + quote('案例样板演示.html')})
  66. ev('new Promise((resolve,reject)=>{const end=Date.now()+12000;const poll=()=>{if(document.querySelectorAll("[data-view]").length===15)return resolve(true);if(Date.now()>end)return reject(Error("load timeout"));setTimeout(poll,50)};poll()})', True)
  67. expected = {
  68. 'prompt': (WORK / 'rounds/R01/prompt-r6-short.txt').read_text(encoding='utf-8'),
  69. 'reply': (WORK / 'rounds/R01/reply-display.md').read_text(encoding='utf-8'),
  70. 'followup': (WORK / 'rounds/R02/prompt-display.txt').read_text(encoding='utf-8'),
  71. 'supplement': (WORK / 'rounds/R02/reply-display.md').read_text(encoding='utf-8'),
  72. 'verification': (WORK / 'rounds/R03/prompt-display.txt').read_text(encoding='utf-8'),
  73. 'reviewed': (WORK / 'rounds/R03/reply-display.md').read_text(encoding='utf-8'),
  74. 'comparison-request': (WORK / 'rounds/R04/prompt-display.txt').read_text(encoding='utf-8'),
  75. 'comparison': (WORK / 'rounds/R04/reply-display.md').read_text(encoding='utf-8'),
  76. 'specifications-request': (WORK / 'rounds/R05/prompt-current.txt').read_text(encoding='utf-8'),
  77. 'specifications': (WORK / 'rounds/R05/reply-display.md').read_text(encoding='utf-8'),
  78. 'consumer-request': (WORK / 'rounds/R06/prompt-verbatim.txt').read_text(encoding='utf-8'),
  79. 'consumer': (WORK / 'rounds/R06/reply-display.md').read_text(encoding='utf-8'),
  80. 'checklist-request': (WORK / 'rounds/R07/prompt-verbatim.txt').read_text(encoding='utf-8'),
  81. 'checklist': (WORK / 'rounds/R07/reply-display.md').read_text(encoding='utf-8'),
  82. }
  83. assert ev('JSON.parse(document.querySelector("#dialogue-data").textContent)') == expected
  84. assert ev('document.querySelectorAll(".raw pre").length') == 0
  85. ev('window.screeningContent=document.documentElement.innerHTML.replace(/data:image\\/[^;]+;base64,[A-Za-z0-9+/=]+/g,"[embedded image]")')
  86. assert not ev('/D[12]-(?:\\d\\d|N)|课表|skill|r6|待转发|王刚001|我们的审阅/i.test(window.screeningContent)')
  87. assert not ev('/本轮不付费|未付费测试|零费用|新增费用|免费公开|paidCalls/.test(window.screeningContent)')
  88. for version, round_id in [('v1', 'R02'), ('v2', 'R03')]:
  89. assert ev('document.querySelector(' + json.dumps('[data-atlas="' + version + '"]') + ').srcdoc') == (WORK / f'rounds/{round_id}/artifacts/atlas-display.html').read_text(encoding='utf-8')
  90. assert ev('document.querySelector("[data-atlas=products]").srcdoc') == (WORK / 'rounds/R04/artifacts/comparison-display.html').read_text(encoding='utf-8')
  91. assert ev('document.querySelector("[data-atlas=four-products]").srcdoc') == (WORK / 'rounds/R05/artifacts/four-products-display.html').read_text(encoding='utf-8')
  92. assert ev('document.querySelector("[data-atlas=consumer]").srcdoc') == (WORK / 'rounds/R06/artifacts/consumer-research-display.html').read_text(encoding='utf-8')
  93. assert ev('document.querySelector("[data-atlas=checklist]").srcdoc') == (WORK / 'rounds/R07/artifacts/checklist-display.html').read_text(encoding='utf-8')
  94. # Check original archive hashes, not just consistency of the display copies.
  95. edits = json.loads((WORK / 'presentation-edits.json').read_bytes())
  96. for path, digest in edits['originalFileSha256'].items():
  97. assert hashlib.sha256((WORK / path).read_bytes()).hexdigest() == digest, path
  98. assert edits['originalFileSha256']['rounds/R01/reply-verbatim.md'] == '9be5a3ca2bf65a93eb324d871cecb5dc30c2d5b145e87cb578c7fe6abc3e7465'
  99. assert edits['originalFileSha256']['rounds/R02/artifacts/atlas-received.html'] == '3e1266bd89be7319a4a3a60f926944f81634964219d68c16410cc36d66cff1f4'
  100. assert edits['originalFileSha256']['rounds/R03/artifacts/atlas-received.html'] == '4d74feea33f265dbad084c1f344adc7e533103505cd37a84b317a62df4a12cdb'
  101. assert edits['originalFileSha256']['rounds/R04/artifacts/comparison-received.html'] == '31ad9ca3f6cedfcc33f69c3dbff41f7c43ce584048a5edacf0b76156fbb8dc9f'
  102. assert edits['originalFileSha256']['rounds/R05/artifacts/four-products-received.html'] == '949bdc8db4423e66128e6afd48c7fd10e5df445f54ec6c60b65b52cefe609ac9'
  103. assert edits['originalFileSha256']['rounds/R06/artifacts/consumer-research-received.html'] == '8b20c8d1efe03e7c09ad64e3108e6dde2646a34f814799f3cec136987beda2e6'
  104. assert edits['originalFileSha256']['rounds/R07/artifacts/checklist-received.html'] == '1606d541fdd2712511c365d68bae6a886b04527f1c799514adeeaf182f2cdbdb'
  105. assert (WORK / 'rounds/R08/prompt-verbatim.txt').read_text(encoding='utf-8').strip() not in (WORK / '案例样板演示.html').read_text(encoding='utf-8')
  106. ev('new Promise((resolve,reject)=>{const end=Date.now()+10000;const poll=()=>{if([...document.querySelectorAll("iframe")].every(f=>f.contentDocument?.querySelector("h1")))return resolve(true);if(Date.now()>end)return reject(Error("artifact load timeout"));setTimeout(poll,50)};poll()})', True)
  107. for width, height, name in [(1440, 1000, 'desktop'), (390, 844, 'mobile'), (360, 800, 'narrow')]:
  108. call('Emulation.setDeviceMetricsOverride', {'width': width, 'height': height, 'deviceScaleFactor': 1, 'mobile': width < 500})
  109. for key in [*expected, 'atlas']:
  110. ev('document.querySelector(' + json.dumps('[data-view="' + key + '"]') + ').click()')
  111. settle()
  112. result = ev('''(()=>{const panel=[...document.querySelectorAll('.panel')].find(p=>!p.hidden);return {width:innerWidth,scroll:document.documentElement.scrollWidth,panel:panel.id,visiblePanels:document.querySelectorAll('.panel:not([hidden])').length,minFont:Math.min(...[...panel.querySelectorAll('p,td,th')].map(e=>parseFloat(getComputedStyle(e).fontSize))),tables:panel.querySelectorAll('table').length};})()''')
  113. assert result['scroll'] <= width and result['visiblePanels'] == 1 and result['minFont'] >= 15, result
  114. assert result['panel'] == 'panel-' + key
  115. if key == 'reply':
  116. assert result['tables'] == 2
  117. assert ev('document.querySelector("#panel-reply").innerText.includes("白象火鸡类 61 条对康师傅 31 条")')
  118. if key == 'supplement':
  119. assert ev('document.querySelector("#panel-supplement").innerText.includes("口味酱体矩阵")')
  120. assert ev('document.querySelector("#panel-supplement").innerText.includes("不用洗碗")')
  121. assert not ev('document.querySelector("#panel-supplement").innerText.includes("ownerName")')
  122. if key == 'reviewed':
  123. assert result['tables'] == 5
  124. assert ev('document.querySelector("#panel-reviewed").innerText.includes("去重后只有 1 条")')
  125. assert not ev('document.querySelector("#panel-reviewed").innerText.includes("VALIDATION_FAILED")')
  126. if key == 'comparison':
  127. assert result['tables'] == 7
  128. assert ev('document.querySelector("#panel-comparison").innerText.includes("三养火鸡 140g")')
  129. assert not ev('document.querySelector("#panel-comparison").innerText.includes("owner 用户8108")')
  130. if key == 'specifications':
  131. assert result['tables'] == 1
  132. assert ev('document.querySelector("#panel-specifications").innerText.includes("棕榈油")')
  133. assert ev('document.querySelector("#panel-specifications").innerText.includes("四款全部 0 条")')
  134. assert not ev('document.querySelector("#panel-specifications").innerText.includes("HTTP 200")')
  135. if key == 'consumer':
  136. assert result['tables'] == 2
  137. assert ev('document.querySelector("#panel-consumer").innerText.includes("愿意接受的代价")')
  138. assert ev('document.querySelector("#panel-consumer").innerText.includes("四款的同款评价:全部 0 条")')
  139. assert not ev('document.querySelector("#panel-consumer").innerText.includes("HTTP 200")')
  140. if key == 'checklist':
  141. assert result['tables'] == 1
  142. assert ev('document.querySelector("#panel-checklist").innerText.includes("他最后还是买了要煮的袋装面")')
  143. assert ev('document.querySelector("#panel-checklist").innerText.includes("访谈提纲(草稿·未执行)")')
  144. assert not ev('document.querySelector("#panel-checklist").innerText.includes("HTTP 200")')
  145. if key == 'atlas':
  146. result['artifacts'] = []
  147. for version, tables, cards in [('checklist', 2, 10), ('consumer', 3, 3), ('four-products', 3, 2), ('products', 5, 8), ('v2', 4, 4), ('v1', 2, 4)]:
  148. ev('document.querySelector(' + json.dumps('[data-artifact="' + version + '"]') + ').click()')
  149. settle()
  150. atlas_check = ev('''(()=>{const d=document.querySelector('iframe:not([hidden])').contentDocument,w=d.defaultView;return {width:w.innerWidth,scroll:d.documentElement.scrollWidth,cards:d.querySelectorAll(d.querySelector('.qs-item')?'.q':'.card,.hyp,.ex').length,minFont:Math.min(...[...d.querySelectorAll('p,td,th,li,.row,.val,.src')].map(e=>parseFloat(w.getComputedStyle(e).fontSize))),tables:d.querySelectorAll('.table-scroll,.tw,.tscroll,.sc').length,sourceLinks:d.querySelectorAll('a[href]').length,images:d.querySelectorAll('img').length};})()''')
  151. assert atlas_check['scroll'] <= atlas_check['width'] and atlas_check['cards'] == cards and atlas_check['minFont'] >= 15 and atlas_check['tables'] == tables, atlas_check
  152. assert ev('document.querySelectorAll("iframe:not([hidden])").length') == 1
  153. if version == 'checklist':
  154. assert atlas_check['sourceLinks'] == 37
  155. assert ev('(()=>{const f=document.querySelector("iframe:not([hidden])");return f.sandbox.contains("allow-popups")&&f.sandbox.contains("allow-popups-to-escape-sandbox")&&[...f.contentDocument.querySelectorAll("a[href]")].every(a=>a.target==="_blank"&&a.href.startsWith("https://"))})()')
  156. result['artifacts'].append(atlas_check | {'version': version})
  157. ev('selectArtifact("checklist")')
  158. checks.append(result | {'viewport': name, 'view': key})
  159. if name in ('desktop', 'mobile') and key in ('checklist', 'atlas'):
  160. ev('scrollTo(0,0)')
  161. settle()
  162. if key == 'atlas':
  163. ev('document.querySelector("iframe:not([hidden])").scrollIntoView({block:"start"})')
  164. settle()
  165. screenshot = call('Page.captureScreenshot', {'format': 'png', 'captureBeyondViewport': False})
  166. (OUT / (name + '-' + key + '.png')).write_bytes(base64.b64decode(screenshot['data']))
  167. if key == 'atlas':
  168. ev('(()=>{const d=document.querySelector("iframe:not([hidden])").contentDocument;d.defaultView.scrollTo(0,d.querySelector(".q").offsetTop)})()')
  169. settle()
  170. screenshot = call('Page.captureScreenshot', {'format': 'png', 'captureBeyondViewport': False})
  171. (OUT / (name + '-artifact-quotes.png')).write_bytes(base64.b64decode(screenshot['data']))
  172. # Keyboard navigation reaches the business dialogue and the actual artifact.
  173. ev('selectView("prompt");document.querySelector("#tab-prompt").focus();document.querySelector("#tab-prompt").dispatchEvent(new KeyboardEvent("keydown",{key:"End",bubbles:true}))')
  174. assert ev('document.activeElement.id') == 'tab-atlas'
  175. assert ev('document.querySelector("#panel-atlas").hidden') is False
  176. assert not errors, errors
  177. external = [url for url in requests if not url.startswith(base + '/') and not url.startswith('data:')]
  178. assert not external, external
  179. receipt = {'ok': True, 'scope': 'local HTML display; not research validation or production board',
  180. 'browser': 'Edge headless', 'renderChecks': len(checks), 'checks': checks,
  181. 'presentationTextsMatch': True, 'rawArchiveKeptSeparately': True,
  182. 'courseAndOperationalTracesRemoved': True, 'viewports': [1440, 390, 360], 'pageHorizontalOverflow': False,
  183. 'runtimeErrors': errors, 'externalPageRequests': external, 'screenshotsManuallyReviewed': False,
  184. 'originalHashesPreserved': True, 'keyboardNavigationPassed': True,
  185. 'receivedArtifactEmbedded': True, 'pendingEighthPromptNotPresentedAsDialogue': True,
  186. 'sourceAnchorsRetained': 37, 'explicitSourcePopupsPermitted': True,
  187. 'externalSourceAccessibilityVerifiedByBrowser': False,
  188. 'latestReplyComplete': True, 'receivedArtifacts': 6}
  189. (WORK / 'browser-checks.json').write_text(json.dumps(receipt, ensure_ascii=False, indent=2) + '\n', encoding='utf-8')
  190. print(json.dumps({k: v for k, v in receipt.items() if k != 'checks'}, ensure_ascii=False))
  191. finally:
  192. if ws:
  193. ws.close()
  194. proc.terminate()
  195. proc.wait(timeout=10)
  196. server.shutdown()
  197. server.server_close()