@celestia-island/hikari 0.55.412 → 0.55.413

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.
@@ -1,3 +1,12 @@
1
+ // HkUserPicker — the search-first anchored panel (HkSelectPanel surface).
2
+ //
3
+ // The contract these tests pin (2026-11 user direction): the panel opens
4
+ // EMPTY — no candidates and no request for an empty query — typing is
5
+ // debounced into ONE query, single mode emits + closes on a row, multi
6
+ // mode only commits from its in-panel Confirm, a rejection renders a retry
7
+ // affordance (never a throw), and a slow older answer can never clobber
8
+ // the newer one. The keyboard lives on the search field (combobox
9
+ // activedescendant) and the surface owns Escape.
1
10
  import { afterEach, describe, expect, it, vi } from "vitest";
2
11
  import { createApp, h, nextTick } from "vue";
3
12
 
@@ -25,8 +34,52 @@ const alice: HkUserOption = { uid: "u-1", username: "alice", email: "alice@examp
25
34
  const bob: HkUserOption = { uid: "u-2", username: "bob", email: "bob@example.com" };
26
35
  const carol: HkUserOption = { uid: "u-3", username: "carol", email: "carol@example.com", displayName: "Carol" };
27
36
 
28
- function rows() {
29
- return [...document.body.querySelectorAll<HTMLElement>(".hk-user-picker-row")];
37
+ /** The generated debounce interval (+ slack) — the component's own 250ms
38
+ * pause; tests wait it out on real timers instead of fighting fake ones. */
39
+ const DEBOUNCE_WAIT = 320;
40
+ /** Long enough for the surface's leave transition to unmount its content. */
41
+ const CLOSE_WAIT = 420;
42
+
43
+ const field = () => document.body.querySelector<HTMLElement>(".hk-user-picker-field")!;
44
+ const rows = () => [...document.body.querySelectorAll<HTMLElement>(".hk-user-picker-row")];
45
+ const names = () => rows().map((row) => row.querySelector(".hk-user-picker-row-name")!.textContent);
46
+ const rowsBand = () => document.body.querySelector<HTMLElement>(".hk-user-picker-rows");
47
+ const searchField = () => document.body.querySelector<HTMLInputElement>(".hk-user-picker-search input");
48
+
49
+ function wait(ms: number) {
50
+ return new Promise((resolve) => setTimeout(resolve, ms));
51
+ }
52
+
53
+ async function settle() {
54
+ await nextTick();
55
+ await nextTick();
56
+ }
57
+
58
+ /** Open the panel from the field and let the surface render its content. */
59
+ async function openPanel() {
60
+ field().click();
61
+ await settle();
62
+ await nextTick();
63
+ }
64
+
65
+ /** Type into the panel's search field (the input event the field listens
66
+ * to), leaving the debounce pending. */
67
+ async function type(text: string) {
68
+ const input = searchField()!;
69
+ input.value = text;
70
+ input.dispatchEvent(new Event("input"));
71
+ await nextTick();
72
+ }
73
+
74
+ /** Type and wait out the debounce, so exactly one query has run. */
75
+ async function searchFor(text: string) {
76
+ await type(text);
77
+ await wait(DEBOUNCE_WAIT);
78
+ await settle();
79
+ }
80
+
81
+ function key(target: HTMLElement, name: string) {
82
+ target.dispatchEvent(new KeyboardEvent("keydown", { key: name, bubbles: true, cancelable: true }));
30
83
  }
31
84
 
32
85
  describe("HkUserPicker", () => {
@@ -53,156 +106,243 @@ describe("HkUserPicker", () => {
53
106
  expect(onUpdate).toHaveBeenCalledWith([bob]);
54
107
  });
55
108
 
56
- it("opening the dialog runs the injected search with the empty query", async () => {
109
+ // ── The hard requirement: empty query ⇒ empty candidates ────────────
110
+ it("opens with an empty query: no request, no candidates, only the hint", async () => {
57
111
  const search = vi.fn(async () => [alice, bob]);
58
112
  mount(() => h(HkUserPicker, { modelValue: [], search }));
59
- document.body.querySelector<HTMLElement>(".hk-user-picker-field")!.click();
60
- await nextTick();
61
- await nextTick();
62
- expect(search).toHaveBeenCalledWith("");
113
+ await openPanel();
114
+ // Well past the debounce window: an empty query must never fire.
115
+ await wait(DEBOUNCE_WAIT);
116
+ await settle();
117
+ expect(search).not.toHaveBeenCalled();
118
+ expect(rows()).toHaveLength(0);
119
+ expect(rowsBand()!.getAttribute("data-state")).toBe("empty");
120
+ expect(rowsBand()!.textContent).toContain("Type a nickname, username or email to search.");
121
+ });
122
+
123
+ it("clearing the query drops the candidates without a request", async () => {
124
+ const search = vi.fn(async () => [alice, bob]);
125
+ mount(() => h(HkUserPicker, { modelValue: [], search }));
126
+ await openPanel();
127
+ await searchFor("bo");
63
128
  expect(rows()).toHaveLength(2);
129
+ await type(" ");
130
+ await wait(DEBOUNCE_WAIT);
131
+ await settle();
132
+ // Only the real query ran; the whitespace-only one never did.
133
+ expect(search).toHaveBeenCalledTimes(1);
134
+ expect(rows()).toHaveLength(0);
135
+ expect(rowsBand()!.getAttribute("data-state")).toBe("empty");
64
136
  });
65
137
 
66
- it("single mode emits one user on pick", async () => {
138
+ // ── Debounce ────────────────────────────────────────────────────────
139
+ it("debounces typing into exactly one request with the final query", async () => {
140
+ const search = vi.fn(async () => [alice]);
141
+ mount(() => h(HkUserPicker, { modelValue: [], search }));
142
+ await openPanel();
143
+ await type("a");
144
+ await type("al");
145
+ await type("ali");
146
+ expect(search).not.toHaveBeenCalled();
147
+ await wait(120);
148
+ expect(search, "still inside the debounce window").not.toHaveBeenCalled();
149
+ await wait(DEBOUNCE_WAIT);
150
+ await settle();
151
+ expect(search).toHaveBeenCalledTimes(1);
152
+ expect(search).toHaveBeenCalledWith("ali");
153
+ });
154
+
155
+ // ── Single mode ─────────────────────────────────────────────────────
156
+ it("single mode emits one user and closes the panel on a row pick", async () => {
67
157
  const onUpdate = vi.fn();
158
+ const search = vi.fn(async () => [alice, bob]);
68
159
  mount(() => h(HkUserPicker, {
69
160
  modelValue: [],
70
- search: async () => [alice, bob],
161
+ search,
71
162
  "onUpdate:modelValue": (users: HkUserOption[]) => onUpdate(users),
72
163
  }));
73
- document.body.querySelector<HTMLElement>(".hk-user-picker-field")!.click();
74
- await nextTick();
75
- await nextTick();
164
+ await openPanel();
165
+ await searchFor("bo");
166
+ expect(names()).toEqual(["Alice", "bob"]);
76
167
  rows()[1].click();
77
168
  await nextTick();
78
169
  expect(onUpdate).toHaveBeenCalledWith([bob]);
170
+ await wait(CLOSE_WAIT);
171
+ expect(document.body.querySelector(".hk-user-picker-panel"), "panel closed").toBeNull();
79
172
  });
80
173
 
81
- it("multi mode accumulates picks and commits on confirm", async () => {
174
+ // ── Multi mode: draft + in-panel confirm ────────────────────────────
175
+ it("multi mode toggles a draft and emits only on the in-panel confirm", async () => {
82
176
  const onUpdate = vi.fn();
177
+ const search = vi.fn(async () => [alice, bob, carol]);
83
178
  mount(() => h(HkUserPicker, {
84
179
  modelValue: [],
85
180
  mode: "multi",
86
- search: async () => [alice, bob, carol],
181
+ search,
87
182
  "onUpdate:modelValue": (users: HkUserOption[]) => onUpdate(users),
88
183
  }));
89
- document.body.querySelector<HTMLElement>(".hk-user-picker-field")!.click();
90
- await nextTick();
91
- await nextTick();
184
+ await openPanel();
185
+ await searchFor("a");
186
+ // Nothing drafted yet → the confirm is inert.
187
+ const disabledConfirm = document.body.querySelector<HTMLButtonElement>(".hk-user-picker-confirm")!;
188
+ expect(disabledConfirm.disabled).toBe(true);
189
+
92
190
  rows()[0].click();
93
191
  rows()[2].click();
94
- // Re-picking a drafted row toggles it OUT again — the de-dup half of
95
- // pickRow (pinned after the R2 mutation round caught it untested).
192
+ // Re-picking a drafted row toggles it OUT again (the de-dup half).
96
193
  rows()[0].click();
97
194
  await nextTick();
98
195
  expect(onUpdate).not.toHaveBeenCalled();
99
- const confirm = [...document.body.querySelectorAll<HTMLButtonElement>(".hk-modal-footer button")]
100
- .find((b) => b.textContent === "Confirm")!;
196
+ expect(rows()[2].getAttribute("aria-selected")).toBe("true");
197
+ expect(rows()[0].getAttribute("aria-selected")).toBe("false");
198
+ expect(document.body.querySelector(".hk-user-picker-count")!.textContent).toContain("1 selected");
199
+
200
+ const confirm = document.body.querySelector<HTMLButtonElement>(".hk-user-picker-confirm")!;
201
+ expect(confirm.disabled).toBe(false);
101
202
  confirm.click();
102
203
  await nextTick();
103
204
  expect(onUpdate).toHaveBeenCalledWith([carol]);
205
+ await wait(CLOSE_WAIT);
206
+ expect(document.body.querySelector(".hk-user-picker-panel"), "panel closed").toBeNull();
207
+ });
208
+
209
+ // ── Failure + retry ─────────────────────────────────────────────────
210
+ it("a rejecting search renders the retry note and retries without throwing", async () => {
211
+ let attempts = 0;
212
+ const search = vi.fn(async () => {
213
+ attempts += 1;
214
+ if (attempts === 1) throw new Error("down");
215
+ return [bob];
216
+ });
217
+ mount(() => h(HkUserPicker, { modelValue: [], search }));
218
+ await openPanel();
219
+ await searchFor("bob");
220
+ expect(rowsBand()!.getAttribute("data-state")).toBe("failed");
221
+ expect(rows()).toHaveLength(0);
222
+ const retry = document.body.querySelector<HTMLButtonElement>(".hk-user-picker-retry");
223
+ expect(retry, "retry affordance").toBeTruthy();
224
+ retry!.click();
225
+ await settle();
226
+ await nextTick();
227
+ expect(search).toHaveBeenCalledTimes(2);
228
+ expect(names()).toEqual(["bob"]);
229
+ expect(rowsBand()!.getAttribute("data-state")).toBe("ready");
104
230
  });
105
231
 
106
- it("a stale search resolving late cannot clobber the newer results", async () => {
107
- let releaseFirst: ((value: HkUserOption[]) => void) | null = null;
108
- const first = new Promise<HkUserOption[]>((resolve) => { releaseFirst = resolve; });
232
+ // ── Race guard ──────────────────────────────────────────────────────
233
+ it("a stale answer resolving late cannot clobber the newer results", async () => {
234
+ let releaseSlow: ((value: HkUserOption[]) => void) | null = null;
235
+ const slow = new Promise<HkUserOption[]>((resolve) => { releaseSlow = resolve; });
109
236
  const search = vi.fn()
110
- .mockImplementationOnce(() => first)
111
- .mockImplementationOnce(async () => [bob]);
237
+ .mockImplementationOnce(() => slow)
238
+ .mockImplementationOnce(async () => [carol]);
112
239
  mount(() => h(HkUserPicker, { modelValue: [], search }));
113
- document.body.querySelector<HTMLElement>(".hk-user-picker-field")!.click();
240
+ await openPanel();
241
+ await searchFor("bo");
242
+ await searchFor("carol");
243
+ expect(names()).toEqual(["Carol"]);
244
+ // …then the stale first answer lands LAST — it must not win.
245
+ releaseSlow!([alice]);
246
+ await settle();
247
+ expect(names()).toEqual(["Carol"]);
248
+ });
249
+
250
+ // ── Keyboard ────────────────────────────────────────────────────────
251
+ it("arrows walk the rows, publish the activedescendant and Enter picks", async () => {
252
+ const onUpdate = vi.fn();
253
+ const search = vi.fn(async () => [alice, bob, carol]);
254
+ mount(() => h(HkUserPicker, {
255
+ modelValue: [],
256
+ search,
257
+ "onUpdate:modelValue": (users: HkUserOption[]) => onUpdate(users),
258
+ }));
259
+ await openPanel();
260
+ await searchFor("a");
261
+ const input = searchField()!;
262
+ expect(input.getAttribute("aria-activedescendant"), "nothing active initially").toBeNull();
263
+ key(input, "ArrowDown");
114
264
  await nextTick();
115
- // Second query (the component's race token advances)…
116
- // Reach runSearch directly through the input's search event wiring:
117
- // HkSearchInput debounces at 250ms; fake timers advance past it.
118
- vi.useFakeTimers();
119
- const input = document.body.querySelector<HTMLInputElement>(".hk-user-picker-dialog input");
120
- input!.value = "bob";
121
- input!.dispatchEvent(new Event("input"));
122
- // Flush the modelValue watch so HkSearchInput schedules its debounce
123
- // timer on the fake clock BEFORE time advances past it.
265
+ expect(input.getAttribute("aria-activedescendant")).toBe(rows()[0].id);
266
+ key(input, "ArrowDown");
124
267
  await nextTick();
125
- vi.advanceTimersByTime(300);
126
- await Promise.resolve();
127
- await Promise.resolve();
268
+ expect(input.getAttribute("aria-activedescendant")).toBe(rows()[1].id);
269
+ key(input, "ArrowUp");
128
270
  await nextTick();
129
- // …then the stale first answer lands LAST — it must not win.
130
- releaseFirst!([alice]);
131
- await Promise.resolve();
132
- await Promise.resolve();
271
+ expect(input.getAttribute("aria-activedescendant")).toBe(rows()[0].id);
272
+ key(input, "Enter");
133
273
  await nextTick();
134
- vi.useRealTimers();
135
- const names = rows().map((r) => r.querySelector(".hk-user-picker-row-name")!.textContent);
136
- expect(names).toEqual(["bob"]);
274
+ expect(onUpdate).toHaveBeenCalledWith([alice]);
137
275
  });
138
276
 
139
- it("searches again on reopen after a full close (non-empty prior query)", async () => {
140
- const search = vi.fn(async (q: string) => (q ? [bob] : [alice, bob]));
141
- mount(() => h(HkUserPicker, { modelValue: [], search }));
142
- const field = () => document.body.querySelector<HTMLElement>(".hk-user-picker-field")!;
143
-
144
- // Session 1: open (search #1, ""), type "bob" (search #2), pick — single
145
- // mode closes the dialog.
146
- field().click();
147
- await nextTick(); await nextTick();
148
- const input = document.body.querySelector<HTMLInputElement>(".hk-user-picker-dialog input")!;
149
- input.value = "bob";
150
- input.dispatchEvent(new Event("input"));
151
- await new Promise((r) => setTimeout(r, 300));
152
- await nextTick();
153
- rows()[0].click();
277
+ it("Escape pressed in the search field closes the panel", async () => {
278
+ mount(() => h(HkUserPicker, { modelValue: [], search: async () => [alice] }));
279
+ await openPanel();
280
+ expect(document.body.querySelector(".hk-user-picker-panel")).toBeTruthy();
281
+ key(searchField()!, "Escape");
154
282
  await nextTick();
155
-
156
- // FULL close: wait past HkModal's leave window so the dialog content
157
- // actually unmounts (the precondition of the R3 finding — while still
158
- // mounted, the input's watcher masked the bug).
159
- await new Promise((r) => setTimeout(r, 700));
160
- expect(document.body.querySelector(".hk-user-picker-dialog")).toBeNull();
161
-
162
- // Session 2: reopen MUST fire the initial-batch search("") again.
163
- field().click();
164
- await nextTick(); await nextTick();
165
- expect(search).toHaveBeenLastCalledWith("");
166
- expect(search.mock.calls.filter((c) => c[0] === "").length).toBe(2);
167
- expect(rows()).toHaveLength(2);
283
+ await wait(CLOSE_WAIT);
284
+ expect(document.body.querySelector(".hk-user-picker-panel")).toBeNull();
168
285
  });
169
286
 
170
- it("fires exactly one search per open within the modal leave window", async () => {
171
- const search = vi.fn(async (q: string) => (q ? [bob] : [alice, bob]));
287
+ // ── A11y wiring ─────────────────────────────────────────────────────
288
+ it("wires the search field as the combobox of the option listbox", async () => {
289
+ const search = vi.fn(async () => [alice, bob]);
172
290
  mount(() => h(HkUserPicker, { modelValue: [], mode: "multi", search }));
173
- const field = () => document.body.querySelector<HTMLElement>(".hk-user-picker-field")!;
174
-
175
- // Session 1: open + type.
176
- field().click();
177
- await nextTick(); await nextTick();
178
- const input = document.body.querySelector<HTMLInputElement>(".hk-user-picker-dialog input")!;
179
- input.value = "bob";
180
- input.dispatchEvent(new Event("input"));
181
- await new Promise((r) => setTimeout(r, 300));
182
- await nextTick();
291
+ await openPanel();
292
+ await searchFor("a");
293
+ const input = searchField()!;
294
+ const listbox = rowsBand()!;
295
+ expect(input.getAttribute("role")).toBe("combobox");
296
+ expect(input.getAttribute("aria-controls")).toBe(listbox.getAttribute("id"));
297
+ expect(input.getAttribute("aria-label")).toBe("Nickname, username or email");
298
+ expect(listbox.getAttribute("role")).toBe("listbox");
299
+ expect(listbox.getAttribute("aria-multiselectable")).toBe("true");
300
+ const options = rows();
301
+ for (const option of options) {
302
+ expect(option.getAttribute("role")).toBe("option");
303
+ expect(option.getAttribute("aria-selected")).toBe("false");
304
+ expect(option.getAttribute("tabindex")).toBe("-1");
305
+ }
306
+ });
183
307
 
184
- // Close WITHOUT picking: the modal header's own × button.
185
- const closeBtn = document.body.querySelector<HTMLButtonElement>(".hk-modal-close");
186
- expect(closeBtn, "modal close button").toBeDefined();
187
- closeBtn!.click();
308
+ it("focuses the search field when the panel opens", async () => {
309
+ mount(() => h(HkUserPicker, { modelValue: [], search: async () => [alice] }));
310
+ await openPanel();
188
311
  await nextTick();
189
- // Reopen IMMEDIATELY (inside the leave window — the input may still be
190
- // mounted): the session-key remount + explicit search("") must yield
191
- // exactly ONE new empty-query search, no watcher-originated duplicate.
192
- field().click();
193
- await nextTick(); await nextTick();
194
- await new Promise((r) => setTimeout(r, 320));
195
- const empties = search.mock.calls.filter((c) => c[0] === "");
196
- expect(empties.length).toBe(2);
197
- expect(search.mock.calls.filter((c) => c[0] === "bob").length).toBe(1);
312
+ expect(document.activeElement).toBe(searchField());
198
313
  });
199
314
 
200
- it("a rejecting search renders the failure note instead of throwing", async () => {
201
- mount(() => h(HkUserPicker, { modelValue: [], search: async () => { throw new Error("down"); } }));
202
- document.body.querySelector<HTMLElement>(".hk-user-picker-field")!.click();
203
- await nextTick();
315
+ // ── Caps + session reset ────────────────────────────────────────────
316
+ it("caps the rendered rows at maxResults", async () => {
317
+ mount(() => h(HkUserPicker, {
318
+ modelValue: [],
319
+ maxResults: 2,
320
+ search: async () => [alice, bob, carol],
321
+ }));
322
+ await openPanel();
323
+ await searchFor("a");
324
+ expect(rows()).toHaveLength(2);
325
+ });
326
+
327
+ it("reopening starts a fresh session: query cleared, no stale candidates", async () => {
328
+ const search = vi.fn(async (q: string) => (q === "bo" ? [bob] : [alice, bob]));
329
+ mount(() => h(HkUserPicker, { modelValue: [], search }));
330
+ await openPanel();
331
+ await searchFor("bo");
332
+ expect(names()).toEqual(["bob"]);
333
+ key(searchField()!, "Escape");
204
334
  await nextTick();
205
- expect(document.body.querySelector(".hk-user-picker-rows")!.getAttribute("data-state")).toBe("failed");
335
+ await wait(CLOSE_WAIT);
336
+ expect(document.body.querySelector(".hk-user-picker-panel")).toBeNull();
337
+
338
+ await openPanel();
339
+ await wait(DEBOUNCE_WAIT);
340
+ await settle();
341
+ // Exactly one search ran across both sessions — the empty reopen
342
+ // queries nothing, and the panel is blank with its hint back.
343
+ expect(search).toHaveBeenCalledTimes(1);
206
344
  expect(rows()).toHaveLength(0);
345
+ expect(searchField()!.value).toBe("");
346
+ expect(rowsBand()!.textContent).toContain("Type a nickname, username or email to search.");
207
347
  });
208
- });
348
+ });