@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
package/tsconfig.json CHANGED
@@ -1,10 +1,10 @@
1
- {
2
- "$schema": "https://json.schemastore.org/tsconfig",
3
- "extends": "@tsconfig/bun/tsconfig.json",
4
- "compilerOptions": {
5
- "types": ["bun"],
6
- "noUnusedLocals": true,
7
- "lib": ["ESNext", "DOM", "DOM.Iterable"],
8
- "noUncheckedIndexedAccess": false
9
- }
10
- }
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "@tsconfig/bun/tsconfig.json",
4
+ "compilerOptions": {
5
+ "types": ["bun"],
6
+ "noUnusedLocals": true,
7
+ "lib": ["ESNext", "DOM", "DOM.Iterable"],
8
+ "noUncheckedIndexedAccess": false
9
+ }
10
+ }
@@ -1,203 +0,0 @@
1
- import { describe, expect, test } from "bun:test"
2
- import stringWidth from "string-width"
3
- import { DiagramCanvas, DiagramCanvasSizeError, type DiagramCanvasCell } from "./canvas.js"
4
-
5
- describe("DiagramCanvas", () => {
6
- test("rejects canvases that exceed the rendering budget", () => {
7
- expect(() => new DiagramCanvas(1_000, 251)).toThrow(DiagramCanvasSizeError)
8
- })
9
-
10
- test("rejects invalid canvas dimensions", () => {
11
- for (const [width, height] of [
12
- [-1, 10],
13
- [10, -1],
14
- [1.5, 10],
15
- [Number.NaN, 10],
16
- [Number.POSITIVE_INFINITY, 10],
17
- ]) {
18
- expect(() => new DiagramCanvas(width, height)).toThrow(DiagramCanvasSizeError)
19
- }
20
- })
21
-
22
- test("writes cells and text while clipping out-of-bounds positions", () => {
23
- const canvas = new DiagramCanvas<"label">(5, 2)
24
-
25
- canvas.setCell(0, 0, "A", "label")
26
- canvas.setCell(9, 0, "X", "label")
27
- canvas.setText(2, 1, "hey", "label")
28
-
29
- expect(canvas.toString()).toBe("A\n hey")
30
- })
31
-
32
- test("uses measured character widths for text placement", () => {
33
- const canvas = new DiagramCanvas<"label">(5, 1)
34
-
35
- canvas.setText(0, 0, "a界b", "label")
36
-
37
- expect(canvas.toString()).toBe("a界b")
38
- expect(stringWidth(canvas.toString())).toBe(4)
39
- })
40
-
41
- test("does not emit partial wide graphemes", () => {
42
- const clipped = new DiagramCanvas<"label">(1, 1)
43
- clipped.setText(0, 0, "界", "label")
44
- expect(clipped.toString()).toBe("")
45
- })
46
-
47
- test("keeps custom measurement for ASCII text", () => {
48
- let measurements = 0
49
- const canvas = new DiagramCanvas<"label">(5, 1, {
50
- measure: () => {
51
- measurements += 1
52
- return 2
53
- },
54
- })
55
-
56
- canvas.setText(0, 0, "ab", "label")
57
-
58
- expect(measurements).toBe(2)
59
- expect(canvas.getCell(2, 0)?.char).toBe("b")
60
- })
61
-
62
- test("preserves combined graphemes while placing later text", () => {
63
- const canvas = new DiagramCanvas<"label">(4, 1)
64
-
65
- canvas.setText(0, 0, "e\u0301x", "label")
66
-
67
- expect(canvas.toString()).toBe("e\u0301x")
68
- expect(stringWidth(canvas.toString())).toBe(2)
69
- })
70
-
71
- test("merges cells through the adapter-provided merge function", () => {
72
- type Style = "line"
73
- const canvas = new DiagramCanvas<Style>(3, 1, {
74
- mergeCell: (existing, incoming): DiagramCanvasCell<Style> => ({
75
- ...incoming,
76
- char: existing.char === "─" && incoming.char === "│" ? "┼" : incoming.char,
77
- }),
78
- })
79
-
80
- canvas.setCell(1, 0, "─", "line")
81
- canvas.setCell(1, 0, "│", "line")
82
-
83
- expect(canvas.toString()).toBe(" ┼")
84
-
85
- canvas.replaceCell(1, 0, "│", "line")
86
- expect(canvas.toString()).toBe(" │")
87
- })
88
-
89
- test("iterates style and metadata runs", () => {
90
- interface Metadata {
91
- stateId?: string
92
- }
93
- const canvas = new DiagramCanvas<"state", Metadata>(4, 1)
94
- const runs: string[] = []
95
-
96
- canvas.setText(0, 0, "AB", "state", { stateId: "A" })
97
- canvas.setText(2, 0, "CD", "state", { stateId: "B" })
98
- canvas.forEachRun(
99
- (run) => runs.push(`${run.text}:${run.style}:${run.cell.stateId}`),
100
- () => runs.push("newline"),
101
- { key: (cell) => [cell.style, cell.stateId] },
102
- )
103
-
104
- expect(runs).toEqual(["AB:state:A", "CD:state:B"])
105
- })
106
-
107
- test("retains styled trailing padding in render runs without changing plain text", () => {
108
- const canvas = new DiagramCanvas<"label">(8, 1)
109
- const runs: string[] = []
110
- canvas.setText(1, 0, " start ", "label")
111
-
112
- canvas.forEachRun(
113
- (run) => runs.push(run.text),
114
- () => {},
115
- )
116
-
117
- expect(canvas.toString()).toBe(" start")
118
- expect(runs).toEqual([" ", " start "])
119
- })
120
-
121
- test("can trim bottom whitespace for renderers with dynamic height", () => {
122
- const canvas = new DiagramCanvas<"label">(3, 3)
123
- const runs: string[] = []
124
- canvas.setText(0, 0, "top", "label")
125
-
126
- expect(canvas.toString()).toBe("top\n\n")
127
- expect(canvas.toString({ trimBottom: true })).toBe("top")
128
- expect(canvas.getTextSize()).toEqual({ width: 3, height: 3 })
129
- expect(canvas.getTextSize({ trimBottom: true })).toEqual({ width: 3, height: 1 })
130
-
131
- canvas.forEachRun(
132
- (run) => runs.push(run.text),
133
- () => runs.push("newline"),
134
- { trimBottom: true },
135
- )
136
- expect(runs).toEqual(["top"])
137
- })
138
-
139
- test("can trim unused leading whitespace reserved by layout", () => {
140
- const canvas = new DiagramCanvas<"label">(3, 3)
141
- canvas.setText(0, 2, "end", "label")
142
-
143
- expect(canvas.toString({ trimTop: true })).toBe("end")
144
- expect(canvas.getTextSize({ trimTop: true })).toEqual({ width: 3, height: 1 })
145
- })
146
-
147
- test("measures trim-aware text height without measuring row width", () => {
148
- let measurements = 0
149
- const canvas = new DiagramCanvas(8, 5, {
150
- measure: (text) => {
151
- measurements += 1
152
- return stringWidth(text)
153
- },
154
- })
155
- canvas.setText(1, 2, "middle")
156
- measurements = 0
157
-
158
- expect(canvas.getTextHeight({ trimTop: true, trimBottom: true })).toBe(1)
159
- expect(measurements).toBe(0)
160
- })
161
-
162
- test("updates tracked row extents when the last visible cell is cleared", () => {
163
- const canvas = new DiagramCanvas(8, 1)
164
- canvas.setText(1, 0, "abc")
165
- canvas.setCell(3, 0, " ")
166
-
167
- expect(canvas.toString()).toBe(" ab")
168
- expect(canvas.getTextSize()).toEqual({ width: 3, height: 1 })
169
- })
170
-
171
- test("keeps tracked extents equivalent to scanning after mixed writes", () => {
172
- const canvas = new DiagramCanvas<"line">(20, 10, {
173
- mergeCell: (_existing, incoming) => incoming,
174
- })
175
- let seed = 42
176
- const next = (limit: number) => {
177
- seed = (seed * 1_664_525 + 1_013_904_223) >>> 0
178
- return seed % limit
179
- }
180
-
181
- for (let index = 0; index < 200; index++) {
182
- const x = next(canvas.width)
183
- const y = next(canvas.height)
184
- const char = [" ", "x", "─"][next(3)]!
185
- if (next(2) === 0) canvas.setCell(x, y, char, "line")
186
- else canvas.replaceCell(x, y, char, "line")
187
- }
188
-
189
- const scanned = canvas.rows.map((row) => {
190
- let end = row.length
191
- while (end > 0 && row[end - 1]?.char === " ") end -= 1
192
- return row
193
- .slice(0, end)
194
- .map((cell) => cell.char)
195
- .join("")
196
- })
197
- const first = scanned.findIndex((line) => line.length > 0)
198
- const last = scanned.findLastIndex((line) => line.length > 0)
199
-
200
- expect(canvas.toString()).toBe(scanned.join("\n"))
201
- expect(canvas.getTextHeight({ trimTop: true, trimBottom: true })).toBe(first < 0 ? 0 : last - first + 1)
202
- })
203
- })
@@ -1,24 +0,0 @@
1
- import { describe, expect, test } from "bun:test"
2
- import { RGBA } from "@opentui/core"
3
- import { blendColor, createColorRampTheme, numberedStyleKeys, rgba } from "./style.js"
4
-
5
- describe("diagram style helpers", () => {
6
- test("converts rgb tuples and blends optional RGBA values", () => {
7
- const black = RGBA.fromInts(0, 0, 0, 255)
8
- const white = RGBA.fromInts(10, 20, 30, 255)
9
-
10
- expect(rgba([1, 2, 3]).equals(RGBA.fromInts(1, 2, 3, 255))).toBe(true)
11
- expect(blendColor(black, white, 0.5).equals(RGBA.fromInts(5, 10, 15, 255))).toBe(true)
12
- expect(blendColor(undefined, white, 0.5)?.equals(white)).toBe(true)
13
- expect(blendColor(undefined, undefined, 0.5)).toBeUndefined()
14
- })
15
-
16
- test("creates numbered style keys and color ramps", () => {
17
- const styles = numberedStyleKeys("requestFade", [1, 2, 3] as const)
18
- const theme = createColorRampTheme(styles, RGBA.fromInts(0, 0, 0, 255), RGBA.fromInts(12, 24, 36, 255))
19
-
20
- expect(styles).toEqual(["requestFade1", "requestFade2", "requestFade3"])
21
- expect(theme.requestFade1.equals(RGBA.fromInts(3, 6, 9, 255))).toBe(true)
22
- expect(theme.requestFade3.equals(RGBA.fromInts(9, 18, 27, 255))).toBe(true)
23
- })
24
- })
@@ -1,116 +0,0 @@
1
- import { describe, expect, test } from "bun:test"
2
- import { BorderChars } from "@opentui/core"
3
- import { DiagramCanvas } from "./canvas.js"
4
- import {
5
- diagramArrowHead,
6
- drawDiagramDiamond,
7
- drawDiagramFrame,
8
- drawOrthogonalPath,
9
- mergeDiagramLineGlyph,
10
- } from "./drawing.js"
11
-
12
- describe("diagram drawing", () => {
13
- test("merges line glyphs with square and rounded corners", () => {
14
- expect(mergeDiagramLineGlyph("─", "│")).toBe("┼")
15
- expect(mergeDiagramLineGlyph("─", "│", "rounded")).toBe("┼")
16
- expect(mergeDiagramLineGlyph("─", "╭", "rounded")).toBe("┬")
17
- expect(mergeDiagramLineGlyph("a", "│")).toBeUndefined()
18
- })
19
-
20
- test("draws frames through caller-provided cell writers", () => {
21
- const canvas = new DiagramCanvas<"frame">(8, 4)
22
- drawDiagramFrame(
23
- { left: 1, top: 0, width: 6, height: 4, centerX: 4, centerY: 2 },
24
- BorderChars.rounded,
25
- (x, y, char) => canvas.setCell(x, y, char, "frame"),
26
- )
27
-
28
- expect(canvas.toString({ trimBottom: true })).toBe(" ╭────╮\n │ │\n │ │\n ╰────╯")
29
- })
30
-
31
- test("draws diamond frames through caller-provided cell writers", () => {
32
- const canvas = new DiagramCanvas<"frame">(9, 5)
33
- drawDiagramDiamond({ left: 0, top: 0, width: 9, height: 5, centerX: 4, centerY: 2 }, (x, y, char) =>
34
- canvas.setCell(x, y, char, "frame"),
35
- )
36
-
37
- expect(canvas.toString({ trimBottom: true })).toBe(" ╭───╮\n╭─╯ ╰─╮\n│ │\n╰─╮ ╭─╯\n ╰───╯")
38
- expect(canvas.toString()).not.toMatch(/[╱╲\\/]/)
39
- })
40
-
41
- test("draws orthogonal paths with selected corner style", () => {
42
- const canvas = new DiagramCanvas<"edge">(7, 4)
43
- drawOrthogonalPath(
44
- [
45
- { x: 0, y: 0 },
46
- { x: 3, y: 0 },
47
- { x: 3, y: 2 },
48
- { x: 6, y: 2 },
49
- ],
50
- (x, y, char) => canvas.setCell(x, y, char, "edge"),
51
- { cornerStyle: "rounded" },
52
- )
53
-
54
- expect(canvas.toString({ trimBottom: true })).toBe("───╮\n │\n ╰──")
55
- })
56
-
57
- test("draws orthogonal paths with heavy line style", () => {
58
- const canvas = new DiagramCanvas<"edge">(7, 4)
59
- drawOrthogonalPath(
60
- [
61
- { x: 0, y: 0 },
62
- { x: 3, y: 0 },
63
- { x: 3, y: 2 },
64
- { x: 6, y: 2 },
65
- ],
66
- (x, y, char) => canvas.setCell(x, y, char, "edge"),
67
- { lineStyle: "heavy" },
68
- )
69
-
70
- expect(canvas.toString({ trimBottom: true })).toBe("━━━┓\n ┃\n ┗━━")
71
- })
72
-
73
- test("draws orthogonal paths with dashed line style", () => {
74
- const canvas = new DiagramCanvas<"edge">(9, 1)
75
- drawOrthogonalPath(
76
- [
77
- { x: 0, y: 0 },
78
- { x: 8, y: 0 },
79
- ],
80
- (x, y, char) => canvas.setCell(x, y, char, "edge"),
81
- { lineStyle: "dashed" },
82
- )
83
-
84
- expect(canvas.toString({ trimBottom: true })).toBe("─ ─ ─ ─")
85
- })
86
-
87
- test("keeps container frame policy separate from edge drawing policy", () => {
88
- const canvas = new DiagramCanvas<"group" | "edge">(8, 5, {
89
- mergeCell: (existing, incoming) => {
90
- if (incoming.style === "edge") return incoming
91
- return existing.char === " " ? incoming : existing
92
- },
93
- })
94
-
95
- drawDiagramFrame(
96
- { left: 1, top: 0, width: 6, height: 5, centerX: 4, centerY: 2 },
97
- BorderChars.rounded,
98
- (x, y, char) => canvas.setCell(x, y, char, "group"),
99
- )
100
- drawOrthogonalPath(
101
- [
102
- { x: 0, y: 2 },
103
- { x: 7, y: 2 },
104
- ],
105
- (x, y, char) => canvas.setCell(x, y, char, "edge"),
106
- )
107
- canvas.setCell(7, 2, "▶", "edge")
108
-
109
- expect(canvas.toString({ trimBottom: true })).toBe(" ╭────╮\n │ │\n───────▶\n │ │\n ╰────╯")
110
- })
111
-
112
- test("selects filled and line arrow heads", () => {
113
- expect(diagramArrowHead("right")).toBe("▶")
114
- expect(diagramArrowHead("left", "line")).toBe("←")
115
- })
116
- })
@@ -1,112 +0,0 @@
1
- import { describe, expect, test } from "bun:test"
2
- import {
3
- diagramBoundsFromBounds,
4
- diagramBoundsFromPoints,
5
- diagramBoundsFromRect,
6
- boundsSidePoint,
7
- directionBetween,
8
- lane,
9
- orthogonalPath,
10
- orthogonalPathPoints,
11
- pathThrough,
12
- pathViaLane,
13
- point,
14
- translateDiagramBounds,
15
- walkOrthogonalSegment,
16
- } from "./geometry.js"
17
-
18
- describe("diagram geometry", () => {
19
- test("directions are only defined for orthogonal point pairs", () => {
20
- expect(directionBetween(point(1, 2), point(5, 2))).toBe("right")
21
- expect(directionBetween(point(1, 2), point(1, 0))).toBe("up")
22
- expect(directionBetween(point(1, 2), point(5, 4))).toBeUndefined()
23
- expect(directionBetween(point(1, 2), point(1, 2))).toBeUndefined()
24
- })
25
-
26
- test("bounds side points describe border and outside ports", () => {
27
- const bounds = { left: 10, top: 4, width: 8, height: 5, centerX: 14, centerY: 6 }
28
-
29
- expect(boundsSidePoint(bounds, "left", "border")).toEqual(point(10, 6))
30
- expect(boundsSidePoint(bounds, "left")).toEqual(point(9, 6))
31
- expect(boundsSidePoint(bounds, "right", "border")).toEqual(point(17, 6))
32
- expect(boundsSidePoint(bounds, "right")).toEqual(point(18, 6))
33
- expect(boundsSidePoint(bounds, "top", "border")).toEqual(point(14, 4))
34
- expect(boundsSidePoint(bounds, "bottom")).toEqual(point(14, 9))
35
- })
36
-
37
- test("bounds helpers create, translate, and union bounds", () => {
38
- const bounds = diagramBoundsFromRect(2, 3, 5, 4)
39
-
40
- expect(bounds).toEqual({ left: 2, top: 3, width: 5, height: 4, centerX: 4, centerY: 5 })
41
- translateDiagramBounds(bounds, 3, -1)
42
- expect(bounds).toEqual({ left: 5, top: 2, width: 5, height: 4, centerX: 7, centerY: 4 })
43
- expect(diagramBoundsFromBounds([bounds, diagramBoundsFromRect(0, 0, 2, 2)])).toEqual({
44
- left: 0,
45
- top: 0,
46
- width: 10,
47
- height: 6,
48
- centerX: 5,
49
- centerY: 3,
50
- })
51
- expect(diagramBoundsFromPoints([point(2, 2), point(4, 5)])).toEqual({
52
- left: 2,
53
- top: 2,
54
- width: 3,
55
- height: 4,
56
- centerX: 3,
57
- centerY: 4,
58
- })
59
- })
60
-
61
- test("paths compose through lanes while removing duplicate joints", () => {
62
- expect(pathThrough([point(0, 0), point(0, 0), point(3, 0)])).toEqual([point(0, 0), point(3, 0)])
63
- expect(pathViaLane(point(0, 0), lane("x", 4), point(8, 3))).toEqual([
64
- point(0, 0),
65
- point(4, 0),
66
- point(4, 3),
67
- point(8, 3),
68
- ])
69
- })
70
-
71
- test("composed paths do not alias caller-owned points", () => {
72
- const start = point(0, 0)
73
- const end = point(8, 3)
74
- const firstPath = pathViaLane(start, lane("x", 4), end)
75
- const secondPath = pathViaLane(start, lane("x", 5), end)
76
-
77
- expect(firstPath[0]).not.toBe(start)
78
- expect(firstPath[firstPath.length - 1]).not.toBe(end)
79
- expect(firstPath[0]).not.toBe(secondPath[0])
80
-
81
- firstPath[0]!.x += 10
82
- firstPath[firstPath.length - 1]!.y += 10
83
-
84
- expect(start).toEqual(point(0, 0))
85
- expect(end).toEqual(point(8, 3))
86
- expect(secondPath[0]).toEqual(point(0, 0))
87
- expect(secondPath[secondPath.length - 1]).toEqual(point(8, 3))
88
- })
89
-
90
- test("orthogonal paths choose a terminal lane on the dominant axis", () => {
91
- expect(orthogonalPath(point(0, 0), point(10, 4))).toEqual([point(0, 0), point(6, 0), point(6, 4), point(10, 4)])
92
- })
93
-
94
- test("orthogonal segment walkers exclude endpoints", () => {
95
- const visited: Array<{ x: number; y: number }> = []
96
- walkOrthogonalSegment(point(0, 0), point(3, 0), false, (next) => {
97
- visited.push(next)
98
- })
99
- expect(visited).toEqual([point(1, 0), point(2, 0)])
100
- })
101
-
102
- test("orthogonal path points include endpoints without duplicating joints", () => {
103
- expect(orthogonalPathPoints([point(0, 0), point(3, 0), point(3, 2)])).toEqual([
104
- point(0, 0),
105
- point(1, 0),
106
- point(2, 0),
107
- point(3, 0),
108
- point(3, 1),
109
- point(3, 2),
110
- ])
111
- })
112
- })
@@ -1,87 +0,0 @@
1
- import { describe, expect, test } from "bun:test"
2
- import { SpatialIndex, spatialPathClaim, spatialRectClaim } from "./spatial.js"
3
-
4
- const body = spatialRectClaim("body", "node:A", "body", { left: 2, top: 1, width: 4, height: 3 })
5
- const label = spatialRectClaim("label", "edge:A-B", "label", { left: 8, top: 1, width: 5, height: 1 })
6
- const route = spatialPathClaim("route", "edge:A-B", "route", [
7
- { x: 5, y: 2 },
8
- { x: 10, y: 2 },
9
- ])
10
-
11
- describe("SpatialIndex", () => {
12
- test("composition is associative, commutative, idempotent, and has an identity", () => {
13
- const a = SpatialIndex.empty().add(body)
14
- const b = SpatialIndex.empty().add(label)
15
- const c = SpatialIndex.empty().add(route)
16
-
17
- expect(SpatialIndex.empty().overlay(a).claims).toEqual(a.claims)
18
- expect(a.overlay(b).claims).toEqual(b.overlay(a).claims)
19
- expect(a.overlay(b).overlay(c).claims).toEqual(a.overlay(b.overlay(c)).claims)
20
- expect(a.overlay(a).claims).toEqual(a.claims)
21
- })
22
-
23
- test("routes may share routes but cannot cross unrelated semantic bodies", () => {
24
- const index = SpatialIndex.empty().add(body, route)
25
- const crossingBody = spatialPathClaim("cross-body", "edge:C-D", "route", [
26
- { x: 0, y: 2 },
27
- { x: 8, y: 2 },
28
- ])
29
- const crossingRoute = spatialPathClaim("cross-route", "edge:C-D", "route", [
30
- { x: 7, y: 0 },
31
- { x: 7, y: 4 },
32
- ])
33
-
34
- expect(index.isFree(crossingBody)).toBe(false)
35
- expect(index.isFree(crossingRoute)).toBe(true)
36
- })
37
-
38
- test("declared endpoint contacts do not permit contact elsewhere", () => {
39
- const index = SpatialIndex.empty().add(body)
40
- const candidate = spatialPathClaim("candidate", "edge:B-A", "route", [
41
- { x: 0, y: 2 },
42
- { x: 2, y: 2 },
43
- ])
44
-
45
- expect(index.isFree(candidate)).toBe(false)
46
- expect(index.isFree(candidate, { contacts: [{ owner: "node:A", points: [{ x: 2, y: 2 }] }] })).toBe(true)
47
- })
48
-
49
- test("firstFit chooses the first collision-free candidate", () => {
50
- const index = SpatialIndex.empty().add(body)
51
- const blocked = spatialRectClaim("blocked", "label:B", "label", { left: 3, top: 2, width: 2, height: 1 })
52
- const clear = spatialRectClaim("clear", "label:B", "label", { left: 7, top: 2, width: 2, height: 1 })
53
-
54
- expect(index.firstFit([{ claim: blocked }, { claim: clear }])?.claim.id).toBe("clear")
55
- })
56
-
57
- test("clearance is symmetric in both axes", () => {
58
- const index = SpatialIndex.empty().add(body)
59
- const touchingRight = spatialRectClaim("right", "label:B", "label", { left: 6, top: 1, width: 2, height: 1 })
60
- const touchingBelow = spatialRectClaim("below", "label:C", "label", { left: 2, top: 4, width: 2, height: 1 })
61
-
62
- expect(index.isFree(touchingRight)).toBe(true)
63
- expect(index.isFree(touchingRight, { clearance: 1 })).toBe(false)
64
- expect(index.isFree(touchingBelow)).toBe(true)
65
- expect(index.isFree(touchingBelow, { clearance: 1 })).toBe(false)
66
- })
67
-
68
- test("axis-specific clearance does not move unrelated rows", () => {
69
- const index = SpatialIndex.empty().add(body)
70
- const touchingRight = spatialRectClaim("right", "label:B", "label", { left: 6, top: 1, width: 2, height: 1 })
71
- const touchingBelow = spatialRectClaim("below", "label:C", "label", { left: 2, top: 4, width: 2, height: 1 })
72
-
73
- expect(index.isFree(touchingRight, { clearance: { x: 1, y: 0 } })).toBe(false)
74
- expect(index.isFree(touchingBelow, { clearance: { x: 1, y: 0 } })).toBe(true)
75
- })
76
-
77
- test("rejects malformed geometry instead of weakening collision checks", () => {
78
- expect(() => spatialRectClaim("zero", "node", "body", { left: 0, top: 0, width: 0, height: 1 })).toThrow()
79
- expect(() =>
80
- spatialPathClaim("diagonal", "edge", "route", [
81
- { x: 0, y: 0 },
82
- { x: 1, y: 1 },
83
- ]),
84
- ).toThrow()
85
- expect(() => SpatialIndex.empty().add(body).isFree(label, { clearance: Number.POSITIVE_INFINITY })).toThrow()
86
- })
87
- })
@@ -1,33 +0,0 @@
1
- import { describe, expect, test } from "bun:test"
2
- import { diagramTextWidth, measureDiagramTextBox, parseDiagramTextLines, splitDiagramLines } from "./text.js"
3
-
4
- describe("diagram text helpers", () => {
5
- test("splits Mermaid-style line breaks", () => {
6
- expect(splitDiagramLines("one<br/> two <br>three")).toEqual(["one", "two", "three"])
7
- })
8
-
9
- test("extracts Mermaid italic markup from visible text", () => {
10
- expect(parseDiagramTextLines("plain <i>italic</i><br/><em>again</em>")).toEqual([
11
- {
12
- text: "plain italic",
13
- runs: [
14
- { text: "plain ", italic: false },
15
- { text: "italic", italic: true },
16
- ],
17
- },
18
- { text: "again", runs: [{ text: "again", italic: true }] },
19
- ])
20
- })
21
-
22
- test("measures padded text boxes", () => {
23
- expect(measureDiagramTextBox("wide<br/>x", { paddingX: 2, paddingY: 1 })).toEqual({
24
- width: 8,
25
- height: 4,
26
- lines: ["wide", "x"],
27
- })
28
- })
29
-
30
- test("measures terminal cell width", () => {
31
- expect(diagramTextWidth("abc")).toBe(3)
32
- })
33
- })