@celestia-island/hikari 0.55.145 → 0.55.147
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/HkBoard.nodeActions.test.tsx +101 -0
- package/src/components/HkBoard.scss +15 -0
- package/src/components/HkBoard.tsx +20 -0
- package/src/composables/useConnectionProbe.test.ts +179 -0
- package/src/composables/useConnectionProbe.ts +54 -14
- package/src/composables/useEngineHealth.test.ts +128 -0
- package/src/composables/useEngineHealth.ts +28 -4
package/package.json
CHANGED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createApp, defineComponent, h } from "vue";
|
|
3
|
+
|
|
4
|
+
import HkBoard, { type BoardNodeInput } from "./HkBoard";
|
|
5
|
+
|
|
6
|
+
/** Pointer event the suite can dispatch (happy-dom has PointerEvent). */
|
|
7
|
+
function pointerEvent(type: string, x: number, y: number): Event {
|
|
8
|
+
const Ctor = (globalThis as { PointerEvent?: typeof MouseEvent }).PointerEvent ?? MouseEvent;
|
|
9
|
+
const ev = new Ctor(type, { bubbles: true, cancelable: true, clientX: x, clientY: y });
|
|
10
|
+
(ev as { pointerId?: number }).pointerId = 1;
|
|
11
|
+
return ev;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const nodes: BoardNodeInput[] = [
|
|
15
|
+
{ id: "a", x: 0, y: 0, w: 100, h: 60, draggable: true, label: "A" },
|
|
16
|
+
{ id: "b", x: 160, y: 0, w: 100, h: 60, draggable: true, label: "B" },
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
/** Mounts the board with the given slots, the repo's own way (no
|
|
20
|
+
* @vue/test-utils in this package): a throwaway app on document.body. */
|
|
21
|
+
function mountBoard(slots: Record<string, () => unknown> = {}, onNodeMove?: () => void): unknown {
|
|
22
|
+
const host = document.createElement("div");
|
|
23
|
+
document.body.appendChild(host);
|
|
24
|
+
// (defineComponent(fn) treats fn as a FUNCTIONAL component — the wrapper
|
|
25
|
+
// needs the explicit setup form to hand HkBoard its slots.)
|
|
26
|
+
const wrapper = defineComponent({
|
|
27
|
+
setup: () => () => h(HkBoard, { nodes, onNodeMove }, slots),
|
|
28
|
+
});
|
|
29
|
+
const app = createApp(wrapper);
|
|
30
|
+
app.mount(host);
|
|
31
|
+
return app;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
afterEach(() => {
|
|
35
|
+
document.body.innerHTML = "";
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("HkBoard nodeActions slot", () => {
|
|
39
|
+
it("renders the slot per node, docked inside the node shell", () => {
|
|
40
|
+
mountBoard({
|
|
41
|
+
nodeActions: () => [
|
|
42
|
+
h("button", { "data-test": "act" }, "act"),
|
|
43
|
+
],
|
|
44
|
+
});
|
|
45
|
+
const docks = [...document.body.querySelectorAll<HTMLElement>(".hk-board-node-actions")];
|
|
46
|
+
expect(docks).toHaveLength(2);
|
|
47
|
+
expect(docks[0].querySelector("[data-test=act]")).toBeTruthy();
|
|
48
|
+
expect(docks[1].querySelector("[data-test=act]")).toBeTruthy();
|
|
49
|
+
// The dock lives INSIDE the node shell (the SCSS anchors it to the
|
|
50
|
+
// node's bottom-end corner — pinned by the source contract below).
|
|
51
|
+
expect(docks[0].closest(".hk-board-node")).toBeTruthy();
|
|
52
|
+
expect(docks[0].parentElement!.classList.contains("hk-board-node")).toBe(true);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("renders nothing when the slot is absent", () => {
|
|
56
|
+
mountBoard();
|
|
57
|
+
expect(document.body.querySelector(".hk-board-node-actions")).toBeNull();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("keeps a press that starts on a dock action from becoming a node drag", () => {
|
|
61
|
+
// The board captures the pointer on its own root for draggable nodes;
|
|
62
|
+
// a dock press must not enter that gesture (upstream HkMinimap guards
|
|
63
|
+
// its zoom chrome the same way). happy-dom cannot emulate capture, so
|
|
64
|
+
// the observable contract here is the DRAG: a pointer that travels
|
|
65
|
+
// after starting on the dock leaves the node where it was.
|
|
66
|
+
const onNodeMove = vi.fn();
|
|
67
|
+
const host = document.createElement("div");
|
|
68
|
+
document.body.appendChild(host);
|
|
69
|
+
const app = mountBoard(
|
|
70
|
+
{ nodeActions: () => [h("button", { "data-test": "act" }, "act")] },
|
|
71
|
+
onNodeMove,
|
|
72
|
+
) as { unmount: () => void };
|
|
73
|
+
|
|
74
|
+
const dock = document.body.querySelector<HTMLElement>(".hk-board-node-actions")!;
|
|
75
|
+
const shell = dock.closest(".hk-board-node") as HTMLElement;
|
|
76
|
+
const before = shell.style.left;
|
|
77
|
+
dock.dispatchEvent(pointerEvent("pointerdown", 90, 50));
|
|
78
|
+
shell.dispatchEvent(pointerEvent("pointermove", 400, 50));
|
|
79
|
+
shell.dispatchEvent(pointerEvent("pointerup", 400, 50));
|
|
80
|
+
expect(shell.style.left).toBe(before);
|
|
81
|
+
expect(onNodeMove).not.toHaveBeenCalled();
|
|
82
|
+
app.unmount();
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
/** Source contract for the dock's load-bearing rules (vitest cannot see
|
|
87
|
+
* stylesheets — a deleted rule leaves every DOM guard green). */
|
|
88
|
+
const boardScss = (await import("node:fs")).readFileSync(
|
|
89
|
+
(await import("node:path")).resolve(__dirname, "./HkBoard.scss"),
|
|
90
|
+
"utf8",
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
describe("HkBoard nodeActions dock styles", () => {
|
|
94
|
+
it("docks the actions to the node's bottom-end corner above the body", () => {
|
|
95
|
+
const block = boardScss.slice(boardScss.indexOf("&-actions {"));
|
|
96
|
+
expect(block.slice(0, 400)).toMatch(/position:\s*absolute/);
|
|
97
|
+
expect(block.slice(0, 400)).toMatch(/inset-inline-end/);
|
|
98
|
+
expect(block.slice(0, 400)).toMatch(/bottom:/);
|
|
99
|
+
expect(block.slice(0, 400)).toMatch(/z-index/);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -44,6 +44,21 @@
|
|
|
44
44
|
position: absolute;
|
|
45
45
|
box-sizing: border-box;
|
|
46
46
|
|
|
47
|
+
// Per-node action dock (the `nodeActions` slot): consumer-injected
|
|
48
|
+
// controls ride the node's bottom-end corner above the body, so hosts
|
|
49
|
+
// stop hand-drawing their own buttons into the node slot. The dock owns
|
|
50
|
+
// its own capture-safety (see HkBoard.tsx) — a press that starts here
|
|
51
|
+
// never becomes a node drag.
|
|
52
|
+
&-actions {
|
|
53
|
+
position: absolute;
|
|
54
|
+
inset-inline-end: var(--hk-board-node-action-inset, 4px);
|
|
55
|
+
bottom: var(--hk-board-node-action-inset, 4px);
|
|
56
|
+
z-index: 6;
|
|
57
|
+
display: flex;
|
|
58
|
+
align-items: center;
|
|
59
|
+
gap: 4px;
|
|
60
|
+
}
|
|
61
|
+
|
|
47
62
|
&--draggable {
|
|
48
63
|
cursor: grab;
|
|
49
64
|
|
|
@@ -453,6 +453,26 @@ export default defineComponent({
|
|
|
453
453
|
{slots.node
|
|
454
454
|
? slots.node({ node: n })
|
|
455
455
|
: <div class="hk-board-node-shell">{n.label ?? ""}</div>}
|
|
456
|
+
{slots.nodeActions
|
|
457
|
+
? (
|
|
458
|
+
<div
|
|
459
|
+
class="hk-board-node-actions"
|
|
460
|
+
// A press that starts on an action belongs to that
|
|
461
|
+
// action: this component captures the pointer on its own
|
|
462
|
+
// root for every draggable node, and a captured press
|
|
463
|
+
// retargets the following events to the ancestor — an
|
|
464
|
+
// action button's own click would never fire. Stopping
|
|
465
|
+
// the bubble here is the same guard HkMinimap puts on
|
|
466
|
+
// its zoom chrome. The keydown twin keeps a focused
|
|
467
|
+
// action's Enter/Space from reaching the node's own key
|
|
468
|
+
// handling.
|
|
469
|
+
onPointerdown={(e: PointerEvent) => e.stopPropagation()}
|
|
470
|
+
onKeydown={(e: KeyboardEvent) => e.stopPropagation()}
|
|
471
|
+
>
|
|
472
|
+
{slots.nodeActions({ node: n })}
|
|
473
|
+
</div>
|
|
474
|
+
)
|
|
475
|
+
: null}
|
|
456
476
|
</div>
|
|
457
477
|
))}
|
|
458
478
|
</div>
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createApp, defineComponent, h, type Ref } from "vue";
|
|
3
|
+
|
|
4
|
+
import { useConnectionProbe, type ProbeResult } from "./useConnectionProbe";
|
|
5
|
+
|
|
6
|
+
/** The probe is a mount-scoped composable (onMounted drives the first
|
|
7
|
+
* fetch); each test rides a minimal component harness. Fake timers own
|
|
8
|
+
* both the clock (latency, countdown ladder) and the interval bus. */
|
|
9
|
+
|
|
10
|
+
interface Deferred {
|
|
11
|
+
promise: Promise<Response>;
|
|
12
|
+
resolve: (r: Response) => void;
|
|
13
|
+
reject: (e: unknown) => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function deferredResponse(): Deferred {
|
|
17
|
+
let resolve!: (r: Response) => void;
|
|
18
|
+
let reject!: (e: unknown) => void;
|
|
19
|
+
const promise = new Promise<Response>((res, rej) => {
|
|
20
|
+
resolve = res;
|
|
21
|
+
reject = rej;
|
|
22
|
+
});
|
|
23
|
+
return { promise, resolve, reject };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function okResponse(): Response {
|
|
27
|
+
return { ok: true } as Response;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function mountProbe() {
|
|
31
|
+
let out!: { result: Ref<ProbeResult>; retryNow: () => void };
|
|
32
|
+
const app = createApp(
|
|
33
|
+
defineComponent({
|
|
34
|
+
setup() {
|
|
35
|
+
out = useConnectionProbe();
|
|
36
|
+
return () => h("div");
|
|
37
|
+
},
|
|
38
|
+
}),
|
|
39
|
+
);
|
|
40
|
+
const container = document.createElement("div");
|
|
41
|
+
document.body.appendChild(container);
|
|
42
|
+
app.mount(container);
|
|
43
|
+
return {
|
|
44
|
+
probe: () => out,
|
|
45
|
+
unmount: () => {
|
|
46
|
+
app.unmount();
|
|
47
|
+
container.remove();
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
afterEach(() => {
|
|
53
|
+
vi.useRealTimers();
|
|
54
|
+
vi.unstubAllGlobals();
|
|
55
|
+
document.body.innerHTML = "";
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("useConnectionProbe continuous observation", () => {
|
|
59
|
+
it("keeps polling after a successful probe — parked-forever was the bug", async () => {
|
|
60
|
+
vi.useFakeTimers();
|
|
61
|
+
const fetchMock = vi.fn(() => Promise.resolve(okResponse()));
|
|
62
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
63
|
+
const { probe, unmount } = mountProbe();
|
|
64
|
+
try {
|
|
65
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
66
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
67
|
+
expect(probe().result.value.state).toBe("connected");
|
|
68
|
+
|
|
69
|
+
// The scheduled re-check fires 15s later — the light KEEPS
|
|
70
|
+
// observing instead of recording the initial verdict forever.
|
|
71
|
+
await vi.advanceTimersByTimeAsync(15_000);
|
|
72
|
+
expect(fetchMock, "healthy backend keeps being polled").toHaveBeenCalledTimes(2);
|
|
73
|
+
expect(probe().result.value.state).toBe("connected");
|
|
74
|
+
expect(probe().result.value.connected).toBe(true);
|
|
75
|
+
} finally {
|
|
76
|
+
unmount();
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("a healthy re-check stays GREEN in flight — no reconnecting blink, no flash", async () => {
|
|
81
|
+
vi.useFakeTimers();
|
|
82
|
+
const first = deferredResponse();
|
|
83
|
+
const second = deferredResponse();
|
|
84
|
+
// Route by call order: first mount fetch, then the scheduled re-check.
|
|
85
|
+
let calls = 0;
|
|
86
|
+
const fetchMock = vi.fn(() => {
|
|
87
|
+
calls += 1;
|
|
88
|
+
return calls === 1 ? first.promise : second.promise;
|
|
89
|
+
});
|
|
90
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
91
|
+
const { probe, unmount } = mountProbe();
|
|
92
|
+
try {
|
|
93
|
+
first.resolve(okResponse());
|
|
94
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
95
|
+
expect(probe().result.value.state).toBe("connected");
|
|
96
|
+
|
|
97
|
+
// Fire the scheduled re-check and let it hang mid-fetch: the OLD
|
|
98
|
+
// behavior flipped the state to "reconnecting" here (yellow blink
|
|
99
|
+
// + recovery flash every interval). Quiet re-checks must not.
|
|
100
|
+
await vi.advanceTimersByTimeAsync(15_000);
|
|
101
|
+
await Promise.resolve(); // enter the awaiting fetch
|
|
102
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
103
|
+
expect(probe().result.value.state, "in-flight re-check is quiet").toBe("connected");
|
|
104
|
+
expect(probe().result.value.connected).toBe(true);
|
|
105
|
+
|
|
106
|
+
second.resolve(okResponse());
|
|
107
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
108
|
+
expect(probe().result.value.state).toBe("connected");
|
|
109
|
+
} finally {
|
|
110
|
+
unmount();
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("a re-check that fails drops into the retry ladder and recovers", async () => {
|
|
115
|
+
vi.useFakeTimers();
|
|
116
|
+
const fetchMock = vi.fn();
|
|
117
|
+
fetchMock.mockResolvedValueOnce(okResponse()); // mount: healthy
|
|
118
|
+
fetchMock.mockRejectedValueOnce(new Error("backend died")); // re-check: dead
|
|
119
|
+
fetchMock.mockResolvedValueOnce(okResponse()); // ladder retry: back
|
|
120
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
121
|
+
const { probe, unmount } = mountProbe();
|
|
122
|
+
try {
|
|
123
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
124
|
+
expect(probe().result.value.state).toBe("connected");
|
|
125
|
+
|
|
126
|
+
await vi.advanceTimersByTimeAsync(15_000);
|
|
127
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
128
|
+
// The failure is REAL and must show: red, disconnected, ladder
|
|
129
|
+
// armed (attempt 1 of the retryTotal, countdown to the next try).
|
|
130
|
+
expect(probe().result.value.connected).toBe(false);
|
|
131
|
+
expect(probe().result.value.state).toBe("disconnected");
|
|
132
|
+
expect(probe().result.value.attemptNumber).toBe(1);
|
|
133
|
+
expect(probe().result.value.countdown).toBeGreaterThan(0);
|
|
134
|
+
|
|
135
|
+
// Ladder retry succeeds: green again, and the attempt counter
|
|
136
|
+
// RESET — a lifetime odometer would read "Retrying N/3" with N
|
|
137
|
+
// grown by every healthy poll.
|
|
138
|
+
await vi.advanceTimersByTimeAsync(15_000);
|
|
139
|
+
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
140
|
+
expect(probe().result.value.connected).toBe(true);
|
|
141
|
+
expect(probe().result.value.state).toBe("connected");
|
|
142
|
+
expect(probe().result.value.attemptNumber).toBe(0);
|
|
143
|
+
} finally {
|
|
144
|
+
unmount();
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("stops observing once unmounted", async () => {
|
|
149
|
+
vi.useFakeTimers();
|
|
150
|
+
const fetchMock = vi.fn(() => Promise.resolve(okResponse()));
|
|
151
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
152
|
+
const { probe, unmount } = mountProbe();
|
|
153
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
154
|
+
await vi.advanceTimersByTimeAsync(15_000);
|
|
155
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
156
|
+
unmount();
|
|
157
|
+
await vi.advanceTimersByTimeAsync(45_000);
|
|
158
|
+
expect(fetchMock, "no polls after unmount").toHaveBeenCalledTimes(2);
|
|
159
|
+
expect(probe().result.value.state).toBe("connected");
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("an in-flight probe settling AFTER unmount arms no zombie loop", async () => {
|
|
163
|
+
vi.useFakeTimers();
|
|
164
|
+
const hang = deferredResponse();
|
|
165
|
+
const fetchMock = vi.fn(() => hang.promise);
|
|
166
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
167
|
+
const { probe, unmount } = mountProbe();
|
|
168
|
+
// The race (R2 catch, 2026-10-11): the mount fetch is still in
|
|
169
|
+
// flight when the component unmounts; its late SUCCESS used to arm
|
|
170
|
+
// a fresh 15s slot on a dead component — a poll loop that never
|
|
171
|
+
// stopped (4+ fetches per 45s, forever). The disposed guard must
|
|
172
|
+
// swallow the arming; the late state write itself is harmless.
|
|
173
|
+
unmount();
|
|
174
|
+
hang.resolve(okResponse());
|
|
175
|
+
await vi.advanceTimersByTimeAsync(60_000);
|
|
176
|
+
expect(fetchMock, "no post-unmount polls, ever").toHaveBeenCalledTimes(1);
|
|
177
|
+
expect(probe().result.value.connected, "late write is harmless").toBe(true);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
@@ -78,23 +78,38 @@ export function useConnectionProbe(): {
|
|
|
78
78
|
let tickHandle: IntervalHandle | null = null;
|
|
79
79
|
let nextAttemptAt = 0;
|
|
80
80
|
let consecutiveFailures = 0;
|
|
81
|
+
// Set at unmount, read by schedulePoll: an in-flight probe resolving
|
|
82
|
+
// AFTER unmount must not arm a fresh slot — that race resurrected a
|
|
83
|
+
// 15s zombie loop polling forever on a dead component (R2 catch,
|
|
84
|
+
// 2026-10-11). Late state writes into the dead ref are harmless;
|
|
85
|
+
// late ARMING is not.
|
|
86
|
+
let disposed = false;
|
|
81
87
|
|
|
82
88
|
function applyCountdown(): void {
|
|
83
89
|
const seconds = Math.max(0, Math.ceil((nextAttemptAt - Date.now()) / 1000));
|
|
84
90
|
result.value = { ...result.value, countdown: seconds };
|
|
85
91
|
}
|
|
86
92
|
|
|
87
|
-
async function probeOnce(): Promise<void> {
|
|
93
|
+
async function probeOnce(opts?: { quiet?: boolean }): Promise<void> {
|
|
88
94
|
pollHandle?.disconnect();
|
|
89
95
|
pollHandle = null;
|
|
96
|
+
const wasConnected = result.value.connected;
|
|
97
|
+
// Quiet re-checks (the scheduled poll while healthy) keep the light
|
|
98
|
+
// GREEN through the fetch: flipping every healthy re-check through
|
|
99
|
+
// "reconnecting" would blink the pill and re-fire the recovery flash
|
|
100
|
+
// once per interval. A quiet re-check that FAILS falls through to
|
|
101
|
+
// the ladder below — that transition is real and must show.
|
|
102
|
+
const quiet = opts?.quiet === true && wasConnected;
|
|
90
103
|
const attempt = result.value.attemptNumber + 1;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
104
|
+
if (!quiet) {
|
|
105
|
+
result.value = {
|
|
106
|
+
...result.value,
|
|
107
|
+
state: wasConnected ? "reconnecting" : "connecting",
|
|
108
|
+
attemptNumber: attempt,
|
|
109
|
+
retryCount: attempt,
|
|
110
|
+
countdown: 0,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
98
113
|
const started = Date.now();
|
|
99
114
|
try {
|
|
100
115
|
const resp = await fetch(endpointUrl(), {
|
|
@@ -104,12 +119,26 @@ export function useConnectionProbe(): {
|
|
|
104
119
|
const latencyMs = Date.now() - started;
|
|
105
120
|
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
|
|
106
121
|
consecutiveFailures = 0;
|
|
122
|
+
nextAttemptAt = 0;
|
|
107
123
|
result.value = {
|
|
108
124
|
...result.value,
|
|
109
125
|
connected: true,
|
|
110
126
|
state: "connected",
|
|
111
127
|
latencyMs,
|
|
128
|
+
// The attempt counter is LADDER state, not a lifetime odometer:
|
|
129
|
+
// a healthy probe zeroes it so the next outage's ladder reads
|
|
130
|
+
// "Retrying 1/3" instead of an ever-growing number. Continuous
|
|
131
|
+
// polling made this matter — the counter used to stop moving
|
|
132
|
+
// once connected, so the creep was invisible.
|
|
133
|
+
attemptNumber: 0,
|
|
134
|
+
retryCount: 0,
|
|
135
|
+
countdown: 0,
|
|
112
136
|
};
|
|
137
|
+
// Continuous observation (2026-10-11 auth-surface report): the
|
|
138
|
+
// light must FOLLOW the backend, not just record the initial
|
|
139
|
+
// verdict. Parked-forever-on-success meant a backend dying later
|
|
140
|
+
// left a permanently green light on sessionless surfaces.
|
|
141
|
+
schedulePoll(true);
|
|
113
142
|
} catch {
|
|
114
143
|
consecutiveFailures += 1;
|
|
115
144
|
const failed = consecutiveFailures >= result.value.retryTotal;
|
|
@@ -118,6 +147,8 @@ export function useConnectionProbe(): {
|
|
|
118
147
|
connected: false,
|
|
119
148
|
state: failed ? "failed" : "disconnected",
|
|
120
149
|
latencyMs: null,
|
|
150
|
+
attemptNumber: attempt,
|
|
151
|
+
retryCount: attempt,
|
|
121
152
|
};
|
|
122
153
|
nextAttemptAt = Date.now() + DEFAULT_POLL_MS;
|
|
123
154
|
applyCountdown();
|
|
@@ -125,15 +156,21 @@ export function useConnectionProbe(): {
|
|
|
125
156
|
}
|
|
126
157
|
}
|
|
127
158
|
|
|
128
|
-
function schedulePoll(): void {
|
|
129
|
-
//
|
|
130
|
-
//
|
|
131
|
-
//
|
|
132
|
-
//
|
|
159
|
+
function schedulePoll(quiet = false): void {
|
|
160
|
+
// The zombie guard: an in-flight probe settling after unmount lands
|
|
161
|
+
// here with nothing left to serve — arming would loop forever on a
|
|
162
|
+
// dead component. Both call sites (success re-check AND the failure
|
|
163
|
+
// ladder) route through this single gate.
|
|
164
|
+
if (disposed) return;
|
|
165
|
+
// Visibility-aware one-shot: the 15s window holds while the page is
|
|
166
|
+
// hidden instead of burning down on a throttled background timer
|
|
167
|
+
// (intervalBus — this is its designed case: data polling, not
|
|
168
|
+
// animation). Retry-ladder hops stay loud; the success-path
|
|
169
|
+
// re-check passes `quiet` so a healthy poll never blinks the light.
|
|
133
170
|
pollHandle?.disconnect();
|
|
134
171
|
pollHandle = scheduleIntervalAfter(() => {
|
|
135
172
|
pollHandle = null;
|
|
136
|
-
void probeOnce();
|
|
173
|
+
void probeOnce({ quiet });
|
|
137
174
|
}, DEFAULT_POLL_MS);
|
|
138
175
|
}
|
|
139
176
|
|
|
@@ -151,6 +188,9 @@ export function useConnectionProbe(): {
|
|
|
151
188
|
onUnmounted(() => {
|
|
152
189
|
liveProbes = Math.max(0, liveProbes - 1);
|
|
153
190
|
probeRuntime?.pulse({ liveProbes });
|
|
191
|
+
// Disarm FIRST: an in-flight probe's late success must find the
|
|
192
|
+
// guard already up when it reaches schedulePoll.
|
|
193
|
+
disposed = true;
|
|
154
194
|
pollHandle?.disconnect();
|
|
155
195
|
pollHandle = null;
|
|
156
196
|
tickHandle?.disconnect();
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createApp, defineComponent, h, type Ref } from "vue";
|
|
3
|
+
|
|
4
|
+
import { useEngineHealth, type EngineHealth } from "./useEngineHealth";
|
|
5
|
+
|
|
6
|
+
/** useEngineHealth is mount-scoped (onMounted drives the first fetch);
|
|
7
|
+
* each test rides a minimal component harness. Fake timers own the
|
|
8
|
+
* 30s refresh loop. */
|
|
9
|
+
|
|
10
|
+
interface Deferred {
|
|
11
|
+
promise: Promise<Response>;
|
|
12
|
+
resolve: (r: Response) => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function deferredJsonResponse(): Deferred {
|
|
16
|
+
let resolve!: (r: Response) => void;
|
|
17
|
+
const promise = new Promise<Response>((res) => {
|
|
18
|
+
resolve = res;
|
|
19
|
+
});
|
|
20
|
+
return { promise, resolve };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function okBody(body: Record<string, unknown>): Response {
|
|
24
|
+
return { ok: true, json: () => Promise.resolve(body) } as unknown as Response;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function mountHealth() {
|
|
28
|
+
let out!: {
|
|
29
|
+
health: Ref<EngineHealth | null>;
|
|
30
|
+
loading: Ref<boolean>;
|
|
31
|
+
error: Ref<string | null>;
|
|
32
|
+
refresh: () => Promise<void>;
|
|
33
|
+
};
|
|
34
|
+
const app = createApp(
|
|
35
|
+
defineComponent({
|
|
36
|
+
setup() {
|
|
37
|
+
out = useEngineHealth();
|
|
38
|
+
return () => h("div");
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
const container = document.createElement("div");
|
|
43
|
+
document.body.appendChild(container);
|
|
44
|
+
app.mount(container);
|
|
45
|
+
return {
|
|
46
|
+
api: () => out,
|
|
47
|
+
unmount: () => {
|
|
48
|
+
app.unmount();
|
|
49
|
+
container.remove();
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
afterEach(() => {
|
|
55
|
+
vi.useRealTimers();
|
|
56
|
+
vi.unstubAllGlobals();
|
|
57
|
+
document.body.innerHTML = "";
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("useEngineHealth refresh loop", () => {
|
|
61
|
+
it("recovers the engine row after a transient mount-time failure", async () => {
|
|
62
|
+
vi.useFakeTimers();
|
|
63
|
+
const fetchMock = vi.fn();
|
|
64
|
+
fetchMock.mockRejectedValueOnce(new Error("transient")); // mount: fails
|
|
65
|
+
fetchMock.mockResolvedValueOnce(okBody({ engine_version: "9.8.7", build_hash: "abc1234" }));
|
|
66
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
67
|
+
const { api, unmount } = mountHealth();
|
|
68
|
+
try {
|
|
69
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
70
|
+
expect(api().health.value, "mount-time failure blanks the row").toBeNull();
|
|
71
|
+
expect(api().error.value).toBeTruthy();
|
|
72
|
+
|
|
73
|
+
// The one-shot era left this row dead for the page's whole life;
|
|
74
|
+
// the 30s loop revives it (2026-10-11 auth-surface report).
|
|
75
|
+
await vi.advanceTimersByTimeAsync(30_000);
|
|
76
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
77
|
+
expect(api().health.value?.engineVersion).toBe("9.8.7");
|
|
78
|
+
expect(api().health.value?.engineBuildHash).toBe("abc1234");
|
|
79
|
+
expect(api().error.value).toBeNull();
|
|
80
|
+
} finally {
|
|
81
|
+
unmount();
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("keeps the row fresh on cadence and stops on unmount", async () => {
|
|
86
|
+
vi.useFakeTimers();
|
|
87
|
+
const fetchMock = vi.fn(() => Promise.resolve(okBody({ engine_version: "1.0.0" })));
|
|
88
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
89
|
+
const { unmount } = mountHealth();
|
|
90
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
91
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
92
|
+
await vi.advanceTimersByTimeAsync(30_000);
|
|
93
|
+
expect(fetchMock, "cadence refresh fires").toHaveBeenCalledTimes(2);
|
|
94
|
+
await vi.advanceTimersByTimeAsync(30_000);
|
|
95
|
+
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
96
|
+
unmount();
|
|
97
|
+
await vi.advanceTimersByTimeAsync(90_000);
|
|
98
|
+
expect(fetchMock, "no refreshes after unmount").toHaveBeenCalledTimes(3);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("never stacks requests — a slow refresh skips the next beat", async () => {
|
|
102
|
+
vi.useFakeTimers();
|
|
103
|
+
const slow = deferredJsonResponse();
|
|
104
|
+
const fetchMock = vi.fn();
|
|
105
|
+
fetchMock.mockResolvedValueOnce(okBody({ engine_version: "1.0.0" })); // mount
|
|
106
|
+
fetchMock.mockImplementationOnce(() => slow.promise); // t+30s: hangs
|
|
107
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
108
|
+
const { api, unmount } = mountHealth();
|
|
109
|
+
try {
|
|
110
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
111
|
+
await vi.advanceTimersByTimeAsync(30_000);
|
|
112
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
113
|
+
expect(api().loading.value).toBe(true);
|
|
114
|
+
|
|
115
|
+
// The t+60s beat arrives while the t+30s refresh still hangs — it
|
|
116
|
+
// must be skipped, not stacked.
|
|
117
|
+
await vi.advanceTimersByTimeAsync(30_000);
|
|
118
|
+
expect(fetchMock, "in-flight refresh holds the beat").toHaveBeenCalledTimes(2);
|
|
119
|
+
|
|
120
|
+
slow.resolve(okBody({ engine_version: "2.0.0" }));
|
|
121
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
122
|
+
expect(api().loading.value).toBe(false);
|
|
123
|
+
expect(api().health.value?.engineVersion).toBe("2.0.0");
|
|
124
|
+
} finally {
|
|
125
|
+
unmount();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
});
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { onMounted, ref, type Ref } from "vue";
|
|
1
|
+
import { onMounted, onUnmounted, ref, type Ref } from "vue";
|
|
2
|
+
|
|
3
|
+
import { scheduleInterval, type IntervalHandle } from "../runtime/intervalBus";
|
|
2
4
|
|
|
3
5
|
/** Inlined subset of the `/api/health` network payload — kept local so
|
|
4
6
|
* hikari stays dependency-free of protocol packages. */
|
|
@@ -24,9 +26,18 @@ export interface EngineHealth {
|
|
|
24
26
|
network: NetworkInfo | null;
|
|
25
27
|
}
|
|
26
28
|
|
|
29
|
+
/** Mounted refresh cadence (visibility-aware via the interval bus — a
|
|
30
|
+
* hidden tab parks instead of burning radio). 30s: engine identity is
|
|
31
|
+
* not latency-critical, but a transient mount-time failure must not
|
|
32
|
+
* blank the engine row for the page's whole life (2026-10-11
|
|
33
|
+
* auth-surface report — the one-shot fetch left a dead row forever). */
|
|
34
|
+
const REFRESH_MS = 30_000;
|
|
35
|
+
|
|
27
36
|
/**
|
|
28
|
-
* Fetch `/api/health`
|
|
29
|
-
*
|
|
37
|
+
* Fetch `/api/health` on mount and keep it fresh on a visibility-aware
|
|
38
|
+
* 30s loop; expose the engine/network subset. Replaces per-app
|
|
39
|
+
* hand-rolled health fetches. Call `refresh()` to force an immediate
|
|
40
|
+
* re-poll outside the cadence.
|
|
30
41
|
*
|
|
31
42
|
* @param baseUrl Optional API origin (defaults to same-origin "").
|
|
32
43
|
*/
|
|
@@ -39,6 +50,7 @@ export function useEngineHealth(baseUrl?: string): {
|
|
|
39
50
|
const health = ref<EngineHealth | null>(null);
|
|
40
51
|
const loading = ref(false);
|
|
41
52
|
const error = ref<string | null>(null);
|
|
53
|
+
let tickHandle: IntervalHandle | null = null;
|
|
42
54
|
|
|
43
55
|
async function refresh(): Promise<void> {
|
|
44
56
|
loading.value = true;
|
|
@@ -63,7 +75,19 @@ export function useEngineHealth(baseUrl?: string): {
|
|
|
63
75
|
}
|
|
64
76
|
}
|
|
65
77
|
|
|
66
|
-
onMounted(() => {
|
|
78
|
+
onMounted(() => {
|
|
79
|
+
void refresh();
|
|
80
|
+
tickHandle = scheduleInterval(() => {
|
|
81
|
+
// Never stack requests on a slow endpoint — skip the beat while a
|
|
82
|
+
// refresh is still in flight.
|
|
83
|
+
if (!loading.value) void refresh();
|
|
84
|
+
}, REFRESH_MS);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
onUnmounted(() => {
|
|
88
|
+
tickHandle?.disconnect();
|
|
89
|
+
tickHandle = null;
|
|
90
|
+
});
|
|
67
91
|
|
|
68
92
|
return { health, loading, error, refresh };
|
|
69
93
|
}
|