const test = require("node:test"); const assert = require("node:assert/strict"); const background = require("./background.js"); test("normalizePageState preserves focused and pending field targets", () => { const state = background.normalizePageState({ tabId: 7, pageUrl: "https://vault.example.invalid/login", focusTarget: { role: "username", formIndex: 0, fieldIndex: 1 }, pendingTarget: { role: "password", formIndex: 0, fieldIndex: 2 } }); assert.deepEqual(state.focusTarget, { role: "username", formIndex: 0, fieldIndex: 1 }); assert.deepEqual(state.pendingTarget, { role: "password", formIndex: 0, fieldIndex: 2 }); }); test("shouldReuseMatches only reuses recent non-pending page matches", () => { const recentState = { pageHasLoginForm: true, matches: [{ id: "vault-console" }], pendingFill: false, updatedAt: Date.now() }; assert.equal(background.shouldReuseMatches(recentState, false), true); assert.equal(background.shouldReuseMatches({ ...recentState, pendingFill: true }, false), false); assert.equal(background.shouldReuseMatches({ ...recentState, pageHasLoginForm: false }, false), false); assert.equal(background.shouldReuseMatches(recentState, true), false); }); test("actionPresentationForState prioritizes approval visibility", () => { const presentation = background.actionPresentationForState({ pendingFill: true, pendingMessage: "Approve the browser fill request in KeePassGO.", configured: true, success: true, pageHasLoginForm: true, matches: [{ id: "vault-console" }] }); assert.equal(presentation.badgeText, "!"); assert.equal(presentation.color, "#9f5f0e"); assert.match(presentation.title, /approve/i); }); test("tokenPendingApprovalCount reads token-scoped approval state", () => { assert.equal(background.tokenPendingApprovalCount({ tokenPendingApprovalCount: 2 }), 2); assert.equal(background.tokenPendingApprovalCount({}), 0); }); test("shouldContinueWatchingState keeps polling locked login pages", () => { assert.equal(background.shouldContinueWatchingState({ pageHasLoginForm: true, pendingFill: false, status: { locked: true } }), true); assert.equal(background.shouldContinueWatchingState({ pageHasLoginForm: true, pendingFill: true, status: { locked: false } }), true); assert.equal(background.shouldContinueWatchingState({ pageHasLoginForm: true, pendingFill: false, status: { locked: false } }), false); }); test("default settings include a blank bearer token that can be overridden by harness patching", () => { assert.equal(background.defaultSettings.bearerToken, ""); });