~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
main
73 lines · 3.1 KB Raw History
// node --test examples/*/*.test.mjs
// The page's load (serve.js boot), signed out: the did:key, the ingress started, the URL, the domain form.
import assert from 'node:assert/strict'
import { test } from 'node:test'

import { boot } from './serve.js'

// What boot touches of the page: elements by selector, plain objects here.
const page = () => {
	const els = {}
	return { els, $: (selector) => (els[selector] ??= {}) }
}
const store = () => {
	const map = new Map()
	return { map, get: async (k) => structuredClone(map.get(k)), set: async (k, v) => void map.set(k, structuredClone(v)) }
}
const broken = { get: async () => Promise.reject(new Error('blocked')), set: async () => Promise.reject(new Error('blocked')) }
let made = 0
const createDidKey = async (jwk) => {
	jwk ??= { kty: 'EC', d: `k${++made}` }
	return { did: `did:key:zDnae${jwk.d.toUpperCase()}`, jwk }
}
const hostOf = (did) => `${did.replaceAll(':', '-').toLowerCase()}.xrpc.fedproxy.com`

async function load(settings, signedIn = false) {
	const { els, $ } = page()
	const started = []
	const out = await boot({ $, settings, createDidKey, hostOf, start: (key, aliases) => started.push({ did: key.did, aliases }), signedIn, locks: null })
	return { els, started, ...out }
}

test('signed out: a did:key is made and kept, the ingress starts with no aliases, its URL shows, the domain form is disabled', async () => {
	const settings = store()
	const { els, started, key, host } = await load(settings)
	assert.match(key.did, /^did:key:z/)
	assert.deepEqual(settings.map.get('didkeys'), { ingress: key.jwk })
	assert.deepEqual(started, [{ did: key.did, aliases: [] }])
	assert.equal(host, hostOf(key.did))
	assert.deepEqual(els['#urls .ingress'], { href: `https://${host}/`, textContent: `https://${host}/` })
	assert.equal(els['#urls'].hidden, false)
	assert.equal(els['#urls .kept'].textContent, '')
	assert.equal(els['#serve fieldset'].disabled, true)
	assert.equal(els['#serve .signin'].hidden, false)
})

test('signed out: the next load reuses the did:key (same URL); other keys in the map stay', async () => {
	const settings = store()
	await settings.set('didkeys', { other: { d: 'x' } })
	const first = await load(settings)
	const again = await load(settings)
	assert.equal(again.key.did, first.key.did)
	assert.equal(again.els['#urls .ingress'].href, first.els['#urls .ingress'].href)
	assert.deepEqual(Object.keys(settings.map.get('didkeys')).sort(), ['ingress', 'other'])
})

test('signed out, storage blocked: a key for this load only, still serving, and the page says so', async () => {
	const a = await load(broken)
	const b = await load(broken)
	assert.notEqual(a.key.did, b.key.did)
	assert.equal(a.started.length, 1)
	assert.match(a.els['#urls .kept'].textContent, /new did:key/)
	assert.equal(a.els['#serve fieldset'].disabled, true)
})

test('signed in: the same did:key serves, and the domain form is enabled', async () => {
	const settings = store()
	const out = (await load(settings)).key.did
	const { els, key, started } = await load(settings, true)
	assert.equal(key.did, out)
	assert.equal(started.length, 1)
	assert.equal(els['#serve fieldset'].disabled, false)
	assert.equal(els['#serve .signin'].hidden, true)
})