An SVG path whose first command is an arc (A) — or one whose leading commands push no anchor point — reaches the arc branch with an empty `points` array, so `points[points.length - 1].type` threw a TypeError and aborted the entire PPTX export. toPoints() documents that a malformed path should return [] (mirroring getSvgPathRange) so a single bad or LLM-hallucinated shape does not take down the export; this restores that contract with a `!lastPoint` guard. Adds a regression test for the arc-first case. Co-authored-by: ly-wang19 <ly-wang19@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: wyuc <wang-yc24@mails.tsinghua.edu.cn>
36 lines
1.5 KiB
TypeScript
36 lines
1.5 KiB
TypeScript
import { describe, test, expect, vi, beforeEach } from 'vitest';
|
|
import { toPoints, getSvgPathRange } from '@/lib/export/svg-path-parser';
|
|
|
|
describe('toPoints', () => {
|
|
beforeEach(() => {
|
|
// Silence the parser's warn log for malformed-path cases.
|
|
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
});
|
|
|
|
test('parses a valid M/L/Z path', () => {
|
|
const points = toPoints('M 0 0 L 1 0 L 1 1 L 0 1 Z');
|
|
expect(points.length).toBeGreaterThan(0);
|
|
expect(points[0]).toMatchObject({ type: 'M', x: 0, y: 0 });
|
|
});
|
|
|
|
test('returns [] for a malformed path so the export does not crash', () => {
|
|
// Real-world malformed path observed in an imported course manifest:
|
|
// upstream LLM produced "alert" instead of an "A" arc command.
|
|
const malformed = 'M 1 0.5 alert 0.5 0.5 0 1 1 0 0.5 A 0.5 0.5 0 1 1 1 0.5 Z';
|
|
expect(toPoints(malformed)).toEqual([]);
|
|
});
|
|
|
|
test('returns [] for an arc-first path instead of throwing', () => {
|
|
// A syntactically valid path whose first command is an arc (A): `points` is
|
|
// still empty when the arc is reached, so reading the previous point must not
|
|
// throw. Guards the documented "malformed path returns []" contract.
|
|
expect(toPoints('A 0.5 0.5 0 1 1 1 0.5')).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('getSvgPathRange', () => {
|
|
test('returns zero range for malformed path (existing tolerant behaviour)', () => {
|
|
expect(getSvgPathRange('not a path')).toEqual({ minX: 0, minY: 0, maxX: 0, maxY: 0 });
|
|
});
|
|
});
|