Repository navigation
Expand file tree
/
Copy pathmermaid-types.test.js
More file actions
720 lines (639 loc) · 25.7 KB
/
Copy pathmermaid-types.test.js
File metadata and controls
720 lines (639 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
import { test } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { createRequire } from "node:module";
import { pathToFileURL } from "node:url";
import { buildHtml } from "../src/shared.js";
import { transformPages } from "../../shared/mx-xml.js";
// Mermaid in, editable draw.io out: every Mermaid diagram type draw.io
// converts, run through the converter on viewer.diagrams.net in a headless
// Chrome, Edge or Chromium, twice: by the app server's viewer
// (convertMermaidToXml in the app's HTML, what a chat shows) and by the
// plugins' scripts/mermaid-to-drawio.js (the desktop CLI's conversion, for
// the skill without draw.io Desktop). For each type the result must be
// native draw.io cells: no image of the diagram, images only as icons, every
// label in a cell's own text and none locked, connectors attached to the
// shapes they join. What the converter doesn't do yet (drawio-mermaid) is
// written down per type as the most it may leave undone, so a regression
// fails and a fix upstream passes.
//
// Needs a browser and the network: without a browser (CHROME_PATH, or the
// usual install paths) or with DRAWIO_TEST_BROWSER=off the tests are skipped,
// and so they are when the converter can't be loaded. MERMAID_TYPES_REPORT=
// <file> writes what was found per type as JSON.
const require = createRequire(import.meta.url);
const converter = require("../../plugins/claude-code/skills/drawio/scripts/mermaid-to-drawio.js");
const referenceText = fs.readFileSync(new URL("../../shared/mermaid-reference.md", import.meta.url), "utf-8");
// type: the samples, the labels that must each be the text of a cell, the
// most edges that may lack an end (loose), the most images (icons), and
// [container, child] labels where the child must be inside the container.
// Most samples are the reference's examples (shared/mermaid-reference.md).
const TYPES =
[
{
type: "flowchart",
mermaid: "flowchart TD\n A[Start] --> B{Decision?}\n B -->|Yes| C[Do thing]\n B -->|No| D[Skip]\n C --> E((End))\n D --> E",
labels: ["Start", "Decision?", "Do thing", "Skip", "End", "Yes", "No"]
},
{
type: "flowchart with subgraphs",
mermaid: "flowchart LR\n subgraph Backend\n API[API server] --> DB[(Database)]\n end\n Web[Web app] -->|REST| API",
labels: ["Backend", "API server", "Database", "Web app", "REST"],
inside: [["Backend", "API server"], ["Backend", "Database"]]
},
{
type: "sequence",
mermaid: "sequenceDiagram\n participant U as User\n participant S as Server\n U->>S: Request\n S-->>U: Response\n Note right of S: Logged",
labels: ["User", "Server", "Request", "Response", "Logged"]
},
{
// drawio-mermaid: a message that creates a participant ends next to its head, not on it
type: "sequence with a created participant",
mermaid: "sequenceDiagram\n participant A as Alice\n create participant B as Bob\n A->>B: Hello Bob\n B-->>A: Hi",
labels: ["Alice", "Bob", "Hello Bob", "Hi"],
loose: 1
},
{
type: "class",
mermaid: "classDiagram\n class Animal {\n +String name\n +int age\n +eat() void\n }\n class Dog\n Animal <|-- Dog : inherits\n Dog \"1\" --> \"*\" Bone : has",
labels: ["Animal", "+String name", "+eat() void", "Dog", "Bone", "inherits", "has"],
inside: [["Animal", "+String name"]]
},
{
type: "state",
mermaid: "stateDiagram-v2\n [*] --> Idle\n Idle --> Running : start\n Running --> Idle : stop\n Running --> [*]\n state Running {\n [*] --> Working\n Working --> Waiting : block\n Waiting --> Working : unblock\n }",
labels: ["Idle", "Running", "start", "stop", "Working", "Waiting", "block", "unblock"],
inside: [["Running", "Working"], ["Running", "Waiting"]]
},
{
type: "er",
mermaid: "erDiagram\n CUSTOMER ||--o{ ORDER : places\n ORDER ||--|{ LINE-ITEM : contains\n CUSTOMER {\n string name\n string email PK\n }",
labels: ["CUSTOMER", "ORDER", "LINE-ITEM", "places", "contains", "email"],
inside: [["CUSTOMER", "email"]]
},
{
// The axis arrow and the dashed lines under the tasks are drawings, not connectors
type: "journey",
mermaid: "journey\n title Morning routine\n section Wake up\n Coffee: 5: Me\n Read news: 3: Me\n section Commute\n Drive: 2: Me, Traffic",
labels: ["Morning routine", "Wake up", "Commute", "Coffee", "Read news", "Drive"],
loose: 4
},
{
type: "pie",
mermaid: "pie showData title Browser share\n \"Chrome\" : 60\n \"Firefox\" : 20\n \"Safari\" : 20",
labels: ["Browser share", "Chrome", "Firefox", "Safari"]
},
{
type: "gantt",
mermaid: "gantt\n title Project timeline\n dateFormat YYYY-MM-DD\n section Phase 1\n Design : a1, 2025-01-01, 7d\n Build : after a1, 14d\n section Phase 2\n Test : 2025-01-25, 5d",
labels: ["Project timeline", "Phase 1", "Phase 2", "Design", "Build", "Test"]
},
{
type: "gitgraph",
mermaid: "gitGraph\n commit\n branch develop\n checkout develop\n commit\n commit\n checkout main\n merge develop",
labels: ["main", "develop"]
},
{
type: "mindmap",
mermaid: "mindmap\n root((Project))\n Frontend\n React\n CSS\n Backend\n Node\n DB",
labels: ["Project", "Frontend", "React", "CSS", "Backend", "Node", "DB"]
},
{
// drawio-mermaid: the lines from a period to its events and the axis are
// loose lines, and each period's label is a text cell laid over its box
type: "timeline",
mermaid: "timeline\n title Company history\n section 2020s\n 2021 : Founded\n 2022 : Series A\n : Launched product\n section 2030s\n 2030 : IPO",
labels: ["Company history", "2020s", "2021", "Founded", "Series A", "Launched product", "IPO"],
loose: 4
},
{
type: "quadrant",
mermaid: "quadrantChart\n title Reach vs Engagement\n x-axis Low --> High\n y-axis Low --> High\n quadrant-1 Stars\n quadrant-2 Question Marks\n quadrant-3 Dogs\n quadrant-4 Cash Cows\n Campaign A: [0.3, 0.6]\n Campaign B: [0.75, 0.85]",
labels: ["Reach vs Engagement", "Stars", "Question Marks", "Dogs", "Cash Cows", "Campaign A", "Campaign B"]
},
{
type: "requirement",
mermaid: "requirementDiagram\n requirement req1 {\n id: \"1\"\n text: \"The system shall...\"\n risk: high\n verifymethod: test\n }\n element user_story {\n type: \"story\"\n }\n user_story - satisfies -> req1",
labels: ["req1", "user_story", "satisfies"]
},
{
type: "sankey",
mermaid: "sankey-beta\nSource,Intermediate,10\nSource,Direct,5\nIntermediate,Sink,10",
labels: ["Source", "Intermediate", "Direct", "Sink"]
},
{
// The line of a line series is a drawing between its points
type: "xychart",
mermaid: "xychart-beta\n title \"Revenue\"\n x-axis [jan, feb, mar, apr]\n y-axis \"USD\" 0 --> 10000\n bar [2500, 5000, 7500, 9000]\n line [3000, 4500, 6500, 8500]",
labels: ["Revenue", "jan", "apr", "USD"],
loose: 1
},
{
type: "block",
mermaid: "block-beta\n columns 3\n A B C\n D[\"Wide\"]:2 E\n A --> D",
labels: ["A", "B", "C", "Wide", "E"]
},
{
type: "c4",
mermaid: "C4Context\n Person(user, \"User\")\n System(app, \"App\", \"Does things\")\n Rel(user, app, \"Uses\")",
labels: ["User", "App", "Does things", "Uses"]
},
{
// The service icons are images. drawio-mermaid: a service's name is a
// text cell under its icon, and the group is a frame, not a container
type: "architecture",
mermaid: "architecture-beta\n group cloud(cloud)[Cloud]\n service api(server)[API] in cloud\n service db(database)[DB] in cloud\n api:R --> L:db",
labels: ["Cloud", "API", "DB"],
images: 3
},
{
// drawio-mermaid: the curves are SVG images, not shapes; the axes are lines
type: "radar",
mermaid: "radar-beta\n title Skills\n axis js[\"JS\"], py[\"Python\"], go[\"Go\"]\n curve alice[\"Alice\"]{80, 60, 70}\n curve bob[\"Bob\"]{50, 90, 65}",
labels: ["Skills", "JS", "Python", "Go", "Alice", "Bob"],
loose: 3,
images: 2
},
{
type: "packet",
mermaid: "packet-beta\n 0-15: \"Source Port\"\n 16-31: \"Dest Port\"\n 32-63: \"Seq Number\"",
labels: ["Source Port", "Dest Port", "Seq Number"]
},
{
type: "venn",
mermaid: "venn-beta\n set A [\"Set A\"]\n set B [\"Set B\"]\n union A,B\n text A [\"only A\"]\n text A,B [\"shared\"]",
labels: ["Set A", "Set B", "only A", "shared"]
},
{
type: "treemap",
mermaid: "treemap-beta\n\"Category\"\n \"Leaf 1\": 40\n \"Leaf 2\": 60",
labels: ["Category", "Leaf 1", "Leaf 2"]
},
{
// drawio-mermaid: the branches are loose lines
type: "treeview",
mermaid: "treeView-beta\n \"Root\"\n \"Child 1\"\n \"Grandchild\"\n \"Child 2\"",
labels: ["Root", "Child 1", "Grandchild", "Child 2"],
loose: 7
},
{
// drawio-mermaid: the bones are loose lines
type: "ishikawa",
mermaid: "ishikawa-beta\n Main Problem\n Category\n Cause\n Sub-cause\n Another Category\n Cause",
labels: ["Main Problem", "Category", "Cause", "Sub-cause", "Another Category"],
loose: 5
},
{
type: "kanban",
mermaid: "kanban\n todo[To Do]\n task1[Write spec]@{ assigned: \"Alice\", priority: \"High\" }\n doing[In progress]\n task2[Build feature]\n done[Done]",
labels: ["To Do", "Write spec", "In progress", "Build feature", "Done"],
inside: [["To Do", "Write spec"], ["In progress", "Build feature"]]
},
{
// The participant icons are images
type: "zenuml",
mermaid: "zenuml\n @Actor User\n @Boundary Web\n @Control Service\n User -> Web: request\n Web -> Service: process()\n Service -> Web: result",
labels: ["User", "Web", "Service", "request", "process()", "result"],
images: 3
},
{
// The axes, the stage lines and the evolve arrow are drawings; the links
// are attached. drawio-mermaid: a component's name is a text cell beside it
type: "wardley",
mermaid: "wardley-beta\n title Tea Shop\n anchor Business [0.95, 0.63]\n component Cup of Tea [0.79, 0.61]\n component Kettle [0.43, 0.35] (inertia)\n Business -> Cup of Tea\n Cup of Tea -> Kettle\n evolve Kettle 0.62",
labels: ["Tea Shop", "Business", "Cup of Tea", "Kettle"],
loose: 6
},
{
// drawio-mermaid: the arrows between the frames are loose lines
type: "eventmodeling",
mermaid: "eventmodeling\n tf 01 ui CartUI\n tf 02 cmd AddItem\n tf 03 evt ItemAdded\n tf 04 rmo Cart",
labels: ["CartUI", "AddItem", "ItemAdded", "Cart"],
loose: 3
},
// Types draw.io converts that the reference doesn't teach
{
type: "swimlane",
mermaid: "swimlane-beta LR\n subgraph Customer\n Browse[Browse catalogue]\n Pay[Pay]\n end\n subgraph Warehouse\n Pick[Pick items]\n Ship[Ship order]\n end\n Browse --> Pay\n Pay --> Pick\n Pick --> Ship",
labels: ["Customer", "Warehouse", "Browse catalogue", "Pay", "Pick items", "Ship order"],
inside: [["Customer", "Pay"], ["Warehouse", "Ship order"]]
},
{
type: "usecase",
mermaid: "usecase-beta\ndirection LR\nactor Customer(\"Customer\")\nsystemBoundary \"Order system\"\n Checkout(\"Place order\")\nend\nCustomer --> Checkout",
labels: ["Customer", "Order system", "Place order"]
},
{
type: "agentflow",
mermaid: "agentflow-beta TB\n flow reviewer[\"Review Agent\"]\n changes[\"Gather changes\"]@{ shape: input }\n analyse[\"Analyse diff\"]@{ shape: task }\n ok[\"Clean?\"]@{ shape: decision }\n changes --> analyse --> ok\n end",
labels: ["Review Agent", "Gather changes", "Analyse diff", "Clean?"],
inside: [["Review Agent", "Analyse diff"]]
},
{
// drawio-mermaid: the tracks are loose lines, and the title isn't drawn
type: "railroad",
mermaid: "railroad-ebnf-beta\ntitle \"Sign\"\n\nsign = \"+\" | \"-\" ;",
labels: ["sign", "+", "-"],
loose: 6
},
{
// drawio-mermaid: the items are not inside their domains
type: "cynefin",
mermaid: "cynefin-beta\n title Incident Response\n\n complex\n \"Investigate root cause\"\n\n clear\n \"Restart service\"\n\n chaotic\n \"Page on-call immediately\"",
labels: ["Incident Response", "Investigate root cause", "Restart service", "Page on-call immediately"],
loose: 3
}
];
// The ```blocks of the reference, each of which must convert to cells
function referenceExamples()
{
var blocks = [];
var re = /```[a-z]*\n([\s\S]*?)```/g;
var m;
while ((m = re.exec(referenceText)) != null)
{
var text = m[1].trim();
// Fragments (a frontmatter alone, lines to add to a diagram) aren't diagrams
if (/^(---|%%|flowchart|graph|sequenceDiagram|classDiagram|stateDiagram|erDiagram|journey|pie|gantt|gitGraph|mindmap|timeline|quadrantChart|requirementDiagram|sankey|xychart|block|C4|architecture|radar|packet|venn|treemap|treeView|ishikawa|kanban|zenuml|wardley|eventmodeling)/.test(text) &&
!/^---[\s\S]*---$/.test(text))
{
blocks.push(text);
}
}
return blocks;
}
function decode(text)
{
return String(text == null ? "" : text)
.replace(/<br\s*\/?>/gi, " ")
.replace(/<[^>]*>/g, "")
.replace(/ /g, " ")
.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, "\"")
.replace(/'/g, "'").replace(/&#x([0-9a-f]+);/gi, function(_m, h)
{
return String.fromCharCode(parseInt(h, 16));
}).replace(/&/g, "&")
.replace(/\s+/g, " ").trim();
}
function styleOf(cell)
{
var map = {};
String(cell.style || "").split(";").forEach(function(part)
{
var i = part.indexOf("=");
if (part.length > 0)
{
map[i < 0 ? part : part.substring(0, i)] = (i < 0) ? "1" : part.substring(i + 1);
}
});
return map;
}
// What a converted diagram is made of, as far as editing it in draw.io goes
function inspect(xml)
{
var found = null;
transformPages(xml, function(graph)
{
var cells = [];
(function walk(cell)
{
cells.push(cell);
(cell.children || []).forEach(walk);
})(graph.getModel().root);
found = { cells: cells };
});
assert.ok(found != null, "no cells in the XML");
var cells = found.cells;
var vertices = cells.filter(function(c) { return c.vertex; });
var edges = cells.filter(function(c) { return c.edge; });
var images = vertices.filter(function(c)
{
var s = styleOf(c);
return s.shape === "image" || s.image != null;
});
return {
cells: cells,
vertices: vertices.length,
edges: edges.length,
loose: edges.filter(function(e) { return e.source == null || e.target == null; }).length,
images: images,
locked: cells.filter(function(c)
{
var s = styleOf(c);
return s.editable === "0" || s.locked === "1" || s.movable === "0" || s.deletable === "0";
}).length,
brokenGeometry: cells.filter(function(c)
{
var g = c.geometry;
return g != null && [g.x, g.y, g.width, g.height].some(function(v)
{
return v != null && !Number.isFinite(v);
});
}).length,
mermaidImage: /mermaidData/.test(xml) && /shape=image/.test(xml)
};
}
function cellWithText(cells, text)
{
return cells.find(function(c) { return decode(c.value) === text; }) ||
cells.find(function(c) { return decode(c.value).indexOf(text) >= 0; });
}
function isInside(container, cell)
{
for (var p = cell.parent; p != null; p = p.parent)
{
if (p === container)
{
return true;
}
}
return false;
}
// Runs in the app's page: the viewer's conversion, as a chat shows the diagram
function viewerConvert(texts)
{
var start = Date.now();
return new Promise(function waitForViewer(resolve)
{
if (typeof convertMermaidToXml === "function" && typeof mxMermaidToDrawio !== "undefined" &&
typeof Graph !== "undefined")
{
resolve();
}
else if (Date.now() - start > 60000)
{
resolve();
}
else
{
setTimeout(function() { waitForViewer(resolve); }, 50);
}
}).then(function()
{
if (typeof mxMermaidToDrawio === "undefined")
{
return null;
}
var results = [];
var chain = Promise.resolve();
texts.forEach(function(text)
{
chain = chain.then(function()
{
return convertMermaidToXml(text).then(function(xml)
{
results.push({ xml: xml });
}, function(e)
{
results.push({ error: e.message });
});
});
});
return chain.then(function() { return results; });
});
}
// Streamed Mermaid whose last partial is the final text: the preview's parse
// of it, then the final conversion, as a host that streams the input does
const STREAM_CASES = [
// The last line comes with the final text: the preview drops it
{ name: "last line", text: "flowchart TD\n A[Alpha] --> B[Beta]\n B --> C[Gamma]" },
// Only a newline after the last line: the preview's parse is reused
{ name: "newline", text: "flowchart TD\n D[Delta] --> E[Epsilon]\n" },
// A statement the parser doesn't know: nothing to draw
{ name: "empty", text: "classDiagram\n ???\n" }
];
// Runs in the app's page, after viewerConvert has loaded the fonts
function viewerStream(cases)
{
var results = [];
var chain = Promise.resolve();
cases.forEach(function(c)
{
chain = chain.then(function()
{
handleMermaidPartial(c.text);
var reused = lastConvertedMermaidText === c.text;
return convertMermaidToXml(c.text).then(function(xml)
{
results.push({ xml: xml, reused: reused });
}, function(e)
{
results.push({ error: e.message, reused: reused });
});
});
});
return chain.then(function() { return results; });
}
// Until the app's page (not the blank one before it) has loaded
async function pageLoaded(evaluate)
{
var start = Date.now();
while (Date.now() - start < 60000)
{
try
{
if (await evaluate(function()
{
return location.protocol === "file:" && document.readyState === "complete";
}))
{
return;
}
}
catch (e)
{
// The page navigated while it was asked
}
await new Promise(function(resolve) { setTimeout(resolve, 100); });
}
throw new Error("The app's page did not load");
}
// The MCP Apps host isn't there: the viewer's App gets nothing from it
const APP_STUB = "var App = function() {};" +
"App.prototype.connect = function() { return new Promise(function() {}); };" +
"App.prototype.getHostContext = function() { return null; };" +
"App.prototype.getHostCapabilities = function() { return {}; };" +
"App.prototype.getHostVersion = function() { return null; };" +
"App.prototype.sendSizeChanged = function() { return Promise.resolve(); };" +
"App.prototype.updateModelContext = function() { return Promise.resolve(); };";
async function convertAll()
{
if (/^(off|0|false|none)$/i.test(process.env.DRAWIO_TEST_BROWSER || ""))
{
return { skip: "DRAWIO_TEST_BROWSER is off" };
}
var browser = converter.findBrowser();
if (browser == null)
{
return { skip: "no Chrome, Edge or Chromium found (set CHROME_PATH)" };
}
var texts = TYPES.map(function(t) { return t.mermaid; });
var examples = referenceExamples();
var dir = fs.mkdtempSync(path.join(os.tmpdir(), "mermaid-types-"));
var htmlFile = path.join(dir, "viewer.html");
try
{
// The plugins' converter: the desktop CLI's conversion
var plugin;
try
{
plugin = await converter.convertMermaid(texts.concat(examples), { browser: browser });
}
catch (e)
{
return { skip: "the converter could not be loaded: " + e.message };
}
// The app server's viewer, from the HTML the server serves
fs.writeFileSync(htmlFile, buildHtml(APP_STUB, "", null, {}));
var cdp = converter.launch(browser);
var viewer;
var stream;
try
{
var evaluate;
try
{
evaluate = await converter.openPage(cdp, pathToFileURL(htmlFile).href);
}
catch (e)
{
if (e.code !== "NO_SANDBOX")
{
throw e;
}
await cdp.close();
cdp = converter.launch(browser, ["--no-sandbox"]);
evaluate = await converter.openPage(cdp, pathToFileURL(htmlFile).href);
}
await pageLoaded(evaluate);
// The class diagram first: the first conversion in a page is the one
// that has to wait for the diagram's font
var classText = TYPES.find(function(t) { return t.type === "class"; }).mermaid;
viewer = await evaluate(viewerConvert, [classText].concat(texts));
stream = viewer != null ? await evaluate(viewerStream, STREAM_CASES) : null;
}
finally
{
await cdp.close();
}
if (viewer == null)
{
return { skip: "the viewer's converter could not be loaded" };
}
return { plugin: plugin.slice(0, texts.length), examples: plugin.slice(texts.length),
firstViewer: viewer[0], viewer: viewer.slice(1), exampleTexts: examples, stream: stream };
}
finally
{
fs.rmSync(dir, { recursive: true, force: true });
}
}
const results = await convertAll();
const skip = results.skip || false;
const report = [];
function checkEditable(spec, result, where)
{
assert.ok(result != null, where + ": no result");
assert.equal(result.error, undefined, where + ": " + result.error);
var found = inspect(result.xml);
var row = { type: spec.type, path: where, vertices: found.vertices, edges: found.edges,
loose: found.loose, images: found.images.length, missing: [], outside: [] };
report.push(row);
assert.ok(found.vertices > 0, where + ": no shapes");
assert.equal(found.mermaidImage, false, where + ": the diagram is an image");
assert.equal(found.brokenGeometry, 0, where + ": shapes without a usable geometry");
assert.equal(found.locked, 0, where + ": cells that can't be edited, moved or deleted");
// Images are icons, never a picture of the diagram or of a part of it
// larger than an icon (radar curves are, see TYPES)
found.images.forEach(function(img)
{
var g = img.geometry || {};
assert.ok((spec.images || 0) > 0, where + ": an image cell " + g.width + "x" + g.height);
});
assert.ok(found.images.length <= (spec.images || 0),
where + ": " + found.images.length + " images, at most " + (spec.images || 0));
// Every label is the text of a cell, so it is edited in place
spec.labels.forEach(function(label)
{
if (cellWithText(found.cells, label) == null)
{
row.missing.push(label);
}
});
assert.deepEqual(row.missing, [], where + ": labels that are not the text of a cell");
// Connectors end on the shapes they join
assert.ok(found.loose <= (spec.loose || 0),
where + ": " + found.loose + " edges without both ends attached, at most " + (spec.loose || 0));
(spec.inside || []).forEach(function(pair)
{
var container = cellWithText(found.cells, pair[0]);
var child = cellWithText(found.cells, pair[1]);
if (container == null || child == null || !isInside(container, child))
{
row.outside.push(pair.join(" > "));
}
});
assert.deepEqual(row.outside, [], where + ": shapes not inside their container");
}
for (let i = 0; i < TYPES.length; i++)
{
const spec = TYPES[i];
test("Mermaid " + spec.type + " converts to editable draw.io cells", { skip: skip }, function()
{
checkEditable(spec, results.viewer[i], "viewer");
checkEditable(spec, results.plugin[i], "mermaid-to-drawio.js");
});
}
test("every example in the Mermaid reference converts to draw.io cells", { skip: skip }, function()
{
assert.ok(results.examples.length >= 28, results.examples.length + " examples");
results.examples.forEach(function(result, i)
{
var head = results.exampleTexts[i].split("\n")[0];
assert.equal(result.error, undefined, head + ": " + result.error);
assert.ok(inspect(result.xml).vertices > 0, head + ": no shapes");
});
});
test("the viewer measures Mermaid labels in the diagram's font, as the plugins' converter does", { skip: skip }, function()
{
// The first conversion in a page: drawio-mermaid adds the font's
// stylesheet itself and measures in a fallback font unless the viewer
// waits for the font first (Recursive is about 20 % wider)
var i = TYPES.findIndex(function(t) { return t.type === "class"; });
var width = function(xml)
{
return Number(/<mxGeometry [^>]*width="(\d+)"/.exec(xml)[1]);
};
assert.equal(results.firstViewer.error, undefined, results.firstViewer.error);
assert.equal(width(results.firstViewer.xml), width(results.plugin[i].xml));
assert.equal(width(results.viewer[i].xml), width(results.plugin[i].xml));
});
test("a Mermaid diagram without cells is an error in the viewer", { skip: skip }, function()
{
// Checked on the template: the viewer rejects a conversion without cells
var source = fs.readFileSync(new URL("../src/shared.js", import.meta.url), "utf-8");
assert.match(source, /if \(!mermaidXmlHasCells\(xml\)\)\s*\{\s*return Promise\.reject\(new Error\(MERMAID_NOTHING_TO_DRAW\)\);/);
});
test("a streamed Mermaid diagram ends as the final text, not as the preview's parse", { skip: skip }, function()
{
var byName = {};
STREAM_CASES.forEach(function(c, i)
{
byName[c.name] = results.stream[i];
});
// The preview parsed the text without its last line
assert.equal(byName["last line"].reused, false);
assert.equal(byName["last line"].error, undefined, byName["last line"].error);
assert.match(byName["last line"].xml, /Gamma/);
assert.equal(byName.newline.reused, true);
assert.match(byName.newline.xml, /Epsilon/);
// A preview without cells isn't kept, so the final conversion says so
assert.equal(byName.empty.reused, false);
assert.match(byName.empty.error || "", /^Nothing to draw/);
});
test("write the report", { skip: !process.env.MERMAID_TYPES_REPORT || skip }, function()
{
fs.writeFileSync(process.env.MERMAID_TYPES_REPORT, JSON.stringify(report, null, 1));
});