Skip to content

Commit ea94dd8

Browse files
committed
fix(notifications): ignore single-item results after auth changes
Capture the authentication generation and token for single Read and Done actions. Guard Done retries and local notification commits so pending results cannot overwrite notifications, badges, or polling validators after logout or a new login. Add five regression cases covering pending responses and snapshot loading across logout and same-token login. Verify all 635 tests, lint, formatting, Firefox dev setup, and Chrome/Firefox release builds.
1 parent bbb8d2a commit ea94dd8

2 files changed

Lines changed: 65 additions & 5 deletions

File tree

‎src/background/service-worker.js‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,21 +596,28 @@ async function runNotificationMutation(operation) {
596596
}
597597

598598
function markNotification(notificationId, action = MESSAGE_TYPES.MARK_AS_READ) {
599+
const generation = authGeneration;
600+
const token = github.token;
601+
const isCurrent = () => authGeneration === generation && github.token === token;
599602
return runNotificationMutation(async () => {
600603
const isDone = action === MESSAGE_TYPES.MARK_AS_DONE;
601604
try {
602605
if (isDone) {
603606
const notification = (await storage.getNotifications()).find(
604607
(n) => n.id === notificationId,
605608
);
609+
if (!isCurrent()) throw new Error("Authentication changed. Please try again.");
606610
if (!notification) return { success: false, error: "Notification not found" };
607-
await github.markAsDone(notificationId);
608-
github.lastModified = null;
609-
github.lastModifiedAt = null;
611+
await github.markAsDone(notificationId, isCurrent);
610612
} else {
611613
await github.markAsRead(notificationId);
612614
}
613-
await removeStoredNotifications((n) => n.id === notificationId);
615+
if (!isCurrent()) throw new Error("Authentication changed. Please try again.");
616+
if (isDone) {
617+
github.lastModified = null;
618+
github.lastModifiedAt = null;
619+
}
620+
await removeStoredNotifications((n) => n.id === notificationId, { isCurrent });
614621
return { success: true };
615622
} catch (error) {
616623
console.error(`Failed to mark as ${isDone ? "done" : "read"}:`, error);

‎tests/service-worker.test.js‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ describe("service-worker", () => {
658658
expect(await callHandler({ action: "markAsDone", notificationId: "123" })).toEqual({
659659
success: true,
660660
});
661-
expect(mockGithub.markAsDone).toHaveBeenCalledExactlyOnceWith("123");
661+
expect(mockGithub.markAsDone).toHaveBeenCalledExactlyOnceWith("123", expect.any(Function));
662662
expect(mockGithub.markAsRead).not.toHaveBeenCalled();
663663
expect(mockStorageFunctions.setNotifications).toHaveBeenCalledWith([
664664
{ id: "456", title: "Another" },
@@ -680,6 +680,59 @@ describe("service-worker", () => {
680680
expect(mockGithub.lastModified).toBe("old");
681681
});
682682

683+
it.each([
684+
["markAsDone", false],
685+
["markAsDone", true],
686+
["markAsRead", false],
687+
["markAsRead", true],
688+
])(
689+
"does not commit a pending %s after logout (same-token login: %s)",
690+
async (action, login) => {
691+
mockGithub.token = "original-token";
692+
let finish;
693+
mockGithub[action].mockImplementationOnce(
694+
() => new Promise((resolve) => (finish = resolve)),
695+
);
696+
const operation = callHandler({ action, notificationId: "123" });
697+
await vi.waitFor(() => expect(finish).toBeDefined());
698+
await callHandler({ action: "logout" });
699+
if (login) {
700+
mockGithub.getNotifications.mockResolvedValue({ items: [], hasMore: false, count: 0 });
701+
await callHandler({ action: "login", token: "original-token" });
702+
}
703+
mockStorageFunctions.getNotifications.mockResolvedValue([{ id: "123", unread: true }]);
704+
mockStorageFunctions.setNotifications.mockClear();
705+
mockAction.setBadgeText.mockClear();
706+
mockGithub.lastModified = "current-session";
707+
mockGithub.lastModifiedAt = 123;
708+
finish(true);
709+
expect(await operation).toEqual({
710+
success: false,
711+
error: "Authentication changed. Please try again.",
712+
});
713+
expect(mockStorageFunctions.setNotifications).not.toHaveBeenCalled();
714+
expect(mockAction.setBadgeText).not.toHaveBeenCalled();
715+
expect(mockGithub.lastModified).toBe("current-session");
716+
expect(mockGithub.lastModifiedAt).toBe(123);
717+
},
718+
);
719+
720+
it("does not send Done after logout while the notification snapshot is loading", async () => {
721+
let finishReading;
722+
mockStorageFunctions.getNotifications.mockImplementationOnce(
723+
() => new Promise((resolve) => (finishReading = resolve)),
724+
);
725+
const operation = callHandler({ action: "markAsDone", notificationId: "123" });
726+
await callHandler({ action: "logout" });
727+
finishReading([{ id: "123" }]);
728+
expect(await operation).toEqual({
729+
success: false,
730+
error: "Authentication changed. Please try again.",
731+
});
732+
expect(mockGithub.markAsDone).not.toHaveBeenCalled();
733+
expect(mockStorageFunctions.setNotifications).not.toHaveBeenCalled();
734+
});
735+
683736
it.each([
684737
["markAsDone", { notificationId: "456" }, []],
685738
["markAsRead", { notificationId: "456" }, []],

0 commit comments

Comments
 (0)