Skip to content

[Test Improver] Add unit tests for crypto/encryption utilities - #89

Draft
github-actions[bot] wants to merge 1 commit into
masterfrom
test-assist/encryption-utils-fa4a0d8e5db4b455
Draft

[Test Improver] Add unit tests for crypto/encryption utilities#89
github-actions[bot] wants to merge 1 commit into
masterfrom
test-assist/encryption-utils-fa4a0d8e5db4b455

Conversation

@github-actions

Copy link
Copy Markdown

🤖 This PR was created by Test Improver, an automated AI assistant focused on improving test coverage.

Goal and Rationale

shell/utils/crypto/encryption.ts provides the deriveKey, encrypt, and decrypt functions that are the cryptographic backbone of the notification store — it encrypts sensitive notification data before persisting it to localStorage. Despite being security-critical code, it had zero test coverage.

Approach

Wrote 15 unit tests across three describe blocks:

Describe What is tested
deriveKey Algorithm name (AES-GCM), non-extractability, correct key usages, per-password uniqueness (verified by cross-key decryption failure)
encrypt Output shape (cipher + iv), base64 format, random IV per call
decrypt Round-trip fidelity for plain text, unicode, long strings, JSON, special characters; rejection on wrong key; rejection on tampered cipher

The JSDOM test environment doesn't ship the Web Crypto API, so the test file wires up Node.js's built-in webcrypto via Object.defineProperty(global, 'crypto', { value: webcrypto }) — a pattern limited to this single test file.

Coverage Impact

File Before After
shell/utils/crypto/encryption.ts 0 % ~100 %

Test Status

All 15 tests pass:

PASS shell/utils/crypto/__tests__/encryption.test.ts
Tests: 15 passed, 15 total

Lint: no errors or warnings (eslint --max-warnings 0).

Trade-offs

  • The PBKDF2 key derivation runs 100,000 iterations, so tests involving deriveKey take ~100–200 ms each. This is acceptable for 15 tests and reflects the real cost of the function.
  • Tampered-cipher test appends base64 characters to the ciphertext; this changes the decoded byte length which AES-GCM rejects (integrity check fails).

Reproducibility

yarn test:ci shell/utils/crypto/__tests__/encryption.test.ts

Warning

Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • download.cypress.io

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "download.cypress.io"

See Network Configuration for more information.

Generated by Daily Test Improver · ● 4.3M ·

Add tests for the deriveKey, encrypt, and decrypt functions in
shell/utils/crypto/encryption.ts. These cryptographic utilities
are used by the notifications store for persisting encrypted
notifications in local storage and had no existing test coverage.

Tests cover:
- deriveKey: algorithm, non-extractability, key usages, and key
  uniqueness per password
- encrypt: output shape, base64 encoding of cipher and iv fields,
  and random IV generation per call
- decrypt: round-trip fidelity for plain, unicode, long, JSON, and
  special-character strings; rejection on wrong key and tampered cipher

Wire up Node.js webcrypto to the global scope so the Web Crypto API
is available in the JSDOM test environment.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants