Files
clide/test/draw/draw_dispatch_test.dart
T
jpmschweitzerandClaude Opus 4.8 64d77ec5dc feat(draw): d2 diagram template — compile d2 source to SVG (T-494)
The d2 drawing template compiles a diagram's source to SVG through the d2
binary (resolved via the D-104 path layer), then paints it with the same
renderer the svg card uses. `clide draw --file x.d2` infers the type from
the extension; `.svg` files render directly. Template handlers now return
a DrawResult so a compile failure or an unresolved d2 surface as an honest
userError with an install hint, not a generic "no SVG". Real d2 0.7.1
verified end to end.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-29 11:19:48 +02:00

52 lines
2.3 KiB
Dart

import 'package:clide/src/draw/draw_dispatch.dart';
import 'package:clide/src/draw/draw_doc.dart';
import 'package:test/test.dart';
void main() {
DrawingFileReader reader(Map<String, String> files) =>
(p) async => files[p];
Future<DrawResult> resolve(DrawingCardDoc doc, DrawingRegistry reg, {Map<String, String> files = const {}}) =>
resolveDrawingSvg(doc, reg, readFile: reader(files));
group('resolveDrawingSvg', () {
test('primitive: inline svg passes through', () async {
final r = await resolve(parseDrawingCardDoc({'svg': '<svg id="x"/>'})!, DrawingRegistry());
expect((r as DrawOk).svg, '<svg id="x"/>');
});
test('primitive: svgPath is read via the injected reader', () async {
final r = await resolve(parseDrawingCardDoc({'svgPath': 'd.svg'})!, DrawingRegistry(), files: {'d.svg': '<svg id="file"/>'});
expect((r as DrawOk).svg, '<svg id="file"/>');
});
test('primitive: an unreadable svgPath is an honest error', () async {
final r = await resolve(parseDrawingCardDoc({'svgPath': 'missing.svg'})!, DrawingRegistry());
expect(r, isA<DrawErr>());
expect((r as DrawErr).message, contains('missing.svg'));
});
test('primitive: no source at all is an error', () async {
final r = await resolve(parseDrawingCardDoc(const {})!, DrawingRegistry());
expect(r, isA<DrawErr>());
});
test('template: an unknown template is an error', () async {
final r = await resolve(parseDrawingCardDoc({'template': 'd2', 'source': 'a -> b'})!, DrawingRegistry());
expect(r, isA<DrawErr>());
expect((r as DrawErr).message, contains('d2'));
});
test('template: a registered handler lowers the doc to SVG', () async {
final reg = DrawingRegistry()..register('d2', (doc) async => DrawOk('<svg data-src="${doc.fields['source']}"/>'));
final r = await resolve(parseDrawingCardDoc({'template': 'd2', 'source': 'a -> b'})!, reg);
expect((r as DrawOk).svg, '<svg data-src="a -> b"/>');
});
test('template: a handler error propagates with its message', () async {
final reg = DrawingRegistry()..register('d2', (_) async => const DrawErr('d2 not found'));
final r = await resolve(parseDrawingCardDoc({'template': 'd2'})!, reg);
expect((r as DrawErr).message, contains('d2 not found'));
});
});
}