Skip to content

Commit 5cd083c

Browse files
authored
test: config propagation to subdomains (#459)
1 parent f96431e commit 5cd083c

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

test-e2e/config-loading.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { testSubdomainRouting as test, expect } from './fixtures/config-test-fixtures.js'
2+
import { getConfig, setConfig } from './fixtures/set-sw-config.js'
3+
import { waitForServiceWorker } from './fixtures/wait-for-service-worker.js'
4+
import type { ConfigDb } from '../src/lib/config-db'
5+
6+
test.describe('/#/ipfs-sw-config', () => {
7+
const testConfig: ConfigDb = {
8+
gateways: [process.env.KUBO_GATEWAY as string, 'http://example.com'],
9+
routers: [process.env.KUBO_GATEWAY as string, 'http://example.com/routing/v1'],
10+
dnsJsonResolvers: {
11+
'.': 'example.com/dns-query'
12+
},
13+
debug: 'testDebug',
14+
enableWss: false,
15+
enableWebTransport: true,
16+
enableRecursiveGateways: false,
17+
enableGatewayProviders: false
18+
}
19+
test('setting the config actually works', async ({ page, baseURL }) => {
20+
await page.goto(baseURL, { waitUntil: 'networkidle' })
21+
await waitForServiceWorker(page)
22+
23+
await setConfig({ page, config: testConfig })
24+
expect(await getConfig({ page })).toEqual(testConfig)
25+
})
26+
27+
test('root config is propagated to subdomain', async ({ page, baseURL, rootDomain, protocol }) => {
28+
await page.goto(baseURL, { waitUntil: 'networkidle' })
29+
await waitForServiceWorker(page)
30+
// set the config on the root..
31+
await setConfig({
32+
page,
33+
config: testConfig
34+
})
35+
const rootConfig = await getConfig({ page })
36+
37+
// now query a new subdomain and make sure that the config on this page is the same as the root after the page loads
38+
await page.goto(`${protocol}://bafkqablimvwgy3y.ipfs.${rootDomain}/`, { waitUntil: 'networkidle' })
39+
40+
// now get the config from the subdomain
41+
await waitForServiceWorker(page)
42+
const subdomainConfig = await getConfig({ page })
43+
44+
// ensure it equals the root config
45+
expect(subdomainConfig).toEqual(rootConfig)
46+
expect(subdomainConfig).toEqual(testConfig)
47+
})
48+
})

test-e2e/fixtures/set-sw-config.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function setConfig ({ page, config }: { page: Page, config: Partial
5959
}
6060
})
6161
const db = await openDb()
62-
const put = async (key, value): Promise<void> => {
62+
const put = async (key: keyof ConfigDb, value): Promise<void> => {
6363
const transaction = db.transaction(storeName, 'readwrite')
6464
const store = transaction.objectStore(storeName)
6565
const request = store.put(value, key)
@@ -88,6 +88,49 @@ export async function setConfig ({ page, config }: { page: Page, config: Partial
8888
})
8989
}
9090

91+
export async function getConfig ({ page }: { page: Page }): Promise<ConfigDb> {
92+
const config: ConfigDb = await page.evaluate(async () => {
93+
const dbName = 'helia-sw'
94+
const storeName = 'config'
95+
const openDb = async (): Promise<IDBDatabase> => new Promise((resolve, reject) => {
96+
const request = indexedDB.open(dbName, 1)
97+
request.onerror = () => { reject(request.error) }
98+
request.onsuccess = () => { resolve(request.result) }
99+
request.onupgradeneeded = (event) => {
100+
const db = request.result
101+
db.createObjectStore(storeName)
102+
}
103+
})
104+
const db = await openDb()
105+
const get = async (key): Promise<any> => {
106+
const transaction = db.transaction(storeName, 'readonly')
107+
const store = transaction.objectStore(storeName)
108+
const request = store.get(key)
109+
return new Promise((resolve, reject) => {
110+
request.onerror = () => { reject(request.error) }
111+
request.onsuccess = () => { resolve(request.result) }
112+
})
113+
}
114+
115+
const config: ConfigDb = {
116+
gateways: await get('gateways'),
117+
routers: await get('routers'),
118+
dnsJsonResolvers: await get('dnsJsonResolvers'),
119+
enableWss: await get('enableWss'),
120+
enableWebTransport: await get('enableWebTransport'),
121+
enableRecursiveGateways: await get('enableRecursiveGateways'),
122+
enableGatewayProviders: await get('enableGatewayProviders'),
123+
debug: await get('debug')
124+
}
125+
126+
db.close()
127+
128+
return config
129+
}, {})
130+
131+
return config
132+
}
133+
91134
export async function setSubdomainConfig ({ page, config }: { page: Page, config: Partial<ConfigDb> }): Promise<void> {
92135
await waitForServiceWorker(page)
93136

0 commit comments

Comments
 (0)