@celestia-island/hikari 0.55.369 → 0.55.370

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 (31) hide show
  1. package/package.json +1 -1
  2. package/src/animation/registerAnimations.ts +6 -0
  3. package/src/components/HkEmergencyAlert.layering.contract.test.ts +99 -0
  4. package/src/components/HkEmergencyAlert.scss +279 -0
  5. package/src/components/HkEmergencyAlert.test.tsx +249 -0
  6. package/src/components/HkEmergencyAlert.tsx +232 -0
  7. package/src/i18n/locales/ar/components.json +5 -1
  8. package/src/i18n/locales/de/components.json +5 -1
  9. package/src/i18n/locales/en/components.json +5 -1
  10. package/src/i18n/locales/es/components.json +5 -1
  11. package/src/i18n/locales/fr/components.json +5 -1
  12. package/src/i18n/locales/ja/components.json +5 -1
  13. package/src/i18n/locales/ko/components.json +5 -1
  14. package/src/i18n/locales/pt/components.json +5 -1
  15. package/src/i18n/locales/ru/components.json +5 -1
  16. package/src/i18n/locales/zh-Hans/components.json +5 -1
  17. package/src/i18n/locales/zh-Hant/components.json +5 -1
  18. package/src/i18n/usageSurface.test.ts +6 -0
  19. package/src/index.ts +12 -0
  20. package/src/overlay/hudRoot.ts +1 -0
  21. package/src/overlay/overlayRoot.styles.test.ts +19 -0
  22. package/src/overlay/overlayRoot.ts +1 -1
  23. package/src/plugins/boot-recovery.details.test.ts +1 -1
  24. package/src/plugins/boot-recovery.diagnostics.test.ts +1 -1
  25. package/src/plugins/boot-recovery.locales.test.ts +1 -1
  26. package/src/plugins/bootLandingCss.js +1 -1
  27. package/src/runtime/index.ts +1 -0
  28. package/src/runtime/useEmergencyAlert.test.ts +121 -0
  29. package/src/runtime/useEmergencyAlert.ts +219 -0
  30. package/src/scale.scss +8 -1
  31. package/src/styles/theme/scale.scss +8 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celestia-island/hikari",
3
- "version": "0.55.369",
3
+ "version": "0.55.370",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Hikari UI library, Vue 3 runtime — one design system for Vue 3 and Rust/WASM applications",
@@ -151,3 +151,9 @@ registerCssAnimation("hk-badge-dot-pulse", { infinite: true });
151
151
  // Reduced motion keeps the band and drops the loop (house pattern, see
152
152
  // HkSkeleton); the global CSS-animation switch pauses it as usual.
153
153
  registerCssAnimation("hk-edge-glow-breathe", { infinite: true });
154
+ // components/HkEmergencyAlert.scss — the takeover band's steady-state
155
+ // life signs: the tone badge breathe and the hairline edge signal pulse
156
+ // (both loop forever while the alert stands). Reduced motion keeps the
157
+ // band and drops the loops (house pattern, see HkSkeleton).
158
+ registerCssAnimation("hk-emergency-breathe", { infinite: true });
159
+ registerCssAnimation("hk-emergency-signal", { infinite: true });
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Source contract: the emergency takeover is a document-space top
3
+ * owner with TWO legs (the same contract HkModal rides, pinned by
4
+ * chromeBand.contract tests on the chest side):
5
+ *
6
+ * 1. z leg — `var(--hk-z-emergency, 96000)` sits above the error
7
+ * landing (95000) and strictly below the window titlebar
8
+ * (100001): the band demands attention but must NEVER cover the
9
+ * titlebar (user direction 2026-11-13).
10
+ * 2. geometry leg — the layer insets below the host chrome band via
11
+ * `var(--hk-chrome-band-inset, 0px)` so a native shell's titlebar
12
+ * strip stays visible, draggable and clickable.
13
+ *
14
+ * Plus the width and blocking doctrines: the band spans edge to edge
15
+ * (no viewport gutter — user direction 2026-11-13) and the layer
16
+ * itself is pointer-transparent (only the band intercepts), so the
17
+ * emergency never blocks work happening underneath.
18
+ */
19
+ import { describe, expect, it } from "vitest";
20
+ import { readFileSync } from "node:fs";
21
+ import { dirname, join } from "node:path";
22
+ import { fileURLToPath } from "node:url";
23
+
24
+ const here = dirname(fileURLToPath(import.meta.url));
25
+
26
+ function sheet(): string {
27
+ const raw = readFileSync(join(here, "HkEmergencyAlert.scss"), "utf-8");
28
+ // Full-line `//` comments are stripped before ANY assertion (the
29
+ // registerAnimations parity convention): a commented-out declaration
30
+ // is not a declaration — R5 B1 deleted the root pointer-events line
31
+ // and left `// pointer-events: none;` behind, and every contains()
32
+ // here happily read the comment as the declaration.
33
+ return raw.replace(/^[ \t]*\/\/.*$/gm, "");
34
+ }
35
+
36
+ /** The `.hk-emergency { … }` root rule, brace-balanced (it nests the
37
+ * webkit scrollbar rule). Assertions about the LAYER must bind here,
38
+ * not to the whole sheet: the band's pseudo-elements carry their own
39
+ * `pointer-events: none` lines, which a whole-file contains() would
40
+ * happily satisfy even with the root declaration deleted (R3 M6).
41
+ * Comment-blindness is handled upstream in sheet() — stripping here
42
+ * as well would double-handle; braces inside comments were the other
43
+ * R5 hazard, and they are gone before the walk runs. */
44
+ function rootBlock(css: string): string {
45
+ const start = css.indexOf(".hk-emergency {");
46
+ if (start < 0) throw new Error("root rule .hk-emergency { not found");
47
+ let depth = 0;
48
+ for (let i = start; i < css.length; i++) {
49
+ if (css[i] === "{") depth++;
50
+ else if (css[i] === "}") {
51
+ depth--;
52
+ if (depth === 0) return css.slice(start, i + 1);
53
+ }
54
+ }
55
+ throw new Error("unbalanced braces in .hk-emergency root rule");
56
+ }
57
+
58
+ describe("emergency alert layering contract", () => {
59
+ it("pins the document-space z band between error landing and titlebar", () => {
60
+ const css = sheet();
61
+ expect(css).toContain("z-index: var(--hk-z-emergency, 96000);");
62
+ });
63
+
64
+ it("pins the chrome-band geometry leg on the top edge", () => {
65
+ const css = sheet();
66
+ expect(css).toContain("inset: var(--hk-chrome-band-inset, 0px) 0 0 0;");
67
+ });
68
+
69
+ it("the band spans the full width — no viewport gutter on it", () => {
70
+ const css = sheet();
71
+ // The band (not the reading column) is width:100%…
72
+ expect(css).toContain("width: 100%;");
73
+ // …and the sheet never insets the band from the screen edges.
74
+ expect(css).not.toMatch(/inset-inline-(start|end):\s*var\(--viewport-gutter/);
75
+ });
76
+
77
+ it("the layer is pointer-transparent; only the band is interactive", () => {
78
+ const css = sheet();
79
+ // Root rule: transparent (the scrim never blocks work) — bound to
80
+ // the ROOT block because the band pseudo-elements also carry
81
+ // pointer-events:none lines that would mask its deletion (M6).
82
+ expect(rootBlock(css)).toContain("pointer-events: none;");
83
+ expect(rootBlock(css)).not.toContain("pointer-events: auto;");
84
+ // …and exactly ONE rule in the whole sheet opts back in — the band.
85
+ expect(css.match(/pointer-events:\s*auto;/g)?.length).toBe(1);
86
+ });
87
+
88
+ it("long content stays reachable: the layer scrolls and the band safe-centers", () => {
89
+ // A fixed full-height layer never grows the document, so clipped
90
+ // content would strand the actions forever (R2 finding 5). The
91
+ // layer must scroll, and the band must center via margin-block
92
+ // (align-items:center on the LAYER clips both ends with no way
93
+ // back; the icon/actions rows may still center their own content).
94
+ const css = sheet();
95
+ expect(css).toContain("overflow-y: auto;");
96
+ expect(css).toContain("margin-block: auto;");
97
+ expect(css).not.toMatch(/\.hk-emergency\s*\{[^}]*align-items\s*:/s);
98
+ });
99
+ });
@@ -0,0 +1,279 @@
1
+ // HkEmergencyAlert — the toast family's full-screen takeover member.
2
+ //
3
+ // Token strategy: severity colors read the host's RGB-triplet theme
4
+ // channels (--color-error / --color-warning) with neutral fallbacks,
5
+ // mirroring HkErrorLanding so the band renders correctly standalone
6
+ // and adapts to host themes. Themes customize through the exposed
7
+ // --hk-emergency-* variable family (line / wash / band / scrim) —
8
+ // geometry (full-width band, circle badge, inset, z) is component
9
+ // owned and never theme-overridable.
10
+
11
+ @use "../styles/polyfill" as *;
12
+
13
+ .hk-emergency {
14
+ // Severity tone defaults; a theme re-points these per band state.
15
+ --hk-emergency-tone: rgb(var(--color-error, 226 84 84));
16
+ --hk-emergency-line: var(--hk-emergency-tone);
17
+ --hk-emergency-line-soft: rgb(var(--color-error, 226 84 84) / 55%);
18
+ --hk-emergency-wash: rgb(var(--color-error, 226 84 84) / 9%);
19
+ --hk-emergency-band-bg: rgb(var(--color-surface, 24 26 32) / 96%);
20
+ --hk-emergency-scrim: rgb(var(--color-background, 10 12 16) / 52%);
21
+
22
+ position: fixed;
23
+ // Chrome-band leg: in native hosts the window titlebar owns the top
24
+ // strip (--hk-chrome-band-inset, e.g. the 3rem header); the layer
25
+ // starts BELOW it so the bar stays visible and draggable. Browser
26
+ // hosts leave the var unset → 0px → true full-screen.
27
+ inset: var(--hk-chrome-band-inset, 0px) 0 0 0;
28
+ // Document-space z band (scale.scss): above error landing (95000),
29
+ // strictly below the window titlebar (100001) — the alert never
30
+ // covers the titlebar (user direction 2026-11-13).
31
+ z-index: var(--hk-z-emergency, 96000);
32
+ display: flex;
33
+ // Safe centering: the band centers through margin-block:auto (below)
34
+ // instead of a flex align rule — when long content (wrapped actions,
35
+ // tall description) outgrows the viewport the same rules let the
36
+ // layer SCROLL to it, where flex centering would clip both ends with
37
+ // no way back (fixed layers never grow the document). The wheel
38
+ // reaches this container only over the band itself (pointer
39
+ // transparency elsewhere), and an overflowing band covers the layer
40
+ // anyway — so the actions are always reachable. The native bar is
41
+ // hidden per the family scrollbar invariant (scrolling is the
42
+ // rare tall-content escape hatch, not part of the look).
43
+ overflow-y: auto;
44
+ scrollbar-width: none;
45
+ &::-webkit-scrollbar {
46
+ display: none;
47
+ }
48
+ // The scrim never blocks work (user direction 2026-11-13): the
49
+ // layer itself lets pointers through; only the band is interactive.
50
+ pointer-events: none;
51
+ }
52
+
53
+ .hk-emergency.is-warning {
54
+ --hk-emergency-tone: rgb(var(--color-warning, 222 168 62));
55
+ --hk-emergency-line: var(--hk-emergency-tone);
56
+ --hk-emergency-line-soft: rgb(var(--color-warning, 222 168 62) / 55%);
57
+ --hk-emergency-wash: rgb(var(--color-warning, 222 168 62) / 10%);
58
+ }
59
+
60
+ .hk-emergency-scrim {
61
+ position: absolute;
62
+ inset: 0;
63
+ background: var(--hk-emergency-scrim);
64
+ @include backdrop-filter(blur(2px));
65
+ }
66
+
67
+ .hk-emergency-band {
68
+ position: relative;
69
+ pointer-events: auto;
70
+ // The other half of safe centering: margin-block:auto centers the
71
+ // band when it fits and collapses to flush-top when it overflows
72
+ // (the layer then scrolls — see .hk-emergency).
73
+ margin-block: auto;
74
+ // Edge to edge — no viewport gutter, the emergency band spans the
75
+ // full width by design (user direction 2026-11-13); only the inner
76
+ // column constrains reading width.
77
+ width: 100%;
78
+ padding: clamp(1.75rem, 4vh, 2.75rem) var(--space-20, 1.25rem);
79
+ background:
80
+ linear-gradient(to bottom, var(--hk-emergency-wash), transparent 34%, transparent 66%, var(--hk-emergency-wash)),
81
+ var(--hk-emergency-band-bg);
82
+ border-top: 2px solid var(--hk-emergency-line);
83
+ border-bottom: 2px solid var(--hk-emergency-line);
84
+ text-align: center;
85
+ color: rgb(var(--color-text, 236 238 242));
86
+ font-family: var(--font-sans, system-ui, sans-serif);
87
+
88
+ // FUI double line: a second, fainter signal line hugging each edge
89
+ // just inside the solid one — reads as instrument chrome, not a
90
+ // decorative frame. Pure gradient layers, no extra DOM.
91
+ &::before,
92
+ &::after {
93
+ content: "";
94
+ position: absolute;
95
+ left: 0;
96
+ right: 0;
97
+ height: 1px;
98
+ background: var(--hk-emergency-line-soft);
99
+ pointer-events: none;
100
+ }
101
+ &::before {
102
+ top: 4px;
103
+ }
104
+ &::after {
105
+ bottom: 4px;
106
+ }
107
+ }
108
+
109
+ .hk-emergency-inner {
110
+ max-width: 680px;
111
+ margin-inline: auto;
112
+ }
113
+
114
+ // The tone circular badge — the HkErrorLanding __icon recipe scaled
115
+ // up for the band: gradient disc, hairline ring, layered halo plus a
116
+ // long soft drop shadow. A circle is the one badge shape legal across
117
+ // every shipped theme language (instrument dial / circles-stay-
118
+ // circles / measure-circle), so no rounded-square variant exists.
119
+ .hk-emergency-icon {
120
+ width: 72px;
121
+ height: 72px;
122
+ margin: 0 auto var(--space-14, 0.875rem);
123
+ display: flex;
124
+ align-items: center;
125
+ justify-content: center;
126
+ border-radius: 50%;
127
+ color: var(--hk-emergency-tone);
128
+ background: linear-gradient(145deg, rgb(var(--color-error, 226 84 84) / 18%), rgb(var(--color-error, 226 84 84) / 6%));
129
+ border: 1px solid rgb(var(--color-error, 226 84 84) / 26%);
130
+ box-shadow:
131
+ 0 0 0 8px rgb(var(--color-error, 226 84 84) / 5%),
132
+ 0 10px 28px -12px rgb(var(--color-error, 226 84 84) / 45%);
133
+ }
134
+
135
+ .is-warning .hk-emergency-icon {
136
+ background: linear-gradient(145deg, rgb(var(--color-warning, 222 168 62) / 20%), rgb(var(--color-warning, 222 168 62) / 7%));
137
+ border-color: rgb(var(--color-warning, 222 168 62) / 28%);
138
+ box-shadow:
139
+ 0 0 0 8px rgb(var(--color-warning, 222 168 62) / 6%),
140
+ 0 10px 28px -12px rgb(var(--color-warning, 222 168 62) / 45%);
141
+ }
142
+
143
+ .hk-emergency-meta {
144
+ display: flex;
145
+ justify-content: center;
146
+ margin-bottom: var(--space-8, 0.5rem);
147
+ }
148
+
149
+ // FUI micro-label: mono, wide tracking, tone ink — the "PLATFORM LINK
150
+ // LOST // 01" line above the headline. Decorative duplicate of the
151
+ // severity, hence aria-hidden in the tsx.
152
+ .hk-emergency-kicker {
153
+ font-family: var(--font-mono, ui-monospace, monospace);
154
+ font-size: 0.75rem;
155
+ font-weight: 600;
156
+ letter-spacing: 0.28em;
157
+ text-transform: uppercase;
158
+ color: var(--hk-emergency-tone);
159
+ margin-bottom: var(--space-6, 0.375rem);
160
+ }
161
+
162
+ .hk-emergency-title {
163
+ margin: 0 0 var(--space-8, 0.5rem);
164
+ font-size: clamp(1.75rem, 4vw, 2.5rem);
165
+ font-weight: 700;
166
+ line-height: 1.2;
167
+ letter-spacing: 0.01em;
168
+ color: rgb(var(--color-text, 236 238 242));
169
+ }
170
+
171
+ .hk-emergency-desc {
172
+ margin: 0 auto var(--space-10, 0.625rem);
173
+ max-width: 56ch;
174
+ font-size: clamp(1rem, 2vw, 1.125rem);
175
+ line-height: 1.6;
176
+ color: rgb(var(--color-muted, 158 162 170));
177
+ white-space: pre-line;
178
+ }
179
+
180
+ .hk-emergency-more {
181
+ margin: 0 0 var(--space-10, 0.625rem);
182
+ font-family: var(--font-mono, ui-monospace, monospace);
183
+ font-size: 0.8125rem;
184
+ letter-spacing: 0.08em;
185
+ color: rgb(var(--color-muted, 158 162 170));
186
+ }
187
+
188
+ .hk-emergency-actions {
189
+ display: flex;
190
+ justify-content: center;
191
+ flex-wrap: wrap;
192
+ gap: var(--space-12, 0.75rem);
193
+ margin-top: var(--space-14, 0.875rem);
194
+ }
195
+
196
+ // ── Motion ────────────────────────────────────────────────────────────
197
+ // Enter/leave: the band draws its signal edges and lifts into place;
198
+ // leave mirrors it. Steady state stays ALIVE but quiet — the badge
199
+ // breathes ±2% and the hairline edges pulse — the cinematic klaxon
200
+ // turned down to instrument level. All motion dies under
201
+ // reduced-motion (OS level and the .reduce-motion class), matching
202
+ // the theme packages' dual kill-switch convention.
203
+
204
+ @keyframes hk-emergency-breathe {
205
+ 0%,
206
+ 100% {
207
+ transform: scale(1);
208
+ }
209
+ 50% {
210
+ transform: scale(1.02);
211
+ }
212
+ }
213
+
214
+ @keyframes hk-emergency-signal {
215
+ 0%,
216
+ 100% {
217
+ opacity: 0.45;
218
+ }
219
+ 50% {
220
+ opacity: 1;
221
+ }
222
+ }
223
+
224
+ .hk-emergency-icon {
225
+ animation: hk-emergency-breathe 2.8s ease-in-out infinite;
226
+ }
227
+
228
+ .hk-emergency-band::before,
229
+ .hk-emergency-band::after {
230
+ animation: hk-emergency-signal 2.8s ease-in-out infinite;
231
+ }
232
+
233
+ .hk-emergency-enter-active,
234
+ .hk-emergency-leave-active {
235
+ transition: opacity 240ms var(--ease-out, cubic-bezier(0.4, 0, 0.2, 1));
236
+
237
+ .hk-emergency-band {
238
+ transition: transform 320ms var(--ease-out-expo, cubic-bezier(0.19, 1, 0.22, 1));
239
+ }
240
+ }
241
+
242
+ .hk-emergency-enter-from,
243
+ .hk-emergency-leave-to {
244
+ opacity: 0;
245
+
246
+ .hk-emergency-band {
247
+ transform: scaleY(0.72);
248
+ }
249
+ }
250
+
251
+ @media (prefers-reduced-motion: reduce) {
252
+ .hk-emergency-enter-active,
253
+ .hk-emergency-leave-active,
254
+ .hk-emergency-enter-active .hk-emergency-band,
255
+ .hk-emergency-leave-active .hk-emergency-band {
256
+ transition: none;
257
+ }
258
+
259
+ .hk-emergency-icon,
260
+ .hk-emergency-band::before,
261
+ .hk-emergency-band::after {
262
+ animation: none;
263
+ }
264
+ }
265
+
266
+ html.reduce-motion {
267
+ .hk-emergency-enter-active,
268
+ .hk-emergency-leave-active,
269
+ .hk-emergency-enter-active .hk-emergency-band,
270
+ .hk-emergency-leave-active .hk-emergency-band {
271
+ transition: none;
272
+ }
273
+
274
+ .hk-emergency-icon,
275
+ .hk-emergency-band::before,
276
+ .hk-emergency-band::after {
277
+ animation: none;
278
+ }
279
+ }
@@ -0,0 +1,249 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import { createApp, h, nextTick } from "vue";
3
+
4
+ import HkEmergencyAlert from "./HkEmergencyAlert";
5
+ import {
6
+ clearEmergencyAlert,
7
+ raiseEmergencyAlert,
8
+ resetEmergencyAlerts,
9
+ } from "../runtime/useEmergencyAlert";
10
+
11
+ /**
12
+ * HkEmergencyAlert contract tests:
13
+ * - nothing renders with no active alerts
14
+ * - raise renders the takeover band (role=alert, severity class, tone
15
+ * badge, kicker default, host actions + built-in snooze)
16
+ * - dismissible=false drops the snooze button (red alerts stay)
17
+ * - snooze hides the band until the episode clears
18
+ * - multiple alerts: only the most severe takes the band, the rest
19
+ * count in the "+N more" line; clearing the winner promotes the next
20
+ * - Esc snoozes when the focus is inside the band
21
+ *
22
+ * (Repo test convention: raw createApp + document queries, no
23
+ * @vue/test-utils dependency.)
24
+ */
25
+
26
+ const mounts: Array<{ app: ReturnType<typeof createApp>; container: HTMLElement }> = [];
27
+
28
+ function mountHost() {
29
+ const container = document.createElement("div");
30
+ document.body.appendChild(container);
31
+ const app = createApp({ render: () => h(HkEmergencyAlert) });
32
+ mounts.push({ app, container });
33
+ app.mount(container);
34
+ return container;
35
+ }
36
+
37
+ async function settle() {
38
+ await nextTick();
39
+ await new Promise((r) => setTimeout(r, 30));
40
+ await nextTick();
41
+ }
42
+
43
+ function layer(): HTMLElement | null {
44
+ return document.querySelector<HTMLElement>(".hk-emergency");
45
+ }
46
+
47
+ function buttons(): HTMLButtonElement[] {
48
+ return Array.from(layer()?.querySelectorAll<HTMLButtonElement>(".hk-emergency-actions button") ?? []);
49
+ }
50
+
51
+ afterEach(() => {
52
+ for (const { app, container } of mounts) {
53
+ app.unmount();
54
+ container.remove();
55
+ }
56
+ mounts.length = 0;
57
+ resetEmergencyAlerts();
58
+ });
59
+
60
+ describe("HkEmergencyAlert rendering", () => {
61
+ it("renders nothing while no alert is active", async () => {
62
+ mountHost();
63
+ await settle();
64
+ expect(layer()).toBeNull();
65
+ });
66
+
67
+ it("renders the takeover band with severity, title, badge and kicker", async () => {
68
+ mountHost();
69
+ raiseEmergencyAlert({
70
+ id: "platform-link",
71
+ severity: "danger",
72
+ title: "警告:所有平台连接已断开",
73
+ description: "没有任何边缘或计算平台在线",
74
+ code: "ALL_PLATFORMS_DOWN",
75
+ kicker: "PLATFORM LINK LOST",
76
+ dismissible: false,
77
+ });
78
+ await settle();
79
+ const el = layer();
80
+ expect(el).not.toBeNull();
81
+ expect(el!.getAttribute("role")).toBe("alert");
82
+ expect(el!.classList.contains("is-danger")).toBe(true);
83
+ expect(el!.textContent).toContain("警告:所有平台连接已断开");
84
+ expect(el!.textContent).toContain("没有任何边缘或计算平台在线");
85
+ expect(el!.textContent).toContain("ALL_PLATFORMS_DOWN");
86
+ expect(el!.textContent).toContain("PLATFORM LINK LOST");
87
+ // Non-dismissible: no built-in snooze button.
88
+ expect(buttons().some((b) => b.textContent === "Hide for now")).toBe(false);
89
+ });
90
+
91
+ it("falls back to the per-severity kicker word when none is given", async () => {
92
+ mountHost();
93
+ raiseEmergencyAlert({ id: "q", severity: "warning", title: "t" });
94
+ await settle();
95
+ expect(layer()!.textContent).toContain("WARNING");
96
+ });
97
+
98
+ it("clearing the alert removes the band", async () => {
99
+ mountHost();
100
+ raiseEmergencyAlert({ id: "q", severity: "warning", title: "t" });
101
+ await settle();
102
+ expect(layer()).not.toBeNull();
103
+ clearEmergencyAlert("q");
104
+ await settle();
105
+ expect(layer()).toBeNull();
106
+ });
107
+ });
108
+
109
+ describe("actions & snooze", () => {
110
+ it("renders host actions plus the built-in snooze and wires their handlers", async () => {
111
+ mountHost();
112
+ const handle = vi.fn();
113
+ const onSnooze = vi.fn();
114
+ raiseEmergencyAlert({
115
+ id: "quota",
116
+ severity: "warning",
117
+ title: "额度耗尽",
118
+ actions: [{ label: "立即处理", onClick: handle }],
119
+ onSnooze,
120
+ });
121
+ await settle();
122
+ const [primary, snooze] = buttons();
123
+ expect(primary.textContent).toBe("立即处理");
124
+ expect(snooze.textContent).toBe("Hide for now");
125
+
126
+ primary.click();
127
+ expect(handle).toHaveBeenCalledTimes(1);
128
+ // The action does NOT dismiss the band — only the condition
129
+ // clearing (or a snooze) does; "handle now" navigates away but the
130
+ // emergency stands until resolved.
131
+ expect(layer()).not.toBeNull();
132
+
133
+ snooze.click();
134
+ await settle();
135
+ expect(onSnooze).toHaveBeenCalledTimes(1);
136
+ expect(layer()).toBeNull();
137
+ // Snoozed, not cleared: the context still holds the alert.
138
+ expect(document.querySelector(".hk-emergency")).toBeNull();
139
+ });
140
+
141
+ it("Esc snoozes a dismissible band while the focus is inside it", async () => {
142
+ mountHost();
143
+ raiseEmergencyAlert({ id: "q", severity: "warning", title: "t" });
144
+ await settle();
145
+ const el = layer()!;
146
+ // Focus is moved onto the band's primary action on raise — the Esc
147
+ // path below only acts while the band owns the focus.
148
+ expect(el.contains(document.activeElement)).toBe(true);
149
+ el.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
150
+ await settle();
151
+ expect(layer()).toBeNull();
152
+ });
153
+
154
+ it("Esc does nothing on a non-dismissible band", async () => {
155
+ mountHost();
156
+ raiseEmergencyAlert({ id: "q", severity: "danger", title: "t", dismissible: false });
157
+ await settle();
158
+ layer()!.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
159
+ await settle();
160
+ expect(layer()).not.toBeNull();
161
+ });
162
+ });
163
+
164
+ describe("focus choreography", () => {
165
+ it("raise then clear returns the focus to the element that held it", async () => {
166
+ const outside = document.createElement("button");
167
+ outside.textContent = "work";
168
+ document.body.appendChild(outside);
169
+ outside.focus();
170
+ mountHost();
171
+ raiseEmergencyAlert({ id: "q", severity: "warning", title: "t" });
172
+ await settle();
173
+ // The band took the focus on raise…
174
+ expect(layer()!.contains(document.activeElement)).toBe(true);
175
+ clearEmergencyAlert("q");
176
+ await settle();
177
+ // …and gave it back on clear (first-raise capture works even though
178
+ // the layer was not yet in the DOM during the pre-flush watch).
179
+ expect(document.activeElement).toBe(outside);
180
+ outside.remove();
181
+ });
182
+
183
+ it("focus lands on the primary action when the alert pre-dates the host mount", async () => {
184
+ raiseEmergencyAlert({
185
+ id: "pre",
186
+ severity: "danger",
187
+ title: "t",
188
+ dismissible: false,
189
+ actions: [{ label: "Go" }],
190
+ });
191
+ mountHost();
192
+ await settle();
193
+ const primary = layer()!.querySelector<HTMLButtonElement>(".hk-emergency-actions button");
194
+ expect(primary?.textContent).toBe("Go");
195
+ expect(document.activeElement).toBe(primary);
196
+ });
197
+
198
+ it("clearing never steals the focus back from work below", async () => {
199
+ const input = document.createElement("input");
200
+ document.body.appendChild(input);
201
+ mountHost();
202
+ raiseEmergencyAlert({ id: "q", severity: "warning", title: "t" });
203
+ await settle();
204
+ // The user moves into the page below while the alert stands (the
205
+ // page stays focusable by design)…
206
+ input.focus();
207
+ clearEmergencyAlert("q");
208
+ await settle();
209
+ // …and the clear must leave that focus alone.
210
+ expect(document.activeElement).toBe(input);
211
+ input.remove();
212
+ });
213
+ });
214
+
215
+ describe("single-band policy", () => {
216
+ it("the most severe alert takes the band and counts the suppressed", async () => {
217
+ mountHost();
218
+ raiseEmergencyAlert({ id: "quota", severity: "warning", title: "额度耗尽" });
219
+ raiseEmergencyAlert({ id: "link", severity: "danger", title: "全平台失联" });
220
+ await settle();
221
+ const el = layer()!;
222
+ expect(el.classList.contains("is-danger")).toBe(true);
223
+ expect(el.textContent).toContain("全平台失联");
224
+ expect(el.textContent).toContain("+1");
225
+ expect(el.textContent).toContain("more alert(s) pending");
226
+
227
+ // Clearing the winner promotes the suppressed warning.
228
+ clearEmergencyAlert("link");
229
+ await settle();
230
+ const promoted = layer()!;
231
+ expect(promoted.classList.contains("is-warning")).toBe(true);
232
+ expect(promoted.textContent).toContain("额度耗尽");
233
+ expect(promoted.textContent).not.toContain("+1");
234
+ });
235
+
236
+ it("a snoozed alert neither takes the band nor counts as suppressed", async () => {
237
+ mountHost();
238
+ raiseEmergencyAlert({ id: "a", severity: "warning", title: "a" });
239
+ raiseEmergencyAlert({ id: "b", severity: "danger", title: "b" });
240
+ await settle();
241
+ // Snooze the winner through the context; only "a" remains visible.
242
+ const snooze = buttons().find((b) => b.textContent === "Hide for now")!;
243
+ snooze.click();
244
+ await settle();
245
+ const el = layer()!;
246
+ expect(el.textContent).toContain("a");
247
+ expect(el.textContent).not.toContain("+1");
248
+ });
249
+ });