Skip to content

Commit 1f3059b

Browse files
committed
Ignore pairing events after cancellation
1 parent d7a8320 commit 1f3059b

2 files changed

Lines changed: 69 additions & 25 deletions

File tree

‎desktop/src/features/settings/ui/MobilePairingCard.tsx‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,13 @@ export function MobilePairingCard({
179179
const [sasCode, setSasCode] = useState<string | null>(null);
180180
const [error, setError] = useState<string | null>(null);
181181
const requestIdRef = useRef(0);
182+
const pairingActiveRef = useRef(false);
182183
const stepRef = useRef(step);
183184
stepRef.current = step;
184185

185186
const beginPairing = useCallback(() => {
186187
const requestId = ++requestIdRef.current;
188+
pairingActiveRef.current = true;
187189
setStep("generating");
188190
setQrUri(null);
189191
setSasCode(null);
@@ -198,6 +200,7 @@ export function MobilePairingCard({
198200
},
199201
(err) => {
200202
if (requestId === requestIdRef.current) {
203+
pairingActiveRef.current = false;
201204
setError(pairingErrorMessage(err));
202205
setStep("error");
203206
}
@@ -207,6 +210,7 @@ export function MobilePairingCard({
207210

208211
useEffect(() => {
209212
++requestIdRef.current;
213+
pairingActiveRef.current = false;
210214
setStep("idle");
211215
setQrUri(null);
212216
setSasCode(null);
@@ -220,7 +224,7 @@ export function MobilePairingCard({
220224
const unlisteners: (() => void)[] = [];
221225

222226
listen<{ sas: string }>("pairing-sas-received", (event) => {
223-
if (!cancelled) {
227+
if (!cancelled && pairingActiveRef.current) {
224228
setSasCode(event.payload.sas);
225229
setStep("sas");
226230
}
@@ -230,7 +234,8 @@ export function MobilePairingCard({
230234
});
231235

232236
listen("pairing-complete", () => {
233-
if (!cancelled) {
237+
if (!cancelled && pairingActiveRef.current) {
238+
pairingActiveRef.current = false;
234239
setStep("done");
235240
}
236241
}).then((fn) => {
@@ -239,7 +244,8 @@ export function MobilePairingCard({
239244
});
240245

241246
listen<{ reason: string }>("pairing-aborted", (event) => {
242-
if (!cancelled) {
247+
if (!cancelled && pairingActiveRef.current) {
248+
pairingActiveRef.current = false;
243249
setError(`Pairing stopped: ${event.payload.reason}`);
244250
setStep("error");
245251
}
@@ -249,7 +255,8 @@ export function MobilePairingCard({
249255
});
250256

251257
listen<{ message: string }>("pairing-error", (event) => {
252-
if (!cancelled) {
258+
if (!cancelled && pairingActiveRef.current) {
259+
pairingActiveRef.current = false;
253260
if (isPairingSessionTimeout(event.payload.message)) {
254261
setQrUri(null);
255262
setSasCode(null);
@@ -269,6 +276,7 @@ export function MobilePairingCard({
269276
return () => {
270277
cancelled = true;
271278
++requestIdRef.current;
279+
pairingActiveRef.current = false;
272280
for (const fn of unlisteners) fn();
273281
if (stepRef.current !== "idle" && stepRef.current !== "done") {
274282
cancelPairing().catch(() => {});
@@ -292,17 +300,20 @@ export function MobilePairingCard({
292300
? err.message
293301
: "We couldn't send your identity. Try again.",
294302
);
303+
pairingActiveRef.current = false;
295304
setStep("error");
296305
}
297306
}
298307

299308
function handleDenySas() {
309+
pairingActiveRef.current = false;
300310
cancelPairing().catch(() => {});
301311
setError("The codes didn't match. Pairing was canceled.");
302312
setStep("error");
303313
}
304314

305315
function handleStatusDialogClose() {
316+
pairingActiveRef.current = false;
306317
if (stepRef.current === "done") {
307318
setStep("idle");
308319
setQrUri(null);

‎desktop/tests/e2e/mobile-pairing-qr.spec.ts‎

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect, test, type Page } from "@playwright/test";
22
import { mkdirSync } from "node:fs";
33

44
import { installMockBridge } from "../helpers/bridge";
@@ -10,6 +10,31 @@ test.beforeEach(async ({ page }) => {
1010
await installMockBridge(page, { pairingStartDelayMs: 300 });
1111
});
1212

13+
async function emitPairingEvent(page: Page, event: string, payload?: unknown) {
14+
await page.evaluate(
15+
async ({ eventName, eventPayload }) => {
16+
const internals = (
17+
window as Window & {
18+
__TAURI_INTERNALS__?: {
19+
invoke?: (
20+
command: string,
21+
args: Record<string, unknown>,
22+
) => Promise<unknown>;
23+
};
24+
}
25+
).__TAURI_INTERNALS__;
26+
if (!internals?.invoke) {
27+
throw new Error("Tauri E2E event bridge is unavailable");
28+
}
29+
await internals.invoke("plugin:event|emit", {
30+
event: eventName,
31+
payload: eventPayload,
32+
});
33+
},
34+
{ eventName: event, eventPayload: payload },
35+
);
36+
}
37+
1338
test("mobile pairing starts on demand and reveals the QR code", async ({
1439
page,
1540
}) => {
@@ -103,26 +128,8 @@ test("mobile pairing starts on demand and reveals the QR code", async ({
103128
0.5,
104129
);
105130

106-
await page.evaluate(async () => {
107-
const internals = (
108-
window as Window & {
109-
__TAURI_INTERNALS__?: {
110-
invoke?: (
111-
command: string,
112-
args: Record<string, unknown>,
113-
) => Promise<unknown>;
114-
};
115-
}
116-
).__TAURI_INTERNALS__;
117-
if (!internals?.invoke) {
118-
throw new Error("Tauri E2E event bridge is unavailable");
119-
}
120-
await internals.invoke("plugin:event|emit", {
121-
event: "pairing-error",
122-
payload: {
123-
message: "Session timed out",
124-
},
125-
});
131+
await emitPairingEvent(page, "pairing-error", {
132+
message: "Session timed out",
126133
});
127134

128135
await expect(qrCode).toHaveCount(0);
@@ -150,3 +157,29 @@ test("mobile pairing starts on demand and reveals the QR code", async ({
150157
await card.screenshot({ path: `${SCREENSHOT_DIR}/pairing-card.png` });
151158
await qrCode.screenshot({ path: `${SCREENSHOT_DIR}/pairing-qr.png` });
152159
});
160+
161+
test("late pairing events are ignored after canceling", async ({ page }) => {
162+
await page.goto("/");
163+
await page.getByTestId("open-settings").click();
164+
await page.getByTestId("profile-popover-settings").click();
165+
await page.getByTestId("settings-nav-mobile").click();
166+
167+
const card = page.getByTestId("mobile-pairing-card");
168+
await card.getByTestId("start-pairing-button").click();
169+
await expect(page.getByTestId("mobile-pairing-qr")).toBeVisible();
170+
171+
await emitPairingEvent(page, "pairing-sas-received", { sas: "123456" });
172+
const dialog = page.getByTestId("mobile-pairing-dialog");
173+
await expect(dialog).toBeVisible();
174+
await dialog.getByRole("button", { name: "Close" }).click();
175+
176+
await expect(dialog).toHaveCount(0);
177+
await expect(card.getByText("Pairing was canceled.")).toBeVisible();
178+
179+
await emitPairingEvent(page, "pairing-complete");
180+
await emitPairingEvent(page, "pairing-sas-received", { sas: "654321" });
181+
182+
await expect(dialog).toHaveCount(0);
183+
await expect(page.getByTestId("mobile-pairing-done")).toHaveCount(0);
184+
await expect(card.getByText("Pairing was canceled.")).toBeVisible();
185+
});

0 commit comments

Comments
 (0)