|
| 1 | +import { copyFile, mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import * as path from 'node:path'; |
| 4 | + |
| 5 | +import { chromium, expect, test } from '@playwright/test'; |
| 6 | + |
| 7 | +import { createBaseEnvironmentJSON } from '../../../packages/clerk-js/src/test/fixtures'; |
| 8 | + |
| 9 | +for (const environmentType of ['development', 'production'] as const) { |
| 10 | + test(`no-RHC runtime ignores configured script loaders (${environmentType}) @chrome-extension`, async () => { |
| 11 | + const extensionPath = await mkdtemp(path.join(tmpdir(), 'clerk-no-rhc-')); |
| 12 | + await Promise.all([ |
| 13 | + copyFile( |
| 14 | + path.resolve(import.meta.dirname, '../../../packages/clerk-js/dist/clerk.no-rhc.mjs'), |
| 15 | + path.join(extensionPath, 'clerk.mjs'), |
| 16 | + ), |
| 17 | + writeFile( |
| 18 | + path.join(extensionPath, 'manifest.json'), |
| 19 | + JSON.stringify({ |
| 20 | + manifest_version: 3, |
| 21 | + name: 'Clerk no-RHC regression', |
| 22 | + version: '1.0.0', |
| 23 | + background: { service_worker: 'background.js' }, |
| 24 | + host_permissions: ['https://clerk.example.com/*'], |
| 25 | + content_security_policy: { extension_pages: "script-src 'self'; object-src 'self'" }, |
| 26 | + }), |
| 27 | + ), |
| 28 | + writeFile(path.join(extensionPath, 'background.js'), 'chrome.runtime.onInstalled.addListener(() => {});'), |
| 29 | + writeFile(path.join(extensionPath, 'popup.html'), '<body><script type="module" src="popup.js"></script></body>'), |
| 30 | + writeFile( |
| 31 | + path.join(extensionPath, 'popup.js'), |
| 32 | + `import { Clerk } from './clerk.mjs'; |
| 33 | +const clerk = new Clerk('${environmentType === 'production' ? 'pk_live_' : 'pk_test_'}' + btoa('clerk.example.com$')); |
| 34 | +await clerk.load({ standardBrowser: false, telemetry: false }); |
| 35 | +document.body.dataset.loaded = String(clerk.loaded); |
| 36 | +`, |
| 37 | + ), |
| 38 | + ]); |
| 39 | + |
| 40 | + const context = await chromium.launchPersistentContext('', { |
| 41 | + headless: false, |
| 42 | + bypassCSP: false, |
| 43 | + args: ['--headless=new', `--disable-extensions-except=${extensionPath}`, `--load-extension=${extensionPath}`], |
| 44 | + }); |
| 45 | + |
| 46 | + try { |
| 47 | + const environment = createBaseEnvironmentJSON(); |
| 48 | + environment.display_config.instance_environment_type = environmentType; |
| 49 | + environment.protect_config = { |
| 50 | + id: 'protect_config_test', |
| 51 | + object: 'protect_config', |
| 52 | + loaders: [ |
| 53 | + { type: 'script', attributes: { src: 'https://example.com/device-intelligence.js' } }, |
| 54 | + { type: 'script', text_content: 'window.remoteLoaderExecuted = true;' }, |
| 55 | + ], |
| 56 | + }; |
| 57 | + |
| 58 | + let environmentRequests = 0; |
| 59 | + await context.route('https://clerk.example.com/**', async route => { |
| 60 | + const isEnvironment = new URL(route.request().url()).pathname === '/v1/environment'; |
| 61 | + if (isEnvironment) { |
| 62 | + environmentRequests++; |
| 63 | + } |
| 64 | + await route.fulfill({ |
| 65 | + json: { |
| 66 | + response: isEnvironment ? environment : { object: 'client', id: 'client_test', sessions: [] }, |
| 67 | + }, |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + const background = context.serviceWorkers()[0] || (await context.waitForEvent('serviceworker')); |
| 72 | + const extensionId = new URL(background.url()).host; |
| 73 | + const page = await context.newPage(); |
| 74 | + const remoteScripts: string[] = []; |
| 75 | + const errors: string[] = []; |
| 76 | + page.on('request', request => { |
| 77 | + if (request.resourceType() === 'script' && request.url().startsWith('https://')) { |
| 78 | + remoteScripts.push(request.url()); |
| 79 | + } |
| 80 | + }); |
| 81 | + page.on('console', message => { |
| 82 | + if (message.type() === 'error') { |
| 83 | + errors.push(message.text()); |
| 84 | + } |
| 85 | + }); |
| 86 | + page.on('pageerror', error => errors.push(error.message)); |
| 87 | + |
| 88 | + await page.goto(`chrome-extension://${extensionId}/popup.html`); |
| 89 | + await expect(page.locator('body')).toHaveAttribute('data-loaded', 'true'); |
| 90 | + expect(environmentRequests).toBeGreaterThan(0); |
| 91 | + await expect(page.locator('script[src^="https://"], script:not([src])')).toHaveCount(0); |
| 92 | + expect(remoteScripts).toEqual([]); |
| 93 | + expect(errors).toEqual([]); |
| 94 | + } finally { |
| 95 | + await context.close(); |
| 96 | + await rm(extensionPath, { recursive: true, force: true }); |
| 97 | + } |
| 98 | + }); |
| 99 | +} |
0 commit comments