@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.
Files changed (92) hide show
  1. package/package.json +29 -4
  2. package/script/layout-audit.ts +98 -0
  3. package/src/core/canvas.test.ts +203 -0
  4. package/src/core/canvas.ts +228 -0
  5. package/src/core/color/style.test.ts +24 -0
  6. package/src/core/color/style.ts +56 -0
  7. package/src/core/drawing.test.ts +116 -0
  8. package/src/core/drawing.ts +301 -0
  9. package/src/core/geometry.test.ts +112 -0
  10. package/src/core/geometry.ts +347 -0
  11. package/src/core/mermaid.ts +38 -0
  12. package/src/core/render-grid.ts +51 -0
  13. package/src/core/spatial.test.ts +87 -0
  14. package/src/core/spatial.ts +243 -0
  15. package/src/core/text-lines.ts +32 -0
  16. package/src/core/text.test.ts +33 -0
  17. package/src/core/text.ts +44 -0
  18. package/src/detect.ts +17 -0
  19. package/src/diagnostics.ts +16 -0
  20. package/src/flowchart/drawing.ts +340 -0
  21. package/src/flowchart/flowchart.test.ts +2800 -0
  22. package/src/flowchart/labels.test.ts +127 -0
  23. package/src/flowchart/labels.ts +144 -0
  24. package/src/flowchart/layout.ts +1427 -0
  25. package/src/flowchart/options.ts +12 -0
  26. package/src/flowchart/parser.ts +467 -0
  27. package/src/flowchart/render.ts +10 -0
  28. package/src/flowchart/routing.test.ts +368 -0
  29. package/src/flowchart/routing.ts +1542 -0
  30. package/src/flowchart/style.ts +79 -0
  31. package/src/flowchart/types.ts +65 -0
  32. package/src/gantt/diagram.test.ts +124 -0
  33. package/src/gantt/diagram.ts +8 -0
  34. package/src/gantt/drawing.ts +191 -0
  35. package/src/gantt/parser.ts +161 -0
  36. package/src/gantt/render-grid.ts +17 -0
  37. package/src/gantt/style.ts +36 -0
  38. package/src/gantt/types.ts +44 -0
  39. package/src/gitgraph/diagram.test.ts +183 -0
  40. package/src/gitgraph/diagram.ts +8 -0
  41. package/src/gitgraph/drawing.ts +234 -0
  42. package/src/gitgraph/parser.ts +219 -0
  43. package/src/gitgraph/render-grid.ts +17 -0
  44. package/src/gitgraph/style.ts +37 -0
  45. package/src/gitgraph/types.ts +36 -0
  46. package/src/layout-audit.test.ts +92 -0
  47. package/src/markdown.ts +336 -0
  48. package/src/palette.test.ts +103 -0
  49. package/src/palette.ts +75 -0
  50. package/src/plugin.ts +15 -0
  51. package/src/sequence/diagram.test.ts +757 -0
  52. package/src/sequence/diagram.ts +8 -0
  53. package/src/sequence/drawing.ts +287 -0
  54. package/src/sequence/options.ts +8 -0
  55. package/src/sequence/parser.ts +282 -0
  56. package/src/sequence/placement.test.ts +219 -0
  57. package/src/sequence/placement.ts +684 -0
  58. package/src/sequence/render-grid.ts +22 -0
  59. package/src/sequence/style.ts +81 -0
  60. package/src/sequence/types.ts +74 -0
  61. package/src/state/diagram.test.ts +1386 -0
  62. package/src/state/diagram.ts +8 -0
  63. package/src/state/drawing.ts +360 -0
  64. package/src/state/endpoint.ts +15 -0
  65. package/src/state/layout.test.ts +314 -0
  66. package/src/state/layout.ts +1134 -0
  67. package/src/state/note.ts +59 -0
  68. package/src/state/options.ts +11 -0
  69. package/src/state/parser.ts +224 -0
  70. package/src/state/render-grid.ts +22 -0
  71. package/src/state/routing.test.ts +693 -0
  72. package/src/state/routing.ts +1522 -0
  73. package/src/state/search.ts +103 -0
  74. package/src/state/style.test.ts +113 -0
  75. package/src/state/style.ts +52 -0
  76. package/src/state/types.ts +63 -0
  77. package/src/state/visible-model.test.ts +84 -0
  78. package/src/state/visible-model.ts +77 -0
  79. package/src/test/diagnostics.test.ts +129 -0
  80. package/src/test/diagram.ts +32 -0
  81. package/src/test/layout-audit/fixtures.ts +538 -0
  82. package/src/test/layout-audit/harness.ts +522 -0
  83. package/src/test/markdown.test.ts +474 -0
  84. package/src/timeline/diagram.test.ts +188 -0
  85. package/src/timeline/diagram.ts +8 -0
  86. package/src/timeline/drawing.ts +109 -0
  87. package/src/timeline/parser.ts +119 -0
  88. package/src/timeline/render-grid.ts +17 -0
  89. package/src/timeline/style.ts +36 -0
  90. package/src/timeline/types.ts +31 -0
  91. package/tsconfig.json +10 -0
  92. package/README.md +0 -3
@@ -0,0 +1,757 @@
1
+ import { describe, expect, test } from "bun:test"
2
+ import { diagramTextWidth } from "../core/text.js"
3
+ import { expectDiagram } from "../test/diagram.js"
4
+ import { renderSequenceDiagram } from "./diagram.js"
5
+ import { drawSequenceDiagramGrid } from "./drawing.js"
6
+ import { parseMermaidSequenceDiagram } from "./parser.js"
7
+
8
+ describe("SequenceDiagram", () => {
9
+ test("parses Mermaid sequenceDiagram participants and messages", () => {
10
+ const diagram = parseMermaidSequenceDiagram(`
11
+ sequenceDiagram
12
+ participant B as Browser
13
+ participant S as Server
14
+ B->>S: GET /
15
+ S-->>B: 401 WWW-Auth
16
+ `)
17
+
18
+ expect(diagram.participants).toEqual([
19
+ { id: "B", label: "Browser" },
20
+ { id: "S", label: "Server" },
21
+ ])
22
+ expect(diagram.messages).toEqual([
23
+ { from: "B", to: "S", label: "GET /", style: "solid" },
24
+ { from: "S", to: "B", label: "401 WWW-Auth", style: "dashed" },
25
+ ])
26
+ expect(diagram.steps).toEqual([
27
+ { type: "message", message: { from: "B", to: "S", label: "GET /", style: "solid" } },
28
+ { type: "message", message: { from: "S", to: "B", label: "401 WWW-Auth", style: "dashed" } },
29
+ ])
30
+ })
31
+
32
+ test("decodes HTML entities in participant, message, and note labels", () => {
33
+ const diagram = parseMermaidSequenceDiagram(`sequenceDiagram
34
+ participant A as Worker & signer
35
+ participant B
36
+ A->>B: ack <3s
37
+ Note over A,B: result ≥ 1`)
38
+
39
+ expect(diagram.participants[0]?.label).toBe("Worker & signer")
40
+ expect(diagram.messages[0]?.label).toBe("ack <3s")
41
+ expect(diagram.steps.find((step) => step.type === "note")?.note.label).toBe("result ≥ 1")
42
+ })
43
+
44
+ test("renders a terminal sequence diagram", () => {
45
+ const output = renderSequenceDiagram(`
46
+ sequenceDiagram
47
+ participant B as Browser
48
+ participant S as Server
49
+ B->>S: GET /
50
+ S-->>B: 401 WWW-Auth
51
+ `)
52
+
53
+ expectDiagram(output).toEqualDiagram(`
54
+ Browser Server
55
+ ───┬─── ───┬──
56
+ │ │
57
+ │ GET / │
58
+ ├─────────────────►
59
+ │ │
60
+ │ 401 WWW-Auth │
61
+ ◄─────────────────┤
62
+ │ │
63
+ `)
64
+ })
65
+
66
+ test("renders a compact terminal sequence diagram without participant boxes", () => {
67
+ const output = renderSequenceDiagram(
68
+ `
69
+ sequenceDiagram
70
+ participant Leaf as leaf tool
71
+ participant Location as LocationMutation
72
+ participant File as FileMutation
73
+ Leaf->>Location: resolve(path)
74
+ Location-->>Leaf: Plan(target, authority anchor)
75
+ Leaf->>File: commit(plan)
76
+ File->>Location: revalidate(plan)
77
+ Location-->>File: same target or reject
78
+ `,
79
+ { compact: true },
80
+ )
81
+
82
+ expectDiagram(output).toEqualDiagram(`
83
+ leaf tool LocationMutation FileMutation
84
+ │ │ │
85
+ ├───────── resolve(path) ───────────► │
86
+ │ │ │
87
+ ◄─ Plan(target, authority anchor) ──┤ │
88
+ │ │ │
89
+ ├─────────────────────── commit(plan) ─────────────────────────►
90
+ │ │ │
91
+ │ ◄─── revalidate(plan) ─────┤
92
+ │ │ │
93
+ │ ├─ same target or reject ──►
94
+ │ │ │
95
+ `)
96
+ })
97
+
98
+ test("keeps structured sequence steps visible in compact mode", () => {
99
+ const output = renderSequenceDiagram(
100
+ `
101
+ sequenceDiagram
102
+ participant Worker
103
+ participant Store
104
+ Note over Worker,Store: transaction
105
+ alt accepted
106
+ Worker->>Worker: prepare
107
+ Worker->>Store: commit
108
+ end
109
+ `,
110
+ { compact: true },
111
+ )
112
+
113
+ expectDiagram(output).toContainInOrder("Worker", "Store", "transaction", "alt: accepted", "prepare", "commit")
114
+ })
115
+
116
+ test("keeps compact labels above arrows when they do not fit inline", () => {
117
+ const output = renderSequenceDiagram(
118
+ "sequenceDiagram\n participant A\n participant B\n participant C\n A->>C: this label is deliberately much too long to fit between endpoints despite intermediate spacing",
119
+ { compact: true },
120
+ )
121
+ const lines = output.split("\n")
122
+
123
+ expect(lines.findIndex((line) => line.includes("deliberately"))).toBeLessThan(
124
+ lines.findIndex((line) => line.includes("►")),
125
+ )
126
+ })
127
+
128
+ test("normalizes invalid participant gaps for text rendering", () => {
129
+ const content = "sequenceDiagram\n A->>B: hello"
130
+
131
+ expect(renderSequenceDiagram(content, { minParticipantGap: Number.NaN })).toContain("hello")
132
+ })
133
+
134
+ test("connects participant headers to lifelines", () => {
135
+ const output = renderSequenceDiagram(`
136
+ sequenceDiagram
137
+ participant Browser
138
+ participant Server
139
+ `)
140
+
141
+ const lines = output.split("\n")
142
+ const browserCenter = lines[0]!.indexOf("w")
143
+ const serverCenter = lines[0]!.indexOf("v")
144
+
145
+ expect(lines[1]?.[browserCenter]).toBe("┬")
146
+ expect(lines[2]?.[browserCenter]).toBe("│")
147
+ expect(lines[1]?.[serverCenter]).toBe("┬")
148
+ expect(lines[2]?.[serverCenter]).toBe("│")
149
+ })
150
+
151
+ test("ramps participant frames into neutral lifelines", () => {
152
+ const grid = drawSequenceDiagramGrid(
153
+ parseMermaidSequenceDiagram(
154
+ "sequenceDiagram\n participant Browser\n participant Server\n Note over Browser,Server: context\n Browser->>Server: request",
155
+ ),
156
+ )
157
+ const rampStyles = grid.rows
158
+ .flatMap((row) => row.map((cell) => cell.style))
159
+ .filter((style) => style?.startsWith("lifelineRamp"))
160
+
161
+ expect(new Set(rampStyles)).toEqual(new Set(["lifelineRamp1", "lifelineRamp2", "lifelineRamp3"]))
162
+ })
163
+
164
+ test("renders notes and long cross-participant messages in order", () => {
165
+ const output = renderSequenceDiagram(`
166
+ sequenceDiagram
167
+ participant Browser
168
+ participant Server
169
+ participant Store as Ticket store
170
+ Note over Browser,Server: native browser Basic prompt
171
+ Browser->>Server: POST connect-token
172
+ Server->>Store: issue { ptyID, scope }
173
+ `)
174
+
175
+ expectDiagram(output).toContainInOrder(
176
+ "native browser Basic prompt",
177
+ "POST connect-token",
178
+ "issue { ptyID, scope }",
179
+ )
180
+ })
181
+
182
+ test("renders notes to the left and right of a participant", () => {
183
+ const output = renderSequenceDiagram(`sequenceDiagram
184
+ participant OpenTUI
185
+ Note right of OpenTUI: child is not renderable
186
+ Note left of OpenTUI: remove failed`)
187
+
188
+ expect(output).toContain("child is not renderable")
189
+ expect(output).toContain("remove failed")
190
+ })
191
+
192
+ test("preserves the parsed shape of notes over participants", () => {
193
+ const diagram = parseMermaidSequenceDiagram(`sequenceDiagram
194
+ participant A
195
+ Note over A: hello`)
196
+
197
+ expect(diagram.steps).toContainEqual({ type: "note", note: { over: ["A"], label: "hello" } })
198
+ })
199
+
200
+ test("parses the complete source of a br-delimited note", () => {
201
+ const diagram = parseMermaidSequenceDiagram(`sequenceDiagram
202
+ Note over Tool: directory instead?<br/>the WrongKind error carries the answer —<br/>branch to list, no extra round trip`)
203
+
204
+ expect(diagram.steps).toContainEqual({
205
+ type: "note",
206
+ note: {
207
+ over: ["Tool"],
208
+ label:
209
+ "directory instead?<br/>the WrongKind error carries the answer —<br/>branch to list, no extra round trip",
210
+ },
211
+ })
212
+ })
213
+
214
+ test("parses activation shorthand and control blocks", () => {
215
+ const diagram = parseMermaidSequenceDiagram(`
216
+ sequenceDiagram
217
+ Browser->>+Server: request
218
+ alt accepted
219
+ Server-->>-Browser: response
220
+ else rejected
221
+ activate Server
222
+ Server-->>Browser: error
223
+ deactivate Server
224
+ end
225
+ `)
226
+
227
+ expect(diagram.steps).toEqual([
228
+ {
229
+ type: "message",
230
+ message: {
231
+ from: "Browser",
232
+ to: "Server",
233
+ label: "request",
234
+ style: "solid",
235
+ activate: "Server",
236
+ },
237
+ },
238
+ { type: "fragment", fragment: { kind: "alt", label: "accepted" } },
239
+ {
240
+ type: "message",
241
+ message: {
242
+ from: "Server",
243
+ to: "Browser",
244
+ label: "response",
245
+ style: "dashed",
246
+ deactivate: "Server",
247
+ },
248
+ },
249
+ { type: "fragment", fragment: { kind: "else", label: "rejected" } },
250
+ { type: "activation", activation: { participant: "Server", active: true } },
251
+ {
252
+ type: "message",
253
+ message: { from: "Server", to: "Browser", label: "error", style: "dashed" },
254
+ },
255
+ { type: "activation", activation: { participant: "Server", active: false } },
256
+ { type: "fragment", fragment: { kind: "end", label: "alt" } },
257
+ ])
258
+ })
259
+
260
+ test("renders activation syntax as visible intervals", () => {
261
+ const output = renderSequenceDiagram(`
262
+ sequenceDiagram
263
+ Browser->>+Server: request
264
+ Server-->>-Browser: response
265
+ `)
266
+
267
+ expect(output).toContain("┃")
268
+ expect(output).toContain("request")
269
+ expect(output).toContain("response")
270
+ })
271
+
272
+ test("renders br-delimited participant aliases on separate lines", () => {
273
+ const output = renderSequenceDiagram(`sequenceDiagram
274
+ participant A as First line<br/>Second line
275
+ participant B as Normal
276
+ A->>B: hello`)
277
+
278
+ expect(output).not.toContain("<br")
279
+ expect(output).toContain("First line")
280
+ expect(output).toContain("Second line")
281
+ })
282
+
283
+ test("parses Mermaid arrow head variants", () => {
284
+ const diagram = parseMermaidSequenceDiagram(`
285
+ sequenceDiagram
286
+ A->B: open solid
287
+ B-->A: open dashed
288
+ A-xB: failed solid
289
+ B--xA: failed dashed
290
+ A-)B: async solid
291
+ B--)A: async dashed
292
+ `)
293
+
294
+ expect(diagram.messages).toEqual([
295
+ { from: "A", to: "B", label: "open solid", style: "solid", head: "open" },
296
+ { from: "B", to: "A", label: "open dashed", style: "dashed", head: "open" },
297
+ { from: "A", to: "B", label: "failed solid", style: "solid", head: "cross" },
298
+ { from: "B", to: "A", label: "failed dashed", style: "dashed", head: "cross" },
299
+ { from: "A", to: "B", label: "async solid", style: "solid", head: "async" },
300
+ { from: "B", to: "A", label: "async dashed", style: "dashed", head: "async" },
301
+ ])
302
+ })
303
+
304
+ test("renders Mermaid arrow head variants", () => {
305
+ const output = renderSequenceDiagram(`
306
+ sequenceDiagram
307
+ A->B: open solid
308
+ B-->A: open dashed
309
+ A-xB: failed solid
310
+ B--xA: failed dashed
311
+ A-)B: async solid
312
+ B--)A: async dashed
313
+ `)
314
+
315
+ expect(output).toMatchInlineSnapshot(`
316
+ " A B
317
+ ─┬─ ─┬─
318
+ │ │
319
+ │ open solid │
320
+ ├─────────────────>│
321
+ │ │
322
+ │ open dashed │
323
+ │<─────────────────┤
324
+ │ │
325
+ │ failed solid │
326
+ ├─────────────────✕│
327
+ │ │
328
+ │ failed dashed │
329
+ │✕─────────────────┤
330
+ │ │
331
+ │ async solid │
332
+ ├─────────────────)│
333
+ │ │
334
+ │ async dashed │
335
+ │(─────────────────┤
336
+ │ │"
337
+ `)
338
+ })
339
+
340
+ test("renders boxed alt else regions", () => {
341
+ const output = renderSequenceDiagram(`
342
+ sequenceDiagram
343
+ alt accepted
344
+ Browser->>Server: ok
345
+ else rejected
346
+ Server-->>Browser: no
347
+ end
348
+ `)
349
+
350
+ expectDiagram(output).toContainInOrder("╭─ alt: accepted", "ok", "├─ else: rejected", "no", "╰")
351
+ expect(output).not.toContain("end alt")
352
+ })
353
+
354
+ test("expands a fragment frame for a longer else label", () => {
355
+ const output = renderSequenceDiagram(`sequenceDiagram
356
+ A->>B: start
357
+ alt ok
358
+ A->>B: yes
359
+ else validation failed with a substantially longer explanation
360
+ B-->>A: no
361
+ end`)
362
+ const lines = output.split("\n")
363
+ const elseRow = lines.find((line) => line.includes("validation failed"))!
364
+ const endRow = [...lines].reverse().find((line) => line.includes("╰"))!
365
+
366
+ expect(elseRow.lastIndexOf("┤")).toBe(endRow.lastIndexOf("╯"))
367
+ })
368
+
369
+ test("preserves combined graphemes in participant names", () => {
370
+ const output = renderSequenceDiagram(`sequenceDiagram
371
+ participant A as Cafe\u0301
372
+ participant B
373
+ A->>B: hi`)
374
+
375
+ expect(output).toContain("Cafe\u0301")
376
+ })
377
+
378
+ test("renders fragment boxes with lifeline overhang", () => {
379
+ const output = renderSequenceDiagram(`
380
+ sequenceDiagram
381
+ participant A
382
+ participant B
383
+ alt ok
384
+ A->>B: yes
385
+ end
386
+ `)
387
+ const lines = output.split("\n")
388
+ const participantCenter = lines.find((line) => line.includes(" A"))!.indexOf("A")
389
+ const fragmentStart = lines.find((line) => line.includes("alt: ok"))!.indexOf("╭")
390
+
391
+ expect(fragmentStart).toBeLessThan(participantCenter)
392
+ })
393
+
394
+ test("supports configurable fragment border styles", () => {
395
+ const output = renderSequenceDiagram(
396
+ `
397
+ sequenceDiagram
398
+ participant A
399
+ participant B
400
+ alt ok
401
+ A->>B: yes
402
+ else no
403
+ B-->>A: no
404
+ end
405
+ `,
406
+ { fragmentBorderStyle: "double" },
407
+ )
408
+
409
+ expect(output).toContain("╔")
410
+ expect(output).toContain("╠")
411
+ expect(output).toContain("╚")
412
+ expect(output).toContain("═")
413
+ expect(output).toContain("║")
414
+ })
415
+
416
+ test("parses and renders autonumbered messages", () => {
417
+ const diagram = parseMermaidSequenceDiagram(`
418
+ sequenceDiagram
419
+ autonumber
420
+ Browser->>API: request
421
+ API-->>Browser: response
422
+ `)
423
+ const output = renderSequenceDiagram(`
424
+ sequenceDiagram
425
+ autonumber
426
+ Browser->>API: request
427
+ API-->>Browser: response
428
+ `)
429
+
430
+ expect(diagram.messages.map((message) => message.number)).toEqual([1, 2])
431
+ expect(output).toContain("1. request")
432
+ expect(output).toContain("2. response")
433
+ })
434
+
435
+ test("supports autonumber start and increment", () => {
436
+ const diagram = parseMermaidSequenceDiagram(`
437
+ sequenceDiagram
438
+ autonumber 10 5
439
+ Browser->>API: first
440
+ API-->>Browser: second
441
+ `)
442
+ const output = renderSequenceDiagram(`
443
+ sequenceDiagram
444
+ autonumber 10 5
445
+ Browser->>API: first
446
+ API-->>Browser: second
447
+ `)
448
+
449
+ expect(diagram.messages.map((message) => message.number)).toEqual([10, 15])
450
+ expect(output).toContain("10. first")
451
+ expect(output).toContain("15. second")
452
+ })
453
+
454
+ test("parses and renders loop regions", () => {
455
+ const diagram = parseMermaidSequenceDiagram(`
456
+ sequenceDiagram
457
+ loop retry up to 3x
458
+ Browser->>API: GET /users/42
459
+ API-->>Browser: 503
460
+ end
461
+ `)
462
+ const output = renderSequenceDiagram(`
463
+ sequenceDiagram
464
+ loop retry up to 3x
465
+ Browser->>API: GET /users/42
466
+ API-->>Browser: 503
467
+ end
468
+ `)
469
+
470
+ expect(diagram.steps[0]).toEqual({
471
+ type: "fragment",
472
+ fragment: { kind: "loop", label: "retry up to 3x" },
473
+ })
474
+ expect(output).toContain("╭─ ↻ loop: retry up to 3x")
475
+ expect(output).not.toContain("end loop")
476
+ expect(output.indexOf("loop: retry up to 3x")).toBeLessThan(output.indexOf("GET /users/42"))
477
+ })
478
+
479
+ test("parses Mermaid box participant groups", () => {
480
+ const diagram = parseMermaidSequenceDiagram(`
481
+ sequenceDiagram
482
+ participant Browser
483
+ box Backend
484
+ participant API
485
+ participant Cache
486
+ end
487
+ box Purple Storage Layer
488
+ participant DB
489
+ end
490
+ box "Purple Literal Label"
491
+ participant Worker
492
+ end
493
+ Browser->>API: request
494
+ `)
495
+
496
+ expect(diagram.groups).toEqual([
497
+ { label: "Backend", participantIds: ["API", "Cache"] },
498
+ { label: "Storage Layer", participantIds: ["DB"] },
499
+ { label: "Purple Literal Label", participantIds: ["Worker"] },
500
+ ])
501
+ expect(diagram.steps).toEqual([
502
+ {
503
+ type: "message",
504
+ message: { from: "Browser", to: "API", label: "request", style: "solid" },
505
+ },
506
+ ])
507
+ })
508
+
509
+ test("adds implicit participants inside box groups", () => {
510
+ const diagram = parseMermaidSequenceDiagram(`
511
+ sequenceDiagram
512
+ participant API
513
+ box Backend
514
+ API->>DB: query
515
+ end
516
+ `)
517
+
518
+ expect(diagram.groups).toEqual([{ label: "Backend", participantIds: ["API", "DB"] }])
519
+ expect(diagram.steps).toEqual([
520
+ { type: "message", message: { from: "API", to: "DB", label: "query", style: "solid" } },
521
+ ])
522
+ })
523
+
524
+ test("does not clip long non-adjacent messages or notes", () => {
525
+ const output = renderSequenceDiagram(`
526
+ sequenceDiagram
527
+ participant A
528
+ participant B
529
+ participant C
530
+ A->>C: this message needs room past the final participant
531
+ Note over A,C: this note also needs full horizontal room
532
+ `)
533
+
534
+ expect(output).toContain("this message needs room past the final participant")
535
+ expect(output).toContain("this note also needs full horizontal room")
536
+ })
537
+
538
+ test("keeps long content inside participant groups and fragment frames", () => {
539
+ const group = renderSequenceDiagram(`sequenceDiagram
540
+ box Services
541
+ participant A
542
+ participant B
543
+ participant C
544
+ A->>C: this message text runs far outside of the group container boundary
545
+ end`)
546
+ const fragment = renderSequenceDiagram(`sequenceDiagram
547
+ participant A
548
+ participant B
549
+ participant C
550
+ alt lookup
551
+ A->>C: this non adjacent message is deliberately much wider than the frame
552
+ end`)
553
+
554
+ const groupMessageRow = group.split("\n").find((line) => line.includes("this message text"))!
555
+ const fragmentMessageRow = fragment.split("\n").find((line) => line.includes("this non adjacent message"))!
556
+ expect(groupMessageRow.trimEnd().endsWith("│")).toBe(true)
557
+ expect(fragmentMessageRow).toContain("this non adjacent message is deliberately much wider than the frame")
558
+ expect(fragmentMessageRow.match(/│/g)?.length).toBe(3)
559
+ })
560
+
561
+ test("keeps long notes inside groups and nested fragment frames intact", () => {
562
+ const groupedNote = renderSequenceDiagram(`sequenceDiagram
563
+ box Services
564
+ participant A
565
+ participant B
566
+ participant C
567
+ Note over A,C: this note text runs far outside of the group container boundary
568
+ end`)
569
+ const fragmentNote = renderSequenceDiagram(`sequenceDiagram
570
+ participant A
571
+ participant B
572
+ participant C
573
+ alt lookup
574
+ Note over A,C: this non adjacent note is deliberately much wider than the frame
575
+ end`)
576
+ const nested = renderSequenceDiagram(`sequenceDiagram
577
+ participant A
578
+ participant B
579
+ alt outer
580
+ loop inner heading wider than outer frame and participant span
581
+ A->>B: x
582
+ end
583
+ end`)
584
+
585
+ expect(groupedNote).toContain("this note text runs far outside of the group container boundary")
586
+ expect(fragmentNote).toContain("this non adjacent note is deliberately much wider than the frame")
587
+ expect(nested).toContain("span ─╮│")
588
+ expect(nested).toContain("──────╯│")
589
+ })
590
+
591
+ test("does not draw external participants inside groups expanded by self messages", () => {
592
+ const output = renderSequenceDiagram(`sequenceDiagram
593
+ box G
594
+ participant A
595
+ end
596
+ participant B as External
597
+ A->>A: this self-loop extends underneath the external participant header`)
598
+ const groupBorderRight = output.split("\n")[0]!.lastIndexOf("╮")
599
+ const lines = output.split("\n")
600
+ const externalLabelRow = lines.findIndex((line) => line.includes("External"))
601
+ const externalHeaderLeft = lines[externalLabelRow + 1]!.lastIndexOf("─")
602
+
603
+ expect(externalHeaderLeft).toBeGreaterThan(groupBorderRight)
604
+ })
605
+
606
+ test("keeps adjacent wide participant group frames separate", () => {
607
+ const output = renderSequenceDiagram(
608
+ `sequenceDiagram
609
+ box First very wide group heading
610
+ participant A
611
+ end
612
+ box Second very wide group heading
613
+ participant B
614
+ end
615
+ A->>B: hi`,
616
+ { compact: true },
617
+ )
618
+ const topRow = output.split("\n")[0]!
619
+
620
+ expect(topRow).toContain("First very wide group heading")
621
+ expect(topRow).toContain("Second very wide group heading")
622
+ expect(topRow.indexOf("╮")).toBeLessThan(topRow.lastIndexOf("╭"))
623
+ })
624
+
625
+ test("renders many adjacent wide participant groups without excessive canvas growth", () => {
626
+ const groupCount = 16
627
+ const output = renderSequenceDiagram(
628
+ `sequenceDiagram
629
+ ${Array.from(
630
+ { length: groupCount },
631
+ (_, index) => ` box Group ${index} has a deliberately wide heading
632
+ participant P${index}
633
+ end`,
634
+ ).join("\n")}
635
+ P0->>P15: hi`,
636
+ { compact: true },
637
+ )
638
+
639
+ expect(Math.max(...output.split("\n").map(diagramTextWidth))).toBeLessThan(groupCount * 60)
640
+ })
641
+
642
+ test("renders full-height participant group boxes", () => {
643
+ const output = renderSequenceDiagram(`
644
+ sequenceDiagram
645
+ participant Browser
646
+ box Backend
647
+ participant API
648
+ participant Cache
649
+ participant DB
650
+ end
651
+ Browser->>API: GET /users/42
652
+ API->>Cache: get user:42
653
+ `)
654
+
655
+ expect(output).toMatchInlineSnapshot(`
656
+ " ╭─ Backend ───────────────────────────────╮
657
+ Browser │ API Cache DB │
658
+ ───┬─── │ ─┬─ ──┬── ─┬─ │
659
+ │ │ │ │ │ │
660
+ │ GET /users/42 │ │ │ │ │
661
+ ├──────────────────► │ │ │
662
+ │ │ │ │ │ │
663
+ │ │ │ get user:42 │ │ │
664
+ │ │ ├─────────────────► │ │
665
+ │ │ │ │ │ │
666
+ ╰─────────────────────────────────────────╯"
667
+ `)
668
+ })
669
+
670
+ test("lets message lines pass through group borders without intersections", () => {
671
+ const output = renderSequenceDiagram(`
672
+ sequenceDiagram
673
+ participant Browser
674
+ box Backend
675
+ participant API
676
+ end
677
+ Browser->>API: GET /users/42
678
+ `)
679
+ const arrowLine = output.split("\n").find((line) => line.includes("►"))!
680
+
681
+ expect(arrowLine).toContain("───────────────►")
682
+ expect(arrowLine).not.toContain("┼")
683
+ })
684
+
685
+ test("keeps filled arrowheads to one terminal column", () => {
686
+ const output = renderSequenceDiagram(`sequenceDiagram
687
+ box Backend
688
+ participant A
689
+ participant B
690
+ A->>B: request
691
+ end`)
692
+ const lines = output.split("\n")
693
+ const frameWidth = diagramTextWidth(lines.at(-1)!)
694
+
695
+ expect(Math.max(...lines.map(diagramTextWidth))).toBe(frameWidth)
696
+ })
697
+
698
+ test("renders self messages as loopback arrows", () => {
699
+ const output = renderSequenceDiagram(`
700
+ sequenceDiagram
701
+ participant Service
702
+ Service->>Service: Check Permissions
703
+ `)
704
+
705
+ expect(output).toMatchInlineSnapshot(`
706
+ "Service
707
+ ───┬───
708
+ │
709
+ ├────────────────────╮
710
+ │ Check Permissions │
711
+ ◄────────────────────╯
712
+ │"
713
+ `)
714
+ })
715
+
716
+ test("renders note badges in their reserved rows", () => {
717
+ const output = renderSequenceDiagram(`
718
+ sequenceDiagram
719
+ Browser->>Server: one
720
+ Note over Browser,Server: phase
721
+ Browser->>Server: two
722
+ `)
723
+ const lines = output.split("\n")
724
+ const noteRow = lines.findIndex((line) => line.includes("phase"))
725
+ const nextMessageRow = lines.findIndex((line) => line.includes("two"))
726
+
727
+ expect(noteRow).toBeGreaterThan(0)
728
+ expect(lines[noteRow - 1]?.trim()).toBe("│ │")
729
+ expect(lines[noteRow]).toContain(" phase ")
730
+ expect(lines[noteRow + 1]?.trim()).toBe("│ │")
731
+ expect(nextMessageRow).toBe(noteRow + 2)
732
+ })
733
+
734
+ test("renders br-delimited message labels across multiple rows", () => {
735
+ const output = renderSequenceDiagram(`
736
+ sequenceDiagram
737
+ Browser->>Server: POST connect-token<br/>· Basic (cached by browser)<br/>· X-OpenCode-Ticket: 1
738
+ `)
739
+
740
+ expect(output).toContain("POST connect-token")
741
+ expect(output).toContain("· Basic (cached by browser)")
742
+ expect(output).toContain("· X-OpenCode-Ticket: 1")
743
+ expect(output.indexOf("· X-OpenCode-Ticket: 1")).toBeLessThan(output.indexOf("├"))
744
+ })
745
+
746
+ test("renders br-delimited notes as rows without source tags", () => {
747
+ const output = renderSequenceDiagram(`sequenceDiagram
748
+ Note over Tool: directory instead?<br/>the WrongKind error carries the answer —<br/>branch to list, no extra round trip`)
749
+
750
+ expectDiagram(output).toContainInOrder(
751
+ "directory instead?",
752
+ "the WrongKind error carries the answer —",
753
+ "branch to list, no extra round trip",
754
+ )
755
+ expect(output).not.toMatch(/<br\s*\/?\s*>/i)
756
+ })
757
+ })