import test from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' const __dirname = path.dirname(fileURLToPath(import.meta.url)) const schemaPath = path.resolve(__dirname, '../../../docs/schemas/mbr-layout.schema.json') const HEX_64_RE = /^[0-9a-f]{64}$/ function isValidExamplePayload(p) { if (!p || typeof p !== 'object' || Array.isArray(p)) return false if (p.role !== 'seeder') return false if (p.protocol !== 'bare-os-v1') return false if (!(Number.isInteger(p.keyCount) && p.keyCount >= 0 && p.keyCount <= 32)) return false if (!(Number.isInteger(p.activeIndex) && p.activeIndex >= 0 && p.activeIndex <= 31)) return false if (!(Number.isInteger(p.atMs) && p.atMs >= 0)) return false const primaryOk = p.primaryHex === null || (typeof p.primaryHex === 'string' && HEX_64_RE.test(p.primaryHex)) if (!primaryOk) return false if (!Array.isArray(p.failoverHexes) || p.failoverHexes.some((v) => typeof v !== 'string' || !HEX_64_RE.test(v))) { return false } if (p.failoverHexes.length > 31) return false if (p.primaryHex === null && p.keyCount !== 0) return false if (p.primaryHex !== null && p.keyCount < 1) return false if ('keyLabels' in p) { if (!Array.isArray(p.keyLabels) || p.keyLabels.some((v) => typeof v !== 'string' || !v || v.length > 64)) { return false } } if ('regionLabels' in p) { if ( !Array.isArray(p.regionLabels) || p.regionLabels.some((v) => typeof v !== 'string' || !v || v.length > 64) ) { return false } } return true } test('mbr-layout schema includes valid seeder examples', () => { const schema = JSON.parse(readFileSync(schemaPath, 'utf8')) assert.equal(schema.$schema, 'https://json-schema.org/draft/2020-12/schema') assert.equal(schema.type, 'object') assert.ok(Array.isArray(schema.examples) && schema.examples.length >= 1) for (const ex of schema.examples) { assert.equal(isValidExamplePayload(ex), true) } })