|
| 1 | +// Integration guard for bundle-only sdkPatches (nuxt/scripts#720 / Fathom). |
| 2 | +// Exercises the full NuxtScriptBundleTransformer → downloadScript → |
| 3 | +// rewriteScriptUrlsAST pipeline to prove the neutralize-domain-check patch is |
| 4 | +// actually applied to the stored bundle. A prior regression gated the rewrite |
| 5 | +// on proxyRewrites.length, so bundle-only patches were silently dropped while |
| 6 | +// direct unit tests of rewriteScriptUrlsAST still passed. |
| 7 | +importtype{AssetBundlerTransformerOptions}from'../../packages/script/src/plugins/transform' |
| 8 | +import{hash}from'ohash' |
| 9 | +import{hasProtocol}from'ufo' |
| 10 | +import{describe,expect,it,vi}from'vitest' |
| 11 | +import{NuxtScriptBundleTransformer}from'../../packages/script/src/plugins/transform' |
| 12 | + |
| 13 | +vi.mock('ohash',async(og)=>{ |
| 14 | +constmod=awaitog<typeofimport('ohash')>() |
| 15 | +return{ ...mod,hash: vi.fn(mod.hash)} |
| 16 | +}) |
| 17 | +vi.mock('ufo',async(og)=>{ |
| 18 | +constmod=awaitog<typeofimport('ufo')>() |
| 19 | +return{ ...mod,hasProtocol: vi.fn(mod.hasProtocol)} |
| 20 | +}) |
| 21 | + |
| 22 | +constmockBundleStorage: any={ |
| 23 | +getItem: vi.fn(), |
| 24 | +setItem: vi.fn(), |
| 25 | +getItemRaw: vi.fn(), |
| 26 | +setItemRaw: vi.fn(), |
| 27 | +hasItem: vi.fn().mockResolvedValue(false), |
| 28 | +} |
| 29 | +vi.mock('../../packages/script/src/assets',()=>({ |
| 30 | +bundleStorage: vi.fn(()=>mockBundleStorage), |
| 31 | +})) |
| 32 | + |
| 33 | +constfetchMock=vi.fn() |
| 34 | +vi.stubGlobal('fetch',fetchMock) |
| 35 | + |
| 36 | +vi.mock('@nuxt/kit',async(og)=>{ |
| 37 | +constmod=awaitog<typeofimport('@nuxt/kit')>() |
| 38 | +constnuxt={ |
| 39 | +options: {buildDir: '.nuxt',app: {baseURL: '/'},runtimeConfig: {app: {}}}, |
| 40 | +hooks: {hook: vi.fn()}, |
| 41 | +} |
| 42 | +return{ ...mod,useNuxt: ()=>nuxt,tryUseNuxt: ()=>nuxt} |
| 43 | +}) |
| 44 | + |
| 45 | +vi.mocked(hasProtocol).mockImplementation(()=>true) |
| 46 | +vi.mocked(hash).mockImplementation(()=>'fathom-script') |
| 47 | + |
| 48 | +functionmockUpstream(bytes: Buffer){ |
| 49 | +fetchMock.mockResolvedValueOnce({ |
| 50 | +ok: true, |
| 51 | +arrayBuffer: ()=>Promise.resolve(bytes), |
| 52 | +headers: {get: ()=>null}, |
| 53 | +_data: bytes, |
| 54 | +}asany) |
| 55 | +} |
| 56 | + |
| 57 | +asyncfunctionrunTransform(code: string,options: AssetBundlerTransformerOptions){ |
| 58 | +constplugin=NuxtScriptBundleTransformer(options).vite()asany |
| 59 | +awaitplugin.transform.handler.call({},code,'file.js') |
| 60 | +} |
| 61 | + |
| 62 | +describe('bundle-only sdkPatches integration',()=>{ |
| 63 | +constfathomLike=Buffer.from( |
| 64 | +`(function(){var e=document.currentScript;if(e.src.indexOf("cdn.usefathom.com")<0){t="custom"}})();`, |
| 65 | +) |
| 66 | + |
| 67 | +it('applies neutralize-domain-check to bundle-only scripts (no proxy)',async()=>{ |
| 68 | +mockUpstream(fathomLike) |
| 69 | +constrenderedScript=newMap() |
| 70 | + |
| 71 | +awaitrunTransform( |
| 72 | +`const instance = useScriptFathomAnalytics({ site: '123' }, { bundle: true })`, |
| 73 | +{ |
| 74 | + renderedScript, |
| 75 | +scripts: [ |
| 76 | +{ |
| 77 | +bundle: { |
| 78 | +resolve: ()=>'https://cdn.usefathom.com/script.js', |
| 79 | +sdkPatches: [{type: 'neutralize-domain-check',domain: 'cdn.usefathom.com'}], |
| 80 | +}, |
| 81 | +import: {name: 'useScriptFathomAnalytics',from: ''}, |
| 82 | +}, |
| 83 | +], |
| 84 | +}, |
| 85 | +) |
| 86 | + |
| 87 | +conststored=[...renderedScript.values()][0] |
| 88 | +expect(stored,'bundle was not stored').toBeDefined() |
| 89 | +constcontent=(stored.contentasBuffer).toString('utf-8') |
| 90 | +// Patch rewrites `< 0` to `< -1` on the fathom domain indexOf comparison, |
| 91 | +// preserving the original whitespace (minified `<0` stays minified). |
| 92 | +expect(content).toMatch(/indexOf\("cdn\.usefathom\.com"\)\s*<\s*-1/) |
| 93 | +expect(content).not.toMatch(/indexOf\("cdn\.usefathom\.com"\)\s*<\s*0\b/) |
| 94 | +}) |
| 95 | + |
| 96 | +it('leaves bundles untouched when no patches are configured',async()=>{ |
| 97 | +mockUpstream(fathomLike) |
| 98 | +constrenderedScript=newMap() |
| 99 | + |
| 100 | +awaitrunTransform( |
| 101 | +`const instance = useScript('https://cdn.usefathom.com/script.js', { bundle: true })`, |
| 102 | +{ |
| 103 | + renderedScript, |
| 104 | +scripts: [ |
| 105 | +{ |
| 106 | +bundle: {resolve: ()=>'https://cdn.usefathom.com/script.js'}, |
| 107 | +import: {name: 'useScript',from: ''}, |
| 108 | +}, |
| 109 | +], |
| 110 | +}, |
| 111 | +) |
| 112 | + |
| 113 | +conststored=[...renderedScript.values()][0] |
| 114 | +expect(stored).toBeDefined() |
| 115 | +constcontent=(stored.contentasBuffer).toString('utf-8') |
| 116 | +expect(content).toBe(fathomLike.toString('utf-8')) |
| 117 | +}) |
| 118 | +}) |
0 commit comments