Skip to content

Commit

Permalink
test: config propagation to subdomains (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki authored Nov 14, 2024
1 parent f96431e commit 5cd083c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
48 changes: 48 additions & 0 deletions test-e2e/config-loading.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
45 changes: 44 additions & 1 deletion test-e2e/fixtures/set-sw-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function setConfig ({ page, config }: { page: Page, config: Partial
}
})
const db = await openDb()
const put = async (key, value): Promise<void> => {
const put = async (key: keyof ConfigDb, value): Promise<void> => {
const transaction = db.transaction(storeName, 'readwrite')
const store = transaction.objectStore(storeName)
const request = store.put(value, key)
Expand Down Expand Up @@ -88,6 +88,49 @@ export async function setConfig ({ page, config }: { page: Page, config: Partial
})
}

export async function getConfig ({ page }: { page: Page }): Promise<ConfigDb> {
const config: ConfigDb = await page.evaluate(async () => {
const dbName = 'helia-sw'
const storeName = 'config'
const openDb = async (): Promise<IDBDatabase> => 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<any> => {
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<ConfigDb> }): Promise<void> {
await waitForServiceWorker(page)

Expand Down

0 comments on commit 5cd083c

Please sign in to comment.