forge
Explore
Log in

Don't you dare go Hollow

🧪 pin the room's reconnect rules and its terminal frames

Browse files at this commit

63ca05756ec01888cc1f77c75ef815235b450fc2Ahmet Kaan GümüşParent: d67f849
  • test
    • 🧪 pin the room's reconnect rules and its terminal frames
Show full message
test: 🧪 pin the room's reconnect rules and its terminal frames

A blip mid-sitting has to recover on its own, back off while it fails, and
retry fast again once it succeeds — otherwise a student watches a frozen
countdown and a socket that never returns. Anything in flight when the
socket drops fails toward "not saved", never toward Saved.

The load-bearing one is the socket we closed ourselves: a stray reconnect
after finish would re-enter the room, and re-entering clears `left_at`
server-side — the room would quietly reopen a sitting the student left.

`finished` and `expired` are terminal, and a `state` frame arriving behind
them does not reopen the sheet. That is the same hazard the rejoin fix
had to avoid, from the other direction.
src/components/exams/exam-room-ws.test.tsxmodified+113 -6
@@ -75,7 +75,7 @@ vi.mock("@/api/time", () => ({ getTime: async () => ({ now: Date.now() }) }));
75 /** Controllable stand-in for the socket the room owns; drives frames by hand. */75 /** Controllable stand-in for the socket the room owns; drives frames by hand. */
76 class FakeWebSocket {76 class FakeWebSocket {
77 static OPEN = 1;77 static OPEN = 1;
78- static last: FakeWebSocket | null = null;78+ static instances: FakeWebSocket[] = [];
79 readyState = 0;79 readyState = 0;
80 sent: string[] = [];80 sent: string[] = [];
81 onopen: (() => void) | null = null;81 onopen: (() => void) | null = null;
@@ -83,7 +83,7 @@ class FakeWebSocket {
83 onerror: (() => void) | null = null;83 onerror: (() => void) | null = null;
84 onmessage: ((event: { data: string }) => void) | null = null;84 onmessage: ((event: { data: string }) => void) | null = null;
85 constructor(public url: string) {85 constructor(public url: string) {
86- FakeWebSocket.last = this;86+ FakeWebSocket.instances.push(this);
87 }87 }
88 send(data: string) {88 send(data: string) {
89 this.sent.push(data);89 this.sent.push(data);
@@ -106,14 +106,15 @@ class FakeWebSocket {
106 // Fake timers go in only once the room is already up (see the ack-timeout106 // Fake timers go in only once the room is already up (see the ack-timeout
107 // tests), so the same helper has to work on both clocks.107 // tests), so the same helper has to work on both clocks.
108 const tick = () => (vi.isFakeTimers() ? vi.advanceTimersByTimeAsync(0) : new Promise((resolve) => setTimeout(resolve, 0)));108 const tick = () => (vi.isFakeTimers() ? vi.advanceTimersByTimeAsync(0) : new Promise((resolve) => setTimeout(resolve, 0)));
109-const socket = () => FakeWebSocket.last!;109+const socket = () => FakeWebSocket.instances.at(-1)!;
110+const sockets = () => FakeWebSocket.instances.length;
110 const closedBanners = () => screen.queryAllByText("This attempt is closed. Answers are read-only.");111 const closedBanners = () => screen.queryAllByText("This attempt is closed. Answers are read-only.");
111 const choice = (name: string) => screen.getByRole("button", { name: new RegExp(name) }) as HTMLButtonElement;112 const choice = (name: string) => screen.getByRole("button", { name: new RegExp(name) }) as HTMLButtonElement;
112 const badge = (text: string) => screen.queryAllByText(text).length > 0;113 const badge = (text: string) => screen.queryAllByText(text).length > 0;
113 114
114 /** Mount, press Start, land inside the room with the stale snapshot loaded. */115 /** Mount, press Start, land inside the room with the stale snapshot loaded. */
115 async function openRoom() {116 async function openRoom() {
116- render(() => (117+ const mounted = render(() => (
117 <PreferencesProvider>118 <PreferencesProvider>
118 <ExamRoomWS exam={exam as never} />119 <ExamRoomWS exam={exam as never} />
119 </PreferencesProvider>120 </PreferencesProvider>
@@ -121,6 +122,7 @@ async function openRoom() {
121 fireEvent.click(await screen.findByRole("button", { name: "Start exam" }));122 fireEvent.click(await screen.findByRole("button", { name: "Start exam" }));
122 await tick();123 await tick();
123 await tick();124 await tick();
125+ return mounted;
124 }126 }
125 127
126 const liveState = {128 const liveState = {
@@ -135,15 +137,16 @@ const liveState = {
135 137
136 /** Room open, socket in, one `state` frame delivered — the writable baseline. */138 /** Room open, socket in, one `state` frame delivered — the writable baseline. */
137 async function openWritableRoom() {139 async function openWritableRoom() {
138- await openRoom();140+ const mounted = await openRoom();
139 socket().open();141 socket().open();
140 socket().deliver(liveState);142 socket().deliver(liveState);
141 await tick();143 await tick();
144+ return mounted;
142 }145 }
143 146
144 beforeEach(() => {147 beforeEach(() => {
145 attemptRow = staleAttempt();148 attemptRow = staleAttempt();
146- FakeWebSocket.last = null;149+ FakeWebSocket.instances = [];
147 vi.stubGlobal("WebSocket", FakeWebSocket);150 vi.stubGlobal("WebSocket", FakeWebSocket);
148 });151 });
149 afterEach(() => {152 afterEach(() => {
@@ -290,3 +293,107 @@ test("a timed-out send leaves no debt that swallows the next save", async () =>
290 expect(badge("Saved")).toBe(true);293 expect(badge("Saved")).toBe(true);
291 expect(badge("Not saved")).toBe(false);294 expect(badge("Not saved")).toBe(false);
292 });295 });
296+
297+// Reconnect. Same fake-timer discipline: the clock goes fake only once the room
298+// is up, so only the backoff timers are hand-driven.
299+const drop = () => socket().onclose?.();
300+
301+test("a socket that drops mid-sitting reconnects and the room recovers", async () => {
302+ await openWritableRoom();
303+ vi.useFakeTimers();
304+
305+ drop();
306+ await tick();
307+ expect(badge("Disconnected")).toBe(true);
308+
309+ await vi.advanceTimersByTimeAsync(1000);
310+ expect(sockets()).toBe(2); // a fresh socket, not the dead one
311+ socket().open();
312+ socket().deliver({ ...liveState, remaining_ms: HOUR - 60_000 });
313+ await tick();
314+
315+ // Recovered: live again, still writable, clock moving rather than frozen.
316+ expect(badge("Connected")).toBe(true);
317+ expect(closedBanners()).toEqual([]);
318+ expect(choice("Paris").disabled).toBe(false);
319+ expect(badge("00:59:00")).toBe(true);
320+});
321+
322+test("backoff grows between failed opens and resets after one succeeds", async () => {
323+ await openWritableRoom();
324+ vi.useFakeTimers();
325+
326+ drop();
327+ await vi.advanceTimersByTimeAsync(1000);
328+ expect(sockets()).toBe(2);
329+
330+ // That one never opened: the next wait must be longer than the first.
331+ drop();
332+ await vi.advanceTimersByTimeAsync(1000);
333+ expect(sockets()).toBe(2);
334+ await vi.advanceTimersByTimeAsync(1000);
335+ expect(sockets()).toBe(3);
336+
337+ // A successful open clears the debt, so a later blip retries fast again.
338+ socket().open();
339+ await tick();
340+ drop();
341+ await vi.advanceTimersByTimeAsync(1000);
342+ expect(sockets()).toBe(4);
343+});
344+
345+test("a socket we closed ourselves never reconnects", async () => {
346+ const mounted = await openWritableRoom();
347+ vi.useFakeTimers();
348+
349+ const ours = socket();
350+ mounted.unmount();
351+ ours.onclose?.(); // the close WE asked for must not look like a drop
352+
353+ await vi.advanceTimersByTimeAsync(60_000);
354+ expect(sockets()).toBe(1);
355+});
356+
357+test("a save in flight when the socket drops fails toward not saved", async () => {
358+ await openWritableRoom();
359+ await saveParis();
360+ expect(badge("Saving…")).toBe(true);
361+
362+ drop();
363+ await tick();
364+ expect(badge("Not saved")).toBe(true);
365+ expect(badge("Saved")).toBe(false);
366+ expect(screen.getByText(/connection dropped before your answer was saved/)).toBeTruthy();
367+});
368+
369+// Finish / expiry. A terminal frame is terminal: a later `state` frame carries
370+// `left_at: null`, and that must not read as "writable" on a sitting that is over.
371+test("a finished frame closes the sheet and a later state frame does not reopen it", async () => {
372+ await openWritableRoom();
373+ socket().deliver({ type: "finished", finished_at: Date.now() });
374+ await tick();
375+
376+ expect(closedBanners().length).toBeGreaterThan(0);
377+ expect(choice("Paris").disabled).toBe(true);
378+ expect(badge("Submitted")).toBe(true);
379+
380+ socket().deliver({ ...liveState, status: "submitted" });
381+ await tick();
382+ expect(closedBanners().length).toBeGreaterThan(0);
383+ expect(choice("Paris").disabled).toBe(true);
384+});
385+
386+test("an expired frame zeroes the clock and a later state frame does not reopen it", async () => {
387+ await openWritableRoom();
388+ socket().deliver({ type: "expired" });
389+ await tick();
390+
391+ expect(closedBanners().length).toBeGreaterThan(0);
392+ expect(choice("Paris").disabled).toBe(true);
393+ expect(badge("00:00:00")).toBe(true);
394+
395+ socket().deliver({ ...liveState, status: "expired", remaining_ms: 0 });
396+ await tick();
397+ expect(closedBanners().length).toBeGreaterThan(0);
398+ expect(choice("Paris").disabled).toBe(true);
399+});