~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
main
62 lines · 3.3 KB Raw History
// node --test examples/*/*.test.mjs
import assert from 'node:assert/strict'
import { test } from 'node:test'

import { fqdn, plan, toResponse, txtName, txtPlan } from './serve.js'

const target = 'did-key-zabc.xrpc.fedproxy.com'

test('fqdn: @ is the apex', () => {
	assert.equal(fqdn('@', 'mush.at'), 'mush.at')
	assert.equal(fqdn('hello', 'mush.at'), 'hello.mush.at')
})

test('plan: everything else at the name goes, other names stay', () => {
	const zone = [
		{ name: '@', recordType: 'A', value: '97.115.166.21' },
		{ name: '*', recordType: 'A', value: '97.115.166.21' },
		{ name: '@', recordType: 'TXT', value: 'x' },
	]
	assert.deepEqual(plan(zone, '@', target), { remove: [zone[0], zone[2]], create: true })
	assert.deepEqual(plan(zone, 'hello', target), { remove: [], create: true })
})

test('plan: the CNAME already there is kept, an old one replaced', () => {
	const ours = { name: '@', recordType: 'CNAME', value: target }
	const old = { name: '@', recordType: 'CNAME', value: 'did-key-zold.xrpc.fedproxy.com' }
	assert.deepEqual(plan([ours], '@', target), { remove: [], create: false })
	assert.deepEqual(plan([old], '@', target), { remove: [old], create: true })
})

test('toResponse: string, response object, JSON, failures', () => {
	assert.deepEqual(toResponse({ result: { value: 'hello world' } }), { status: 200, body: 'hello world', contentType: 'text/plain; charset=utf-8' })
	assert.deepEqual(toResponse({ result: { value: { status: 404, body: 'no' } } }), { status: 404, body: 'no', contentType: 'text/plain; charset=utf-8' })
	assert.deepEqual(toResponse({ result: { value: { body: '<h1>', contentType: 'text/html' } } }), { status: 200, body: '<h1>', contentType: 'text/html' })
	assert.deepEqual(toResponse({ result: { value: [1] } }), { status: 200, body: [1], contentType: 'application/json' })
	assert.equal(toResponse({ result: { value: null } }).status, 500)
	assert.equal(toResponse({ error: 'WASM_TIMEOUT' }).body, 'handler failed: WASM_TIMEOUT')
})

test('txtPlan: _xrpc TXT holds our did:key; an older did:key there is updated, other TXT stays', () => {
	const did = 'did:key:zDnaeNew'
	assert.equal(txtName('@'), '_xrpc')
	assert.equal(txtName('hello'), '_xrpc.hello')
	const spf = { name: '_xrpc', recordType: 'TXT', value: 'v=spf1 -all' }
	const old = { name: '_xrpc', recordType: 'TXT', value: 'did:key:zDnaeOld' }
	const ours = { name: '_xrpc', recordType: 'TXT', value: did }
	assert.deepEqual(txtPlan([spf], '_xrpc', did), { update: null, create: true })
	assert.deepEqual(txtPlan([spf, old], '_xrpc', did), { update: old, create: false })
	assert.deepEqual(txtPlan([old, ours], '_xrpc', did), { update: null, create: false })
	assert.deepEqual(txtPlan([{ ...ours, name: '_xrpc.hello' }], '_xrpc', did), { update: null, create: true })
	assert.deepEqual(plan([old], '@', 'did-key-znew.xrpc.fedproxy.com').remove, [], 'the CNAME at @ leaves _xrpc alone')
})

test('toResponse: headers pass through for the relay', () => {
	assert.deepEqual(toResponse({ result: { value: { status: 302, headers: { location: '/x', 'access-control-allow-origin': '*' } } } }), {
		status: 302,
		body: '',
		contentType: 'text/plain; charset=utf-8',
		headers: { location: '/x', 'access-control-allow-origin': '*' },
	})
	assert.equal('headers' in toResponse({ result: { value: { body: 'x', headers: ['no'] } } }), false)
})