Files
clide/test/draw/d2_template_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

60 lines
2.4 KiB
Dart

import 'package:clide/src/draw/d2_template.dart';
import 'package:clide/src/draw/draw_dispatch.dart';
import 'package:clide/src/draw/draw_doc.dart';
import 'package:test/test.dart';
void main() {
DrawingCardDoc d2doc(Object? source) => DrawingCardDoc(template: 'd2', fields: {'template': 'd2', if (source != null) 'source': source});
group('d2TemplateHandler', () {
test('compiles the source field via the injected compiler', () async {
final h = d2TemplateHandler(compile: (s) async => DrawOk('<svg data-s="$s"/>'));
final r = await h(d2doc('a -> b'));
expect((r as DrawOk).svg, '<svg data-s="a -> b"/>');
});
test('a missing source is an honest error', () async {
final h = d2TemplateHandler(compile: (_) async => const DrawOk('x'));
expect(await h(d2doc(null)), isA<DrawErr>());
});
test('a blank source is an honest error', () async {
final h = d2TemplateHandler(compile: (_) async => const DrawOk('x'));
expect(await h(d2doc(' ')), isA<DrawErr>());
});
test('propagates a compiler error message', () async {
final h = d2TemplateHandler(compile: (_) async => const DrawErr('boom'));
expect((await h(d2doc('a -> b')) as DrawErr).message, 'boom');
});
});
group('d2CompileViaBinary', () {
Future<DrawResult> compile({String? bin = '/x/d2', D2Run? run}) =>
d2CompileViaBinary('a -> b', resolveD2: () => bin, run: run ?? (exe, src) async => (code: 0, out: '<svg/>', err: 'success:'));
test('unresolved d2 hints how to install', () async {
expect((await compile(bin: null) as DrawErr).message, contains('d2 not found'));
});
test('a clean compile returns the SVG (stderr ignored)', () async {
expect((await compile() as DrawOk).svg, '<svg/>');
});
test('a non-zero exit is a compile error carrying stderr', () async {
final r = await compile(run: (exe, src) async => (code: 1, out: '', err: 'line 2: syntax error'));
expect((r as DrawErr).message, contains('syntax error'));
});
test('empty output is an error', () async {
final r = await compile(run: (exe, src) async => (code: 0, out: ' ', err: ''));
expect(r, isA<DrawErr>());
});
test('a spawn failure is reported honestly', () async {
final r = await compile(run: (exe, src) async => throw 'ENOENT');
expect((r as DrawErr).message, contains('could not run d2'));
});
});
}