clone

a browser-first diagram editor with live svg previews, local drafts and encrypted, accountless sharing

main
diagramzip / apps / editor / test / details-document.test.js
details-document.test.js 2.1K
1import assert from 'node:assert/strict'2import test from 'node:test'3import {4 detailsDocumentFor,5 detailsStateWithTitle,6 parseDetailsDocument,7 serializeDetailsDocument,8} from '../src/details-document.js'910const state = {11 meta: { title: 'Connections', description: 'A small D2 example.' },12 options: { scale: 2, sketch: false },13 presentation: { appearance: 'dark-transparent', background: '', padding: 0, frame: false },14}1516test('round trips the schema-backed details document', () => {17 assert.deepEqual(parseDetailsDocument(serializeDetailsDocument(state)), {18 meta: state.meta,19 options: state.options,20 presentation: state.presentation,21 })22 assert.deepEqual(detailsDocumentFor(state), {23 title: 'Connections',24 description: 'A small D2 example.',25 options: state.options,26 presentation: state.presentation,27 })28})2930test('rejects malformed, incomplete, and extended details documents', () => {31 assert.throws(() => parseDetailsDocument('{'), /valid JSON/)32 assert.throws(() => parseDetailsDocument('{}'), /missing required property "title"/)33 assert.throws(() => parseDetailsDocument(JSON.stringify({34 ...detailsDocumentFor(state),35 extra: true,36 })), /unknown property "extra"/)37})3839test('communicates metadata and presentation restrictions', () => {40 assert.throws(() => parseDetailsDocument(JSON.stringify({41 ...detailsDocumentFor(state),42 title: 'x'.repeat(201),43 })), /at most 200/)44 assert.throws(() => parseDetailsDocument(JSON.stringify({45 ...detailsDocumentFor(state),46 presentation: { ...state.presentation, padding: 257 },47 })), /invalid appearance/)48 assert.throws(() => parseDetailsDocument(JSON.stringify({49 ...detailsDocumentFor(state),50 options: { layout: { mode: 'individual' } },51 })), /must be a string, number, or boolean/)52})5354test('updates the title without changing other details', () => {55 assert.deepEqual(detailsStateWithTitle(state, 'Renamed'), {56 meta: { title: 'Renamed', description: state.meta.description },57 options: state.options,58 presentation: state.presentation,59 })60 assert.throws(() => detailsStateWithTitle(state, 'x'.repeat(201)), /at most 200/)61})