@zyro-sec/merman 1.0.2 → 1.0.3

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.
Files changed (91) hide show
  1. package/package.json +2 -2
  2. package/script/layout-audit.ts +98 -98
  3. package/src/core/canvas.ts +228 -228
  4. package/src/core/color/style.ts +56 -56
  5. package/src/core/drawing.ts +301 -301
  6. package/src/core/geometry.ts +347 -347
  7. package/src/core/mermaid.ts +38 -38
  8. package/src/core/render-grid.ts +51 -51
  9. package/src/core/spatial.ts +243 -243
  10. package/src/core/text-lines.ts +32 -32
  11. package/src/core/text.ts +44 -44
  12. package/src/detect.ts +17 -17
  13. package/src/diagnostics.ts +16 -16
  14. package/src/flowchart/drawing.ts +340 -340
  15. package/src/flowchart/labels.ts +144 -144
  16. package/src/flowchart/layout.ts +1427 -1427
  17. package/src/flowchart/options.ts +12 -12
  18. package/src/flowchart/parser.ts +467 -467
  19. package/src/flowchart/render.ts +10 -10
  20. package/src/flowchart/routing.ts +1542 -1542
  21. package/src/flowchart/style.ts +79 -79
  22. package/src/flowchart/types.ts +65 -65
  23. package/src/gantt/diagram.ts +8 -8
  24. package/src/gantt/drawing.ts +191 -191
  25. package/src/gantt/parser.ts +161 -161
  26. package/src/gantt/render-grid.ts +17 -17
  27. package/src/gantt/style.ts +36 -36
  28. package/src/gantt/types.ts +44 -44
  29. package/src/gitgraph/diagram.ts +8 -8
  30. package/src/gitgraph/drawing.ts +234 -234
  31. package/src/gitgraph/parser.ts +219 -219
  32. package/src/gitgraph/render-grid.ts +17 -17
  33. package/src/gitgraph/style.ts +37 -37
  34. package/src/gitgraph/types.ts +36 -36
  35. package/src/markdown.ts +336 -336
  36. package/src/palette.ts +75 -75
  37. package/src/plugin.ts +15 -15
  38. package/src/sequence/diagram.ts +8 -8
  39. package/src/sequence/drawing.ts +287 -287
  40. package/src/sequence/options.ts +8 -8
  41. package/src/sequence/parser.ts +282 -282
  42. package/src/sequence/placement.ts +684 -684
  43. package/src/sequence/render-grid.ts +22 -22
  44. package/src/sequence/style.ts +81 -81
  45. package/src/sequence/types.ts +74 -74
  46. package/src/state/diagram.ts +8 -8
  47. package/src/state/drawing.ts +360 -360
  48. package/src/state/endpoint.ts +15 -15
  49. package/src/state/layout.ts +1134 -1134
  50. package/src/state/note.ts +59 -59
  51. package/src/state/options.ts +11 -11
  52. package/src/state/parser.ts +224 -224
  53. package/src/state/render-grid.ts +22 -22
  54. package/src/state/routing.ts +1522 -1522
  55. package/src/state/search.ts +103 -103
  56. package/src/state/style.ts +52 -52
  57. package/src/state/types.ts +63 -63
  58. package/src/state/visible-model.ts +77 -77
  59. package/src/test/diagram.ts +32 -32
  60. package/src/test/layout-audit/fixtures.ts +538 -538
  61. package/src/test/layout-audit/harness.ts +522 -522
  62. package/src/timeline/diagram.ts +8 -8
  63. package/src/timeline/drawing.ts +109 -109
  64. package/src/timeline/parser.ts +119 -119
  65. package/src/timeline/render-grid.ts +17 -17
  66. package/src/timeline/style.ts +36 -36
  67. package/src/timeline/types.ts +31 -31
  68. package/tsconfig.json +10 -10
  69. package/src/core/canvas.test.ts +0 -203
  70. package/src/core/color/style.test.ts +0 -24
  71. package/src/core/drawing.test.ts +0 -116
  72. package/src/core/geometry.test.ts +0 -112
  73. package/src/core/spatial.test.ts +0 -87
  74. package/src/core/text.test.ts +0 -33
  75. package/src/flowchart/flowchart.test.ts +0 -2800
  76. package/src/flowchart/labels.test.ts +0 -127
  77. package/src/flowchart/routing.test.ts +0 -368
  78. package/src/gantt/diagram.test.ts +0 -124
  79. package/src/gitgraph/diagram.test.ts +0 -183
  80. package/src/layout-audit.test.ts +0 -92
  81. package/src/palette.test.ts +0 -103
  82. package/src/sequence/diagram.test.ts +0 -757
  83. package/src/sequence/placement.test.ts +0 -219
  84. package/src/state/diagram.test.ts +0 -1386
  85. package/src/state/layout.test.ts +0 -314
  86. package/src/state/routing.test.ts +0 -693
  87. package/src/state/style.test.ts +0 -113
  88. package/src/state/visible-model.test.ts +0 -84
  89. package/src/test/diagnostics.test.ts +0 -129
  90. package/src/test/markdown.test.ts +0 -474
  91. package/src/timeline/diagram.test.ts +0 -188
@@ -1,693 +0,0 @@
1
- import { describe, expect, test } from "bun:test"
2
- import { diagramTextWidth } from "../core/text.js"
3
- import type { StateDiagramBoxBounds } from "./layout.js"
4
- import { createStateDiagramLayout } from "./layout.js"
5
- import { parseMermaidStateDiagram } from "./parser.js"
6
- import {
7
- createStateTransitionJunctionPlans,
8
- createStateTransitionRenderPlans,
9
- createStateTransitionRoutePlans,
10
- } from "./routing.js"
11
- import type { StateVisibleDiagram } from "./visible-model.js"
12
- import { prepareVisibleStateDiagram } from "./visible-model.js"
13
-
14
- function bounds(id: string, centerX: number, centerY: number): StateDiagramBoxBounds {
15
- return { id, left: centerX - 2, top: centerY - 1, width: 5, height: 3, centerX, centerY }
16
- }
17
-
18
- describe("createStateTransitionRoutePlans", () => {
19
- test("classifies horizontal transition behavior before painting", () => {
20
- const diagram: StateVisibleDiagram = {
21
- direction: "LR",
22
- states: ["A", "B", "C"].map((id) => ({ id, label: id, kind: "state" })),
23
- transitions: [
24
- { from: "A", to: "B", label: "forward" },
25
- { from: "B", to: "C", label: "branch" },
26
- { from: "C", to: "A", label: "reset" },
27
- { from: "B", to: "B", label: "retry" },
28
- ],
29
- composites: [],
30
- notes: [],
31
- }
32
- const placements = new Map([
33
- ["A", bounds("A", 4, 4)],
34
- ["B", bounds("B", 14, 4)],
35
- ["C", bounds("C", 24, 10)],
36
- ])
37
-
38
- const plans = createStateTransitionRoutePlans(diagram, placements, 18)
39
-
40
- expect(plans.map((plan) => [plan.transition.label, plan.kind])).toEqual([
41
- ["forward", "horizontal-forward"],
42
- ["branch", "vertical-elbow"],
43
- ["reset", "bottom-feedback"],
44
- ["retry", "self"],
45
- ])
46
- expect(plans.find((plan) => plan.kind === "bottom-feedback")).toMatchObject({ railY: 18 })
47
- })
48
-
49
- test("classifies vertical self-transitions as loops before directional routing", () => {
50
- const diagram: StateVisibleDiagram = {
51
- direction: "TB",
52
- states: [{ id: "Working", label: "Working", kind: "state" }],
53
- transitions: [{ from: "Working", to: "Working", label: "retry" }],
54
- composites: [],
55
- notes: [],
56
- }
57
-
58
- expect(
59
- createStateTransitionRoutePlans(diagram, new Map([["Working", bounds("Working", 5, 3)]]), 12)[0],
60
- ).toMatchObject({
61
- kind: "self",
62
- })
63
- })
64
-
65
- test("allocates separate lanes for parallel transitions", () => {
66
- const horizontal: StateVisibleDiagram = {
67
- direction: "LR",
68
- states: ["A", "B"].map((id) => ({ id, label: id, kind: "state" })),
69
- transitions: [
70
- { from: "A", to: "B", label: "first" },
71
- { from: "A", to: "B", label: "second" },
72
- ],
73
- composites: [],
74
- notes: [],
75
- }
76
- const vertical = { ...horizontal, direction: "TB" as const }
77
- const placements = new Map([
78
- ["A", bounds("A", 4, 4)],
79
- ["B", bounds("B", 18, 4)],
80
- ])
81
- const verticalPlacements = new Map([
82
- ["A", bounds("A", 4, 4)],
83
- ["B", bounds("B", 4, 14)],
84
- ])
85
-
86
- expect(createStateTransitionRoutePlans(horizontal, placements, 12).map((plan) => plan.kind)).toEqual([
87
- "horizontal-forward",
88
- "bottom-parallel",
89
- ])
90
- expect(createStateTransitionRoutePlans(vertical, verticalPlacements, 22).map((plan) => plan.kind)).toEqual([
91
- "vertical",
92
- "side-parallel",
93
- ])
94
- })
95
-
96
- test("uses a bottom lane for same-rank parallel transitions in vertical diagrams", () => {
97
- const diagram: StateVisibleDiagram = {
98
- direction: "TB",
99
- states: ["A", "B"].map((id) => ({ id, label: id, kind: "state" })),
100
- transitions: [
101
- { from: "A", to: "B", label: "first" },
102
- { from: "A", to: "B", label: "second" },
103
- ],
104
- composites: [],
105
- notes: [],
106
- }
107
- const placements = new Map([
108
- ["A", bounds("A", 4, 4)],
109
- ["B", bounds("B", 18, 4)],
110
- ])
111
-
112
- expect(createStateTransitionRoutePlans(diagram, placements, 12).map((plan) => plan.kind)).toEqual([
113
- "horizontal-forward",
114
- "bottom-parallel",
115
- ])
116
- })
117
-
118
- test("routes interleaving independent feedback transitions on opposite sides", () => {
119
- const diagram: StateVisibleDiagram = {
120
- direction: "LR",
121
- states: ["A", "B", "C", "D"].map((id) => ({ id, label: id, kind: "state" })),
122
- transitions: [
123
- { from: "A", to: "B", label: "advance" },
124
- { from: "B", to: "C", label: "continue" },
125
- { from: "C", to: "D", label: "finish" },
126
- { from: "C", to: "A", label: "reset A" },
127
- { from: "D", to: "B", label: "reset B" },
128
- ],
129
- composites: [],
130
- notes: [],
131
- }
132
- const placements = new Map([
133
- ["A", bounds("A", 4, 4)],
134
- ["B", bounds("B", 14, 4)],
135
- ["C", bounds("C", 24, 4)],
136
- ["D", bounds("D", 34, 4)],
137
- ])
138
- const plans = createStateTransitionRenderPlans(diagram, placements, 12).filter((plan) =>
139
- plan.route.transition.label.startsWith("reset"),
140
- )
141
-
142
- expect(plans.map((plan) => plan.route.kind)).toEqual(["bottom-feedback", "top-feedback"])
143
- const firstCells = new Set(plans[0]!.path.map(([x, y]) => `${x}:${y}`))
144
- expect(plans[1]!.path.some(([x, y]) => firstCells.has(`${x}:${y}`))).toBe(false)
145
- })
146
-
147
- test("routes nested same-side feedback transitions from inner to outer rails", () => {
148
- const diagram: StateVisibleDiagram = {
149
- direction: "LR",
150
- states: ["A", "B", "C", "D"].map((id) => ({ id, label: id, kind: "state" })),
151
- transitions: [
152
- { from: "D", to: "A", label: "outer" },
153
- { from: "C", to: "B", label: "inner" },
154
- ],
155
- composites: [],
156
- notes: [],
157
- }
158
- const placements = new Map([
159
- ["A", bounds("A", 4, 4)],
160
- ["B", bounds("B", 14, 4)],
161
- ["C", bounds("C", 24, 4)],
162
- ["D", bounds("D", 34, 4)],
163
- ])
164
- const plans = createStateTransitionRenderPlans(diagram, placements, 12)
165
- const outer = plans.find((plan) => plan.route.transition.label === "outer")!
166
- const inner = plans.find((plan) => plan.route.transition.label === "inner")!
167
- const outerCells = new Set(outer.path.map(([x, y]) => `${x}:${y}`))
168
-
169
- expect(outer.route).toMatchObject({ kind: "bottom-feedback", railY: 15 })
170
- expect(inner.route).toMatchObject({ kind: "bottom-feedback", railY: 12 })
171
- expect(inner.path.some(([x, y]) => outerCells.has(`${x}:${y}`))).toBe(false)
172
- })
173
-
174
- test("allocates duplicate feedback transitions without crossing an independent feedback path", () => {
175
- const diagram: StateVisibleDiagram = {
176
- direction: "LR",
177
- states: ["A", "B", "C", "D"].map((id) => ({ id, label: id, kind: "state" })),
178
- transitions: [
179
- { from: "C", to: "A", label: "ca" },
180
- { from: "D", to: "B", label: "db1" },
181
- { from: "D", to: "B", label: "db2" },
182
- ],
183
- composites: [],
184
- notes: [],
185
- }
186
- const placements = new Map([
187
- ["A", bounds("A", 4, 4)],
188
- ["B", bounds("B", 14, 4)],
189
- ["C", bounds("C", 24, 4)],
190
- ["D", bounds("D", 34, 4)],
191
- ])
192
- const plans = createStateTransitionRenderPlans(diagram, placements, 12)
193
- const independent = plans.find((plan) => plan.route.transition.label === "ca")!
194
- const independentCells = new Set(independent.path.map(([x, y]) => `${x}:${y}`))
195
- const duplicates = plans.filter((plan) => plan.route.transition.label.startsWith("db"))
196
-
197
- expect(duplicates.map((plan) => plan.route.kind)).toEqual(["top-feedback", "top-feedback"])
198
- expect(duplicates.every((plan) => plan.path.every(([x, y]) => !independentCells.has(`${x}:${y}`)))).toBe(true)
199
- })
200
- })
201
-
202
- describe("createStateTransitionRenderPlans", () => {
203
- test("prepares concrete cells, labels, and route paths before painting", () => {
204
- const diagram: StateVisibleDiagram = {
205
- direction: "LR",
206
- states: ["A", "B"].map((id) => ({ id, label: id, kind: "state" })),
207
- transitions: [{ from: "A", to: "B", label: "next" }],
208
- composites: [],
209
- notes: [],
210
- }
211
- const placements = new Map([
212
- ["A", bounds("A", 4, 4)],
213
- ["B", bounds("B", 14, 4)],
214
- ])
215
-
216
- const plan = createStateTransitionRenderPlans(diagram, placements, 18)[0]!
217
-
218
- expect(plan.cells).toEqual([
219
- { x: 6, y: 4, char: "├" },
220
- { x: 7, y: 4, char: "─" },
221
- { x: 8, y: 4, char: "─" },
222
- { x: 9, y: 4, char: "─" },
223
- { x: 10, y: 4, char: "─" },
224
- { x: 11, y: 4, arrowDirection: "right" },
225
- ])
226
- expect(plan.label).toEqual({ x: 8, y: 3, lines: ["next"] })
227
- expect(plan.path).toEqual([
228
- [6, 4],
229
- [7, 4],
230
- [8, 4],
231
- [9, 4],
232
- [10, 4],
233
- [11, 4],
234
- ])
235
- })
236
-
237
- test("anchors choice labels to their distinct outgoing branches", () => {
238
- const diagram = prepareVisibleStateDiagram(
239
- parseMermaidStateDiagram(`stateDiagram-v2
240
- direction TB
241
- [*] --> Pending
242
- Pending --> Decision: review
243
- state Decision <<choice>>
244
- Decision --> Approved: accept
245
- Decision --> Rejected: reject
246
- Rejected --> Pending: retry
247
- Approved --> [*]: complete`),
248
- )
249
- const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
250
- const plans = createStateTransitionRenderPlans(diagram, layout.bounds, 30)
251
- const accepted = plans.find((plan) => plan.route.transition.label === "accept")!
252
- const rejected = plans.find((plan) => plan.route.transition.label === "reject")!
253
-
254
- expect(rejected.label!.x).toBeGreaterThanOrEqual(accepted.label!.x + diagramTextWidth("accept") + 1)
255
- expect(rejected.label!.x + diagramTextWidth("reject")).toBeLessThanOrEqual(rejected.route.to.centerX)
256
- })
257
-
258
- test("keeps crowded loop and forward labels below their source state", () => {
259
- const diagram = prepareVisibleStateDiagram(
260
- parseMermaidStateDiagram(`stateDiagram-v2
261
- direction TB
262
- [*] --> Listening
263
- Listening --> Listening: heartbeat
264
- Listening --> Listening: refresh token
265
- Listening --> Connected: client connected
266
- Connected --> Connected: received message
267
- Connected --> Listening: disconnected
268
- Connected --> [*]: shutdown`),
269
- )
270
- const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
271
- const plans = createStateTransitionRenderPlans(diagram, layout.bounds, 30)
272
-
273
- for (const label of ["heartbeat", "refresh token", "client connected", "received message", "shutdown"]) {
274
- const plan = plans.find((candidate) => candidate.route.transition.label === label)!
275
- expect(plan.label!.y, label).toBeGreaterThanOrEqual(plan.route.from.top + plan.route.from.height)
276
- expect(plan.label!.y, label).toBeLessThanOrEqual(Math.max(...plan.path.map(([, y]) => y)))
277
- }
278
-
279
- const crowded = plans.filter((plan) =>
280
- ["heartbeat", "refresh token", "client connected", "disconnected"].includes(plan.route.transition.label),
281
- )
282
- for (const [index, plan] of crowded.entries()) {
283
- for (const other of crowded.slice(index + 1)) {
284
- expect(Math.abs(plan.label!.y - other.label!.y)).toBeGreaterThan(1)
285
- }
286
- }
287
- })
288
-
289
- test("separates nested feedback labels without moving them outside their routes", () => {
290
- const diagram = prepareVisibleStateDiagram(
291
- parseMermaidStateDiagram(`stateDiagram-v2
292
- direction TB
293
- state Session {
294
- [*] --> Open
295
- state Open {
296
- [*] --> Clean
297
- Clean --> Dirty: edit
298
- Dirty --> Clean: save
299
- Dirty --> [*]
300
- }
301
- Open --> Closing: request close
302
- Closing --> Open: cancel
303
- Closing --> [*]: closed
304
- note right of Dirty: unsaved changes
305
- }
306
- [*] --> Session: hydrate
307
- Session --> [*]: release`),
308
- )
309
- const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
310
- const plans = createStateTransitionRenderPlans(diagram, layout.bounds, 30, { noteBounds: layout.noteBounds })
311
- const crowded = plans.filter((plan) => ["edit", "save", "cancel"].includes(plan.route.transition.label))
312
-
313
- for (const [index, plan] of crowded.entries()) {
314
- expect(plan.label!.y).toBeGreaterThanOrEqual(Math.min(...plan.path.map(([, y]) => y)))
315
- expect(plan.label!.y).toBeLessThanOrEqual(Math.max(...plan.path.map(([, y]) => y)))
316
- for (const other of crowded.slice(index + 1)) {
317
- expect(Math.abs(plan.label!.y - other.label!.y)).toBeGreaterThan(1)
318
- }
319
- }
320
- })
321
-
322
- test("keeps vertical branch routes out of unrelated state bounds", () => {
323
- const diagram = prepareVisibleStateDiagram(
324
- parseMermaidStateDiagram(`stateDiagram-v2
325
- direction TB
326
- state "Branch root" as Root
327
- state "Upper branch" as Upper
328
- state "Lower branch" as Lower
329
- state "Merged branch" as Merge
330
- Root --> Upper: branch-up
331
- Root --> Lower: branch-down
332
- Upper --> Merge: merge-up
333
- Lower --> Merge: merge-down
334
- Merge --> Root: branch-feedback`),
335
- )
336
- const layout = createStateDiagramLayout(diagram, { minStateGap: 4 })
337
- const plans = createStateTransitionRenderPlans(diagram, layout.bounds, 30)
338
-
339
- for (const plan of plans) {
340
- const unrelated = diagram.states
341
- .filter((state) => state.id !== plan.route.transition.from && state.id !== plan.route.transition.to)
342
- .map((state) => layout.bounds.get(state.id)!)
343
- expect(
344
- plan.path.some(([x, y]) =>
345
- unrelated.some(
346
- (bound) =>
347
- x >= bound.left && x < bound.left + bound.width && y >= bound.top && y < bound.top + bound.height,
348
- ),
349
- ),
350
- `${plan.route.transition.from} -> ${plan.route.transition.to}`,
351
- ).toBe(false)
352
- }
353
- })
354
-
355
- test("keeps vertical feedback routes out of compact sibling state bounds", () => {
356
- const diagram = prepareVisibleStateDiagram(
357
- parseMermaidStateDiagram(`stateDiagram-v2
358
- direction TB
359
- [*] --> Root
360
- Root --> A
361
- Root --> B
362
- A --> Merge
363
- B --> Merge
364
- Merge --> A: retry`),
365
- )
366
- const layout = createStateDiagramLayout(diagram, { minStateGap: 12 })
367
- const plan = createStateTransitionRenderPlans(diagram, layout.bounds, 30).find(
368
- (plan) => plan.route.transition.label === "retry",
369
- )!
370
- const sibling = layout.bounds.get("B")!
371
-
372
- expect(
373
- plan.path.some(
374
- ([x, y]) =>
375
- x >= sibling.left && x < sibling.left + sibling.width && y >= sibling.top && y < sibling.top + sibling.height,
376
- ),
377
- ).toBe(false)
378
- })
379
-
380
- test("keeps every dense vertical fan route out of unrelated state bounds", () => {
381
- const diagram = prepareVisibleStateDiagram(
382
- parseMermaidStateDiagram(`stateDiagram-v2
383
- direction TB
384
- state "Alpha" as A
385
- state "Beta" as B
386
- state "Gamma store" as C
387
- state "Delta notifier" as D
388
- A --> B
389
- A --> C
390
- A --> D
391
- B --> A: back
392
- B --> D: across`),
393
- )
394
- const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
395
- const plans = createStateTransitionRenderPlans(diagram, layout.bounds, 30)
396
-
397
- for (const plan of plans) {
398
- const unrelated = diagram.states
399
- .filter((state) => state.id !== plan.route.transition.from && state.id !== plan.route.transition.to)
400
- .map((state) => layout.bounds.get(state.id)!)
401
- expect(
402
- plan.path.some(([x, y]) =>
403
- unrelated.some(
404
- (bound) =>
405
- x >= bound.left && x < bound.left + bound.width && y >= bound.top && y < bound.top + bound.height,
406
- ),
407
- ),
408
- `${plan.route.transition.from} -> ${plan.route.transition.to}`,
409
- ).toBe(false)
410
- }
411
- })
412
-
413
- test("keeps routes to offset end markers continuous", () => {
414
- const diagram = prepareVisibleStateDiagram(
415
- parseMermaidStateDiagram(`stateDiagram-v2
416
- [*] --> Pending
417
- Pending --> Running
418
- Running --> Idle
419
- Running --> Interrupted
420
- Interrupted --> [*]`),
421
- )
422
- const layout = createStateDiagramLayout(diagram, { minStateGap: 12 })
423
- const plan = createStateTransitionRenderPlans(diagram, layout.bounds, 30).find(
424
- (plan) => plan.route.transition.to === "__end",
425
- )!
426
-
427
- expect(
428
- plan.path.slice(1).every(([x, y], index) => {
429
- const previous = plan.path[index]!
430
- return Math.abs(x - previous[0]) + Math.abs(y - previous[1]) === 1
431
- }),
432
- ).toBe(true)
433
- })
434
-
435
- test.each(["LR", "RL", "TB", "TD"] as const)(
436
- "keeps endpoint-disjoint %s transitions on separate cells when a detour exists",
437
- (direction) => {
438
- const diagram = prepareVisibleStateDiagram(
439
- parseMermaidStateDiagram(`stateDiagram-v2
440
- direction ${direction}
441
- state "A" as A
442
- state "B" as B
443
- state "C" as C
444
- state "D" as D
445
- B --> D: e0
446
- C --> A: e1`),
447
- )
448
- const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
449
- const plans = createStateTransitionRenderPlans(diagram, layout.bounds, 30)
450
- const first = new Set(plans[0]!.path.map(([x, y]) => `${x}:${y}`))
451
-
452
- expect(plans[1]!.path.every(([x, y]) => !first.has(`${x}:${y}`))).toBe(true)
453
- },
454
- )
455
-
456
- test("anchors labels to final repaired transition geometry", () => {
457
- const diagram = prepareVisibleStateDiagram(
458
- parseMermaidStateDiagram(`stateDiagram-v2
459
- direction TB
460
- state "A" as A
461
- state "B" as B
462
- state "C" as C
463
- A --> B: e0
464
- A --> C: e1
465
- B --> A: e2`),
466
- )
467
- const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
468
- const plan = createStateTransitionRenderPlans(diagram, layout.bounds, 30).find(
469
- (candidate) => candidate.route.transition.label === "e2",
470
- )!
471
- const width = Math.max(...plan.label!.lines.map(diagramTextWidth))
472
- const distance = Math.min(
473
- ...plan.path.map(([pathX, pathY]) => {
474
- const dx =
475
- pathX < plan.label!.x
476
- ? plan.label!.x - pathX
477
- : pathX >= plan.label!.x + width
478
- ? pathX - (plan.label!.x + width - 1)
479
- : 0
480
- const dy =
481
- pathY < plan.label!.y
482
- ? plan.label!.y - pathY
483
- : pathY >= plan.label!.y + plan.label!.lines.length
484
- ? pathY - (plan.label!.y + plan.label!.lines.length - 1)
485
- : 0
486
- return dx + dy
487
- }),
488
- )
489
-
490
- expect(plan.pathRepaired).toBe(true)
491
- expect(distance).toBeLessThanOrEqual(4)
492
- })
493
-
494
- test.each(["LR", "RL", "TB", "TD"] as const)(
495
- "allocates distinct connected self-transition lanes in %s diagrams",
496
- (direction) => {
497
- for (const count of [2, 3, 4]) {
498
- const diagram = prepareVisibleStateDiagram(
499
- parseMermaidStateDiagram(`stateDiagram-v2
500
- direction ${direction}
501
- ${Array.from({ length: count }, (_, index) => ` A --> A: loop-${index}`).join("\n")}`),
502
- )
503
- const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
504
- const plans = createStateTransitionRenderPlans(diagram, layout.bounds, 30)
505
-
506
- expect(new Set(plans.map((plan) => plan.path.map(([x, y]) => `${x}:${y}`).join("|"))).size).toBe(count)
507
- for (const plan of plans) {
508
- expect(
509
- plan.path.slice(1).every(([x, y], index) => {
510
- const previous = plan.path[index]!
511
- return Math.abs(x - previous[0]) + Math.abs(y - previous[1]) === 1
512
- }),
513
- ).toBe(true)
514
- }
515
- }
516
- },
517
- )
518
-
519
- test("uses fixed-width side lanes for long parallel vertical labels", () => {
520
- const diagram = prepareVisibleStateDiagram(
521
- parseMermaidStateDiagram(`stateDiagram-v2
522
- direction TB
523
- A --> B: alpha route label that is deliberately long
524
- A --> B: beta route label that is deliberately long
525
- A --> B: gamma route label that is deliberately long`),
526
- )
527
- const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
528
- const rails = createStateTransitionRoutePlans(diagram, layout.bounds, 30)
529
- .filter((plan) => plan.kind === "side-parallel")
530
- .map((plan) => plan.railX)
531
-
532
- expect(rails).toHaveLength(2)
533
- expect(rails[1]! - rails[0]!).toBe(3)
534
- })
535
-
536
- test.each([
537
- [
538
- "parallel",
539
- `stateDiagram-v2
540
- direction TB
541
- A --> B: alpha route label that is deliberately long
542
- A --> B: beta route label that is deliberately long
543
- A --> B: gamma route label that is deliberately long`,
544
- 3,
545
- ],
546
- [
547
- "self",
548
- `stateDiagram-v2
549
- direction LR
550
- A --> A: one
551
- A --> A: two
552
- A --> A: three`,
553
- 3,
554
- ],
555
- ] as const)("keeps %s labels one column clear of frames and route rails", (_, source, count) => {
556
- const diagram = prepareVisibleStateDiagram(parseMermaidStateDiagram(source))
557
- const layout = createStateDiagramLayout(diagram, { minStateGap: 5 })
558
- const plans = createStateTransitionRenderPlans(diagram, layout.bounds, 30)
559
- const routeCells = plans.flatMap((plan) => plan.path)
560
-
561
- expect(plans).toHaveLength(count)
562
- for (const plan of plans) {
563
- const width = Math.max(...plan.label!.lines.map(diagramTextWidth))
564
- for (const [row, line] of plan.label!.lines.entries()) {
565
- const lineWidth = diagramTextWidth(line)
566
- const y = plan.label!.y + row
567
- expect(
568
- routeCells
569
- .filter(([, routeY]) => routeY === y)
570
- .every(([routeX]) => routeX < plan.label!.x - 1 || routeX > plan.label!.x + lineWidth),
571
- ).toBe(true)
572
- for (const bound of layout.bounds.values()) {
573
- if (y < bound.top || y >= bound.top + bound.height) continue
574
- expect(bound.left + bound.width <= plan.label!.x - 1 || bound.left >= plan.label!.x + width + 1).toBe(true)
575
- }
576
- }
577
- }
578
-
579
- if (source.includes("A --> A")) {
580
- const arrowXs = plans
581
- .flatMap((plan) => plan.cells.filter((cell) => cell.arrowDirection).map((cell) => cell.x))
582
- .sort((left, right) => left - right)
583
- expect(arrowXs.slice(1).every((x, index) => x - arrowXs[index]! >= 4)).toBe(true)
584
- }
585
- })
586
- })
587
-
588
- describe("createStateTransitionJunctionPlans", () => {
589
- test("prepares choice topology from connected transitions", () => {
590
- const diagram: StateVisibleDiagram = {
591
- direction: "LR",
592
- states: [
593
- { id: "A", label: "A", kind: "state" },
594
- { id: "Decision", label: "Decision", kind: "choice" },
595
- { id: "B", label: "B", kind: "state" },
596
- { id: "C", label: "C", kind: "state" },
597
- ],
598
- transitions: [
599
- { from: "A", to: "Decision", label: "" },
600
- { from: "Decision", to: "B", label: "yes" },
601
- { from: "Decision", to: "C", label: "no" },
602
- ],
603
- composites: [],
604
- notes: [],
605
- }
606
- const placements = new Map([
607
- ["A", bounds("A", 4, 4)],
608
- ["Decision", { id: "Decision", left: 14, top: 4, width: 1, height: 1, centerX: 14, centerY: 4 }],
609
- ["B", bounds("B", 24, 4)],
610
- ["C", bounds("C", 4, 10)],
611
- ])
612
-
613
- const plan = createStateTransitionJunctionPlans(
614
- diagram,
615
- placements,
616
- createStateTransitionRenderPlans(diagram, placements, 18),
617
- )[0]!
618
-
619
- expect(plan.kind).toBe("choice")
620
- expect([...plan.connections]).toEqual(["left", "right", "down"])
621
- expect(plan.transitions.map((transition) => transition.label)).toEqual(["", "yes", "no"])
622
- })
623
-
624
- test("derives a lower choice connection from its routed elbow approach", () => {
625
- const diagram: StateVisibleDiagram = {
626
- direction: "LR",
627
- states: [
628
- { id: "Upper", label: "Upper", kind: "state" },
629
- { id: "Lower", label: "Lower", kind: "state" },
630
- { id: "Decision", label: "Decision", kind: "choice" },
631
- { id: "Done", label: "Done", kind: "state" },
632
- ],
633
- transitions: [
634
- { from: "Upper", to: "Decision", label: "" },
635
- { from: "Lower", to: "Decision", label: "" },
636
- { from: "Decision", to: "Done", label: "" },
637
- ],
638
- composites: [],
639
- notes: [],
640
- }
641
- const placements = new Map([
642
- ["Upper", bounds("Upper", 14, 4)],
643
- ["Lower", bounds("Lower", 14, 11)],
644
- ["Decision", { ...bounds("Decision", 24, 4), left: 24, top: 4, width: 1, height: 1, centerX: 24, centerY: 4 }],
645
- ["Done", bounds("Done", 34, 4)],
646
- ])
647
- const renderPlans = createStateTransitionRenderPlans(diagram, placements, 18)
648
-
649
- const plan = createStateTransitionJunctionPlans(diagram, placements, renderPlans)[0]!
650
-
651
- expect([...plan.connections]).toContain("down")
652
- })
653
- })
654
-
655
- describe("reconverging vertical elbows", () => {
656
- test("uses separate top connectors for a lower parallel lane", () => {
657
- const diagram: StateVisibleDiagram = {
658
- direction: "LR",
659
- states: ["Fork", "Upper", "Lower", "Join"].map((id) => ({ id, label: id, kind: "state" })),
660
- transitions: [
661
- { from: "Fork", to: "Upper", label: "" },
662
- { from: "Fork", to: "Lower", label: "down" },
663
- { from: "Upper", to: "Join", label: "" },
664
- { from: "Lower", to: "Join", label: "up" },
665
- ],
666
- composites: [],
667
- notes: [],
668
- }
669
- const placements = new Map([
670
- ["Fork", bounds("Fork", 4, 4)],
671
- ["Upper", bounds("Upper", 14, 4)],
672
- ["Lower", bounds("Lower", 14, 11)],
673
- ["Join", bounds("Join", 24, 4)],
674
- ])
675
-
676
- const plans = createStateTransitionRenderPlans(diagram, placements, 18)
677
- const entering = plans.find((plan) => plan.route.transition.to === "Lower")!
678
- const leaving = plans.find((plan) => plan.route.transition.from === "Lower")!
679
-
680
- expect(entering.route).toMatchObject({ kind: "vertical-elbow", offsetConnector: true })
681
- expect(leaving.route).toMatchObject({ kind: "vertical-elbow", offsetConnector: true })
682
- expect(entering.path.at(-1)).not.toEqual(leaving.path[0])
683
- expect(entering.cells.at(-1)).toMatchObject({ arrowDirection: "down" })
684
- expect(entering.cells.at(-2)).toMatchObject({ x: entering.cells.at(-1)!.x, char: "│" })
685
- expect(entering.cells.at(-2)!.y).toBe(entering.cells.at(-1)!.y - 1)
686
- expect(entering.cells.at(-3)).toMatchObject({ x: entering.cells.at(-1)!.x, char: "╮" })
687
- expect(entering.cells.at(-3)!.y).toBe(entering.cells.at(-1)!.y - 2)
688
- const enteringHorizontalY = entering.cells.find((cell) => cell.char === "╰")!.y
689
- const leavingHorizontalY = leaving.cells.find((cell) => cell.char === "╭")!.y
690
- expect(entering.label!.y).toBeLessThan(enteringHorizontalY)
691
- expect(leaving.label!.y).toBeLessThan(leavingHorizontalY)
692
- })
693
- })