Files
progcode/docs/design-system/implementation-v3/candidates/fb6161223ba8/analytics-contract.js
T
huncode 12562baeb5
Build and deploy / deploy (push) Successful in 2m39s
Implement ProgCode identity and real shared reading UI kit
2026-10-27 15:19:49 +03:00

38 lines
7.8 KiB
JavaScript

async page => {
const context=page.context();const calls=[],external=[],violations=[],errors=[];
const sourceIdentity=await(await context.request.get('http://127.0.0.1:64268/build-identity.json')).json();if(sourceIdentity.sourceHash!=='fb6161223ba8391ed209678d65f509f346a08143ef89a0545bf27d1f72fa0fe9')throw Error('Candidate source mismatch');
await context.route('**/*',async route=>{const url=route.request().url();if(['https://progcode.ru/','https://www.progcode.ru/','http://progcode.ru/','https://progcode.ru.evil.test/'].some(origin=>url.startsWith(origin))){const r=await context.request.get(url.replace(/^https?:\/\/[^/]+/,'http://127.0.0.1:64268'));await route.fulfill({response:r});}else if(url==='https://mc.yandex.ru/metrika/tag.js'){external.push({url,action:'stub SDK'});await route.fulfill({contentType:'application/javascript',body:'const queued=window.ym.a||[];window.__ymCalls=[];window.ym=function(){window.__ymCalls.push(Array.from(arguments));};for(const args of queued)window.ym(...args);'});}else if(url.startsWith('http://127.0.0.1:64268/')) await route.continue();else{external.push({url,action:'aborted'});await route.abort();}});
await context.addInitScript(()=>document.addEventListener('securitypolicyviolation',e=>(window.__cspViolations??=[]).push(e.violatedDirective)));
page.on('pageerror',e=>errors.push(e.message));
const read=()=>page.evaluate(()=>window.__ymCalls||[]);
const count=(list,event)=>list.filter(x=>x[1]==='reachGoal'&&x[2]===event).length;
const check=(condition,message)=>{if(!condition)throw Error(message);};
await page.clock.install();await page.goto('http://127.0.0.1:64268/');await page.waitForTimeout(700);
check(await page.evaluate(()=>!window.ym&&!document.querySelector('script[src*="mc.yandex"]')),'localhost loaded counter');
calls.push({case:'localhost guard',passed:true});
for(const host of ['http://progcode.ru/','https://progcode.ru.evil.test/']){await page.goto(host);await page.waitForTimeout(300);check(await page.evaluate(()=>!window.ym),'guard failure '+host);}
await page.goto('https://www.progcode.ru/');await page.waitForFunction(()=>window.__ymCalls?.length>=2);calls.push({case:'HTTP and hostname suffix denied; www allowed',passed:true});
for(const path of ['/about/','/контакты/','/resources/','/tools/','/privacy/','/sources/','/ui-kit/','/accessibility/']){await page.goto('https://progcode.ru'+path);await page.waitForFunction(()=>window.__ymCalls?.length>=2);await page.evaluate(()=>scrollTo({top:document.body.scrollHeight,behavior:'instant'}));await page.clock.fastForward(61000);check(!count(await read(),'engaged_read'),'nonarticle read '+path);check(!count(await read(),'content_problem'),'inapplicable content problem '+path);}
calls.push({case:'61s at bottom on eight nonarticle pages including all new service/demo pages: no article event or inapplicable diagnostic',passed:true});
await page.goto('https://progcode.ru/');await page.waitForFunction(()=>window.__ymCalls?.length>=2);
let list=await read();check(list.filter(x=>x[1]==='init').length===1,'duplicate init');check(list.filter(x=>x[1]==='hit').length===1,'initial hit');check(!count(list,'engaged_read')&&!count(list,'search_zero_results'),'initial goals');
await page.locator('#search').fill('private@example.test');await page.waitForTimeout(650);list=await read();check(count(list,'search_zero_results')===1,'zero result event missing');check(!JSON.stringify(list).includes('private@example.test'),'query leaked');
await page.locator('#search').fill('мусорщика');await page.locator('.journal-list a').first().click();await page.waitForSelector('.article-content');await page.waitForTimeout(300);list=await read();check(count(list,'search_used')===1,'selection search event missing');check(list.filter(x=>x[1]==='hit').length===2,'SPA hit missing/duplicate');check(list.filter(x=>x[1]==='init').length===1,'SPA reinit');
calls.push({case:'initial / search privacy / client navigation',passed:true,calls:list});
await page.bringToFront();await page.evaluate(()=>scrollTo({top:0,behavior:'instant'}));await page.clock.fastForward(61000);check(!count(await read(),'engaged_read'),'read without depth');
await page.evaluate(()=>document.querySelector('.article-content').scrollIntoView({block:'end',behavior:'instant'}));await page.clock.fastForward(1000);check(count(await read(),'engaged_read')===1,'qualified read missing');await page.clock.fastForward(61000);check(count(await read(),'engaged_read')===1,'read repeated');
calls.push({case:'61 seconds without depth; depth then once',passed:true});
const related=page.locator('.related a').first();await related.waitFor({timeout:5000});await related.click();await page.waitForTimeout(300);check(count(await read(),'related_content_click')===1,'related event missing');
await page.evaluate(()=>document.querySelector('.article-content').scrollIntoView({block:'end',behavior:'instant'}));await page.clock.fastForward(30000);
await page.evaluate(()=>{Object.defineProperty(document,'hasFocus',{value:()=>false,configurable:true});Object.defineProperty(document,'visibilityState',{value:'hidden',configurable:true});dispatchEvent(new Event('blur'));document.dispatchEvent(new Event('visibilitychange'));});await page.clock.fastForward(61000);check(count(await read(),'engaged_read')===1,'unfocused time counted');await page.evaluate(()=>{delete document.hasFocus;delete document.visibilityState;dispatchEvent(new Event('focus'));document.dispatchEvent(new Event('visibilitychange'));});await page.clock.fastForward(28000);check(count(await read(),'engaged_read')===1,'focus resumed too early');await page.clock.fastForward(3000);check(count(await read(),'engaged_read')===2,'focused total not counted');
calls.push({case:'related navigation and excluded 61s simulated hidden/unfocused state',passed:true});
const beforeHash=await read();await page.evaluate(()=>{location.hash='main';});await page.clock.fastForward(1000);check((await read()).filter(x=>x[1]==='hit').length===beforeHash.filter(x=>x[1]==='hit').length,'hash fabricated hit');
await page.evaluate(()=>{location.hash='nonexistent-private-fragment';});await page.clock.fastForward(1000);check((await read()).some(x=>x[2]==='content_problem'&&x[3]?.reason==='fragment_missing'),'missing fragment missing');
const img=page.locator('.article-content img').first();if(await img.count()){await img.evaluate(el=>{el.src='/broken-test-image.jpg';el.loading='eager';});await page.waitForTimeout(300);check((await read()).some(x=>x[2]==='content_problem'&&x[3]?.reason==='image_failed'),'failed image event missing');}
calls.push({case:'fragment / failed real content image',passed:true,imagePresent:!!(await img.count()),calls:await read()});
const beforeNav=await read();await page.locator('.masthead a[href="/resources/"]').click();await page.waitForTimeout(300);const afterNav=await read();check(afterNav.filter(x=>x[1]==='hit').length===beforeNav.filter(x=>x[1]==='hit').length+1,'ordinary navigation hit');check(afterNav.filter(x=>x[1]==='reachGoal').length===beforeNav.filter(x=>x[1]==='reachGoal').length,'ordinary navigation goal');check(!count(afterNav,'download_click'),'download fabricated');calls.push({case:'existing hash no extra hit; ordinary navigation no recommendation/download goal',passed:true});
await page.goto('https://progcode.ru/unknown-private-path/');await page.waitForFunction(()=>window.__ymCalls?.some(x=>x[2]==='content_problem'&&x[3]?.reason==='not_found'));list=await read();check(!JSON.stringify(list).includes('unknown-private-path'),'unknown URL leaked in calls');check(!list.some(x=>x[1]==='notBounce'),'forced bounce');
calls.push({case:'confirmed 404 and missing fragment; unavailable downloads stay silent',passed:true,calls:list});
return {sourceHash:sourceIdentity.sourceHash,localOrigin:'http://127.0.0.1:64268',cases:calls,external,errors,cspViolations:await page.evaluate(()=>window.__cspViolations||[]),statisticsSent:0};
}