@zyro-sec/merman 0.0.0-stage → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +29 -4
- package/script/layout-audit.ts +98 -0
- package/src/core/canvas.test.ts +203 -0
- package/src/core/canvas.ts +228 -0
- package/src/core/color/style.test.ts +24 -0
- package/src/core/color/style.ts +56 -0
- package/src/core/drawing.test.ts +116 -0
- package/src/core/drawing.ts +301 -0
- package/src/core/geometry.test.ts +112 -0
- package/src/core/geometry.ts +347 -0
- package/src/core/mermaid.ts +38 -0
- package/src/core/render-grid.ts +51 -0
- package/src/core/spatial.test.ts +87 -0
- package/src/core/spatial.ts +243 -0
- package/src/core/text-lines.ts +32 -0
- package/src/core/text.test.ts +33 -0
- package/src/core/text.ts +44 -0
- package/src/detect.ts +17 -0
- package/src/diagnostics.ts +16 -0
- package/src/flowchart/drawing.ts +340 -0
- package/src/flowchart/flowchart.test.ts +2800 -0
- package/src/flowchart/labels.test.ts +127 -0
- package/src/flowchart/labels.ts +144 -0
- package/src/flowchart/layout.ts +1427 -0
- package/src/flowchart/options.ts +12 -0
- package/src/flowchart/parser.ts +467 -0
- package/src/flowchart/render.ts +10 -0
- package/src/flowchart/routing.test.ts +368 -0
- package/src/flowchart/routing.ts +1542 -0
- package/src/flowchart/style.ts +79 -0
- package/src/flowchart/types.ts +65 -0
- package/src/gantt/diagram.test.ts +124 -0
- package/src/gantt/diagram.ts +8 -0
- package/src/gantt/drawing.ts +191 -0
- package/src/gantt/parser.ts +161 -0
- package/src/gantt/render-grid.ts +17 -0
- package/src/gantt/style.ts +36 -0
- package/src/gantt/types.ts +44 -0
- package/src/gitgraph/diagram.test.ts +183 -0
- package/src/gitgraph/diagram.ts +8 -0
- package/src/gitgraph/drawing.ts +234 -0
- package/src/gitgraph/parser.ts +219 -0
- package/src/gitgraph/render-grid.ts +17 -0
- package/src/gitgraph/style.ts +37 -0
- package/src/gitgraph/types.ts +36 -0
- package/src/layout-audit.test.ts +92 -0
- package/src/markdown.ts +336 -0
- package/src/palette.test.ts +103 -0
- package/src/palette.ts +75 -0
- package/src/plugin.ts +15 -0
- package/src/sequence/diagram.test.ts +757 -0
- package/src/sequence/diagram.ts +8 -0
- package/src/sequence/drawing.ts +287 -0
- package/src/sequence/options.ts +8 -0
- package/src/sequence/parser.ts +282 -0
- package/src/sequence/placement.test.ts +219 -0
- package/src/sequence/placement.ts +684 -0
- package/src/sequence/render-grid.ts +22 -0
- package/src/sequence/style.ts +81 -0
- package/src/sequence/types.ts +74 -0
- package/src/state/diagram.test.ts +1386 -0
- package/src/state/diagram.ts +8 -0
- package/src/state/drawing.ts +360 -0
- package/src/state/endpoint.ts +15 -0
- package/src/state/layout.test.ts +314 -0
- package/src/state/layout.ts +1134 -0
- package/src/state/note.ts +59 -0
- package/src/state/options.ts +11 -0
- package/src/state/parser.ts +224 -0
- package/src/state/render-grid.ts +22 -0
- package/src/state/routing.test.ts +693 -0
- package/src/state/routing.ts +1522 -0
- package/src/state/search.ts +103 -0
- package/src/state/style.test.ts +113 -0
- package/src/state/style.ts +52 -0
- package/src/state/types.ts +63 -0
- package/src/state/visible-model.test.ts +84 -0
- package/src/state/visible-model.ts +77 -0
- package/src/test/diagnostics.test.ts +129 -0
- package/src/test/diagram.ts +32 -0
- package/src/test/layout-audit/fixtures.ts +538 -0
- package/src/test/layout-audit/harness.ts +522 -0
- package/src/test/markdown.test.ts +474 -0
- package/src/timeline/diagram.test.ts +188 -0
- package/src/timeline/diagram.ts +8 -0
- package/src/timeline/drawing.ts +109 -0
- package/src/timeline/parser.ts +119 -0
- package/src/timeline/render-grid.ts +17 -0
- package/src/timeline/style.ts +36 -0
- package/src/timeline/types.ts +31 -0
- package/tsconfig.json +10 -0
- package/README.md +0 -3
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import { diagramTextWidth } from "../core/text.js"
|
|
3
|
+
import { flowchartEdgeLabelLayout } from "./labels.js"
|
|
4
|
+
import type { FlowchartDiagram, FlowchartNodeBounds } from "./types.js"
|
|
5
|
+
import { routeFlowchartEdges } from "./routing.js"
|
|
6
|
+
|
|
7
|
+
function bounds(id: string, left: number, top: number): FlowchartNodeBounds {
|
|
8
|
+
const width = 5
|
|
9
|
+
const height = 3
|
|
10
|
+
return {
|
|
11
|
+
id,
|
|
12
|
+
width,
|
|
13
|
+
height,
|
|
14
|
+
lines: [id],
|
|
15
|
+
left,
|
|
16
|
+
top,
|
|
17
|
+
centerX: left + Math.floor(width / 2),
|
|
18
|
+
centerY: top + Math.floor(height / 2),
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function diagram(direction: FlowchartDiagram["direction"], edges: FlowchartDiagram["edges"]): FlowchartDiagram {
|
|
23
|
+
return { direction, nodes: [], edges, subgraphs: [] }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function routeIntersectsBounds(
|
|
27
|
+
points: readonly { x: number; y: number }[],
|
|
28
|
+
nodeBounds: { left: number; top: number; width: number; height: number },
|
|
29
|
+
): boolean {
|
|
30
|
+
const right = nodeBounds.left + nodeBounds.width - 1
|
|
31
|
+
const bottom = nodeBounds.top + nodeBounds.height - 1
|
|
32
|
+
return points.slice(1).some((to, index) => {
|
|
33
|
+
const from = points[index]!
|
|
34
|
+
if (from.x === to.x) {
|
|
35
|
+
return (
|
|
36
|
+
from.x >= nodeBounds.left &&
|
|
37
|
+
from.x <= right &&
|
|
38
|
+
Math.max(from.y, to.y) >= nodeBounds.top &&
|
|
39
|
+
Math.min(from.y, to.y) <= bottom
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
return (
|
|
43
|
+
from.y >= nodeBounds.top &&
|
|
44
|
+
from.y <= bottom &&
|
|
45
|
+
Math.max(from.x, to.x) >= nodeBounds.left &&
|
|
46
|
+
Math.min(from.x, to.x) <= right
|
|
47
|
+
)
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
describe("flowchart routing", () => {
|
|
52
|
+
test("routes a simple horizontal edge from source port to target port", () => {
|
|
53
|
+
const edge = { from: "A", to: "B", label: "" }
|
|
54
|
+
const routes = routeFlowchartEdges(
|
|
55
|
+
diagram("LR", [edge]),
|
|
56
|
+
new Map([
|
|
57
|
+
["A", bounds("A", 0, 0)],
|
|
58
|
+
["B", bounds("B", 20, 0)],
|
|
59
|
+
]),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
expect(routes).toEqual([
|
|
63
|
+
{
|
|
64
|
+
edge,
|
|
65
|
+
points: [
|
|
66
|
+
{ x: 5, y: 1 },
|
|
67
|
+
{ x: 19, y: 1 },
|
|
68
|
+
],
|
|
69
|
+
},
|
|
70
|
+
])
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test("routes a simple reverse horizontal edge into the target right port", () => {
|
|
74
|
+
const edge = { from: "A", to: "B", label: "" }
|
|
75
|
+
const routes = routeFlowchartEdges(
|
|
76
|
+
diagram("RL", [edge]),
|
|
77
|
+
new Map([
|
|
78
|
+
["A", bounds("A", 20, 0)],
|
|
79
|
+
["B", bounds("B", 0, 0)],
|
|
80
|
+
]),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
expect(routes).toEqual([
|
|
84
|
+
{
|
|
85
|
+
edge,
|
|
86
|
+
points: [
|
|
87
|
+
{ x: 19, y: 1 },
|
|
88
|
+
{ x: 5, y: 1 },
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
])
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test("routes horizontal back-edges above forward lanes", () => {
|
|
95
|
+
const edge = { from: "B", to: "A", label: "" }
|
|
96
|
+
const routes = routeFlowchartEdges(
|
|
97
|
+
diagram("LR", [edge]),
|
|
98
|
+
new Map([
|
|
99
|
+
["A", bounds("A", 0, 0)],
|
|
100
|
+
["B", bounds("B", 20, 0)],
|
|
101
|
+
]),
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
expect(routes).toEqual([
|
|
105
|
+
{
|
|
106
|
+
edge,
|
|
107
|
+
points: [
|
|
108
|
+
{ x: 22, y: -1 },
|
|
109
|
+
{ x: 22, y: -4 },
|
|
110
|
+
{ x: 2, y: -4 },
|
|
111
|
+
{ x: 2, y: -1 },
|
|
112
|
+
],
|
|
113
|
+
},
|
|
114
|
+
])
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test("routes parallel horizontal edges on independent lanes", () => {
|
|
118
|
+
const edges = [
|
|
119
|
+
{ from: "A", to: "B", label: "first" },
|
|
120
|
+
{ from: "A", to: "B", label: "second" },
|
|
121
|
+
]
|
|
122
|
+
const routes = routeFlowchartEdges(
|
|
123
|
+
diagram("LR", edges),
|
|
124
|
+
new Map([
|
|
125
|
+
["A", bounds("A", 0, 0)],
|
|
126
|
+
["B", bounds("B", 20, 0)],
|
|
127
|
+
]),
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
const laneYs = routes.map(
|
|
131
|
+
(route) => route.points.slice(1).find((point, index) => point.y === route.points[index]!.y)?.y,
|
|
132
|
+
)
|
|
133
|
+
expect(routes).toHaveLength(2)
|
|
134
|
+
expect(new Set(routes.map((route) => JSON.stringify(route.points))).size).toBe(2)
|
|
135
|
+
expect(new Set(laneYs).size).toBe(2)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test("spaces parallel horizontal lanes for multiline labels", () => {
|
|
139
|
+
const edges = [
|
|
140
|
+
{ from: "A", to: "B", label: "first" },
|
|
141
|
+
{ from: "A", to: "B", label: "second 1<br/>second 2<br/>second 3" },
|
|
142
|
+
{ from: "A", to: "B", label: "third 1<br/>third 2<br/>third 3" },
|
|
143
|
+
]
|
|
144
|
+
const routes = routeFlowchartEdges(
|
|
145
|
+
diagram("LR", edges),
|
|
146
|
+
new Map([
|
|
147
|
+
["A", bounds("A", 0, 0)],
|
|
148
|
+
["B", bounds("B", 20, 0)],
|
|
149
|
+
]),
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
expect(routes[2]!.points[1]!.y).toBeGreaterThan(routes[1]!.points[1]!.y + 3)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test("routes horizontal fan-out through a shared bus lane", () => {
|
|
156
|
+
const edges = [
|
|
157
|
+
{ from: "A", to: "B", label: "" },
|
|
158
|
+
{ from: "A", to: "C", label: "" },
|
|
159
|
+
]
|
|
160
|
+
const routes = routeFlowchartEdges(
|
|
161
|
+
diagram("LR", edges),
|
|
162
|
+
new Map([
|
|
163
|
+
["A", bounds("A", 0, 6)],
|
|
164
|
+
["B", bounds("B", 20, 0)],
|
|
165
|
+
["C", bounds("C", 20, 12)],
|
|
166
|
+
]),
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
expect(routes.map((route) => route.points)).toEqual([
|
|
170
|
+
[
|
|
171
|
+
{ x: 5, y: 7 },
|
|
172
|
+
{ x: 8, y: 7 },
|
|
173
|
+
{ x: 8, y: 1 },
|
|
174
|
+
{ x: 19, y: 1 },
|
|
175
|
+
],
|
|
176
|
+
[
|
|
177
|
+
{ x: 5, y: 7 },
|
|
178
|
+
{ x: 8, y: 7 },
|
|
179
|
+
{ x: 8, y: 13 },
|
|
180
|
+
{ x: 19, y: 13 },
|
|
181
|
+
],
|
|
182
|
+
])
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
test("routes each horizontal edge once when fan-in and fan-out overlap", () => {
|
|
186
|
+
const edges = [
|
|
187
|
+
{ from: "A", to: "C", label: "" },
|
|
188
|
+
{ from: "A", to: "D", label: "" },
|
|
189
|
+
{ from: "B", to: "C", label: "" },
|
|
190
|
+
{ from: "B", to: "D", label: "" },
|
|
191
|
+
]
|
|
192
|
+
const routes = routeFlowchartEdges(
|
|
193
|
+
diagram("LR", edges),
|
|
194
|
+
new Map([
|
|
195
|
+
["A", bounds("A", 0, 0)],
|
|
196
|
+
["B", bounds("B", 0, 12)],
|
|
197
|
+
["C", bounds("C", 24, 0)],
|
|
198
|
+
["D", bounds("D", 24, 12)],
|
|
199
|
+
]),
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
expect(routes).toHaveLength(edges.length)
|
|
203
|
+
expect(routes.map((route) => `${route.edge.from}->${route.edge.to}`).sort()).toEqual([
|
|
204
|
+
"A->C",
|
|
205
|
+
"A->D",
|
|
206
|
+
"B->C",
|
|
207
|
+
"B->D",
|
|
208
|
+
])
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
test("routes vertical back-edges around the left side", () => {
|
|
212
|
+
const edge = { from: "B", to: "A", label: "" }
|
|
213
|
+
const routes = routeFlowchartEdges(
|
|
214
|
+
diagram("TD", [edge]),
|
|
215
|
+
new Map([
|
|
216
|
+
["A", bounds("A", 0, 0)],
|
|
217
|
+
["B", bounds("B", 0, 12)],
|
|
218
|
+
]),
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
expect(routes).toEqual([
|
|
222
|
+
{
|
|
223
|
+
edge,
|
|
224
|
+
points: [
|
|
225
|
+
{ x: -1, y: 13 },
|
|
226
|
+
{ x: -4, y: 13 },
|
|
227
|
+
{ x: -4, y: 1 },
|
|
228
|
+
{ x: -1, y: 1 },
|
|
229
|
+
],
|
|
230
|
+
},
|
|
231
|
+
])
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
test("routes self edges below the source node", () => {
|
|
235
|
+
const edge = { from: "A", to: "A", label: "" }
|
|
236
|
+
const routes = routeFlowchartEdges(diagram("TD", [edge]), new Map([["A", bounds("A", 0, 0)]]))
|
|
237
|
+
|
|
238
|
+
expect(routes).toEqual([
|
|
239
|
+
{
|
|
240
|
+
edge,
|
|
241
|
+
points: [
|
|
242
|
+
{ x: 5, y: 1 },
|
|
243
|
+
{ x: 8, y: 1 },
|
|
244
|
+
{ x: 8, y: 4 },
|
|
245
|
+
{ x: 2, y: 4 },
|
|
246
|
+
{ x: 2, y: 3 },
|
|
247
|
+
],
|
|
248
|
+
},
|
|
249
|
+
])
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
test("routes same-column horizontal-flow edges through vertical ports", () => {
|
|
253
|
+
const edge = { from: "A", to: "B", label: "rollback" }
|
|
254
|
+
const routes = routeFlowchartEdges(
|
|
255
|
+
diagram("LR", [edge]),
|
|
256
|
+
new Map([
|
|
257
|
+
["A", bounds("A", 20, 0)],
|
|
258
|
+
["B", bounds("B", 20, 8)],
|
|
259
|
+
]),
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
expect(routes).toEqual([
|
|
263
|
+
{
|
|
264
|
+
edge,
|
|
265
|
+
points: [
|
|
266
|
+
{ x: 22, y: 3 },
|
|
267
|
+
{ x: 22, y: 7 },
|
|
268
|
+
],
|
|
269
|
+
},
|
|
270
|
+
])
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
test("routes overlapping horizontal-flow columns through vertical ports", () => {
|
|
274
|
+
const edge = { from: "A", to: "B", label: "merge" }
|
|
275
|
+
const routes = routeFlowchartEdges(
|
|
276
|
+
diagram("RL", [edge]),
|
|
277
|
+
new Map([
|
|
278
|
+
["A", bounds("A", 1, 8)],
|
|
279
|
+
["B", bounds("B", 0, 0)],
|
|
280
|
+
]),
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
expect(routes).toEqual([
|
|
284
|
+
{
|
|
285
|
+
edge,
|
|
286
|
+
points: [
|
|
287
|
+
{ x: 2, y: 7 },
|
|
288
|
+
{ x: 2, y: 3 },
|
|
289
|
+
],
|
|
290
|
+
},
|
|
291
|
+
])
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
test("does not route a fallback through its own source node", () => {
|
|
295
|
+
const labeled = { from: "A", to: "B", label: "route" }
|
|
296
|
+
const crossing = { from: "C", to: "D", label: "" }
|
|
297
|
+
const nodeBounds = new Map([
|
|
298
|
+
["A", bounds("A", 0, 0)],
|
|
299
|
+
["B", bounds("B", 100, 0)],
|
|
300
|
+
["C", bounds("C", 48, -12)],
|
|
301
|
+
["D", bounds("D", 48, 12)],
|
|
302
|
+
])
|
|
303
|
+
const routes = routeFlowchartEdges(diagram("LR", [labeled, crossing]), nodeBounds, undefined, new Map())
|
|
304
|
+
const route = routes.find((candidate) => candidate.edge === labeled)!
|
|
305
|
+
|
|
306
|
+
expect(routeIntersectsBounds(route.points, nodeBounds.get("A")!)).toBe(false)
|
|
307
|
+
expect(routeIntersectsBounds(route.points, nodeBounds.get("B")!)).toBe(false)
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
test("ignores zero-width blank label interiors as route obstacles", () => {
|
|
311
|
+
const blankLabel = { from: "A", to: "B", label: "<br/>" }
|
|
312
|
+
const crossing = { from: "C", to: "D", label: "" }
|
|
313
|
+
const routes = routeFlowchartEdges(
|
|
314
|
+
diagram("TD", [blankLabel, crossing]),
|
|
315
|
+
new Map([
|
|
316
|
+
["A", bounds("A", 0, 0)],
|
|
317
|
+
["B", bounds("B", 0, 100)],
|
|
318
|
+
["C", bounds("C", -20, 50)],
|
|
319
|
+
["D", bounds("D", 20, 50)],
|
|
320
|
+
]),
|
|
321
|
+
(edge) => (edge === blankLabel ? "TD" : "LR"),
|
|
322
|
+
new Map(),
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
expect(routes.find((route) => route.edge === blankLabel)!.points).toEqual([
|
|
326
|
+
{ x: 2, y: 3 },
|
|
327
|
+
{ x: 2, y: 99 },
|
|
328
|
+
])
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
test("checks earlier labels against finalized later fallback routes", () => {
|
|
332
|
+
const edges = [
|
|
333
|
+
{ from: "C", to: "B", label: "alpha" },
|
|
334
|
+
{ from: "A", to: "F", label: "beta long" },
|
|
335
|
+
{ from: "C", to: "D", label: "gamma" },
|
|
336
|
+
{ from: "A", to: "B", label: "" },
|
|
337
|
+
]
|
|
338
|
+
const directions = ["TD", "RL", "LR", "BT"] as const
|
|
339
|
+
const routes = routeFlowchartEdges(
|
|
340
|
+
diagram("LR", edges),
|
|
341
|
+
new Map([
|
|
342
|
+
["A", bounds("A", -24, 6)],
|
|
343
|
+
["B", bounds("B", 48, 24)],
|
|
344
|
+
["C", bounds("C", -24, 24)],
|
|
345
|
+
["D", bounds("D", -24, -18)],
|
|
346
|
+
["F", bounds("F", -16, -6)],
|
|
347
|
+
]),
|
|
348
|
+
(edge) => directions[edges.indexOf(edge)]!,
|
|
349
|
+
new Map(),
|
|
350
|
+
)
|
|
351
|
+
const labeled = routes.find((route) => route.edge === edges[0])!
|
|
352
|
+
const laterFallback = routes.find((route) => route.edge === edges[3])!
|
|
353
|
+
const label = flowchartEdgeLabelLayout(labeled.points, labeled.edge.label, diagramTextWidth)
|
|
354
|
+
|
|
355
|
+
expect(labeled.points).toEqual([
|
|
356
|
+
{ x: -19, y: 25 },
|
|
357
|
+
{ x: 47, y: 25 },
|
|
358
|
+
])
|
|
359
|
+
expect(
|
|
360
|
+
routeIntersectsBounds(laterFallback.points, {
|
|
361
|
+
left: label.point.x + 1,
|
|
362
|
+
top: label.point.y,
|
|
363
|
+
width: label.width - 2,
|
|
364
|
+
height: label.height,
|
|
365
|
+
}),
|
|
366
|
+
).toBe(false)
|
|
367
|
+
})
|
|
368
|
+
})
|