Skip to content
This repository was archived by the owner on Jul 30, 2026. It is now read-only.
This repository was archived by the owner on Jul 30, 2026. It is now read-only.

refactor: extract handleBeforeSend keepAlive + popup handleEncrypt/handleDecrypt closures for testability #116

Description

@dobby-coder

Background

Follow-up to #105. The unit-test sweep landed in PRs #110#115 and brought the suite from 21 passed / 108 todo to 126 passed / 22 todo. The remaining 22 placeholders all sit inside two listener closures that aren't testable in their current shape — they need a production refactor (deps injection), not just test authoring, so they were intentionally split out.

Remaining todos (22)

tests/crypto-popup.test.ts — 19 todos

Cover handleEncrypt / handleDecrypt in src/pages/yivi-popup/yivi-popup.ts:

  • init / DOM render paths
  • auto-close behavior
  • error paths (SDK failure, missing recipients, cancelled QR, etc.)

tests/encryption-flow.test.ts — 3 todos

Cover handleBeforeSend's keepAlive closure body in src/background/background.ts:

  • encryption-failure path
  • popup-cancel path
  • plaintext-leak guard

Suggested approach

Both closures currently capture module-scoped singletons (pg-js SDK, browser.* namespaces, DOM globals) directly. To make them testable:

  1. Lift the closures to top-level functions that take their dependencies as parameters (SDK client, runtime, storage, DOM root, etc.).
  2. Listener bodies become thin call sites that pass the live deps in — no behavior change.
  3. Tests instantiate the function with mocked deps from the existing tests/helpers/browser-mock.ts harness plus a small pg-js mock.

The PR #110 harness already covers most of browser.*. The new lift is the pg-js client and a DOM mock for the popup tests.

Verification

  • After refactor: npm test should reach ~148 passing / 0 todo.
  • npm run typecheck stays green.
  • npm run build:dev still produces a working extension.
  • Manual smoke-test the encrypt + decrypt happy paths in Thunderbird, since the closures are the live encryption/decryption code paths.

Risk

This touches the production code that PR #68's persistence fix lives in. The lift must keep the existing guards intact (composeTabs.has() check, persistence on toggle, keepAlive semantics on onBeforeSend). The state-lifecycle tests added in #110 should pin those, but a manual smoke-test through Thunderbird before merge is still warranted.

Out of scope

  • Higher-level end-to-end / integration testing — tracked separately in Integration tests #53.
  • Any test-authoring work outside these two closures — the rest of the suite is real now.

Follow-up to #105.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions