|
| 1 | +// cSpell:ignore GOOGLEANALYTICS |
| 2 | + |
| 3 | +import assert from 'node:assert/strict'; |
| 4 | +import test from 'node:test'; |
| 5 | + |
| 6 | +import { |
| 7 | + enqueueAssetFetchEvent, |
| 8 | + normalizeContentType, |
| 9 | + resolveClientId, |
| 10 | +} from './ga4-asset-fetch.ts'; |
| 11 | + |
| 12 | +test('normalizeContentType extracts media type', () => { |
| 13 | + assert.equal(normalizeContentType('application/yaml'), 'application/yaml'); |
| 14 | + assert.equal(normalizeContentType('text/html; charset=utf-8'), 'text/html'); |
| 15 | + assert.equal( |
| 16 | + normalizeContentType(' Application/JSON ; q=0.9'), |
| 17 | + 'application/json', |
| 18 | + ); |
| 19 | + assert.equal(normalizeContentType(null), 'none'); |
| 20 | + assert.equal(normalizeContentType(''), 'none'); |
| 21 | +}); |
| 22 | + |
| 23 | +test('resolveClientId returns fallback when no GA cookie is present', () => { |
| 24 | + const request = new Request('https://example.com/', { |
| 25 | + headers: {}, |
| 26 | + }); |
| 27 | + assert.equal(resolveClientId(request), 'asset_fetch.anonymous'); |
| 28 | +}); |
| 29 | + |
| 30 | +test('resolveClientId extracts client id from GA cookie', () => { |
| 31 | + const request = new Request('https://example.com/', { |
| 32 | + headers: { cookie: '_ga=GA1.1.123456789.1234567890' }, |
| 33 | + }); |
| 34 | + assert.equal(resolveClientId(request), '123456789.1234567890'); |
| 35 | +}); |
| 36 | + |
| 37 | +test('resolveClientId returns raw value for non-standard GA cookie', () => { |
| 38 | + const request = new Request('https://example.com/', { |
| 39 | + headers: { cookie: '_ga=custom-value' }, |
| 40 | + }); |
| 41 | + assert.equal(resolveClientId(request), 'custom-value'); |
| 42 | +}); |
| 43 | + |
| 44 | +test('resolveClientId finds GA cookie among multiple cookies', () => { |
| 45 | + const request = new Request('https://example.com/', { |
| 46 | + headers: { cookie: 'session=abc; _ga=GA1.2.111.222; other=xyz' }, |
| 47 | + }); |
| 48 | + assert.equal(resolveClientId(request), '111.222'); |
| 49 | +}); |
| 50 | + |
| 51 | +test('enqueueAssetFetchEvent no-ops without waitUntil', () => { |
| 52 | + const request = new Request('https://example.com/schemas/1.40.0'); |
| 53 | + // No waitUntil on context — should not throw. |
| 54 | + enqueueAssetFetchEvent( |
| 55 | + request, |
| 56 | + {}, |
| 57 | + { |
| 58 | + asset_group: 'schema', |
| 59 | + asset_path: '/schemas/1.40.0', |
| 60 | + asset_ext: 'yaml', |
| 61 | + content_type: 'application/yaml', |
| 62 | + status_code: '200', |
| 63 | + }, |
| 64 | + ); |
| 65 | +}); |
| 66 | + |
| 67 | +test('enqueueAssetFetchEvent calls waitUntil with GA4 payload', async (t) => { |
| 68 | + const g = globalThis as Record<string, unknown>; |
| 69 | + const originalNetlify = g.Netlify; |
| 70 | + const originalFetch = globalThis.fetch; |
| 71 | + t.after(() => { |
| 72 | + g.Netlify = originalNetlify; |
| 73 | + globalThis.fetch = originalFetch; |
| 74 | + }); |
| 75 | + |
| 76 | + g.Netlify = { |
| 77 | + env: { |
| 78 | + get: (name: string) => { |
| 79 | + if (name === 'HUGO_SERVICES_GOOGLEANALYTICS_ID') return 'G-TEST123'; |
| 80 | + if (name === 'GA4_API_SECRET') return 'secret-test'; |
| 81 | + return undefined; |
| 82 | + }, |
| 83 | + }, |
| 84 | + }; |
| 85 | + |
| 86 | + let capturedBody: Record<string, unknown> | undefined; |
| 87 | + let capturedUrl: string | undefined; |
| 88 | + |
| 89 | + globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { |
| 90 | + capturedUrl = |
| 91 | + input instanceof URL |
| 92 | + ? input.toString() |
| 93 | + : typeof input === 'string' |
| 94 | + ? input |
| 95 | + : input.url; |
| 96 | + if (init?.body) { |
| 97 | + capturedBody = JSON.parse(init.body as string); |
| 98 | + } |
| 99 | + return new Response('', { status: 200 }); |
| 100 | + }) as typeof fetch; |
| 101 | + |
| 102 | + let waitUntilPromise: Promise<unknown> | undefined; |
| 103 | + const context = { |
| 104 | + waitUntil: (p: Promise<unknown>) => { |
| 105 | + waitUntilPromise = p; |
| 106 | + }, |
| 107 | + requestId: 'req-42', |
| 108 | + }; |
| 109 | + |
| 110 | + const request = new Request('https://example.com/schemas/1.40.0'); |
| 111 | + |
| 112 | + enqueueAssetFetchEvent(request, context, { |
| 113 | + asset_group: 'schema', |
| 114 | + asset_path: '/schemas/1.40.0', |
| 115 | + asset_ext: 'yaml', |
| 116 | + content_type: 'application/yaml', |
| 117 | + status_code: '200', |
| 118 | + }); |
| 119 | + |
| 120 | + assert.ok(waitUntilPromise, 'waitUntil should have been called'); |
| 121 | + await waitUntilPromise; |
| 122 | + |
| 123 | + assert.ok(capturedUrl, 'fetch should have been called'); |
| 124 | + const url = new URL(capturedUrl!); |
| 125 | + assert.equal(url.searchParams.get('api_secret'), 'secret-test'); |
| 126 | + assert.equal(url.searchParams.get('measurement_id'), 'G-TEST123'); |
| 127 | + |
| 128 | + assert.ok(capturedBody); |
| 129 | + assert.equal(capturedBody!.client_id, 'asset_fetch.anonymous'); |
| 130 | + |
| 131 | + const events = capturedBody!.events as Array<{ |
| 132 | + name: string; |
| 133 | + params: Record<string, string>; |
| 134 | + }>; |
| 135 | + assert.equal(events.length, 1); |
| 136 | + assert.equal(events[0].name, 'asset_fetch'); |
| 137 | + assert.equal(events[0].params.asset_group, 'schema'); |
| 138 | + assert.equal(events[0].params.asset_path, '/schemas/1.40.0'); |
| 139 | + assert.equal(events[0].params.asset_ext, 'yaml'); |
| 140 | + assert.equal(events[0].params.content_type, 'application/yaml'); |
| 141 | + assert.equal(events[0].params.status_code, '200'); |
| 142 | + assert.equal( |
| 143 | + events[0].params.original_path, |
| 144 | + undefined, |
| 145 | + 'undefined params should be stripped', |
| 146 | + ); |
| 147 | +}); |
0 commit comments