verify-showcase-browser.mjs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /** Local Edge acceptance. Non-local browser requests are intercepted, never sent. */
  2. import assert from 'node:assert/strict';
  3. import { createServer } from 'node:http';
  4. import { spawn } from 'node:child_process';
  5. import { readFile, stat, mkdir, writeFile } from 'node:fs/promises';
  6. import { existsSync, readFileSync } from 'node:fs';
  7. import path from 'node:path';
  8. import { fileURLToPath } from 'node:url';
  9. import { indexHtml } from './config.mjs';
  10. const root = fileURLToPath(new URL('../', import.meta.url));
  11. const run = path.join(root, 'work', `showcase-browser-${Date.now()}`);
  12. await mkdir(run, { recursive: true });
  13. const checks = [], errors = [], requests = [], forbidden = [], observations = [];
  14. const mime = { '.html': 'text/html', '.js': 'text/javascript', '.mjs': 'text/javascript', '.json': 'application/json', '.png': 'image/png', '.svg': 'image/svg+xml', '.woff2': 'font/woff2' };
  15. const mount = '/preview-test/';
  16. let seedUnavailable = false;
  17. const server = createServer(async (req, res) => {
  18. try {
  19. const url = new URL(req.url, 'http://localhost');
  20. const sub = url.pathname.startsWith(mount);
  21. const rel = decodeURIComponent(sub ? url.pathname.slice(mount.length) : url.pathname.slice(1));
  22. if (/^(?:api|parse)\//.test(rel)) { res.writeHead(451); res.end('No business proxy'); return; }
  23. if (seedUnavailable && rel === 'src/assets/showcase/preview-seed.json') { res.writeHead(503); res.end('sample unavailable'); return; }
  24. const file = path.resolve(root, rel || 'index.html');
  25. if (!file.startsWith(path.resolve(root) + path.sep)) { res.writeHead(403); res.end(); return; }
  26. let body;
  27. let ext = path.extname(file);
  28. if (ext === '.html' || !ext) { body = indexHtml(sub ? mount : '/', { dev: false }); ext = '.html'; }
  29. else { assert.ok((await stat(file)).isFile()); body = await readFile(file); }
  30. res.writeHead(200, { 'content-type': mime[ext] || 'application/octet-stream', 'cache-control': 'no-store' });
  31. res.end(body);
  32. } catch { res.writeHead(404); res.end('missing resource'); }
  33. });
  34. await new Promise((done) => server.listen(0, '127.0.0.1', done));
  35. const origin = `http://127.0.0.1:${server.address().port}`;
  36. const profile = path.join(run, 'profile');
  37. await mkdir(profile);
  38. const executable = process.env.CHROME_PATH || 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe';
  39. assert.ok(existsSync(executable), 'Edge/Chromium executable required');
  40. const browser = spawn(executable, ['--headless=new', '--disable-gpu', '--no-first-run', '--no-default-browser-check', '--disable-background-networking', '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank'], { windowsHide: true, stdio: 'ignore' });
  41. const pause = (ms) => new Promise((done) => setTimeout(done, ms));
  42. let socket;
  43. const check = (name, passed, detail) => { checks.push({ name, passed: Boolean(passed), ...(detail ? { detail } : {}) }); assert.ok(passed, name); };
  44. try {
  45. let port;
  46. for (let i = 0; i < 100; i++) {
  47. try { port = Number(readFileSync(path.join(profile, 'DevToolsActivePort'), 'utf8').split('\n')[0]); } catch {}
  48. if (port) break;
  49. await pause(100);
  50. }
  51. assert.ok(port);
  52. const tabs = await (await fetch(`http://127.0.0.1:${port}/json/list`)).json();
  53. socket = new WebSocket(tabs.find((x) => x.type === 'page').webSocketDebuggerUrl);
  54. await new Promise((done, reject) => { socket.addEventListener('open', done, { once: true }); socket.addEventListener('error', reject, { once: true }); });
  55. let seq = 0;
  56. const pending = new Map();
  57. const cdp = (method, params = {}) => new Promise((resolve, reject) => {
  58. const id = ++seq;
  59. const timer = setTimeout(() => { pending.delete(id); reject(new Error(`${method} timeout`)); }, 15000);
  60. pending.set(id, { resolve, reject, timer });
  61. socket.send(JSON.stringify({ id, method, params }));
  62. });
  63. const evaluate = async (expression) => {
  64. const value = await cdp('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true });
  65. if (value.exceptionDetails) throw new Error(value.exceptionDetails.exception?.description || value.exceptionDetails.text);
  66. return value.result.value;
  67. };
  68. const fixture = { result: { ok: true, data: { project: [], artifact: [], insight: [], source: [], session: [], opportunity: [], asOf: '2026-11-03T00:00:00.000Z', nextCursor: null } } };
  69. fixture.result.data.artifact.push({ objectId: 'fixture-report', title: '原有报告回归', stageCode: 'S4', artifactType: 'competitor_report', report: { title: '原有报告回归', chapters: [{ id: 'scope', title: '核对原文', sections: [{ kind: 'para', text: '公开报告阅读回归正文。' }] }], evidence: {} } });
  70. for (const type of ['category_report', 'voc_report', 'pain_list', 'concept']) fixture.result.data.artifact.push({ objectId: 'fixture-' + type, artifactType: type, title: '中文标题完整显示与多行排版验证', stageCode: 'S1', rawText: '本机回放排版测试资料。', createdAt: '2026-11-03T00:00:00.000Z' });
  71. fixture.result.data.opportunity.push({ objectId: 'fixture-opportunity', title: '原有机会卡回归', status: 'candidate', linkedArtifacts: ['fixture-report'], scores: [], updatedAt: '2026-11-03T00:00:00.000Z' });
  72. socket.addEventListener('message', ({ data }) => {
  73. const m = JSON.parse(data);
  74. if (m.id && pending.has(m.id)) {
  75. const p = pending.get(m.id); clearTimeout(p.timer); pending.delete(m.id);
  76. if (m.error) p.reject(new Error(m.error.message)); else p.resolve(m.result);
  77. }
  78. if (m.method === 'Runtime.exceptionThrown') errors.push(m.params.exceptionDetails.exception?.description || m.params.exceptionDetails.text);
  79. if (m.method === 'Network.requestWillBeSent') requests.push({ url: m.params.request.url, method: m.params.request.method, body: m.params.request.postData || '' });
  80. if (m.method === 'Fetch.requestPaused') {
  81. const { requestId, request } = m.params;
  82. if (request.url.startsWith(origin + '/')) cdp('Fetch.continueRequest', { requestId }).catch((e) => errors.push(e.message));
  83. else {
  84. forbidden.push({ url: request.url, method: request.method, body: request.postData || '' });
  85. const isPublicRead = request.postData?.includes('ksfGetPublicBoard');
  86. const isPreflight = request.method === 'OPTIONS';
  87. const action = request.postData ? JSON.parse(request.postData) : {};
  88. const isPublicAction = ['/ksfSetPublicOpportunityStatus', '/ksfRatePublicOpportunity'].includes(action.path);
  89. if (isPublicAction) {
  90. const opp = fixture.result.data.opportunity[0], params = action.params;
  91. opp.updatedAt = new Date().toISOString();
  92. if (action.path.includes('SetPublic')) opp.status = params.status;
  93. else opp.scores = [{ user: params.by, score: params.score, at: opp.updatedAt }];
  94. }
  95. cdp('Fetch.fulfillRequest', { requestId, responseCode: isPreflight ? 204 : isPublicRead || isPublicAction ? 200 : 451,
  96. responseHeaders: [{ name: 'Content-Type', value: 'application/json' }, { name: 'Access-Control-Allow-Origin', value: '*' }, { name: 'Access-Control-Allow-Headers', value: 'content-type,x-parse-application-id' }, { name: 'Access-Control-Allow-Methods', value: 'GET,POST,OPTIONS' }],
  97. body: Buffer.from(JSON.stringify(isPublicRead ? fixture : isPublicAction ? { result: { ok: true, data: { opportunity: fixture.result.data.opportunity[0] } } } : { error: 'No external traffic in acceptance' })).toString('base64'),
  98. }).catch((e) => errors.push(e.message));
  99. }
  100. }
  101. });
  102. await cdp('Page.enable'); await cdp('Runtime.enable'); await cdp('Network.enable');
  103. await cdp('Fetch.enable', { patterns: [{ urlPattern: '*' }] });
  104. await cdp('Page.addScriptToEvaluateOnNewDocument', { source: `const originalInterval=window.setInterval;window.setInterval=(fn,ms,...args)=>originalInterval(fn,ms===30000?100:ms,...args);` });
  105. const host = `document.querySelector('kf-app')?.shadowRoot?.querySelector('view-showcase')`;
  106. const shadow = `${host}?.shadowRoot`;
  107. const ready = async () => {
  108. for (let i = 0; i < 100; i++) { if (await evaluate(`Boolean(${host}?._model && !${host}?._busy)`)) return; await pause(100); }
  109. throw new Error('showcase did not load');
  110. };
  111. const go = async (route) => { await cdp('Page.navigate', { url: origin + route }); await ready(); await pause(150); };
  112. const shot = async (name, full = false) => {
  113. const params = { format: 'png', captureBeyondViewport: full };
  114. if (full) {
  115. const metrics = await cdp('Page.getLayoutMetrics');
  116. params.clip = { x: 0, y: 0, width: metrics.cssContentSize.width, height: metrics.cssContentSize.height, scale: 1 };
  117. }
  118. const result = await cdp('Page.captureScreenshot', params);
  119. await writeFile(path.join(run, name + '.png'), Buffer.from(result.data, 'base64'));
  120. };
  121. await cdp('Emulation.setDeviceMetricsOverride', { width: 1440, height: 1000, deviceScaleFactor: 1, mobile: false });
  122. for (const key of ['overview', 'leadership', 'research', 'collaboration', 'validation', 'classroom']) {
  123. const route = key === 'overview' ? '/showcase' : '/showcase/' + key;
  124. await go(route);
  125. const info = await evaluate(`(()=>{const el=${host},root=el.shadowRoot;return {system:el.system,total:el._model.total,title:root.querySelector('h1')?.textContent,counts:[...root.querySelectorAll('.count-grid dd')].map(x=>Number(x.textContent)),width:document.documentElement.scrollWidth,viewport:innerWidth,images:[...root.images||root.querySelectorAll('img')].map(x=>({loaded:x.complete&&x.naturalWidth>0,src:x.src})),pending:root.querySelector('.pending')?.textContent};})()`);
  126. check(`${key}: correct route, real totals and loaded image`, info.system === key && info.total === 334 && info.counts.length === 12 && info.images.every((x) => x.loaded));
  127. check(`${key}: no page horizontal overflow`, info.width <= info.viewport + 1);
  128. observations.push(info);
  129. await shot(`desktop-${key}`, key === 'overview' || key === 'classroom');
  130. await cdp('Page.reload', { ignoreCache: true }); await ready();
  131. check(`${key}: survives direct refresh`, await evaluate(`${host}.system===${JSON.stringify(key)}`));
  132. }
  133. await go('/showcase');
  134. await evaluate(`${shadow}.querySelector('a[href$="/showcase/research"]').click()`); await pause(200);
  135. check('Shadow DOM links navigate to the correct page', await evaluate(`location.pathname==='/showcase/research'&&${host}.system==='research'`));
  136. await evaluate(`${shadow}.querySelector('.image-button').click()`); await pause(100);
  137. check('preview opens in a real modal dialog', await evaluate(`${shadow}.querySelector('dialog').open`));
  138. await cdp('Input.dispatchKeyEvent', { type: 'keyDown', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27 });
  139. await cdp('Input.dispatchKeyEvent', { type: 'keyUp', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27 }); await pause(100);
  140. check('Escape closes preview', await evaluate(`!${shadow}.querySelector('dialog').open`));
  141. await go('/showcase?case=P01');
  142. for (let i = 0; i < 7; i++) {
  143. const observation = await evaluate(`(()=>{const el=${host};return {step:el._step,text:el.shadowRoot.querySelector('.story-body').textContent};})()`);
  144. check(`P01 step ${i + 1} displays its real record`, observation.step === i && observation.text.trim().length > 10);
  145. if (i < 6) await evaluate(`${shadow}.querySelector('.story-controls .primary').click()`);
  146. await pause(60);
  147. }
  148. check('P01 retains six questions after classroom completion', await evaluate(`${shadow}.querySelectorAll('.question-list li').length===6`));
  149. await go('/showcase/validation?case=P03');
  150. await evaluate(`${shadow}.querySelectorAll('.story-steps button')[1].click()`); await pause(80);
  151. check('P03 shows preparation conflict', await evaluate(`${shadow}.querySelector('.story-body').textContent.includes('无灶具')`));
  152. await evaluate(`${shadow}.querySelectorAll('.story-steps button')[2].click()`); await pause(80);
  153. check('P03 questions retain owners', await evaluate(`${shadow}.querySelectorAll('.question-list li').length===6&&${shadow}.querySelector('.question-list').textContent.includes('演示学员')`));
  154. await go('/showcase/validation?case=P10');
  155. await evaluate(`${shadow}.querySelectorAll('.story-steps button')[3].click()`); await pause(80);
  156. check('P10 does not fabricate missing validation tasks', await evaluate(`${shadow}.querySelector('.empty-inline').textContent.includes('尚无验证任务')`));
  157. for (const key of ['overview', 'leadership', 'research', 'collaboration', 'validation', 'classroom']) {
  158. await cdp('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true });
  159. await go(key === 'overview' ? '/showcase' : '/showcase/' + key);
  160. check(`${key}: mobile has no horizontal page overflow`, await evaluate('document.documentElement.scrollWidth<=innerWidth+1'));
  161. if (key === 'overview' || key === 'research') await shot('mobile-' + key, true);
  162. }
  163. await go(mount + 'showcase/collaboration?case=P01');
  164. check('nested deployment preserves route and image URLs', await evaluate(`${host}.system==='collaboration'&&${shadow}.querySelector('img').src.includes('/preview-test/src/assets/showcase/')`));
  165. await evaluate(`${shadow}.querySelector('a[href$="/showcase/validation"]').click()`); await pause(150);
  166. check('nested navigation preserves base path', await evaluate(`location.pathname==='/preview-test/showcase/validation'`));
  167. const before = forbidden.length;
  168. await evaluate(`(()=>{const app=document.querySelector('kf-app');for(const type of ['refresh','status-change','score-submit','artifact-open'])app.dispatchEvent(new CustomEvent(type,{detail:{id:'DEMO-O01',status:'approved',by:'模拟'}}));})()`);
  169. await pause(500);
  170. check('sample shell ignores global live business events', forbidden.length === before);
  171. check('all introduction interactions made zero external business requests', forbidden.length === 0);
  172. const sampleHost = `document.querySelector('kf-app').shadowRoot.querySelector('sample-shell')`;
  173. const viewHost = `${sampleHost}.shadowRoot.querySelector('main').firstElementChild`;
  174. const viewRoot = `${viewHost}.shadowRoot`;
  175. const detailHost = `${sampleHost}.shadowRoot.querySelector('sample-detail')`;
  176. const detailRoot = `${detailHost}.shadowRoot`;
  177. const sampleReady = async () => {
  178. for (let i = 0; i < 160; i++) { if (await evaluate(`(()=>{const shell=document.querySelector('kf-app')?.shadowRoot?.querySelector('sample-shell');return Boolean(shell?._model && shell.shadowRoot?.querySelector('main')?.firstElementChild?.shadowRoot?.querySelector('h1'));})()`)) { await pause(100); return; } await pause(100); }
  179. throw new Error('sample did not load');
  180. };
  181. const sampleGo = async (route) => { await cdp('Page.navigate', { url: origin + route }); await sampleReady(); };
  182. const clickRecord = async (id) => { await evaluate(`${viewRoot}.querySelector('button[data-record="${id}"]').click()`); await pause(150); };
  183. const submit = async (action, fields) => {
  184. await evaluate(`(()=>{const form=${detailRoot}.querySelector('[data-action="${action}"]').closest('form');for(const [key,value] of Object.entries(${JSON.stringify(fields)}))form.elements.namedItem(key).value=value;form.requestSubmit();})()`);
  185. await pause(200);
  186. };
  187. await cdp('Emulation.setDeviceMetricsOverride', { width: 1440, height: 1000, deviceScaleFactor: 1, mobile: false });
  188. for (const name of ['cockpit', 'intel', 'coop', 'review', 'class']) {
  189. await sampleGo('/' + name);
  190. check(`${name}: independent sample shell and actual extension`, await evaluate(`${sampleHost}._model.baselineEntityCount===334&&${sampleHost}._model.extension.voices.length===12000`));
  191. check(`${name}: no page horizontal overflow`, await evaluate('document.documentElement.scrollWidth<=innerWidth+1'));
  192. await shot('demo-' + name);
  193. await cdp('Page.reload', { ignoreCache: true }); await sampleReady();
  194. check(`${name}: refresh preserves route`, await evaluate(`${sampleHost}.view===${JSON.stringify(name)}`));
  195. }
  196. await sampleGo('/cockpit?group=DEMO-G2');
  197. check('group filter scopes both project and report counts', await evaluate(`${viewHost}.filtered.projects.length===2&&${viewHost}.filtered.reports.every(r=>r.groupId==='DEMO-G2')`));
  198. await evaluate(`${viewRoot}.querySelector('[data-filter="group"]').value='';${viewRoot}.querySelector('[data-filter="group"]').dispatchEvent(new Event('change',{bubbles:true}));`); await pause(100);
  199. await evaluate(`${viewRoot}.querySelector('[data-filter="member"]').value='DEMO-M31';${viewRoot}.querySelector('[data-filter="member"]').dispatchEvent(new Event('change',{bubbles:true}));`); await pause(150);
  200. check('member selection filters owned and collaborating projects', await evaluate(`${viewHost}.filtered.projects.length<12&&${viewHost}.filtered.projects.some(p=>p.id==='DEMO-P10')`));
  201. await sampleGo('/intel');
  202. await evaluate(`${viewRoot}.querySelector('[data-filter="search"]').value='完全无此关键词XYZ';${viewRoot}.querySelector('[data-filter="search"]').dispatchEvent(new Event('change',{bubbles:true}));`); await pause(150);
  203. check('search has a real zero-result state', await evaluate(`${viewHost}.filtered.voices.length===0&&${viewRoot}.textContent.includes('没有符合条件')`));
  204. await cdp('Page.reload', { ignoreCache: true }); await sampleReady();
  205. check('search is preserved in URL after refresh', await evaluate(`${viewHost}.filters.search==='完全无此关键词XYZ'`));
  206. await sampleGo('/intel');
  207. await evaluate(`${viewRoot}.querySelector('[data-page="voicePage-next"]').click()`); await pause(150);
  208. check('pagination changes the visible voice records', await evaluate(`location.search.includes('voicePage=2')&&${viewRoot}.querySelector('[data-record^="SIM-VOICE"]').dataset.record==='SIM-VOICE-1-0011'`));
  209. await clickRecord('SIM-OBS-0001');
  210. check('product detail shows four distinct price snapshots', await evaluate(`${detailRoot}.querySelectorAll('tbody tr').length===4`));
  211. await cdp('Input.dispatchKeyEvent', { type: 'keyDown', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27 });
  212. await cdp('Input.dispatchKeyEvent', { type: 'keyUp', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27 }); await pause(150);
  213. check('Escape closes record detail and removes deep-link query', await evaluate(`!${detailRoot}.querySelector('dialog').open&&!location.search.includes('record=')`));
  214. await sampleGo('/coop');
  215. await evaluate(`localStorage.setItem('unrelated-demo-sentinel','preserve')`);
  216. await clickRecord('DEMO-O01');
  217. check('opportunity opens local dialog', await evaluate(`${detailRoot}.querySelector('dialog').open&&${detailHost}.recordId==='DEMO-O01'`));
  218. await submit('status', { user: '模拟甲', status: 'candidate' });
  219. check('status action mutates only sample state', await evaluate(`${sampleHost}._model.opportunitiesById['DEMO-O01'].status==='candidate'`));
  220. await submit('score', { user: '模拟甲', score: '80', comment: '先核制备条件' });
  221. await submit('score', { user: '模拟乙', score: '60' });
  222. await submit('score', { user: '模拟甲', score: '100' });
  223. check('two voters and same-name replacement produce correct average', await evaluate(`${sampleHost}._model.opportunitiesById['DEMO-O01'].stats.avg===80&&${sampleHost}._model.opportunitiesById['DEMO-O01'].stats.n===2`));
  224. await cdp('Page.reload', { ignoreCache: true }); await sampleReady();
  225. check('status, scores and open record survive refresh', await evaluate(`${sampleHost}._model.opportunitiesById['DEMO-O01'].status==='candidate'&&${sampleHost}._model.opportunitiesById['DEMO-O01'].stats.avg===80&&${detailRoot}.querySelector('dialog').open`));
  226. await evaluate(`${detailRoot}.querySelector('[data-record="DEMO-P01"]').click()`); await pause(150);
  227. await submit('comment', { user: '模拟甲', body: '请补复水与沥水条件。' });
  228. check('project comment is retained with its project', await evaluate(`${sampleHost}._model.comments['DEMO-P01'].some(c=>c.body==='请补复水与沥水条件。')`));
  229. await evaluate(`${detailRoot}.querySelector('[data-record="DEMO-R-P01-S3"]').click()`); await pause(150);
  230. check('report detail opens actual stage record and pending questions', await evaluate(`${detailHost}.recordId==='DEMO-R-P01-S3'&&${detailRoot}.textContent.includes('依据与适用边界')&&${detailRoot}.querySelectorAll('[data-record^="DEMO-Q01"]').length===6`));
  231. await evaluate(`${detailRoot}.querySelector('[data-record="DEMO-S01"]').click()`); await pause(150);
  232. check('report source opens original sample text', await evaluate(`${detailRoot}.querySelector('.raw').textContent.includes('中午只有热水')`));
  233. await sampleGo('/coop?detail=source&record=UNKNOWN');
  234. check('unknown sample detail shows local error with no production lookup', await evaluate(`${detailRoot}.textContent.includes('不会向线上接口补查')`));
  235. await sampleGo('/review?stage=S3');
  236. check('S3 does not fabricate later validation tasks', await evaluate(`${viewHost}.filtered.validationTasks.length===0&&${viewHost}.filtered.questions.length===12`));
  237. await sampleGo('/class'); await clickRecord('DEMO-A-DEMO-G1-2');
  238. check('class assignment resolves its own report references', await evaluate(`${detailRoot}.querySelectorAll('[data-record^="DEMO-R-"]').length===4`));
  239. for (const [key, count] of [['P01', 7], ['P03', 4], ['P10', 4]]) {
  240. await sampleGo(`/review?case=${key}&step=${count - 1}`);
  241. check(`${key}: clickable guided path resolves to local project records`, await evaluate(`${viewRoot}.querySelectorAll('.steps button').length===${count}&&${viewRoot}.querySelectorAll('.story-records [data-record]').length>0`));
  242. }
  243. await sampleGo('/coop');
  244. await evaluate(`${sampleHost}.shadowRoot.querySelector('.sample-strip button').click()`); await pause(200);
  245. check('reset restores statuses and removes local votes while preserving unrelated storage', await evaluate(`${sampleHost}._model.opportunitiesById['DEMO-O01'].status==='approved'&&${sampleHost}._model.opportunitiesById['DEMO-O01'].stats===null&&localStorage.getItem('unrelated-demo-sentinel')==='preserve'`));
  246. for (const name of ['cockpit', 'intel', 'coop', 'review', 'class']) {
  247. await cdp('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true });
  248. await sampleGo('/' + name);
  249. check(`${name}: mobile layout permits table scrolling without page overflow`, await evaluate('document.documentElement.scrollWidth<=innerWidth+1'));
  250. if (name === 'coop') await shot('demo-mobile-coop');
  251. }
  252. await sampleGo(mount + 'coop?detail=opportunity&record=DEMO-O01');
  253. check('nested demo detail loads without production routing', await evaluate(`${detailHost}.recordId==='DEMO-O01'&&${detailRoot}.querySelector('dialog').open`));
  254. await evaluate(`${detailHost}.close()`); await pause(100);
  255. await evaluate(`(()=>{const app=document.querySelector('kf-app');for(const type of ['refresh','status-change','score-submit'])app.dispatchEvent(new CustomEvent(type,{detail:{id:'DEMO-O01',status:'approved'}}));})()`); await pause(350);
  256. check('five views and local operations made zero external business requests', forbidden.length === 0);
  257. // Existing live route regression uses a local interception fixture, never production.
  258. await evaluate(`import(${JSON.stringify(origin + mount + 'src/utils/router.js')}).then(m=>m.navigate('/'))`); await pause(600);
  259. check('existing live route resumes loading', forbidden.some((r) => r.body.includes('ksfGetPublicBoard')));
  260. check('existing desktop and mobile navigation expose base-safe demo entry', await evaluate(`(()=>{const root=document.querySelector('kf-app').shadowRoot.querySelector('kf-nav').shadowRoot;return root.querySelector('a[data-path="/showcase"]').href.includes('/preview-test/showcase')&&root.querySelector('.showcase-entry').href.includes('/preview-test/showcase');})()`));
  261. const legacyNavigate = async (route) => { await evaluate(`import(${JSON.stringify(origin + mount + 'src/utils/router.js')}).then(m=>m.navigate(${JSON.stringify(route)}))`); await pause(450); };
  262. const measureTitles = async () => evaluate(`(()=>{
  263. const root=document.querySelector('kf-app').shadowRoot;
  264. const headings=[root.querySelector('kf-topbar').shadowRoot.querySelector('h1'),...root.querySelector('main').firstElementChild.shadowRoot.querySelectorAll('[data-type-card] h3,.head h2')];
  265. const canvas=document.createElement('canvas'),ctx=canvas.getContext('2d');
  266. return headings.map(el=>{const s=getComputedStyle(el),r=el.getBoundingClientRect();ctx.font=s.fontWeight+' '+s.fontSize+' '+s.fontFamily;const glyph=ctx.measureText(el.textContent);return {text:el.textContent,height:r.height,glyphHeight:glyph.actualBoundingBoxAscent+glyph.actualBoundingBoxDescent,trim:s.textBoxTrim,clipped:r.height+0.5<glyph.actualBoundingBoxAscent+glyph.actualBoundingBoxDescent};});
  267. })()`);
  268. for (const width of [2048, 1280, 1024, 768, 390]) {
  269. await cdp('Emulation.setDeviceMetricsOverride', { width, height: 1000, deviceScaleFactor: 1, mobile: width < 768 });
  270. await legacyNavigate('/reports');
  271. await evaluate('document.fonts.ready');
  272. const headings = await measureTitles(); observations.push({ reportWidth: width, headings });
  273. if (width === 2048 || width === 390) await shot('reports-typography-' + width);
  274. check(`reports ${width}px: Chinese heading boxes retain the complete glyph height`, headings.length === 6 && headings.every(x => !x.clipped), headings);
  275. check(`reports ${width}px: no page horizontal overflow`, await evaluate('document.documentElement.scrollWidth<=innerWidth+1'));
  276. await evaluate(`document.querySelector('kf-app').shadowRoot.querySelector('view-reports').shadowRoot.querySelector('[data-type-card="voc"]').click()`); await pause(250);
  277. const typeHeadings = await measureTitles();
  278. check(`report category ${width}px: heading and classification retain full glyphs`, typeHeadings.length === 2 && typeHeadings.every(x => !x.clipped), typeHeadings);
  279. check(`report category ${width}px: active tab is exposed correctly`, await evaluate(`document.querySelector('kf-app').shadowRoot.querySelector('view-report-type').shadowRoot.querySelector('.tab.cur').getAttribute('aria-current')==='page'`));
  280. }
  281. await cdp('Emulation.setDeviceMetricsOverride', { width: 1440, height: 1000, deviceScaleFactor: 1, mobile: false });
  282. await legacyNavigate('/pool/fixture-report');
  283. check('legacy wrong report link recovers to report route', await evaluate(`location.pathname==='/preview-test/report/fixture-report'&&Boolean(document.querySelector('kf-app').shadowRoot.querySelector('view-report'))`));
  284. await legacyNavigate('/pool/fixture-opportunity');
  285. const legacyDrawer = `document.querySelector('kf-app').shadowRoot.querySelector('kf-drawer').shadowRoot`;
  286. check('existing public opportunity drawer resolves its record', await evaluate(`${legacyDrawer}.textContent.includes('决策与状态')&&!${legacyDrawer}.textContent.includes('未找到这张机会卡')`));
  287. await evaluate(`${legacyDrawer}.querySelectorAll('.st-row button')[1].click()`); await pause(250);
  288. check('existing state button retains public function contract', forbidden.some((r) => r.body.includes('ksfSetPublicOpportunityStatus') && r.body.includes('fixture-opportunity')));
  289. await evaluate(`document.querySelector('kf-app').dispatchEvent(new CustomEvent('score-submit',{detail:{user:'回归访客',score:85,comment:'本机拦截回放'}}))`); await pause(250);
  290. check('existing rating retains public function contract', forbidden.some((r) => r.body.includes('ksfRatePublicOpportunity') && r.body.includes('fixture-opportunity')));
  291. await evaluate(`import(${JSON.stringify(origin + mount + 'src/utils/router.js')}).then(m=>m.navigate('/showcase'))`); await ready(); await pause(200);
  292. const afterLive = forbidden.length;
  293. await pause(500);
  294. check('returning to introduction stops accelerated live polling', forbidden.length === afterLive);
  295. seedUnavailable = true;
  296. await cdp('Page.navigate', { url: origin + '/showcase' });
  297. for (let i = 0; i < 60; i++) { if (await evaluate(`Boolean(${host}?._error)`)) break; await pause(100); }
  298. check('failed sample fetch shows error instead of live fallback', await evaluate(`${host}._error.includes('503')`));
  299. seedUnavailable = false;
  300. await evaluate(`${shadow}.querySelector('.loading button').click()`); await ready();
  301. check('retry recovers the local sample', await evaluate(`${host}._model.total===334`));
  302. check('no uncaught browser exceptions', errors.length === 0, errors);
  303. await writeFile(path.join(run, 'acceptance.json'), JSON.stringify({ ok: true, checks, observations, requests, interceptedExternalRequests: forbidden, errors, productionRequestsSent: 0, deployed: false }, null, 2));
  304. console.log(JSON.stringify({ ok: true, checks: checks.length, output: run, productionRequestsSent: 0 }));
  305. } catch (error) {
  306. await writeFile(path.join(run, 'acceptance.json'), JSON.stringify({ ok: false, error: error.stack, checks, observations, requests, interceptedExternalRequests: forbidden, errors, deployed: false }, null, 2));
  307. console.error(error);
  308. console.error(`Evidence: ${run}`);
  309. process.exitCode = 1;
  310. } finally {
  311. socket?.close(); browser.kill(); server.closeAllConnections(); server.close();
  312. }