|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { readFile } from 'node:fs/promises'; |
| 3 | +import { test } from 'node:test'; |
| 4 | + |
| 5 | +import { buildMauticPageview } from '../src/lib/shared-tracking-client.mjs'; |
| 6 | +import { docsSharedTrackingEnabled } from '../src/lib/docs-shared-tracking.mjs'; |
| 7 | + |
| 8 | +const root = new URL('../', import.meta.url); |
| 9 | +const componentUrl = new URL('src/components/RegistryTrackingPreferences.astro', root); |
| 10 | +const footerUrl = new URL('src/components/RegistryFooter.astro', root); |
| 11 | +const headUrl = new URL('src/components/RegistryHead.astro', root); |
| 12 | +const privacyUrl = new URL('src/content/docs/privacy.mdx', root); |
| 13 | + |
| 14 | +test('shared tracking is enabled only for canonical released documentation pages', () => { |
| 15 | + assert.equal( |
| 16 | + docsSharedTrackingEnabled({ isReleasedArchiveBuild: true, base: '/', path: '/' }), |
| 17 | + true, |
| 18 | + ); |
| 19 | + assert.equal( |
| 20 | + docsSharedTrackingEnabled({ |
| 21 | + isReleasedArchiveBuild: true, |
| 22 | + base: '/', |
| 23 | + path: '/products/base-registry/', |
| 24 | + }), |
| 25 | + true, |
| 26 | + ); |
| 27 | + assert.equal( |
| 28 | + docsSharedTrackingEnabled({ isReleasedArchiveBuild: false, base: '/dev/', path: '/' }), |
| 29 | + false, |
| 30 | + ); |
| 31 | + assert.equal( |
| 32 | + docsSharedTrackingEnabled({ |
| 33 | + isReleasedArchiveBuild: true, |
| 34 | + base: '/v/0.43.0/', |
| 35 | + path: '/', |
| 36 | + }), |
| 37 | + false, |
| 38 | + ); |
| 39 | + assert.equal( |
| 40 | + docsSharedTrackingEnabled({ isReleasedArchiveBuild: true, base: '/', path: '/404/' }), |
| 41 | + false, |
| 42 | + ); |
| 43 | + assert.equal( |
| 44 | + docsSharedTrackingEnabled({ isReleasedArchiveBuild: true, base: '/', path: '/404' }), |
| 45 | + false, |
| 46 | + ); |
| 47 | +}); |
| 48 | + |
| 49 | +test('page views use public canonical data and only reviewed external campaign labels', () => { |
| 50 | + assert.deepEqual( |
| 51 | + buildMauticPageview({ |
| 52 | + canonicalUrl: 'https://docs.registrystack.org/start/concepts/?private=yes#record', |
| 53 | + currentUrl: |
| 54 | + 'https://docs.registrystack.org/start/concepts/?utm_source=partner&utm_medium=referral&utm_campaign=registry.review&utm_content=guide&utm_term=records&search=person%40example.org', |
| 55 | + pageTitle: ' How the products fit together ', |
| 56 | + referrer: 'https://partner.example.org/report/?person=someone#private', |
| 57 | + }), |
| 58 | + { |
| 59 | + page_url: 'https://docs.registrystack.org/start/concepts/', |
| 60 | + page_title: 'How the products fit together', |
| 61 | + page_referrer: 'https://partner.example.org', |
| 62 | + utm_source: 'partner', |
| 63 | + utm_medium: 'referral', |
| 64 | + utm_campaign: 'registry.review', |
| 65 | + utm_content: 'guide', |
| 66 | + utm_term: 'records', |
| 67 | + }, |
| 68 | + ); |
| 69 | + |
| 70 | + assert.deepEqual( |
| 71 | + buildMauticPageview({ |
| 72 | + canonicalUrl: 'https://docs.registrystack.org/start/concepts/', |
| 73 | + currentUrl: |
| 74 | + 'https://docs.registrystack.org/start/concepts/?utm_source=registrystack&utm_medium=site&utm_campaign=cross_site&utm_content=%2Fpricing%2F', |
| 75 | + pageTitle: 'How the products fit together', |
| 76 | + referrer: 'https://registrystack.org/pricing/?email=person%40example.org', |
| 77 | + }), |
| 78 | + { |
| 79 | + page_url: 'https://docs.registrystack.org/start/concepts/', |
| 80 | + page_title: 'How the products fit together', |
| 81 | + page_referrer: 'https://registrystack.org', |
| 82 | + }, |
| 83 | + ); |
| 84 | +}); |
| 85 | + |
| 86 | +test('tracking controls use the shared bridge and remain available after failure', async () => { |
| 87 | + const [component, footer, head, privacy] = await Promise.all([ |
| 88 | + readFile(componentUrl, 'utf8'), |
| 89 | + readFile(footerUrl, 'utf8'), |
| 90 | + readFile(headUrl, 'utf8'), |
| 91 | + readFile(privacyUrl, 'utf8'), |
| 92 | + ]); |
| 93 | + |
| 94 | + assert.match(component, /SHARED_TRACKING_BRIDGE_URL/); |
| 95 | + assert.match(component, /createSharedTrackingClient/); |
| 96 | + assert.match(component, /buildMauticPageview/); |
| 97 | + assert.match(component, /trackingDeniedByBrowser/); |
| 98 | + assert.match(component, /new URL\(window\.location\.pathname, canonicalOrigin\)/); |
| 99 | + assert.match(component, /Accept optional cookies/); |
| 100 | + assert.match(component, /Reject optional cookies/); |
| 101 | + assert.match(component, /main Registry Stack website and this\s+documentation/i); |
| 102 | + assert.match(component, /data-tracking-error[^>]*role="alert"/); |
| 103 | + assert.match(component, /client\.pageview\(pageview\)/); |
| 104 | + assert.match(component, /let sentGeneration: string \| null = null/); |
| 105 | + assert.match(component, /let pendingGeneration: string \| null = null/); |
| 106 | + assert.match(component, /state\.generation === sentGeneration/); |
| 107 | + assert.match(component, /state\.generation === pendingGeneration/); |
| 108 | + assert.match(component, /sentGeneration = state\.generation/); |
| 109 | + assert.match(component, /currentState\.generation === state\.generation/); |
| 110 | + assert.match(component, /addEventListener\('pagehide', \(event\) => \{/); |
| 111 | + assert.match(component, /if \(!event\.persisted\) trackingClient\.close\(\)/); |
| 112 | + assert.doesNotMatch(component, /location\.reload/); |
| 113 | + assert.doesNotMatch(component, /mautic_device_id|mtc_id|\/mtc\.js/); |
| 114 | + |
| 115 | + assert.match(footer, /<RegistryTrackingPreferences/); |
| 116 | + assert.match(footer, /canonicalOrigin=\{canonicalOrigin\}/); |
| 117 | + assert.match(footer, /route\.entry\.id === '404'/); |
| 118 | + assert.match(footer, /route\.entry\.id\.endsWith\('\/404'\)/); |
| 119 | + assert.match(footer, /path: '\/'/); |
| 120 | + assert.match(footer, /data-tracking-preferences-open/); |
| 121 | + assert.match(footer, /data-tracking-preferences-open hidden/); |
| 122 | + assert.match(footer, />\s*Tracking preferences\s*</); |
| 123 | + assert.match(footer, /docsSharedTrackingEnabled/); |
| 124 | + |
| 125 | + assert.doesNotMatch( |
| 126 | + head, |
| 127 | + /targetSite === 'marketing' \|\| targetSite === 'lab'/, |
| 128 | + ); |
| 129 | + assert.match(head, /targetSite === 'lab'/); |
| 130 | + assert.match(head, /window\.umami\?\.track/); |
| 131 | + |
| 132 | + assert.match(privacy, /main Registry Stack website and Registry\s+Docs/i); |
| 133 | + assert.match(privacy, /tracking bridge/i); |
| 134 | + assert.match(privacy, /six months/i); |
| 135 | + assert.match(privacy, /Do Not Track and Global Privacy Control/i); |
| 136 | + assert.match(privacy, /\/dev\/[\s\S]*\/v\/<version>\/[\s\S]*do not load/i); |
| 137 | +}); |
0 commit comments