@celestia-island/hikari 0.55.413 → 0.55.415
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 +1 -1
- package/src/components/HkSelectionGrid.scss +20 -8
- package/src/components/HkSelectionGrid.test.tsx +357 -0
- package/src/components/HkSelectionGrid.tsx +5 -3
- package/src/components/HkUsageFlow.barrel.test.ts +39 -0
- package/src/components/HkUsageFlow.scss +133 -0
- package/src/components/HkUsageFlow.test.tsx +236 -0
- package/src/components/HkUsageFlow.tsx +253 -0
- package/src/components/usageFlowLayout.test.ts +699 -0
- package/src/components/usageFlowLayout.ts +716 -0
- package/src/index.ts +20 -0
|
@@ -0,0 +1,699 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
flowLabelVisible,
|
|
5
|
+
formatFlowPercent,
|
|
6
|
+
truncateFlowLabel,
|
|
7
|
+
usageFlowLayout,
|
|
8
|
+
USAGE_FLOW_PALETTE,
|
|
9
|
+
type HkUsageFlowRow,
|
|
10
|
+
} from "./usageFlowLayout";
|
|
11
|
+
|
|
12
|
+
/** Three callers → two upstreams → three models, comfortably above the
|
|
13
|
+
* node-visibility floor so no geometry is clamped. */
|
|
14
|
+
const ROWS: HkUsageFlowRow[] = [
|
|
15
|
+
{ source: "k1", middle: "up-a", target: "m1", value: 500 },
|
|
16
|
+
{ source: "k1", middle: "up-b", target: "m2", value: 300 },
|
|
17
|
+
{ source: "k2", middle: "up-a", target: "m1", value: 200 },
|
|
18
|
+
{ source: "k2", middle: "up-b", target: "m3", value: 100 },
|
|
19
|
+
{ source: "k3", middle: "up-a", target: "m3", value: 50 },
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
function columnNodes(layout: ReturnType<typeof usageFlowLayout>, column: number) {
|
|
23
|
+
return layout.nodes.filter((node) => node.column === column);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function linksFrom(layout: ReturnType<typeof usageFlowLayout>, key: string) {
|
|
27
|
+
return layout.links.filter((link) => link.sourceKey === key);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Every coordinate pair inside a ribbon path, in document order. */
|
|
31
|
+
function pathPoints(d: string): Array<{ x: number; y: number }> {
|
|
32
|
+
return [...d.matchAll(/(-?\d+(?:\.\d+)?)\s+(-?\d+(?:\.\d+)?)/g)].map((match) => ({
|
|
33
|
+
x: Number(match[1]),
|
|
34
|
+
y: Number(match[2]),
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("usageFlowLayout", () => {
|
|
39
|
+
it("returns an empty layout when no row carries a positive value", () => {
|
|
40
|
+
const layout = usageFlowLayout([
|
|
41
|
+
{ source: "a", middle: "b", target: "c", value: 0 },
|
|
42
|
+
{ source: "a", middle: "b", target: "d", value: -3 },
|
|
43
|
+
{ source: "a", middle: "b", target: "e", value: Number.NaN },
|
|
44
|
+
]);
|
|
45
|
+
expect(layout.empty).toBe(true);
|
|
46
|
+
expect(layout.total).toBe(0);
|
|
47
|
+
expect(layout.nodes).toHaveLength(0);
|
|
48
|
+
expect(layout.legend).toHaveLength(0);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("sums duplicate triples and keeps the three columns conserved", () => {
|
|
52
|
+
const layout = usageFlowLayout([
|
|
53
|
+
...ROWS,
|
|
54
|
+
{ source: "k1", middle: "up-a", target: "m1", value: 500 },
|
|
55
|
+
]);
|
|
56
|
+
// 500+300+200+100+50 plus the duplicated 500 triple.
|
|
57
|
+
expect(layout.total).toBe(1650);
|
|
58
|
+
for (const column of [0, 1, 2]) {
|
|
59
|
+
const sum = columnNodes(layout, column).reduce((acc, node) => acc + node.value, 0);
|
|
60
|
+
expect(sum, `column ${column} total`).toBe(1650);
|
|
61
|
+
}
|
|
62
|
+
const k1 = layout.nodes.find((node) => node.fullLabel === "k1" && node.column === 0);
|
|
63
|
+
expect(k1?.value).toBe(1300);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("orders each column by value descending and keeps geometry inside the frame", () => {
|
|
67
|
+
const layout = usageFlowLayout(ROWS, { width: 900, height: 400 });
|
|
68
|
+
for (const column of [0, 1, 2]) {
|
|
69
|
+
const nodes = columnNodes(layout, column);
|
|
70
|
+
const values = nodes.map((node) => node.value);
|
|
71
|
+
expect(values, `column ${column} order`).toEqual([...values].sort((a, b) => b - a));
|
|
72
|
+
for (const node of nodes) {
|
|
73
|
+
expect(node.x).toBeGreaterThanOrEqual(0);
|
|
74
|
+
expect(node.x + node.width).toBeLessThanOrEqual(900);
|
|
75
|
+
expect(node.y).toBeGreaterThanOrEqual(0);
|
|
76
|
+
expect(node.y + node.height).toBeLessThanOrEqual(400);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
// The last column's bars sit at the right edge, the first column's at
|
|
80
|
+
// the left edge, and the middle column is centred on the frame.
|
|
81
|
+
const first = columnNodes(layout, 0)[0];
|
|
82
|
+
const middle = columnNodes(layout, 1)[0];
|
|
83
|
+
const last = columnNodes(layout, 2)[0];
|
|
84
|
+
expect(first.x).toBe(8);
|
|
85
|
+
expect(middle.x).toBe(Math.round((900 - 14) / 2));
|
|
86
|
+
expect(last.x + last.width).toBe(900 - 8);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("uses one shared scale: node heights are proportional to their values", () => {
|
|
90
|
+
const layout = usageFlowLayout(ROWS, { height: 480 });
|
|
91
|
+
const k1 = layout.nodes.find((node) => node.fullLabel === "k1" && node.column === 0)!;
|
|
92
|
+
const k2 = layout.nodes.find((node) => node.fullLabel === "k2" && node.column === 0)!;
|
|
93
|
+
expect(k1.height / k1.value).toBeCloseTo(k2.height / k2.value, 3);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("orders equal values by label, not insertion order", () => {
|
|
97
|
+
const layout = usageFlowLayout([
|
|
98
|
+
{ source: "c", middle: "up", target: "m3", value: 10 },
|
|
99
|
+
{ source: "a", middle: "up", target: "m1", value: 10 },
|
|
100
|
+
{ source: "b", middle: "up", target: "m2", value: 10 },
|
|
101
|
+
]);
|
|
102
|
+
expect(columnNodes(layout, 0).map((node) => node.fullLabel)).toEqual(["a", "b", "c"]);
|
|
103
|
+
expect(layout.legend.map((row) => row.label)).toEqual(["m1", "m2", "m3"]);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("stacks a node's ribbons in the neighbour's vertical order", () => {
|
|
107
|
+
const layout = usageFlowLayout([
|
|
108
|
+
{ source: "k1", middle: "up-a", target: "m1", value: 100 },
|
|
109
|
+
{ source: "k1", middle: "up-b", target: "m2", value: 100 },
|
|
110
|
+
]);
|
|
111
|
+
const k1 = layout.nodes.find((node) => node.fullLabel === "k1" && node.column === 0)!;
|
|
112
|
+
const outgoing = layout.links
|
|
113
|
+
.filter((link) => link.sourceKey === k1.key)
|
|
114
|
+
.sort((a, b) => a.startY - b.startY);
|
|
115
|
+
const targets = outgoing.map(
|
|
116
|
+
(link) => layout.nodes.find((node) => node.key === link.targetKey)!.fullLabel,
|
|
117
|
+
);
|
|
118
|
+
// up-a is the higher-valued (first) middle node; its ribbon starts
|
|
119
|
+
// first. A reversed sort would invert this while stacking tests stay
|
|
120
|
+
// order-agnostic.
|
|
121
|
+
expect(targets).toEqual(["up-a", "up-b"]);
|
|
122
|
+
expect(outgoing[0].startY).toBeLessThan(outgoing[1].startY);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("stacks each node's ribbons exactly on its scaled height, on both sides", () => {
|
|
126
|
+
const layout = usageFlowLayout(ROWS, { height: 480 });
|
|
127
|
+
const scale = layout.links[0].thickness / layout.links[0].value;
|
|
128
|
+
for (const link of layout.links) {
|
|
129
|
+
expect(link.thickness, `exact scaled slot for ${link.key}`).toBeCloseTo(link.value * scale, 6);
|
|
130
|
+
expect(link.path.includes("NaN"), `no NaN in ${link.key}`).toBe(false);
|
|
131
|
+
}
|
|
132
|
+
for (const node of layout.nodes) {
|
|
133
|
+
const outgoing = layout.links
|
|
134
|
+
.filter((link) => link.sourceKey === node.key)
|
|
135
|
+
.sort((a, b) => a.startY - b.startY);
|
|
136
|
+
const incoming = layout.links
|
|
137
|
+
.filter((link) => link.targetKey === node.key)
|
|
138
|
+
.sort((a, b) => a.endY - b.endY);
|
|
139
|
+
// Outgoing slots are contiguous from the bar's top edge, and the
|
|
140
|
+
// stack closes exactly at the node's SCALED height.
|
|
141
|
+
let cursor = node.y;
|
|
142
|
+
for (const link of outgoing) {
|
|
143
|
+
expect(link.startY, `outgoing cursor of ${node.fullLabel}`).toBeCloseTo(cursor, 1);
|
|
144
|
+
cursor += link.value * scale;
|
|
145
|
+
}
|
|
146
|
+
if (outgoing.length > 0) {
|
|
147
|
+
expect(cursor, `outgoing stack end of ${node.fullLabel}`).toBeCloseTo(
|
|
148
|
+
node.y + node.value * scale,
|
|
149
|
+
1,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
let end = node.y;
|
|
153
|
+
for (const link of incoming) {
|
|
154
|
+
expect(link.endY, `incoming cursor of ${node.fullLabel}`).toBeCloseTo(end, 1);
|
|
155
|
+
end += link.value * scale;
|
|
156
|
+
}
|
|
157
|
+
if (incoming.length > 0) {
|
|
158
|
+
expect(end, `incoming stack end of ${node.fullLabel}`).toBeCloseTo(
|
|
159
|
+
node.y + node.value * scale,
|
|
160
|
+
1,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
// The floored bar may be TALLER than the stack, never shorter.
|
|
164
|
+
expect(node.value * scale).toBeLessThanOrEqual(node.height + 0.01);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("floors a sub-visible node's bar without letting its layer escape", () => {
|
|
169
|
+
const rows = [
|
|
170
|
+
{ source: "huge", middle: "up", target: "m1", value: 9_800_000 },
|
|
171
|
+
...Array.from({ length: 9 }, (_, i) => ({
|
|
172
|
+
source: `tiny-${i}`,
|
|
173
|
+
middle: "up",
|
|
174
|
+
target: "m1",
|
|
175
|
+
value: 2_000,
|
|
176
|
+
})),
|
|
177
|
+
];
|
|
178
|
+
const layout = usageFlowLayout(rows, { height: 420 });
|
|
179
|
+
const scale = layout.links[0].thickness / layout.links[0].value;
|
|
180
|
+
const tiny = layout.nodes.find((node) => node.fullLabel === "tiny-0")!;
|
|
181
|
+
expect(tiny.height).toBe(2); // the visibility floor
|
|
182
|
+
// A 2-unit bar cannot carry a label; the hint/legend still name it.
|
|
183
|
+
expect(tiny.labelVisible).toBe(false);
|
|
184
|
+
expect(layout.nodes.find((node) => node.fullLabel === "huge")!.labelVisible).toBe(true);
|
|
185
|
+
expect(tiny.value * scale).toBeLessThan(tiny.height);
|
|
186
|
+
const outgoing = layout.links.filter((link) => link.sourceKey === tiny.key);
|
|
187
|
+
expect(outgoing).toHaveLength(1);
|
|
188
|
+
expect(outgoing[0].startY).toBeCloseTo(tiny.y, 2);
|
|
189
|
+
expect(outgoing[0].thickness).toBeCloseTo(tiny.value * scale, 3);
|
|
190
|
+
// No slot origin escapes the drawn bar.
|
|
191
|
+
for (const link of outgoing) {
|
|
192
|
+
expect(link.startY).toBeLessThanOrEqual(tiny.y + tiny.height + 0.01);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("clamps every column into the frame for any requested node cap", () => {
|
|
197
|
+
const many = Array.from({ length: 60 }, (_, i) => ({
|
|
198
|
+
source: `s${String(i).padStart(2, "0")}`,
|
|
199
|
+
middle: "up",
|
|
200
|
+
target: "m1",
|
|
201
|
+
value: 10,
|
|
202
|
+
}));
|
|
203
|
+
for (const maxNodes of [0, 1, 2, 1000, Number.NaN]) {
|
|
204
|
+
const layout = usageFlowLayout(many, { height: 420, maxNodes });
|
|
205
|
+
for (const node of layout.nodes) {
|
|
206
|
+
expect(node.y, `top of ${node.fullLabel} (cap ${maxNodes})`).toBeGreaterThanOrEqual(0);
|
|
207
|
+
expect(node.y + node.height, `bottom of ${node.fullLabel} (cap ${maxNodes})`).toBeLessThanOrEqual(420);
|
|
208
|
+
}
|
|
209
|
+
const sources = columnNodes(layout, 0);
|
|
210
|
+
// The magnitudes survive: the largest bar is not the 2-unit floor.
|
|
211
|
+
expect(Math.max(...sources.map((node) => node.height))).toBeGreaterThan(2);
|
|
212
|
+
// A cap of 2 is honoured; a cap of 1 still leaves room for the bucket.
|
|
213
|
+
if (maxNodes === 2) expect(sources).toHaveLength(2);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("falls back to the empty face when the total itself is non-finite", () => {
|
|
218
|
+
const layout = usageFlowLayout([
|
|
219
|
+
{ source: "a", middle: "u", target: "m1", value: 1e308 },
|
|
220
|
+
{ source: "b", middle: "u", target: "m2", value: 1e308 },
|
|
221
|
+
]);
|
|
222
|
+
// Two FINITE distinct triples whose sum overflows: an empty face is
|
|
223
|
+
// the honest answer — NaN geometry and `d="… NaN …"` are not.
|
|
224
|
+
expect(layout.empty).toBe(true);
|
|
225
|
+
expect(layout.nodes).toHaveLength(0);
|
|
226
|
+
expect(layout.links).toHaveLength(0);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("bails to the empty face when the window total would overflow a fold", () => {
|
|
230
|
+
// Three callers on one target with a 2-node cap WOULD fold two of them
|
|
231
|
+
// into one bucket whose sum overflows. The grand-total guard catches
|
|
232
|
+
// it before any merge runs, so the chart never silently drops traffic
|
|
233
|
+
// while the legend still sums to 300%.
|
|
234
|
+
const layout = usageFlowLayout(
|
|
235
|
+
[
|
|
236
|
+
{ source: "a", middle: "u", target: "m1", value: 1e308 },
|
|
237
|
+
{ source: "b", middle: "u", target: "m1", value: 1e308 },
|
|
238
|
+
{ source: "c", middle: "u", target: "m1", value: 1e308 },
|
|
239
|
+
],
|
|
240
|
+
{ maxNodes: 2 },
|
|
241
|
+
);
|
|
242
|
+
expect(layout.empty).toBe(true);
|
|
243
|
+
expect(layout.nodes).toHaveLength(0);
|
|
244
|
+
expect(layout.legend).toHaveLength(0);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("keeps geometry finite when the total is tiny enough to overflow the scale", () => {
|
|
248
|
+
// NaN/Infinity guards on the VALUES are not enough: 1e-320 + 1e-320 is
|
|
249
|
+
// finite but `usable / total` overflows. The scale clamp keeps every
|
|
250
|
+
// coordinate finite (the bars sit on the visibility floor).
|
|
251
|
+
const layout = usageFlowLayout([
|
|
252
|
+
{ source: "a", middle: "u", target: "m1", value: 1e-320 },
|
|
253
|
+
{ source: "b", middle: "u", target: "m2", value: 1e-320 },
|
|
254
|
+
]);
|
|
255
|
+
expect(layout.empty).toBe(false);
|
|
256
|
+
for (const node of layout.nodes) {
|
|
257
|
+
expect(Number.isFinite(node.y)).toBe(true);
|
|
258
|
+
expect(Number.isFinite(node.height)).toBe(true);
|
|
259
|
+
}
|
|
260
|
+
for (const link of layout.links) expect(link.path.includes("Infinity")).toBe(false);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("orders unregistered models by their real totals, descending", () => {
|
|
264
|
+
const layout = usageFlowLayout([
|
|
265
|
+
{ source: "k", middle: "up", target: "small", value: 10 },
|
|
266
|
+
{ source: "k", middle: "up", target: "big", value: 30 },
|
|
267
|
+
{ source: "k", middle: "up", target: "mid", value: 20 },
|
|
268
|
+
]);
|
|
269
|
+
expect(layout.legend.map((row) => row.label)).toEqual(["big", "mid", "small"]);
|
|
270
|
+
expect(layout.legend.map((row) => row.value)).toEqual([30, 20, 10]);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("keeps proportional bars at capacity (the cap is not just a fit check)", () => {
|
|
274
|
+
const many = Array.from({ length: 30 }, (_, i) => ({
|
|
275
|
+
source: `s${String(i).padStart(2, "0")}`,
|
|
276
|
+
middle: "up",
|
|
277
|
+
target: "m1",
|
|
278
|
+
value: 10,
|
|
279
|
+
}));
|
|
280
|
+
const layout = usageFlowLayout(many, { height: 420, maxNodes: 0 });
|
|
281
|
+
const sources = columnNodes(layout, 0);
|
|
282
|
+
// The cap shrinks below the raw fit count so every kept bar keeps a
|
|
283
|
+
// proportional band — none sits on the 2-unit visibility floor.
|
|
284
|
+
for (const node of sources) {
|
|
285
|
+
if (node.others) continue;
|
|
286
|
+
expect(node.height, `${node.fullLabel} must not be flattened`).toBeGreaterThan(2);
|
|
287
|
+
}
|
|
288
|
+
// ...and the folded bucket still carries the tail.
|
|
289
|
+
expect(sources.some((node) => node.others)).toBe(true);
|
|
290
|
+
expect(sources.reduce((acc, node) => acc + node.value, 0)).toBe(300);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("pins the label-visibility boundary against the ROUNDED bar height", () => {
|
|
294
|
+
// The DOM height is rounded to two decimals; the label decision must
|
|
295
|
+
// follow the rounded value, or a bar rendered at 12 could ship bare.
|
|
296
|
+
expect(flowLabelVisible(11.994)).toBe(false);
|
|
297
|
+
expect(flowLabelVisible(11.995)).toBe(true); // rounds to 12.00
|
|
298
|
+
expect(flowLabelVisible(12)).toBe(true);
|
|
299
|
+
expect(flowLabelVisible(11.99)).toBe(false);
|
|
300
|
+
expect(flowLabelVisible(40)).toBe(true);
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("drops a duplicate sum that overflows instead of poisoning the column", () => {
|
|
304
|
+
const layout = usageFlowLayout([
|
|
305
|
+
{ source: "a", middle: "up", target: "m1", value: 1e308 },
|
|
306
|
+
{ source: "a", middle: "up", target: "m1", value: 1e308 },
|
|
307
|
+
{ source: "b", middle: "up", target: "m1", value: 10 },
|
|
308
|
+
]);
|
|
309
|
+
expect(layout.empty).toBe(false);
|
|
310
|
+
expect(layout.nodes.every((node) => Number.isFinite(node.y) && Number.isFinite(node.height))).toBe(true);
|
|
311
|
+
expect(layout.links.every((link) => !link.path.includes("NaN"))).toBe(true);
|
|
312
|
+
// The overflowing triple is gone; the healthy row survives.
|
|
313
|
+
expect(layout.total).toBe(10);
|
|
314
|
+
expect(layout.nodes.some((node) => node.fullLabel === "a")).toBe(false);
|
|
315
|
+
expect(layout.nodes.some((node) => node.fullLabel === "b")).toBe(true);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("never hands two models the same colour because a roster row repeats", () => {
|
|
319
|
+
const layout = usageFlowLayout(
|
|
320
|
+
[
|
|
321
|
+
{ source: "k", middle: "up", target: "a", value: 3 },
|
|
322
|
+
{ source: "k", middle: "up", target: "b", value: 2 },
|
|
323
|
+
],
|
|
324
|
+
{ models: [{ label: "a" }, { label: "a" }, { label: "b" }], colors: ["c0", "c1", "c2"] },
|
|
325
|
+
);
|
|
326
|
+
const colors = new Map(layout.legend.map((row) => [row.label, row.color]));
|
|
327
|
+
expect(colors.get("a")).toBe("c0");
|
|
328
|
+
expect(colors.get("b")).toBe("c1");
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("gives the folded chart bucket the slot after every real label", () => {
|
|
332
|
+
const layout = usageFlowLayout(
|
|
333
|
+
[
|
|
334
|
+
{ source: "k1", middle: "up", target: "model-kept", value: 60 },
|
|
335
|
+
{ source: "k2", middle: "up", target: "model-folded-a", value: 30 },
|
|
336
|
+
{ source: "k3", middle: "up", target: "model-folded-b", value: 10 },
|
|
337
|
+
],
|
|
338
|
+
{
|
|
339
|
+
maxNodes: 2,
|
|
340
|
+
models: [{ label: "model-kept" }],
|
|
341
|
+
colors: ["c0", "c1", "c2", "c3"],
|
|
342
|
+
},
|
|
343
|
+
);
|
|
344
|
+
const bucket = layout.nodes.find((node) => node.column === 2 && node.others);
|
|
345
|
+
// Slot 0 is the kept roster model, slots 1 and 2 the two unregistered
|
|
346
|
+
// models (folded in the chart, still real labels), so the bucket takes
|
|
347
|
+
// slot 3 — never palette[0].
|
|
348
|
+
expect(bucket?.color).toBe("c3");
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("wraps a palette shorter than the roster", () => {
|
|
352
|
+
const layout = usageFlowLayout(
|
|
353
|
+
[
|
|
354
|
+
{ source: "k", middle: "up", target: "a", value: 3 },
|
|
355
|
+
{ source: "k", middle: "up", target: "b", value: 2 },
|
|
356
|
+
{ source: "k", middle: "up", target: "c", value: 1 },
|
|
357
|
+
],
|
|
358
|
+
{ models: [{ label: "a" }, { label: "b" }, { label: "c" }], colors: ["c0", "c1"] },
|
|
359
|
+
);
|
|
360
|
+
expect(layout.legend.map((row) => row.color)).toEqual(["c0", "c1", "c0"]);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it("draws ribbons with the target node's colour and a closed path", () => {
|
|
364
|
+
const layout = usageFlowLayout(ROWS);
|
|
365
|
+
const byKey = new Map(layout.nodes.map((node) => [node.key, node]));
|
|
366
|
+
expect(layout.links.length).toBeGreaterThan(0);
|
|
367
|
+
for (const link of layout.links) {
|
|
368
|
+
expect(link.color).toBe(byKey.get(link.targetKey)?.color);
|
|
369
|
+
expect(link.path.startsWith("M ")).toBe(true);
|
|
370
|
+
expect(link.path.endsWith(" Z")).toBe(true);
|
|
371
|
+
}
|
|
372
|
+
// Two stages of links exist: caller→upstream and upstream→model.
|
|
373
|
+
const stages = new Set(layout.links.map((link) => (link.sourceKey.startsWith("0:") ? 0 : 1)));
|
|
374
|
+
expect([...stages].sort()).toEqual([0, 1]);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it("collapses the tail of a column into one others bucket without losing value", () => {
|
|
378
|
+
const many = ["a", "b", "c", "d", "e"].map((source, index) => ({
|
|
379
|
+
source,
|
|
380
|
+
middle: "up",
|
|
381
|
+
target: "m1",
|
|
382
|
+
value: (index + 1) * 10,
|
|
383
|
+
}));
|
|
384
|
+
const layout = usageFlowLayout(many, { maxNodes: 3, othersLabel: "Other callers" });
|
|
385
|
+
const sources = columnNodes(layout, 0);
|
|
386
|
+
// The folded bucket outranks the retained tail nodes (it carries
|
|
387
|
+
// a + b + c = 60), so it stacks first — columns rank AFTER folding.
|
|
388
|
+
expect(sources.map((node) => node.fullLabel)).toEqual(["Other callers", "e", "d"]);
|
|
389
|
+
const others = sources[0];
|
|
390
|
+
expect(others.others).toBe(true);
|
|
391
|
+
expect(others.value).toBe(10 + 20 + 30);
|
|
392
|
+
expect(layout.total).toBe(150);
|
|
393
|
+
// The legend covers the MODEL column only: a folded caller bucket is a
|
|
394
|
+
// node, not a model row.
|
|
395
|
+
expect(layout.legend.map((row) => row.label)).toEqual(["m1"]);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it("folds an overflowing model column in the chart but lists every model", () => {
|
|
399
|
+
const rows = ["m1", "m2", "m3", "m4"].map((target) => ({
|
|
400
|
+
source: "k",
|
|
401
|
+
middle: "up",
|
|
402
|
+
target,
|
|
403
|
+
value: 10,
|
|
404
|
+
}));
|
|
405
|
+
const layout = usageFlowLayout(rows, { maxNodes: 3, othersLabel: "Other models" });
|
|
406
|
+
expect(layout.total).toBe(40);
|
|
407
|
+
// The chart collapses the tail into one bucket node...
|
|
408
|
+
const bucket = layout.nodes.find(
|
|
409
|
+
(node) => node.column === 2 && node.fullLabel === "Other models",
|
|
410
|
+
);
|
|
411
|
+
expect(bucket?.value).toBe(20);
|
|
412
|
+
expect(bucket?.others).toBe(true);
|
|
413
|
+
// ...while the legend keeps the FULL model table, pre-fold: folding
|
|
414
|
+
// is a chart layout decision, not a usage fact.
|
|
415
|
+
expect(layout.legend.map((row) => row.label)).toEqual(["m1", "m2", "m3", "m4"]);
|
|
416
|
+
for (const row of layout.legend) {
|
|
417
|
+
expect(row.value, `${row.label} keeps its real total`).toBe(10);
|
|
418
|
+
expect(row.percent).toBe(25);
|
|
419
|
+
expect(row.idle).toBe(false);
|
|
420
|
+
}
|
|
421
|
+
expect(layout.legend.some((row) => row.label === "Other models")).toBe(false);
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("reports a folded registered model's real total, never a zero it did not have", () => {
|
|
425
|
+
const rows = ["m1", "m2", "m3", "m4"].map((target) => ({
|
|
426
|
+
source: "k",
|
|
427
|
+
middle: "up",
|
|
428
|
+
target,
|
|
429
|
+
value: 10,
|
|
430
|
+
}));
|
|
431
|
+
const layout = usageFlowLayout(rows, {
|
|
432
|
+
maxNodes: 2,
|
|
433
|
+
models: [{ label: "m1" }, { label: "m2" }, { label: "m3" }, { label: "m4" }],
|
|
434
|
+
});
|
|
435
|
+
// m3/m4 are folded into the chart's bucket; the legend must still
|
|
436
|
+
// report the traffic they carried (the S1 the round-2 reviewer found).
|
|
437
|
+
const m3 = layout.legend.find((row) => row.label === "m3")!;
|
|
438
|
+
expect(m3.value).toBe(10);
|
|
439
|
+
expect(m3.idle).toBe(false);
|
|
440
|
+
expect(m3.percent).toBe(25);
|
|
441
|
+
expect(layout.legend.map((row) => row.label)).toEqual(["m1", "m2", "m3", "m4"]);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
it("merges a real label that equals the others bucket into the fold", () => {
|
|
445
|
+
const rows = [
|
|
446
|
+
{ source: "a", middle: "up", target: "m1", value: 40 },
|
|
447
|
+
{ source: "b", middle: "up", target: "m1", value: 30 },
|
|
448
|
+
{ source: "Others", middle: "up", target: "m1", value: 20 },
|
|
449
|
+
{ source: "c", middle: "up", target: "m1", value: 10 },
|
|
450
|
+
];
|
|
451
|
+
const layout = usageFlowLayout(rows, { maxNodes: 2, othersLabel: "Others" });
|
|
452
|
+
const sources = columnNodes(layout, 0);
|
|
453
|
+
// A real label equal to the fold label merges into the bucket by label:
|
|
454
|
+
// top 1 is "a" (40), so b + Others + c = 60 fold together.
|
|
455
|
+
expect(sources.map((node) => node.fullLabel).sort()).toEqual(["Others", "a"]);
|
|
456
|
+
expect(sources.find((node) => node.fullLabel === "Others")?.value).toBe(60);
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
it("lets the registered roster fix colours and lists idle models", () => {
|
|
460
|
+
const roster = [
|
|
461
|
+
{ label: "model-b" },
|
|
462
|
+
{ label: "model-a" },
|
|
463
|
+
{ label: "model-idle" },
|
|
464
|
+
];
|
|
465
|
+
const rows = [
|
|
466
|
+
{ source: "k", middle: "up", target: "model-a", value: 60 },
|
|
467
|
+
{ source: "k", middle: "up", target: "model-b", value: 40 },
|
|
468
|
+
];
|
|
469
|
+
const layout = usageFlowLayout(rows, { models: roster });
|
|
470
|
+
const legend = layout.legend;
|
|
471
|
+
expect(legend.map((row) => row.label)).toEqual(["model-b", "model-a", "model-idle"]);
|
|
472
|
+
// Colour order follows the roster, not usage: model-b is slot 0 even
|
|
473
|
+
// though model-a served more.
|
|
474
|
+
expect(legend[0].color).toBe(USAGE_FLOW_PALETTE[0]);
|
|
475
|
+
expect(legend[1].color).toBe(USAGE_FLOW_PALETTE[1]);
|
|
476
|
+
expect(legend[2].idle).toBe(true);
|
|
477
|
+
expect(legend[2].value).toBe(0);
|
|
478
|
+
expect(legend[2].color).toBe(USAGE_FLOW_PALETTE[2]);
|
|
479
|
+
// The node colour matches the legend swatch.
|
|
480
|
+
const nodeA = layout.nodes.find((node) => node.fullLabel === "model-a")!;
|
|
481
|
+
expect(nodeA.color).toBe(USAGE_FLOW_PALETTE[1]);
|
|
482
|
+
expect(layout.legend[0].value).toBe(40);
|
|
483
|
+
expect(layout.legend[1].value).toBe(60);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
it("keeps a real model labelled like the fold bucket as its own row", () => {
|
|
487
|
+
const rows = [
|
|
488
|
+
{ source: "k", middle: "up", target: "Others", value: 50 },
|
|
489
|
+
{ source: "k", middle: "up", target: "m1", value: 30 },
|
|
490
|
+
{ source: "k", middle: "up", target: "m2", value: 10 },
|
|
491
|
+
{ source: "k", middle: "up", target: "m3", value: 5 },
|
|
492
|
+
];
|
|
493
|
+
const layout = usageFlowLayout(rows, { maxNodes: 3, othersLabel: "Others" });
|
|
494
|
+
const row = layout.legend.find((candidate) => candidate.label === "Others")!;
|
|
495
|
+
// The fold bucket merges into the real kept model by label in the
|
|
496
|
+
// CHART; the legend reports the model's own traffic (50), not the
|
|
497
|
+
// merged bucket sum.
|
|
498
|
+
expect(row.value).toBe(50);
|
|
499
|
+
expect(row.percent).toBeCloseTo((50 / 95) * 100, 5);
|
|
500
|
+
const node = layout.nodes.find(
|
|
501
|
+
(candidate) => candidate.column === 2 && candidate.fullLabel === "Others",
|
|
502
|
+
)!;
|
|
503
|
+
expect(node.value).toBe(65);
|
|
504
|
+
// The NODE-side kept-guard too: this label is a kept real model, not
|
|
505
|
+
// the synthetic bucket, so it must not wear the bucket styling.
|
|
506
|
+
expect(node.others).toBe(false);
|
|
507
|
+
expect(layout.total).toBe(95);
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it("appends models with traffic that the roster does not know", () => {
|
|
511
|
+
const layout = usageFlowLayout(
|
|
512
|
+
[
|
|
513
|
+
{ source: "k", middle: "up", target: "registered", value: 70 },
|
|
514
|
+
{ source: "k", middle: "up", target: "unregistered", value: 30 },
|
|
515
|
+
],
|
|
516
|
+
{ models: [{ label: "registered" }] },
|
|
517
|
+
);
|
|
518
|
+
expect(layout.legend.map((row) => row.label)).toEqual(["registered", "unregistered"]);
|
|
519
|
+
expect(layout.legend[1].color).toBe(USAGE_FLOW_PALETTE[1]);
|
|
520
|
+
expect(layout.legend[1].idle).toBe(false);
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
it("draws the exact slot thickness — the path's cap edges carry it", () => {
|
|
524
|
+
const rows = [
|
|
525
|
+
{ source: "huge", middle: "up", target: "m1", value: 9_800_000 },
|
|
526
|
+
...Array.from({ length: 9 }, (_, i) => ({
|
|
527
|
+
source: `tiny-${i}`,
|
|
528
|
+
middle: "up",
|
|
529
|
+
target: "m1",
|
|
530
|
+
value: 2_000,
|
|
531
|
+
})),
|
|
532
|
+
];
|
|
533
|
+
const layout = usageFlowLayout(rows, { height: 420 });
|
|
534
|
+
let thin = 0;
|
|
535
|
+
for (const link of layout.links) {
|
|
536
|
+
const points = pathPoints(link.path);
|
|
537
|
+
for (const x of new Set(points.map((point) => point.x))) {
|
|
538
|
+
const ys = points.filter((point) => point.x === x).map((point) => point.y);
|
|
539
|
+
if (ys.length !== 2) continue;
|
|
540
|
+
expect(
|
|
541
|
+
Math.abs(ys[0] - ys[1]),
|
|
542
|
+
`drawn cap thickness of ${link.key}`,
|
|
543
|
+
).toBeCloseTo(link.thickness, 1);
|
|
544
|
+
}
|
|
545
|
+
if (link.thickness < 0.5) thin += 1;
|
|
546
|
+
}
|
|
547
|
+
// The dataset must actually exercise sub-unit slots: a draw floor
|
|
548
|
+
// (the round-2 defect) only shows up on thin ribbons.
|
|
549
|
+
expect(thin).toBeGreaterThan(0);
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
it("keeps a floored node's multi-link slots contiguous and scaled", () => {
|
|
553
|
+
const rows = [
|
|
554
|
+
{ source: "huge", middle: "up", target: "m1", value: 9_800_000 },
|
|
555
|
+
{ source: "tiny", middle: "up-a", target: "m1", value: 1_000 },
|
|
556
|
+
{ source: "tiny", middle: "up-b", target: "m1", value: 1_000 },
|
|
557
|
+
];
|
|
558
|
+
const layout = usageFlowLayout(rows, { height: 420 });
|
|
559
|
+
const tiny = layout.nodes.find(
|
|
560
|
+
(node) => node.fullLabel === "tiny" && node.column === 0,
|
|
561
|
+
)!;
|
|
562
|
+
expect(tiny.height).toBe(2); // floored bar
|
|
563
|
+
const scale =
|
|
564
|
+
layout.links.find((link) => link.value === 9_800_000)!.thickness / 9_800_000;
|
|
565
|
+
const outgoing = layout.links
|
|
566
|
+
.filter((link) => link.sourceKey === tiny.key)
|
|
567
|
+
.sort((a, b) => a.startY - b.startY);
|
|
568
|
+
expect(outgoing).toHaveLength(2);
|
|
569
|
+
expect(outgoing[0].startY).toBeCloseTo(tiny.y, 2);
|
|
570
|
+
expect(outgoing[0].startY + outgoing[0].thickness).toBeCloseTo(
|
|
571
|
+
outgoing[1].startY,
|
|
572
|
+
2,
|
|
573
|
+
);
|
|
574
|
+
expect(outgoing[1].startY).toBeCloseTo(tiny.y + 1_000 * scale, 2);
|
|
575
|
+
expect(outgoing[1].startY + outgoing[1].thickness).toBeCloseTo(
|
|
576
|
+
tiny.y + tiny.value * scale,
|
|
577
|
+
2,
|
|
578
|
+
);
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
it("falls back to its defaults for non-finite frame/truncation options", () => {
|
|
582
|
+
const layout = usageFlowLayout(
|
|
583
|
+
[{ source: "s".repeat(60), middle: "up", target: "m", value: 1 }],
|
|
584
|
+
{
|
|
585
|
+
width: Number.NaN,
|
|
586
|
+
height: Number.NaN,
|
|
587
|
+
maxNodes: Number.NaN,
|
|
588
|
+
labelMaxChars: Number.NaN,
|
|
589
|
+
},
|
|
590
|
+
);
|
|
591
|
+
expect(layout.width).toBe(960);
|
|
592
|
+
expect(layout.height).toBe(420);
|
|
593
|
+
for (const node of layout.nodes) {
|
|
594
|
+
expect(Number.isFinite(node.x)).toBe(true);
|
|
595
|
+
expect(Number.isFinite(node.y)).toBe(true);
|
|
596
|
+
expect(Number.isFinite(node.height)).toBe(true);
|
|
597
|
+
}
|
|
598
|
+
// Truncation is NOT silently disabled by a NaN budget.
|
|
599
|
+
const source = layout.nodes.find((node) => node.column === 0)!;
|
|
600
|
+
expect(source.label.endsWith("…")).toBe(true);
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
it("offsets unregistered model slots by the ASSIGNED roster slots", () => {
|
|
604
|
+
const layout = usageFlowLayout(
|
|
605
|
+
[
|
|
606
|
+
{ source: "k", middle: "up", target: "a", value: 3 },
|
|
607
|
+
{ source: "k", middle: "up", target: "b", value: 2 },
|
|
608
|
+
],
|
|
609
|
+
{ models: [{ label: "a" }, { label: "a" }], colors: ["c0", "c1", "c2"] },
|
|
610
|
+
);
|
|
611
|
+
const b = layout.legend.find((row) => row.label === "b")!;
|
|
612
|
+
// One DISTINCT roster label, so "b" takes the slot after it (c1) —
|
|
613
|
+
// not the slot after the duplicate-inflated array length (c2).
|
|
614
|
+
expect(b.color).toBe("c1");
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
it("ignores malformed roster entries instead of throwing", () => {
|
|
618
|
+
const layout = usageFlowLayout(
|
|
619
|
+
[{ source: "k", middle: "up", target: "m", value: 1 }],
|
|
620
|
+
{ models: [null as never, { label: "" }, { label: "m" }] },
|
|
621
|
+
);
|
|
622
|
+
expect(layout.legend.map((row) => row.label)).toEqual(["m"]);
|
|
623
|
+
for (const row of layout.legend) {
|
|
624
|
+
expect(typeof row.label).toBe("string");
|
|
625
|
+
expect(row.label).not.toBe("undefined");
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
it("honours explicit roster colours and a custom palette", () => {
|
|
630
|
+
const layout = usageFlowLayout(
|
|
631
|
+
[{ source: "k", middle: "up", target: "m", value: 1 }],
|
|
632
|
+
{
|
|
633
|
+
colors: ["rgb(1 2 3)", "rgb(4 5 6)"],
|
|
634
|
+
models: [{ label: "m", color: "rgb(9 9 9)" }],
|
|
635
|
+
},
|
|
636
|
+
);
|
|
637
|
+
expect(layout.legend[0].color).toBe("rgb(9 9 9)");
|
|
638
|
+
const node = layout.nodes.find((candidate) => candidate.fullLabel === "m")!;
|
|
639
|
+
expect(node.color).toBe("rgb(9 9 9)");
|
|
640
|
+
// Unrostered models ride the custom ladder.
|
|
641
|
+
const other = usageFlowLayout(
|
|
642
|
+
[{ source: "k", middle: "up", target: "x", value: 1 }],
|
|
643
|
+
{ colors: ["rgb(1 2 3)", "rgb(4 5 6)"] },
|
|
644
|
+
);
|
|
645
|
+
expect(other.legend[0].color).toBe("rgb(1 2 3)");
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
it("truncates long labels but keeps the full label on the node", () => {
|
|
649
|
+
const long = "deepseek-v4.1-flash-1017-celestia-edition-very-long";
|
|
650
|
+
const layout = usageFlowLayout(
|
|
651
|
+
[{ source: "k", middle: "up", target: long, value: 1 }],
|
|
652
|
+
{ labelMaxChars: 12 },
|
|
653
|
+
);
|
|
654
|
+
const node = layout.nodes.find((candidate) => candidate.column === 2)!;
|
|
655
|
+
expect(node.label.endsWith("…")).toBe(true);
|
|
656
|
+
expect(node.label.length).toBeLessThanOrEqual(13);
|
|
657
|
+
expect(node.fullLabel).toBe(long);
|
|
658
|
+
// The legend keeps the untruncated roster label.
|
|
659
|
+
expect(layout.legend[0].label).toBe(long);
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
it("counts a CJK label's width as two units when truncating", () => {
|
|
663
|
+
expect(truncateFlowLabel("模型用量分布信息", 8)).toBe("模型用量…");
|
|
664
|
+
expect(truncateFlowLabel("模型用量分布信息", 100)).toBe("模型用量分布信息");
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
it("is deterministic: identical input yields identical geometry and colours", () => {
|
|
668
|
+
const a = usageFlowLayout(ROWS, { height: 420, models: [{ label: "m1" }] });
|
|
669
|
+
const b = usageFlowLayout(ROWS, { height: 420, models: [{ label: "m1" }] });
|
|
670
|
+
expect(a).toEqual(b);
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
it("clamps a degenerate frame instead of emitting negative geometry", () => {
|
|
674
|
+
const layout = usageFlowLayout(ROWS, { width: 1, height: 1 });
|
|
675
|
+
expect(layout.width).toBe(320);
|
|
676
|
+
expect(layout.height).toBe(180);
|
|
677
|
+
for (const node of layout.nodes) {
|
|
678
|
+
expect(node.height).toBeGreaterThanOrEqual(2);
|
|
679
|
+
expect(Number.isFinite(node.y)).toBe(true);
|
|
680
|
+
expect(Number.isFinite(node.height)).toBe(true);
|
|
681
|
+
}
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
it("formats shares without pretending a sub-percent slice is zero", () => {
|
|
685
|
+
expect(formatFlowPercent(0)).toBe("0%");
|
|
686
|
+
expect(formatFlowPercent(0.4)).toBe("<1%");
|
|
687
|
+
expect(formatFlowPercent(1)).toBe("1%");
|
|
688
|
+
expect(formatFlowPercent(33.4)).toBe("33%");
|
|
689
|
+
expect(formatFlowPercent(100)).toBe("100%");
|
|
690
|
+
expect(formatFlowPercent(Number.NaN)).toBe("0%");
|
|
691
|
+
});
|
|
692
|
+
|
|
693
|
+
it("keeps linksFrom() consistent with the flow values", () => {
|
|
694
|
+
const layout = usageFlowLayout(ROWS);
|
|
695
|
+
const k1 = layout.nodes.find((node) => node.fullLabel === "k1" && node.column === 0)!;
|
|
696
|
+
const outgoing = linksFrom(layout, k1.key);
|
|
697
|
+
expect(outgoing.map((link) => link.value).sort((a, b) => a - b)).toEqual([300, 500]);
|
|
698
|
+
});
|
|
699
|
+
});
|