diff --git a/test-e2e/config-loading.test.ts b/test-e2e/config-loading.test.ts new file mode 100644 index 0000000..8b249fb --- /dev/null +++ b/test-e2e/config-loading.test.ts @@ -0,0 +1,48 @@ +import { testSubdomainRouting as test, expect } from './fixtures/config-test-fixtures.js' +import { getConfig, setConfig } from './fixtures/set-sw-config.js' +import { waitForServiceWorker } from './fixtures/wait-for-service-worker.js' +import type { ConfigDb } from '../src/lib/config-db' + +test.describe('/#/ipfs-sw-config', () => { + const testConfig: ConfigDb = { + gateways: [process.env.KUBO_GATEWAY as string, 'http://example.com'], + routers: [process.env.KUBO_GATEWAY as string, 'http://example.com/routing/v1'], + dnsJsonResolvers: { + '.': 'example.com/dns-query' + }, + debug: 'testDebug', + enableWss: false, + enableWebTransport: true, + enableRecursiveGateways: false, + enableGatewayProviders: false + } + test('setting the config actually works', async ({ page, baseURL }) => { + await page.goto(baseURL, { waitUntil: 'networkidle' }) + await waitForServiceWorker(page) + + await setConfig({ page, config: testConfig }) + expect(await getConfig({ page })).toEqual(testConfig) + }) + + test('root config is propagated to subdomain', async ({ page, baseURL, rootDomain, protocol }) => { + await page.goto(baseURL, { waitUntil: 'networkidle' }) + await waitForServiceWorker(page) + // set the config on the root.. + await setConfig({ + page, + config: testConfig + }) + const rootConfig = await getConfig({ page }) + + // now query a new subdomain and make sure that the config on this page is the same as the root after the page loads + await page.goto(`${protocol}://bafkqablimvwgy3y.ipfs.${rootDomain}/`, { waitUntil: 'networkidle' }) + + // now get the config from the subdomain + await waitForServiceWorker(page) + const subdomainConfig = await getConfig({ page }) + + // ensure it equals the root config + expect(subdomainConfig).toEqual(rootConfig) + expect(subdomainConfig).toEqual(testConfig) + }) +}) diff --git a/test-e2e/fixtures/set-sw-config.ts b/test-e2e/fixtures/set-sw-config.ts index cb953bd..958b620 100644 --- a/test-e2e/fixtures/set-sw-config.ts +++ b/test-e2e/fixtures/set-sw-config.ts @@ -59,7 +59,7 @@ export async function setConfig ({ page, config }: { page: Page, config: Partial } }) const db = await openDb() - const put = async (key, value): Promise => { + const put = async (key: keyof ConfigDb, value): Promise => { const transaction = db.transaction(storeName, 'readwrite') const store = transaction.objectStore(storeName) const request = store.put(value, key) @@ -88,6 +88,49 @@ export async function setConfig ({ page, config }: { page: Page, config: Partial }) } +export async function getConfig ({ page }: { page: Page }): Promise { + const config: ConfigDb = await page.evaluate(async () => { + const dbName = 'helia-sw' + const storeName = 'config' + const openDb = async (): Promise => new Promise((resolve, reject) => { + const request = indexedDB.open(dbName, 1) + request.onerror = () => { reject(request.error) } + request.onsuccess = () => { resolve(request.result) } + request.onupgradeneeded = (event) => { + const db = request.result + db.createObjectStore(storeName) + } + }) + const db = await openDb() + const get = async (key): Promise => { + const transaction = db.transaction(storeName, 'readonly') + const store = transaction.objectStore(storeName) + const request = store.get(key) + return new Promise((resolve, reject) => { + request.onerror = () => { reject(request.error) } + request.onsuccess = () => { resolve(request.result) } + }) + } + + const config: ConfigDb = { + gateways: await get('gateways'), + routers: await get('routers'), + dnsJsonResolvers: await get('dnsJsonResolvers'), + enableWss: await get('enableWss'), + enableWebTransport: await get('enableWebTransport'), + enableRecursiveGateways: await get('enableRecursiveGateways'), + enableGatewayProviders: await get('enableGatewayProviders'), + debug: await get('debug') + } + + db.close() + + return config + }, {}) + + return config +} + export async function setSubdomainConfig ({ page, config }: { page: Page, config: Partial }): Promise { await waitForServiceWorker(page)