From e07d40c2ab4c7ce20551abf9bd66082f5ce10ff9 Mon Sep 17 00:00:00 2001 From: Jonathan Prusik Date: Fri, 30 Jan 2026 16:27:23 -0500 Subject: [PATCH] cleanup --- .../notification.background.spec.ts | 29 +++++------- .../background/notification.background.ts | 46 +++++++------------ 2 files changed, 28 insertions(+), 47 deletions(-) diff --git a/apps/browser/src/autofill/background/notification.background.spec.ts b/apps/browser/src/autofill/background/notification.background.spec.ts index fc50271aad3..f76541bdc57 100644 --- a/apps/browser/src/autofill/background/notification.background.spec.ts +++ b/apps/browser/src/autofill/background/notification.background.spec.ts @@ -700,6 +700,11 @@ describe("NotificationBackground", () => { let getAllDecryptedForUrlSpy: jest.SpyInstance; const mockFormattedURI = "archive.org"; const mockFormURI = "https://www.archive.org"; + const expectSkippedCheckingNotification = () => { + expect(getAllDecryptedForUrlSpy).not.toHaveBeenCalled(); + expect(pushChangePasswordToQueueSpy).not.toHaveBeenCalled(); + expect(pushAddLoginToQueueSpy).not.toHaveBeenCalled(); + }; beforeEach(() => { tab = createChromeTabMock(); @@ -749,9 +754,7 @@ describe("NotificationBackground", () => { await notificationBackground.triggerCipherNotification(formEntryData, tab); - expect(getAllDecryptedForUrlSpy).not.toHaveBeenCalled(); - expect(pushChangePasswordToQueueSpy).not.toHaveBeenCalled(); - expect(pushAddLoginToQueueSpy).not.toHaveBeenCalled(); + expectSkippedCheckingNotification(); }); it("skips checking if a notification should trigger if the passed url is not valid", async () => { @@ -774,9 +777,7 @@ describe("NotificationBackground", () => { await notificationBackground.triggerCipherNotification(formEntryData, tab); - expect(getAllDecryptedForUrlSpy).not.toHaveBeenCalled(); - expect(pushChangePasswordToQueueSpy).not.toHaveBeenCalled(); - expect(pushAddLoginToQueueSpy).not.toHaveBeenCalled(); + expectSkippedCheckingNotification(); }); it("skips checking if a notification should trigger if the user has disabled both the new login and update password notification", async () => { @@ -798,9 +799,7 @@ describe("NotificationBackground", () => { await notificationBackground.triggerCipherNotification(formEntryData, tab); - expect(getAllDecryptedForUrlSpy).not.toHaveBeenCalled(); - expect(pushChangePasswordToQueueSpy).not.toHaveBeenCalled(); - expect(pushAddLoginToQueueSpy).not.toHaveBeenCalled(); + expectSkippedCheckingNotification(); }); it("skips checking if a notification should trigger if the user is logged out", async () => { @@ -820,9 +819,7 @@ describe("NotificationBackground", () => { await notificationBackground.triggerCipherNotification(formEntryData, tab); - expect(getAllDecryptedForUrlSpy).not.toHaveBeenCalled(); - expect(pushChangePasswordToQueueSpy).not.toHaveBeenCalled(); - expect(pushAddLoginToQueueSpy).not.toHaveBeenCalled(); + expectSkippedCheckingNotification(); }); it("skips checking if a notification should trigger if there is no active account", async () => { @@ -842,9 +839,7 @@ describe("NotificationBackground", () => { await notificationBackground.triggerCipherNotification(formEntryData, tab); - expect(getAllDecryptedForUrlSpy).not.toHaveBeenCalled(); - expect(pushChangePasswordToQueueSpy).not.toHaveBeenCalled(); - expect(pushAddLoginToQueueSpy).not.toHaveBeenCalled(); + expectSkippedCheckingNotification(); }); it("skips checking if a notification should trigger if the values for the `password` and `newPassword` fields match (no change)", async () => { @@ -863,9 +858,7 @@ describe("NotificationBackground", () => { await notificationBackground.triggerCipherNotification(formEntryData, tab); - expect(getAllDecryptedForUrlSpy).not.toHaveBeenCalled(); - expect(pushChangePasswordToQueueSpy).not.toHaveBeenCalled(); - expect(pushAddLoginToQueueSpy).not.toHaveBeenCalled(); + expectSkippedCheckingNotification(); }); it("skips checking if a notification should trigger if the vault is locked and there is no value for the `newPassword` field", async () => { diff --git a/apps/browser/src/autofill/background/notification.background.ts b/apps/browser/src/autofill/background/notification.background.ts index 6c8a768a0d8..72ee953d9d4 100644 --- a/apps/browser/src/autofill/background/notification.background.ts +++ b/apps/browser/src/autofill/background/notification.background.ts @@ -800,36 +800,24 @@ export default class NotificationBackground { }, ); - let inputScenario = null; + // Handle different field fill combinations and determine the input scenario + const inputScenariosByKey = { + upn: inputScenarios.usernamePasswordNewPassword, + un: inputScenarios.usernameNewPassword, + up: inputScenarios.usernamePassword, + u: inputScenarios.username, + pn: inputScenarios.passwordNewPassword, + n: inputScenarios.newPassword, + p: inputScenarios.password, + } as const; - // Handle different field fill combinations - if (currentPasswordFieldHasValue && newPasswordFieldHasValue && usernameFieldHasValue) { - inputScenario = inputScenarios.usernamePasswordNewPassword; - } else if (newPasswordFieldHasValue && usernameFieldHasValue && !currentPasswordFieldHasValue) { - inputScenario = inputScenarios.usernameNewPassword; - } else if ( - usernameFieldHasValue && - !currentPasswordFieldHasValue && - !newPasswordFieldHasValue - ) { - inputScenario = inputScenarios.username; - } else if (currentPasswordFieldHasValue && newPasswordFieldHasValue && !usernameFieldHasValue) { - inputScenario = inputScenarios.passwordNewPassword; - } else if ( - currentPasswordFieldHasValue && - !newPasswordFieldHasValue && - !usernameFieldHasValue - ) { - inputScenario = inputScenarios.password; - } else if (currentPasswordFieldHasValue && usernameFieldHasValue && !newPasswordFieldHasValue) { - inputScenario = inputScenarios.usernamePassword; - } else if ( - newPasswordFieldHasValue && - !currentPasswordFieldHasValue && - !usernameFieldHasValue - ) { - inputScenario = inputScenarios.newPassword; - } + type InputScenarioKeys = keyof typeof inputScenariosByKey; + + const key = ((usernameFieldHasValue ? "u" : "") + + (currentPasswordFieldHasValue ? "p" : "") + + (newPasswordFieldHasValue ? "n" : "")) as InputScenarioKeys; + + const inputScenario = key in inputScenariosByKey ? inputScenariosByKey[key] : null; if (inputScenario) { return await this.handleInputMatchScenario({