openpencil/tests/engine/vector/validate.test.ts
rcoenen db15d8d583
fix(tools): create vectors from SVG paths (#440)
- Accept SVG path data with tight curve-aware node bounds
- Validate complete VectorNetwork topology while preserving JSON input
- Reject empty and malformed paths before creating blank layers

Co-authored-by: Danila Poyarkov <dev@dannote.net>
2026-08-01 20:17:42 +03:00

76 lines
2.3 KiB
TypeScript

import { describe, test, expect } from 'bun:test'
import { validateVectorNetwork, type VectorNetwork } from '@open-pencil/core'
describe('validateVectorNetwork', () => {
test('valid network returns no errors', () => {
const network: VectorNetwork = {
vertices: [
{ x: 0, y: 0 },
{ x: 10, y: 10 }
],
segments: [{ start: 0, end: 1, tangentStart: { x: 0, y: 0 }, tangentEnd: { x: 0, y: 0 } }],
regions: []
}
expect(validateVectorNetwork(network)).toEqual([])
})
test('segments without tangents are valid (normalize handles them)', () => {
const network = {
vertices: [
{ x: 0, y: 0 },
{ x: 10, y: 10 }
],
segments: [{ start: 0, end: 1 }],
regions: []
} as VectorNetwork
expect(validateVectorNetwork(network)).toEqual([])
})
test('rejects segment with out-of-range start index', () => {
const network = {
vertices: [{ x: 0, y: 0 }],
segments: [{ start: 0, end: 5 }],
regions: []
} as VectorNetwork
const errors = validateVectorNetwork(network)
expect(errors.length).toBe(1)
expect(errors[0]).toContain('end index 5 out of range')
})
test('rejects missing vertices array', () => {
const network = { segments: [], regions: [] } as VectorNetwork
const errors = validateVectorNetwork(network)
expect(errors[0]).toContain('vertices must be an array')
})
test('rejects vertex with non-number coordinates', () => {
const network = {
vertices: [{ x: 'a', y: 0 }],
segments: [],
regions: []
} as VectorNetwork
const errors = validateVectorNetwork(network)
expect(errors[0]).toContain('x and y must be finite numbers')
})
test('rejects non-object input without throwing', () => {
expect(validateVectorNetwork(null)).toEqual(['network must be an object'])
expect(validateVectorNetwork('not a network')).toEqual(['network must be an object'])
})
test('rejects invalid region topology', () => {
const errors = validateVectorNetwork({
vertices: [
{ x: 0, y: 0 },
{ x: 10, y: 0 }
],
segments: [{ start: 0, end: 1 }],
regions: [{ windingRule: 'INVALID', loops: [[1]] }]
})
expect(errors).toContain('region[0]: windingRule must be NONZERO or EVENODD')
expect(errors).toContain('region[0].loop[0]: segment index 1 out of range')
})
})