Skip to content

Commit ce8856c

Browse files
matt2eclaude
andcommitted
test(channels): run the member-search debounce on a controlled clock
The two combined invitation paging tests in ChannelMembersDialog.test.tsx sit at the 5 s Vitest default and time out on loaded CI shards roughly one run in five on main. Of their ~2.5 s, ~1.15 s is pure waiting on the real 200 ms useMemberSearch debounce, paid once each for typing, relay page 2, refresh and the query change. The rest is render work that scales with runner speed and cannot be waited away. pagedInvitations now fakes setTimeout/clearTimeout after setup() and advances the debounce explicitly in idle(). user-event awaits a real setTimeout(0) after every action, so the faked steps drive fireEvent instead; the file's keyboard and highlight tests keep t.user. afterEach restores real timers first, so a failed paging test cannot leak its fake clock into the next setup(). Assertions are unchanged (30/60/90/95/96 rows, page sequences, no publishes). Local: pages 2392 -> 1441 ms, resets 2547 -> 1304 ms; whole file 48/48 in 27.8 s. Fail-then-pass: removing the invitation-page reset from refreshMembers fails resets; removing the relay page-2 search.next() fails both. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent f9a5e6f commit ce8856c

1 file changed

Lines changed: 39 additions & 20 deletions

File tree

‎src/bundled/channels/ChannelMembersDialog.test.tsx‎

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
// @vitest-environment jsdom
22
import "@testing-library/jest-dom/vitest";
3-
import { act, cleanup, render, screen, within } from "@testing-library/react";
3+
import {
4+
act,
5+
cleanup,
6+
fireEvent,
7+
render,
8+
screen,
9+
within,
10+
} from "@testing-library/react";
411
import { createHash } from "node:crypto";
512
import { schnorr } from "@noble/curves/secp256k1.js";
613
import { bytesToHex } from "nostr-tools/utils";
@@ -49,6 +56,8 @@ beforeEach(() => {
4956
});
5057
});
5158
afterEach(async () => {
59+
// First, so a failed paging test cannot leak its fake clock into the next setup().
60+
vi.useRealTimers();
5261
for (const task of tasks) task.controller.abort();
5362
await Promise.all(tasks.splice(0).map(({ outcome }) => outcome));
5463
cleanup();
@@ -1266,16 +1275,28 @@ async function pagedInvitations(
12661275
: remote
12671276
: query(filters);
12681277
});
1278+
// The 200 ms search debounce runs on a controlled clock. user-event awaits a
1279+
// real setTimeout(0) after every action, so these steps drive fireEvent.
1280+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
12691281
const input = screen.getByRole("searchbox");
12701282
const refresh = screen.getByRole("button", { name: "Refresh member data" });
12711283
const idle = () =>
1272-
step(() =>
1273-
vi.waitFor(() => expect(refresh).toHaveAttribute("aria-busy", "false")),
1274-
);
1284+
step(async () => {
1285+
await act(() => vi.advanceTimersByTimeAsync(200));
1286+
await vi.waitFor(() =>
1287+
expect(refresh).toHaveAttribute("aria-busy", "false"),
1288+
);
1289+
});
1290+
const search = (value: string) =>
1291+
step(async () => {
1292+
fireEvent.change(input, { target: { value } });
1293+
});
1294+
const click = (button: HTMLElement) =>
1295+
step(async () => {
1296+
fireEvent.click(button);
1297+
});
12751298
const more = () =>
1276-
step(() =>
1277-
t.user.click(screen.getByRole("button", { name: "Show more results" })),
1278-
);
1299+
click(screen.getByRole("button", { name: "Show more results" }));
12791300
const rows = () =>
12801301
within(
12811302
screen.getByRole("region", { name: "Not in this channel" }),
@@ -1285,16 +1306,16 @@ async function pagedInvitations(
12851306
.flatMap(([filters]) => filters)
12861307
.filter((filter) => filter.search)
12871308
.map((filter) => filter.page);
1288-
await step(() => t.user.type(input, "Helper"));
1309+
await search("Helper");
12891310
await idle();
1290-
return { t, input, refresh, idle, more, rows, pages };
1311+
return { t, refresh, idle, search, click, more, rows, pages };
12911312
}
12921313

12931314
it("pages combined local and relay invitations without losing matches", async ({
12941315
signal,
12951316
}) => {
12961317
return ownedTask(signal, async (step) => {
1297-
const { t, more, rows, pages } = await pagedInvitations(step, 65);
1318+
const { t, idle, more, rows, pages } = await pagedInvitations(step, 65);
12981319
expect(rows()).toHaveLength(30);
12991320
expect(
13001321
rows().every((row) =>
@@ -1308,9 +1329,8 @@ it("pages combined local and relay invitations without losing matches", async ({
13081329
expect(pages()).toEqual([1]);
13091330
}
13101331
await more();
1311-
await step(() =>
1312-
screen.findByRole("button", { name: /^Add Helper final/ }),
1313-
);
1332+
await idle();
1333+
screen.getByRole("button", { name: /^Add Helper final/ });
13141334
expect(rows()).toHaveLength(96);
13151335
expect(pages()).toEqual([1, 2]);
13161336
expect(
@@ -1325,24 +1345,23 @@ it("resets combined invitation paging on refresh or query change", async ({
13251345
}) => {
13261346
return ownedTask(signal, async (step) => {
13271347
// 31 local agents: past one page locally, so a reset is observable.
1328-
const { t, input, refresh, idle, more, rows, pages } =
1348+
const { t, refresh, idle, search, click, more, rows, pages } =
13291349
await pagedInvitations(step, 31);
13301350
for (let i = 0; i < 3; i++) await more();
1331-
await step(() =>
1332-
screen.findByRole("button", { name: /^Add Helper final/ }),
1333-
);
1351+
await idle();
1352+
screen.getByRole("button", { name: /^Add Helper final/ });
13341353
expect(rows()).toHaveLength(62);
13351354
expect(pages()).toEqual([1, 2]);
1336-
await step(() => t.user.click(refresh));
1355+
await click(refresh);
13371356
await idle();
13381357
expect(rows()).toHaveLength(30);
13391358
expect(pages()).toEqual([1, 2, 1]);
13401359
await more();
13411360
expect(rows()).toHaveLength(60);
1342-
await step(() => t.user.type(input, " local"));
1361+
await search("Helper local");
13431362
await idle();
13441363
expect(rows()).toHaveLength(30);
1345-
await step(() => t.user.clear(input));
1364+
await search("");
13461365
expect(
13471366
screen.queryByRole("region", { name: "Not in this channel" }),
13481367
).toBeNull();

0 commit comments

Comments
 (0)