import { beforeAll, describe, expect, it } from 'vitest' import { setCoreRuntime } from '@/core/runtime' import { cleanSvg, composeCleaningPreview } from '@/editor/core/cleaning' import { applyEdit, dependentsOf } from '@/editor/core/editOps' import { testCoreRuntime } from '../helpers/core' const DIRTY = '' + '' + '' beforeAll(async () => { setCoreRuntime(await testCoreRuntime()) }) function cleaned() { const out = cleanSvg(new TextEncoder().encode(DIRTY)) if (!out.ok || out.value.stored === null || out.value.sha256 === null) { throw new Error('not cleanable') } return { cleaning: out.value, drawing: { stored: out.value.stored, sha256: out.value.sha256 }, } } describe('cleanSvg', () => { it('reports a removal and a stored form without the handler', () => { const { cleaning } = cleaned() expect(cleaning.outcome).toBe('cleanable') expect(cleaning.style_removed).toBe(true) expect(cleaning.stored).not.toContain(' { const out = cleanSvg(new TextEncoder().encode('nope')) expect(out).toMatchObject({ ok: true, value: { outcome: 'rejected' } }) const active = cleanSvg( new TextEncoder().encode( '', ), ) expect(active).toMatchObject({ ok: true, value: { reason: 'svg_active_content' }, }) }) }) describe('composeCleaningPreview', () => { it('renders the stored drawing in a scope unique per preview and never the original', () => { const { drawing } = cleaned() const a = composeCleaningPreview(drawing) const b = composeCleaningPreview(drawing) if (!a.ok || !b.ok) throw new Error('compose failed') expect(a.value.scope).toMatch(/^fv\.view-[a-z2-7]{10}\.$/) expect(a.value.scope).not.toBe(b.value.scope) const svg = a.value.preview.canvas(a.value.viewId) if (!svg.ok) throw new Error(svg.kind) expect(svg.value).toContain(`id="${a.value.scope}r"`) expect(svg.value).not.toContain(' { it('refuses a remove of an unknown element with the core kind', () => { const text = JSON.stringify( (() => { const p = composeCleaningPreview(cleaned().drawing) if (!p.ok) throw new Error(p.kind) const document = JSON.parse(p.value.preview.document) p.value.preview.free() return document })(), ) expect( applyEdit(text, { op: 'remove', key: { kind: 'annotation', id: 'anno-none' }, }).ok, ).toBe(false) expect(dependentsOf(text, { kind: 'view', id: 'view-main' })).toMatchObject( { ok: true }, ) }) })