// SPDX-License-Identifier: GPL-2.0-or-later // Copyright (C) 2026 Bryan Joshua Pedini import { expect } from '@playwright/test'; import { waitForZone } from './dig'; import type { World } from './fixtures'; import type { AuditRow, RecordRow } from './mysql'; import { canonicalContent, expectCanonicalContent, expectSerialBump, fqdnOf, uiTtl, uiValue, type RecordData, type RecordType } from './records'; import { expectServed, neverFails } from './served'; import type { Ui } from './ui'; export interface Rec { name: string; type: RecordType; ttl: number; data: RecordData; } export interface Change { serialBefore: number; t0: Date; rows: RecordRow[]; } export async function begin(w: World, zone: string): Promise { const serial = await w.db.soaSerial(zone); expect(serial, `managed serial of ${zone}`).not.toBeNull(); return { serialBefore: serial as number, t0: new Date(), rows: await w.db.records(zone) }; } export function rowsOf(rows: RecordRow[], rec: Pick): RecordRow[] { return rows.filter((r) => r.name === rec.name && r.record_type === rec.type); } export async function storedRow(w: World, zone: string, rec: Rec): Promise { const rows = await w.db.records(zone); const expected = canonicalContent(rec.type, rec.data); const matching = rows.filter((r) => r.zone === zone && r.name === rec.name && r.record_type === rec.type && r.content !== null && normalizeJson(r.content) === expected); expect(matching, `exactly one row ${zone} ${rec.name || '@'} ${rec.type} ${expected} in ${JSON.stringify(rowsOf(rows, rec))}`).toHaveLength(1); const row = matching[0]; expect(row.zone, 'zone column').toBe(zone); expect(row.name, 'name column').toBe(rec.name); expect(row.record_type, 'record_type column').toBe(rec.type); expect(row.ttl, 'ttl column').toBe(rec.ttl); expectCanonicalContent(row.content, rec.type, rec.data, `content of ${rec.name || '@'} ${rec.type}`); return row; } export function normalizeJson(raw: string): string { try { return JSON.stringify(JSON.parse(raw)); } catch { return raw; } } export async function expectNoRow(w: World, zone: string, rec: Rec): Promise { const rows = await w.db.records(zone); const expected = canonicalContent(rec.type, rec.data); expect( rows.filter((r) => r.name === rec.name && r.record_type === rec.type && r.content !== null && normalizeJson(r.content) === expected), `no row ${rec.name || '@'} ${rec.type} ${expected}`, ).toHaveLength(0); } export async function expectBumped(w: World, zone: string, change: Change, what: string): Promise { const serial = await w.db.soaSerial(zone); expect(serial, `${what}: serial`).not.toBeNull(); expectSerialBump(change.serialBefore, serial as number, change.t0, new Date(), `${what}: SOA serial bumped exactly once`); return serial as number; } export async function expectUnchangedSerial(w: World, zone: string, serial: number, what: string): Promise { expect(await w.db.soaSerial(zone), `${what}: serial unchanged`).toBe(serial); } export async function served(w: World, zone: string, qname: string, qtype: string, label?: string): Promise { const rows = await w.db.records(zone); const serial = await w.db.soaSerial(zone); const r = await expectServed(w.g, rows, qname, qtype, { recorder: w.recorder, serial, label }); neverFails(r, label ?? `${qname} ${qtype}`); } export async function servedZone(w: World, zone: string): Promise { const rows = await w.db.records(zone); const seen = new Set(); await served(w, zone, zone, 'SOA'); for (const r of rows) { const key = `${r.name}\u0000${r.record_type}`; if (seen.has(key)) continue; seen.add(key); const name = fqdnOf(r.name.replace(/^\*$/, 'wild-e2e').replace(/^\*\./, 'wild-e2e.'), zone); await served(w, zone, name, r.record_type, `${r.name || '@'} ${r.record_type}`); } } export async function threeWay(w: World, ui: Ui | null, zone: string, rec: Rec, change: Change | null, what: string): Promise { const row = await storedRow(w, zone, rec); let serial = await w.db.soaSerial(zone); if (change) serial = await expectBumped(w, zone, change, what); const qname = fqdnOf(rec.name.replace(/^\*$/, `wild${row.id}`).replace(/^\*\./, `wild${row.id}.`), zone); await served(w, zone, qname, rec.type, `${what}: dig ${qname} ${rec.type}`); await served(w, zone, zone, 'SOA', `${what}: dig ${zone} SOA`); expect(serial).not.toBeNull(); if (ui) await ui.expectRow({ name: rec.name === '' ? '@' : rec.name, type: rec.type, value: uiValue(rec.type, rec.data), ttl: uiTtl(rec.ttl) }); return row; } export async function threeWayGone(w: World, ui: Ui | null, zone: string, rec: Rec, change: Change | null, what: string): Promise { await expectNoRow(w, zone, rec); if (change) await expectBumped(w, zone, change, what); const qname = fqdnOf(rec.name.replace(/^\*$/, 'gone-wild').replace(/^\*\./, 'gone-wild.'), zone); await served(w, zone, qname, rec.type, `${what}: dig ${qname} ${rec.type}`); if (ui) await ui.expectNoRow({ name: rec.name === '' ? '@' : rec.name, type: rec.type, value: uiValue(rec.type, rec.data) }); } export async function zoneServed(w: World, zone: string): Promise { await waitForZone(w.g, zone, { recorder: w.recorder }); } export function recordJson(row: RecordRow): Record { return { id: row.id, zone: row.zone, name: row.name, ttl: row.ttl, record_type: row.record_type, content: row.content }; } function contentEquals(actual: unknown, raw: string | null): boolean { if (raw === null) return actual === null; if (typeof actual === 'string') return normalizeJson(actual) === normalizeJson(raw); try { return JSON.stringify(actual) === JSON.stringify(JSON.parse(raw)); } catch { return false; } } export function expectAuditRecord(value: unknown, row: RecordRow, what: string): void { expect(value, `${what}: audit value is a record object`).toBeTruthy(); const v = value as Record; expect({ id: v.id, zone: v.zone, name: v.name, ttl: v.ttl, record_type: v.record_type }, `${what}: audit record fields`).toEqual({ id: row.id, zone: row.zone, name: row.name, ttl: row.ttl, record_type: row.record_type, }); expect(contentEquals(v.content, row.content), `${what}: audit record content ${JSON.stringify(v.content)} equals ${row.content}`).toBe(true); expect(Object.keys(v).sort(), `${what}: audit record keys`).toEqual(['content', 'id', 'name', 'record_type', 'ttl', 'zone']); } export async function expectAuditChange(w: World, zone: string, audit: AuditRow, change: Change, what: string): Promise { const after = await w.db.records(zone); const check = (value: unknown, from: RecordRow[], to: RecordRow[], side: string) => { const byId = new Map(to.map((row) => [row.id, JSON.stringify(row)])); const changed = from.filter((row) => byId.get(row.id) !== JSON.stringify(row)); const stored = asArray(parsedJson(value)); expect(stored, `${what}: audit ${side} lists exactly the changed records`).toHaveLength(changed.length); for (const row of changed) { expectAuditRecord(stored.find((entry) => (entry as { id: number }).id === row.id), row, `${what}: ${side} row ${row.id}`); } }; check(audit.before_value, change.rows, after, 'before'); check(audit.after_value, after, change.rows, 'after'); } export function asArray(value: unknown): unknown[] { if (value === null || value === undefined) return []; return Array.isArray(value) ? value : [value]; } export function findAudit(rows: AuditRow[], operation: string, status?: number): AuditRow { const found = rows.filter((r) => r.operation === operation && (status === undefined || r.status === status)); expect(found, `exactly one ${operation}${status ? ` ${status}` : ''} audit row among ${rows.map((r) => `${r.operation}:${r.status}`).join(', ')}`).toHaveLength(1); return found[0]; } export function parsedJson(v: unknown): unknown { if (typeof v === 'string') { try { return JSON.parse(v); } catch { return v; } } return v; }