1import assert from 'node:assert/strict'2import test from 'node:test'3import {4 decodeText,5 documentTitle,6 encodeText,7 MAX_IMAGE_URL_LENGTH,8 normalizeMetadata,9 normalizePresentation,10 packedSvgUrl,11} from '../src/state.js'1213test('uses the Kroki-compatible zlib + base64url encoding', () => {14 const source = 'digraph G {Hello->World}\n'15 assert.equal(encodeText(source), 'eNpLyUwvSizIUHBXqPZIzcnJ17ULzy_KSanlAgB1EAjQ')16})1718test('round trips Unicode text', () => {19 const source = 'Alice -> Bob: Grüezi 👋 こんにちは'20 assert.equal(decodeText(encodeText(source)), source)21})2223test('validates structured presentation values', () => {24 assert.deepEqual(normalizePresentation(), { background: '', padding: 0, frame: false, appearance: 'raw' })25 assert.throws(() => normalizePresentation({ background: 'red' }), /presentation/)26 assert.throws(() => normalizePresentation({ padding: 257 }), /presentation/)27 assert.throws(() => normalizePresentation({ appearance: 'sepia' }), /presentation/)28})2930test('validates metadata values', () => {31 assert.deepEqual(normalizeMetadata(), { title: '', description: '' })32 assert.throws(() => normalizeMetadata({ title: 42 }), /metadata/)33})3435test('packs a complete editable SVG into a self-contained image URL', () => {36 const source = '<svg xmlns="http://www.w3.org/2000/svg" data-dz-document="1"><text>hello</text></svg>'37 const url = new URL(packedSvgUrl('https://diagram.zip', source))38 assert.match(url.pathname, /^\/svg\/[A-Za-z0-9_-]+$/)39 assert.equal(decodeText(url.pathname.slice('/svg/'.length)), source)40})4142test('keeps shared image URLs below the edge request limit', () => {43 assert.equal(MAX_IMAGE_URL_LENGTH, 15_000)44})4546test('rejects packed SVG URLs that exceed the edge request limit', () => {47 let seed = 0x1234567848 let payload = ''49 for (let index = 0; index < 30_000; index++) {50 seed = (seed * 1664525 + 1013904223) >>> 051 payload += String.fromCharCode(33 + seed % 90)52 }53 assert.throws(54 () => packedSvgUrl('https://diagram.zip', `<svg xmlns="http://www.w3.org/2000/svg"><metadata>${payload}</metadata></svg>`),55 /too large for a packed SVG URL/,56 )57})5859test('uses the diagram title as the unbranded zip document title', () => {60 assert.equal(documentTitle('Shared task flow'), 'Shared task flow.zip')61 assert.equal(documentTitle(' '), 'untitled.zip')62})