import type { ImportDiagnostic } from '@flighthq/importdiagnostics/contract'; import { getNodeChildAt, getNodeChildCount } from '@flighthq/node/contract'; import type { Node2D } from '@flighthq/scene2d/contract'; import { describe, expect, it } from 'vitest'; import type { SvgRegistry } from './contract.scene2d-formats-types.ts'; import { SvgElementKind } from './contract.scene2d-formats-types.ts'; import { svgContainerElementHandler } from './svgContainerElement.ts'; import { createScene2DFromSvgDocumentWithRegistry } from './svgDocument.ts'; import { svgGeometryElementHandler } from './svgGeometryElement.ts'; import { createScene2DFromSvgDocument } from './svgImport.ts'; import { createSvgRegistry, registerSvgElementHandler } from './svgRegistry.ts'; const MIXED = 'hi'; describe('appendSvgChildren', () => { // ★ DOCUMENT ORDER IS PAINT ORDER IN SVG, and unlike Lottie it is not reversed: the first element is drawn first and // therefore sits underneath. The walk's only job is to keep that, so the assertion is the child sequence. it('keeps document order, which is SVG paint order', () => { expect( childKinds(createScene2DFromSvgDocument('t')), ).toEqual(['Shape', 'Shape', 'TextLabel']); }); }); describe('applySvgElementAppearance', () => { it('lands group opacity on the node alpha', () => { const group = getNodeChildAt( createScene2DFromSvgDocument(''), 0, ) as Node2D; expect(group.alpha).toBe(0.5); }); }); describe('applySvgElementClipFamily', () => { // ★ DECLINING THE CLIP FAMILY MEANS NO CLIPPING, WHICH IS THE POINT OF MAKING IT ONE. The same document clips under the // zero-config entry and is unclipped through a registry with an empty clip family — and it says nothing while doing so, // because a caller who did not register a reader was not asking to be warned about what it would have read. it('clips when the family is registered and does not when it is absent', () => { const svg = ''; expect((getNodeChildAt(createScene2DFromSvgDocument(svg), 0) as Node2D).clip).not.toBeNull(); const diagnostics: ImportDiagnostic[] = []; const root = createScene2DFromSvgDocumentWithRegistry(svg, containerAndGeometry(), diagnostics); expect((getNodeChildAt(root, 0) as Node2D).clip).toBeNull(); expect(diagnostics).toEqual([]); }); // And an unresolvable reference is likewise silent without the family: there is nothing to resolve it against. it('reports nothing for an unresolved clip reference when the family is absent', () => { const diagnostics: ImportDiagnostic[] = []; createScene2DFromSvgDocumentWithRegistry( '', containerAndGeometry(), diagnostics, ); expect(diagnostics).toEqual([]); }); }); describe('createScene2DFromSvgDocumentWithRegistry', () => { // ★ THE SELECTIVE ENTRY READS WHAT IT IS GIVEN AND NOTHING MORE. An empty registry still parses and validates the // document — it simply produces no children, and reports each element it could not claim — which is what makes a // partial family yield a smaller scene rather than a broken one. it('produces no children for an empty registry, and says so per element', () => { const diagnostics: ImportDiagnostic[] = []; const root = createScene2DFromSvgDocumentWithRegistry(MIXED, createSvgRegistry(), diagnostics); expect(childKinds(root)).toEqual([]); expect(diagnostics.map((entry) => entry.kind)).toEqual(['svg.unknown-element', 'svg.unknown-element']); }); // The middle case is the one that matters: registering two families reads those and reports only the rest, so cost // and coverage move together. it('reads exactly the registered families and reports only the unclaimed ones', () => { const diagnostics: ImportDiagnostic[] = []; const root = createScene2DFromSvgDocumentWithRegistry(MIXED, containerAndGeometry(), diagnostics); expect(childKinds(root)).toEqual(['DisplayObject']); expect(getNodeChildCount(getNodeChildAt(root, 0) as Node2D)).toBe(1); expect(diagnostics.map((entry) => entry.kind)).toEqual(['svg.unknown-element']); }); it('rejects a document that is not SVG at all, exactly as the zero-config entry does', () => { const diagnostics: ImportDiagnostic[] = []; const root = createScene2DFromSvgDocumentWithRegistry('', containerAndGeometry(), diagnostics); expect(childKinds(root)).toEqual([]); expect(diagnostics.map((entry) => entry.kind)).toEqual(['svg.invalid-document']); }); }); describe('createSvgElementNode', () => { // The dispatch: an element whose NAME maps to no kind is unknown, and one whose kind has no handler is also unknown — // two different causes with the same honest answer, since either way nothing can read it. it('reports an element no registered family claims', () => { const diagnostics: ImportDiagnostic[] = []; createScene2DFromSvgDocumentWithRegistry( '', createSvgRegistry(), diagnostics, ); expect(diagnostics.map((entry) => entry.kind)).toEqual(['svg.unknown-element']); }); }); describe('reportRemainingUnsupportedSvgElements', () => { // ★ REPORTED ONCE PER ELEMENT NAME THE IMPORTER NEVER REACHED. These are features SVG defines and Flight does not // read, and they are found by sweeping the document rather than by the walk, because the walk never visits them. it('names each unsupported feature the document declares', () => { const diagnostics: ImportDiagnostic[] = []; createScene2DFromSvgDocument('', diagnostics); expect(diagnostics.map((entry) => entry.kind)).toEqual(['svg.unsupported-filter', 'svg.unsupported-foreignObject']); }); }); function childKinds(root: Readonly): string[] { const kinds: string[] = []; for (let index = 0; index < getNodeChildCount(root); index++) { kinds.push((getNodeChildAt(root, index) as Node2D).kind); } return kinds; } function containerAndGeometry(): SvgRegistry { const registry = createSvgRegistry(); registerSvgElementHandler(registry, SvgElementKind.Container, svgContainerElementHandler); registerSvgElementHandler(registry, SvgElementKind.Geometry, svgGeometryElementHandler); return registry; }