@celestia-island/hikari 0.55.412 → 0.55.414
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/HkSearchInput.tsx +45 -2
- 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/HkUserPicker.scss +111 -12
- package/src/components/HkUserPicker.singleScroll.contract.test.ts +81 -0
- package/src/components/HkUserPicker.test.tsx +245 -105
- package/src/components/HkUserPicker.tsx +430 -161
- package/src/components/_menu-item.scss +15 -4
- package/src/components/usageFlowLayout.test.ts +699 -0
- package/src/components/usageFlowLayout.ts +716 -0
- package/src/index.ts +20 -0
package/package.json
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
defineComponent,
|
|
3
|
+
onUnmounted,
|
|
4
|
+
ref,
|
|
5
|
+
useAttrs,
|
|
6
|
+
watch,
|
|
7
|
+
type PropType,
|
|
8
|
+
type StyleValue,
|
|
9
|
+
} from "vue";
|
|
2
10
|
import { useI18n } from "../i18n/context";
|
|
3
11
|
import "./HkSearchInput.scss";
|
|
4
12
|
|
|
@@ -18,6 +26,19 @@ const clearIcon = (
|
|
|
18
26
|
|
|
19
27
|
export default defineComponent({
|
|
20
28
|
name: "HkSearchInput",
|
|
29
|
+
// The styled box and the control are two nodes: the box is a chrome
|
|
30
|
+
// wrapper (`.hk-search-input`), the `<input>` inside it is the actual
|
|
31
|
+
// field. Undeclared attributes therefore belong on the FIELD, not the
|
|
32
|
+
// box — the HkInput contract. A consumer that wires this search field
|
|
33
|
+
// as the combobox of a list surface (HkUserPicker) passes `aria-label`,
|
|
34
|
+
// `aria-controls`, `aria-activedescendant` / `aria-expanded`; the old
|
|
35
|
+
// auto-fallthrough onto the wrapper left the input itself unnamed and
|
|
36
|
+
// the active row unpublished. `class` / `style` stay on the wrapper
|
|
37
|
+
// (its historical home). `role` is DECLARED below rather than passed as
|
|
38
|
+
// an attribute: it carries no dash, so the JSX attribute passthrough
|
|
39
|
+
// cannot accept it (the house rule is the same for `role` on HkInput —
|
|
40
|
+
// HkTagInput reaches for a native input when it needs one).
|
|
41
|
+
inheritAttrs: false,
|
|
21
42
|
props: {
|
|
22
43
|
modelValue: { type: String, default: "" },
|
|
23
44
|
placeholder: { type: String, default: undefined },
|
|
@@ -28,6 +49,10 @@ export default defineComponent({
|
|
|
28
49
|
},
|
|
29
50
|
debounce: { type: Number, default: 0 },
|
|
30
51
|
clearAriaLabel: { type: String, default: "" },
|
|
52
|
+
/** Role of the FIELD element itself — "combobox" when a panel (the
|
|
53
|
+
* HkUserPicker popover) owns a list of options behind this search
|
|
54
|
+
* box. Undefined keeps the native searchbox semantics. */
|
|
55
|
+
role: { type: String, default: undefined },
|
|
31
56
|
},
|
|
32
57
|
emits: {
|
|
33
58
|
"update:modelValue": (_value: string) => true,
|
|
@@ -36,9 +61,22 @@ export default defineComponent({
|
|
|
36
61
|
},
|
|
37
62
|
setup(props, { emit }) {
|
|
38
63
|
const { t } = useI18n();
|
|
64
|
+
const attrs = useAttrs();
|
|
39
65
|
const inputRef = ref<HTMLInputElement | null>(null);
|
|
40
66
|
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
|
|
41
67
|
|
|
68
|
+
/** Undeclared attributes minus the two that keep the wrapper's
|
|
69
|
+
* historical meaning — every other one (aria wiring, `name`, an
|
|
70
|
+
* extra listener) is the FIELD's. */
|
|
71
|
+
const fieldAttrs = () => {
|
|
72
|
+
const { class: _class, style: _style, ...rest } = attrs as Record<string, unknown>;
|
|
73
|
+
return rest;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** `style` is the one wrapper-bound attribute that needs typing: it
|
|
77
|
+
* rides the box beside `class`. */
|
|
78
|
+
const wrapperStyle = () => (attrs as { style?: StyleValue }).style;
|
|
79
|
+
|
|
42
80
|
function flushSearch(val: string) {
|
|
43
81
|
emit("search", val);
|
|
44
82
|
}
|
|
@@ -88,12 +126,17 @@ export default defineComponent({
|
|
|
88
126
|
];
|
|
89
127
|
|
|
90
128
|
return () => (
|
|
91
|
-
<div
|
|
129
|
+
<div
|
|
130
|
+
class={[...wrapperClass, (attrs as Record<string, unknown>).class]}
|
|
131
|
+
style={wrapperStyle()}
|
|
132
|
+
>
|
|
92
133
|
<span class="hk-search-input-icon">{searchIcon}</span>
|
|
93
134
|
<input
|
|
135
|
+
{...fieldAttrs()}
|
|
94
136
|
ref={inputRef}
|
|
95
137
|
type="search"
|
|
96
138
|
class="hk-search-input-field"
|
|
139
|
+
role={props.role}
|
|
97
140
|
value={props.modelValue}
|
|
98
141
|
placeholder={props.placeholder ?? t("hikari::searchInput.placeholder", "Search…")}
|
|
99
142
|
disabled={props.disabled}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import * as barrel from "../index";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The public barrel guard for the usage-flow family (the
|
|
7
|
+
* HkCalendarHeatmap.barrel.test.ts precedent): every other test reaches
|
|
8
|
+
* the modules through their direct paths, so deleting the re-export lines
|
|
9
|
+
* from index.ts would leave the whole suite green — only an external
|
|
10
|
+
* consumer (arona / shittim-chest) would notice.
|
|
11
|
+
*
|
|
12
|
+
* This file deliberately does NOT mock "@celestia-island/hikari": the
|
|
13
|
+
* package's self-reference resolves to this very module.
|
|
14
|
+
*/
|
|
15
|
+
describe("the public barrel", () => {
|
|
16
|
+
it("exports the component under both family names", () => {
|
|
17
|
+
expect(barrel.HkUsageFlow, "HkUsageFlow must be re-exported").toBeTruthy();
|
|
18
|
+
expect(barrel.HUsageFlow).toBe(barrel.HkUsageFlow);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("exports the pure layout helpers", () => {
|
|
22
|
+
expect(typeof barrel.usageFlowLayout).toBe("function");
|
|
23
|
+
expect(typeof barrel.truncateFlowLabel).toBe("function");
|
|
24
|
+
expect(typeof barrel.formatFlowPercent).toBe("function");
|
|
25
|
+
expect(typeof barrel.flowLabelVisible).toBe("function");
|
|
26
|
+
expect(Array.isArray(barrel.USAGE_FLOW_PALETTE)).toBe(true);
|
|
27
|
+
expect(barrel.USAGE_FLOW_PALETTE.length).toBeGreaterThanOrEqual(8);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("lays out a minimal flow through the barrel surface", () => {
|
|
31
|
+
const layout = barrel.usageFlowLayout([
|
|
32
|
+
{ source: "a", middle: "b", target: "c", value: 1 },
|
|
33
|
+
]);
|
|
34
|
+
expect(layout.empty).toBe(false);
|
|
35
|
+
expect(layout.nodes).toHaveLength(3);
|
|
36
|
+
expect(layout.links).toHaveLength(2);
|
|
37
|
+
expect(layout.legend.map((row) => row.label)).toEqual(["c"]);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// HkUsageFlow anatomy — self-sufficient styling (the component
|
|
2
|
+
// self-sufficiency guard): every class lives here, no host atomic or
|
|
3
|
+
// utility layer. Ribbon/node fills are inline theme-token expressions the
|
|
4
|
+
// layout computes; ink here comes from theme tokens, so light/dark/brand
|
|
5
|
+
// flips repaint without a re-render.
|
|
6
|
+
//
|
|
7
|
+
// AI-generated (DeepSeek V4.1 via DeepSeek Harness), 2026-11-17 — SySL-1.0 §2.3.
|
|
8
|
+
|
|
9
|
+
@use "../styles/polyfill" as *;
|
|
10
|
+
|
|
11
|
+
.hk-usage-flow {
|
|
12
|
+
width: 100%;
|
|
13
|
+
|
|
14
|
+
&__body {
|
|
15
|
+
width: 100%;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
&__chart {
|
|
19
|
+
display: block;
|
|
20
|
+
width: 100%;
|
|
21
|
+
height: auto;
|
|
22
|
+
overflow: visible;
|
|
23
|
+
/* The chart's coordinate system is LTR: label anchors are
|
|
24
|
+
direction-relative in SVG, so an RTL host would otherwise flip
|
|
25
|
+
start/end and paint the first column's label across its own bar
|
|
26
|
+
(and the last column's past the frame). The axis is LTR by
|
|
27
|
+
construction; label TEXT still renders with its own bidi. */
|
|
28
|
+
direction: ltr;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Ribbons read as "flow into the target": translucent so stacked paths
|
|
32
|
+
// never turn opaque where two lanes overlap, and the node/label ink stays
|
|
33
|
+
// legible on top.
|
|
34
|
+
&__ribbon {
|
|
35
|
+
fill-opacity: 0.42;
|
|
36
|
+
transition: fill-opacity 0.16s ease;
|
|
37
|
+
stroke: none;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
&__bar {
|
|
41
|
+
shape-rendering: geometricPrecision;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Labels sit over ribbons: a background-coloured halo keeps them legible
|
|
45
|
+
// without a second layout pass (SVG2 paint-order; the halo is the same
|
|
46
|
+
// frame colour the chart sits on).
|
|
47
|
+
&__label {
|
|
48
|
+
font-size: var(--text-xs, 12px);
|
|
49
|
+
fill: rgb(var(--color-text) / 0.9);
|
|
50
|
+
paint-order: stroke;
|
|
51
|
+
stroke: rgb(var(--color-background) / 0.92);
|
|
52
|
+
stroke-width: 3px;
|
|
53
|
+
stroke-linejoin: round;
|
|
54
|
+
pointer-events: none;
|
|
55
|
+
@include user-select(none);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
&__label--others {
|
|
59
|
+
font-style: italic;
|
|
60
|
+
fill: rgb(var(--color-text-secondary) / 0.9);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
&__legend {
|
|
64
|
+
display: flex;
|
|
65
|
+
flex-wrap: wrap;
|
|
66
|
+
gap: 4px 18px;
|
|
67
|
+
margin: 10px 0 0;
|
|
68
|
+
padding: 0;
|
|
69
|
+
list-style: none;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
&__legend-row {
|
|
73
|
+
display: flex;
|
|
74
|
+
align-items: baseline;
|
|
75
|
+
gap: 6px;
|
|
76
|
+
font-size: var(--text-sm, 13px);
|
|
77
|
+
min-width: 0;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// A registered model with no measured usage keeps its slot but recedes.
|
|
81
|
+
&__legend-row--idle {
|
|
82
|
+
opacity: 0.45;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
&__legend-swatch {
|
|
86
|
+
width: 9px;
|
|
87
|
+
height: 9px;
|
|
88
|
+
border-radius: 2px;
|
|
89
|
+
flex: none;
|
|
90
|
+
align-self: center;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
&__legend-label {
|
|
94
|
+
color: rgb(var(--color-text) / 0.88);
|
|
95
|
+
max-width: 22em;
|
|
96
|
+
overflow: hidden;
|
|
97
|
+
text-overflow: ellipsis;
|
|
98
|
+
white-space: nowrap;
|
|
99
|
+
/* The --others row italicizes this box; the inline-end cushion keeps a
|
|
100
|
+
synthetic-oblique trailing stroke from being shaved (textClip
|
|
101
|
+
contract, HkAffixPicker precedent). */
|
|
102
|
+
padding-inline-end: 0.2em;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
&__legend-value {
|
|
106
|
+
color: var(--hi-color-muted);
|
|
107
|
+
font-variant-numeric: tabular-nums;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
&__legend-pct {
|
|
111
|
+
color: var(--hi-color-muted);
|
|
112
|
+
font-variant-numeric: tabular-nums;
|
|
113
|
+
min-width: 3.2em;
|
|
114
|
+
text-align: end;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
&--empty {
|
|
118
|
+
/* Auto height: the empty face may also carry the registered roster,
|
|
119
|
+
and a fixed 120px box would clip a long one. */
|
|
120
|
+
min-height: 120px;
|
|
121
|
+
display: flex;
|
|
122
|
+
flex-direction: column;
|
|
123
|
+
align-items: center;
|
|
124
|
+
justify-content: center;
|
|
125
|
+
gap: 8px;
|
|
126
|
+
padding: 12px 0;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
&__empty {
|
|
130
|
+
font-size: var(--text-sm, 13px);
|
|
131
|
+
color: var(--hi-color-muted);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
import { createApp, h, nextTick, ref } from "vue";
|
|
7
|
+
|
|
8
|
+
import HkUsageFlow from "./HkUsageFlow";
|
|
9
|
+
|
|
10
|
+
const mounts: Array<{ app: ReturnType<typeof createApp>; container: HTMLElement }> = [];
|
|
11
|
+
|
|
12
|
+
function mount(node: ReturnType<typeof h>) {
|
|
13
|
+
const container = document.createElement("div");
|
|
14
|
+
document.body.appendChild(container);
|
|
15
|
+
const app = createApp({ render: () => node });
|
|
16
|
+
app.mount(container);
|
|
17
|
+
mounts.push({ app, container });
|
|
18
|
+
return container;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
for (const { app, container } of mounts.splice(0)) {
|
|
23
|
+
app.unmount();
|
|
24
|
+
container.remove();
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const ROWS = [
|
|
29
|
+
{ source: "caller-a", middle: "up-1", target: "model-x", value: 500 },
|
|
30
|
+
{ source: "caller-a", middle: "up-1", target: "model-y", value: 300 },
|
|
31
|
+
{ source: "caller-b", middle: "up-2", target: "model-x", value: 200 },
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
function texts(el: HTMLElement, selector: string): string[] {
|
|
35
|
+
return [...el.querySelectorAll(selector)].map((node) => node.textContent ?? "");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("HkUsageFlow", () => {
|
|
39
|
+
it("renders one bar per staged label and one ribbon per aggregate link", () => {
|
|
40
|
+
const el = mount(<HkUsageFlow rows={ROWS} />);
|
|
41
|
+
// callers (2) + upstreams (2) + models (2)
|
|
42
|
+
expect(el.querySelectorAll(".hk-usage-flow__node")).toHaveLength(6);
|
|
43
|
+
// caller→upstream: a→1, b→2 (2 links); upstream→model: 1→x, 1→y, 2→x (3)
|
|
44
|
+
expect(el.querySelectorAll(".hk-usage-flow__ribbon")).toHaveLength(5);
|
|
45
|
+
const labels = texts(el, ".hk-usage-flow__label");
|
|
46
|
+
expect(labels).toEqual(
|
|
47
|
+
expect.arrayContaining(["caller-a", "caller-b", "up-1", "up-2", "model-x", "model-y"]),
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("anchors model labels to the left of their bar and caller labels to the right", () => {
|
|
52
|
+
const el = mount(<HkUsageFlow rows={ROWS} />);
|
|
53
|
+
const labels = [...el.querySelectorAll(".hk-usage-flow__label")];
|
|
54
|
+
const byText = new Map(labels.map((label) => [label.textContent, label]));
|
|
55
|
+
expect(byText.get("caller-a")?.getAttribute("text-anchor")).toBe("start");
|
|
56
|
+
expect(byText.get("model-x")?.getAttribute("text-anchor")).toBe("end");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("renders every roster model, greyed when it served no traffic", () => {
|
|
60
|
+
const el = mount(
|
|
61
|
+
<HkUsageFlow
|
|
62
|
+
rows={ROWS}
|
|
63
|
+
models={[{ label: "model-x" }, { label: "model-y" }, { label: "model-idle" }]}
|
|
64
|
+
/>,
|
|
65
|
+
);
|
|
66
|
+
const rows = [...el.querySelectorAll(".hk-usage-flow__legend-row")];
|
|
67
|
+
expect(rows).toHaveLength(3);
|
|
68
|
+
expect(rows.map((row) => row.querySelector(".hk-usage-flow__legend-label")?.textContent)).toEqual([
|
|
69
|
+
"model-x",
|
|
70
|
+
"model-y",
|
|
71
|
+
"model-idle",
|
|
72
|
+
]);
|
|
73
|
+
expect(rows[2].classList.contains("hk-usage-flow__legend-row--idle")).toBe(true);
|
|
74
|
+
expect(rows[0].classList.contains("hk-usage-flow__legend-row--idle")).toBe(false);
|
|
75
|
+
// Shares are computed over the metric total: x = 700/1000, y = 300/1000.
|
|
76
|
+
expect(rows[0].querySelector(".hk-usage-flow__legend-pct")?.textContent).toBe("70%");
|
|
77
|
+
expect(rows[1].querySelector(".hk-usage-flow__legend-pct")?.textContent).toBe("30%");
|
|
78
|
+
expect(rows[2].querySelector(".hk-usage-flow__legend-pct")?.textContent).toBe("0%");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("draws caller labels right of their bar and model labels left of it", () => {
|
|
82
|
+
const el = mount(<HkUsageFlow rows={ROWS} />);
|
|
83
|
+
const labels = [...el.querySelectorAll(".hk-usage-flow__label")] as SVGTextElement[];
|
|
84
|
+
const byText = new Map(labels.map((label) => [label.textContent, label]));
|
|
85
|
+
const caller = byText.get("caller-a")!;
|
|
86
|
+
const model = byText.get("model-x")!;
|
|
87
|
+
const callerX = Number(caller.getAttribute("x"));
|
|
88
|
+
const modelX = Number(model.getAttribute("x"));
|
|
89
|
+
const callerBar = caller.parentElement!.querySelector("rect")!;
|
|
90
|
+
const modelBar = model.parentElement!.querySelector("rect")!;
|
|
91
|
+
expect(callerX).toBeGreaterThan(Number(callerBar.getAttribute("x")) + Number(callerBar.getAttribute("width")));
|
|
92
|
+
expect(modelX).toBeLessThan(Number(modelBar.getAttribute("x")));
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("formats values through the host formatter", () => {
|
|
96
|
+
const el = mount(
|
|
97
|
+
<HkUsageFlow rows={ROWS} models={[{ label: "model-x" }]} formatValue={(v) => `${v / 1000}k`} />,
|
|
98
|
+
);
|
|
99
|
+
const first = el.querySelector(".hk-usage-flow__legend-row .hk-usage-flow__legend-value");
|
|
100
|
+
expect(first?.textContent).toBe("0.7k");
|
|
101
|
+
const bar = el.querySelector(".hk-usage-flow__bar");
|
|
102
|
+
expect(bar?.getAttribute("data-hint")).toContain("caller-a");
|
|
103
|
+
expect(bar?.getAttribute("data-hint")).toContain("k");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("folds overflowing columns into the localized others bucket", () => {
|
|
107
|
+
const rows = ["a", "b", "c", "d", "e"].map((source) => ({
|
|
108
|
+
source,
|
|
109
|
+
middle: "up",
|
|
110
|
+
target: "model-x",
|
|
111
|
+
value: 10,
|
|
112
|
+
}));
|
|
113
|
+
const el = mount(<HkUsageFlow rows={rows} maxNodes={3} othersLabel="其他调用方" />);
|
|
114
|
+
const labels = texts(el, ".hk-usage-flow__label");
|
|
115
|
+
expect(labels).toContain("其他调用方");
|
|
116
|
+
expect(el.querySelectorAll(".hk-usage-flow__node")).toHaveLength(5); // 3 callers + 1 upstream + 1 model
|
|
117
|
+
const others = [...el.querySelectorAll(".hk-usage-flow__label--others")];
|
|
118
|
+
expect(others.map((node) => node.textContent)).toEqual(["其他调用方"]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("renders the empty state when every row is zero", () => {
|
|
122
|
+
const el = mount(
|
|
123
|
+
<HkUsageFlow rows={[{ source: "a", middle: "b", target: "c", value: 0 }]} emptyText="暂无用量" />,
|
|
124
|
+
);
|
|
125
|
+
expect(el.querySelector(".hk-usage-flow--empty")).toBeTruthy();
|
|
126
|
+
expect(el.querySelector(".hk-usage-flow__empty")?.textContent).toBe("暂无用量");
|
|
127
|
+
expect(el.querySelector(".hk-usage-flow__chart")).toBeNull();
|
|
128
|
+
// No roster passed: no legend either.
|
|
129
|
+
expect(el.querySelector(".hk-usage-flow__legend")).toBeNull();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("summarises the legend for assistive tech, with an explicit override", () => {
|
|
133
|
+
const el = mount(<HkUsageFlow rows={ROWS} models={[{ label: "model-x" }]} />);
|
|
134
|
+
const svg = el.querySelector(".hk-usage-flow__chart");
|
|
135
|
+
expect(svg?.getAttribute("role")).toBe("img");
|
|
136
|
+
expect(svg?.getAttribute("aria-label")).toContain("model-x 70%");
|
|
137
|
+
const overridden = mount(<HkUsageFlow rows={ROWS} ariaLabel="模型用量分布" />);
|
|
138
|
+
expect(
|
|
139
|
+
overridden.querySelector(".hk-usage-flow__chart")?.getAttribute("aria-label"),
|
|
140
|
+
).toBe("模型用量分布");
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("still lists the registered roster when nothing measured", () => {
|
|
144
|
+
const el = mount(
|
|
145
|
+
<HkUsageFlow
|
|
146
|
+
rows={[]}
|
|
147
|
+
emptyText="No usage"
|
|
148
|
+
models={[{ label: "model-x" }, { label: "model-idle" }]}
|
|
149
|
+
/>,
|
|
150
|
+
);
|
|
151
|
+
expect(el.querySelector(".hk-usage-flow__empty")?.textContent).toBe("No usage");
|
|
152
|
+
const rows = el.querySelectorAll(".hk-usage-flow__legend-row");
|
|
153
|
+
expect(rows).toHaveLength(2);
|
|
154
|
+
expect(rows[0].classList.contains("hk-usage-flow__legend-row--idle")).toBe(true);
|
|
155
|
+
expect(rows[0].textContent).toContain("0%");
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("pins its frame to the width/height props (the viewBox)", () => {
|
|
159
|
+
const el = mount(<HkUsageFlow rows={ROWS} width={800} height={360} />);
|
|
160
|
+
expect(el.querySelector(".hk-usage-flow__chart")?.getAttribute("viewBox")).toBe(
|
|
161
|
+
"0 0 800 360",
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("re-renders its geometry when the host swaps the rows", async () => {
|
|
166
|
+
const rows = ref(ROWS);
|
|
167
|
+
const container = document.createElement("div");
|
|
168
|
+
document.body.appendChild(container);
|
|
169
|
+
const app = createApp({ render: () => h(HkUsageFlow, { rows: rows.value }) });
|
|
170
|
+
app.mount(container);
|
|
171
|
+
mounts.push({ app, container });
|
|
172
|
+
expect(container.querySelectorAll(".hk-usage-flow__node")).toHaveLength(6);
|
|
173
|
+
rows.value = [{ source: "x", middle: "y", target: "z", value: 1 }];
|
|
174
|
+
await nextTick();
|
|
175
|
+
expect(container.querySelectorAll(".hk-usage-flow__node")).toHaveLength(3);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("leaves the per-bar accessible name to the svg (no dead aria-labels)", () => {
|
|
179
|
+
const el = mount(<HkUsageFlow rows={ROWS} />);
|
|
180
|
+
const bar = el.querySelector(".hk-usage-flow__bar");
|
|
181
|
+
expect(bar?.getAttribute("aria-label")).toBeNull();
|
|
182
|
+
expect(bar?.getAttribute("data-hint")).toBeTruthy();
|
|
183
|
+
expect(el.querySelector(".hk-usage-flow__chart")?.getAttribute("role")).toBe("img");
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("can hide the legend without dropping the chart", () => {
|
|
187
|
+
const el = mount(<HkUsageFlow rows={ROWS} showLegend={false} />);
|
|
188
|
+
expect(el.querySelector(".hk-usage-flow__chart")).toBeTruthy();
|
|
189
|
+
expect(el.querySelector(".hk-usage-flow__legend")).toBeNull();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("omits labels on bars too short to read, keeping their hints", () => {
|
|
193
|
+
const dense = [
|
|
194
|
+
{ source: "huge", middle: "up", target: "model-x", value: 9_800_000 },
|
|
195
|
+
...Array.from({ length: 8 }, (_, i) => ({
|
|
196
|
+
source: `tiny-${i}`,
|
|
197
|
+
middle: "up",
|
|
198
|
+
target: "model-x",
|
|
199
|
+
value: 2_000,
|
|
200
|
+
})),
|
|
201
|
+
];
|
|
202
|
+
const el = mount(<HkUsageFlow rows={dense} />);
|
|
203
|
+
const nodes = el.querySelectorAll(".hk-usage-flow__node");
|
|
204
|
+
const labels = el.querySelectorAll(".hk-usage-flow__label");
|
|
205
|
+
// 9 callers + 1 upstream + 1 model; only the three tall bars label.
|
|
206
|
+
expect(nodes).toHaveLength(11);
|
|
207
|
+
expect(labels).toHaveLength(3);
|
|
208
|
+
// The omitted labels' identity survives on the hint channel.
|
|
209
|
+
const hints = [...el.querySelectorAll(".hk-usage-flow__bar")].map((bar) =>
|
|
210
|
+
bar.getAttribute("data-hint"),
|
|
211
|
+
);
|
|
212
|
+
expect(hints.some((hint) => hint?.includes("tiny-0"))).toBe(true);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("keeps the hover hint channel on nodes and ribbons", () => {
|
|
216
|
+
const el = mount(<HkUsageFlow rows={ROWS} />);
|
|
217
|
+
const hints = [
|
|
218
|
+
...el.querySelectorAll(".hk-usage-flow__ribbon"),
|
|
219
|
+
...el.querySelectorAll(".hk-usage-flow__bar"),
|
|
220
|
+
].map((node) => node.getAttribute("data-hint"));
|
|
221
|
+
expect(hints.every((hint) => typeof hint === "string" && hint.length > 0)).toBe(true);
|
|
222
|
+
expect(hints.some((hint) => hint?.includes("caller-a → up-1"))).toBe(true);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe("HkUsageFlow sheet contracts", () => {
|
|
227
|
+
/** Source-scan pin for the RTL fix: SVG `text-anchor` start/end resolve
|
|
228
|
+
* against the inline base direction, so the chart MUST declare an LTR
|
|
229
|
+
* base — deleting the declaration would flip the labels in an RTL host
|
|
230
|
+
* and no unit test would notice. */
|
|
231
|
+
it("keeps the chart's inline base direction LTR", () => {
|
|
232
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
233
|
+
const sheet = readFileSync(resolve(here, "HkUsageFlow.scss"), "utf8");
|
|
234
|
+
expect(sheet).toMatch(/&__chart\s*\{[^}]*direction:\s*ltr;/s);
|
|
235
|
+
});
|
|
236
|
+
});
|