Skip to content

test(extension-chrome): extract background-core for unit coverage#17

Merged
paperhurts merged 1 commit into
mainfrom
test/background-coverage
May 24, 2026
Merged

test(extension-chrome): extract background-core for unit coverage#17
paperhurts merged 1 commit into
mainfrom
test/background-coverage

Conversation

@paperhurts
Copy link
Copy Markdown
Owner

Summary

Pulls the maybeReconcile + pollRemoteOnce algorithm out of background.ts into a new src/lib/background-core.ts with dependency-injected now, lastReconciledAt, storage hooks, and a runReconcile callback. The new module is fully unit-testable; background.ts becomes a thin wiring layer that resolves the deps and passes them through. Live behavior is preserved — same storage keys, same constants, same error-classification rules.

TDD log

  • RED: wrote 11 tests in test/background-core.test.ts against a module that didn't exist. Watched them fail at import resolution — the right failure mode for "this module hasn't been created yet."
  • GREEN: created src/lib/background-core.ts exporting runMaybeReconcile + runPollRemoteOnce. All 11 new tests pass. Existing 72 still pass (83 total). Refactored background.ts to call the new functions; e2e and unit suites remained green throughout.
  • Hiccup: initial Partial<GitHubClient> typing in the test was too strict for the generic methods. Switched to any to match the existing fakeClient(over: any) pattern used in other test files.

Coverage added

  • 1-hour reconcile throttle (early-return when now - lastReconciledAt < interval)
  • RECONCILED_AT stamp + lastError clear on success
  • lastError persistence with kind: "auth" on GitHubAuthError
  • lastError persistence with kind: "unknown" on other errors
  • No RECONCILED_AT stamp on failure (so retry runs next cold start)
  • read() vs readIfChanged() routing by etag presence
  • Early-return on 304 (null result) — no apply, no etag write
  • applyRemote + etag write + lastError clear on success
  • Silent swallow of GitHubNotFoundError in poll (no lastError)
  • Auth-vs-unknown classification in poll lastError

Test plan

  • pnpm test 83/83 (11 new for this PR)
  • pnpm typecheck clean
  • pnpm build clean
  • CI green

Closes #4

Pulls the maybeReconcile + pollRemoteOnce algorithm out of background.ts
into src/lib/background-core.ts as runMaybeReconcile + runPollRemoteOnce.
Both take dependencies (now, lastReconciledAt, runReconcile, storage
hooks, etc.) as parameters so they're unit-testable without driving
chrome.* or Date.now().

background.ts becomes a thin wiring layer that resolves the dependencies
(chrome.storage.local, GitHubClient construction, IdMap.load, etc.) and
passes them through. The existing live behavior is preserved: same
keys, same constants, same error-classification rules.

TDD:
- RED: wrote 11 tests against src/lib/background-core.js (didn't exist),
  watched them fail to resolve the import.
- GREEN: created background-core.ts implementing the functions. All 11
  new tests pass; existing 72 unaffected (83 total). Typecheck clean.

Coverage now includes:
- 1-hour reconcile throttle (early-return when within window)
- RECONCILED_AT stamp + lastError clear on success
- lastError persistence with kind='auth' on GitHubAuthError
- lastError persistence with kind='unknown' on other GitHubError
- No RECONCILED_AT stamp on failure (so retry runs next cold start)
- read() vs readIfChanged() routing by etag presence
- Early-return on 304 (null result from readIfChanged) — no apply,
  no etag write
- applyRemote + etag write + lastError clear on success
- Silent swallow of GitHubNotFoundError in poll (no lastError)
- auth-vs-unknown classification in poll lastError

Closes #4
@paperhurts paperhurts merged commit c20c918 into main May 24, 2026
1 check passed
@paperhurts paperhurts deleted the test/background-coverage branch May 24, 2026 04:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

background.ts is untested (alarm scheduling, ETag persistence, error classification)

1 participant