Skip to content

refactor: extract shared helper for duplicated logic (#46)#55

Merged
sirdeggen merged 5 commits into
mainfrom
sonar/dedupe-pair
May 6, 2026
Merged

refactor: extract shared helper for duplicated logic (#46)#55
sirdeggen merged 5 commits into
mainfrom
sonar/dedupe-pair

Conversation

@sirdeggen

Copy link
Copy Markdown
Contributor

Summary

Partial fix for #46 (9,792 duplicated lines / 439 blocks).

  • Extracts a new StorageClientBase abstract class from the two near-identical storage remoting classes
  • StorageClient (full logger support) and StorageMobile (lightweight variant) each had ~420 lines of identical WalletStorageProvider method implementations
  • Both classes now extend StorageClientBase and only implement the rpcCall method that differs between them

What changed

File Change
src/storage/remoting/StorageClientBase.ts New: abstract base class with all shared methods
src/storage/remoting/StorageClient.ts Shrunk from 566 → 96 lines; extends StorageClientBase
src/storage/remoting/StorageMobile.ts Shrunk from 544 → 70 lines; extends StorageClientBase

Duplication removed: ~420 lines (one block, two files).

No behavior change. All public APIs, class names, and export paths are identical. The constructor.name === 'StorageClient' check in WalletStorageManager continues to work because both subclasses retain the StorageClient name.

Test plan

  • tsc --noEmit --skipLibCheck passes with no errors in changed files
  • npm run build produces compiled output for all three remoting files
  • CI passes

Refs #46

…geMobile (#46)

Both StorageClient (with logger support) and StorageMobile (lightweight variant)
were ~420-line near-identical classes. Extract all shared WalletStorageProvider
method implementations and entity-validation helpers into StorageClientBase.
Each subclass now only implements the rpcCall method that differs between them.

Partial fix for #46.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Fixes frozen-lockfile CI failure introduced in c4db522 which added tsx
to ts-p2p devDependencies without regenerating the lockfile.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@codecov

codecov Bot commented May 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…ication

Extract validateDate, validateEntity, and validateEntities from both
StorageClientBase and StorageServer into a shared entityValidationHelpers.ts.
Both classes now delegate to the standalone functions, removing the duplicate
implementation that SonarCloud flagged between the two files.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sirdeggen

Copy link
Copy Markdown
Contributor Author

CI Status

  • build-and-test (24.x): in progress (run chore(deps): bump actions/setup-node from 4 to 6 #2 after second commit)
  • CodeQL: ✗ FAILURE — 1 new high-severity alert (js/loop-bound-injection) at packages/wallet/wallet-toolbox/src/storage/remoting/entityValidationHelpers.ts:47. Iteration over a user-controlled object with potentially unbounded .length. Please review whether this issue was already present in the originals being deduped, and either bound the loop or reject the input upstream.
  • SonarCloud Code Analysis: previously failed on new_coverage (0%) and new_duplicated_lines_density (7.5%). Second commit attempts to address duplication.

This is a partial fix and may need a follow-up commit to address the CodeQL finding before merge.

sirdeggen and others added 2 commits May 6, 2026 00:50
…hunk duplication

Both StorageClientBase.getSyncChunk and StorageServer's processSyncChunk case
had the same 13-line SyncChunk entity-validation block. Extract it into
validateSyncChunkEntities() in entityValidationHelpers.ts and replace both sites.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…d injection

CodeQL flags js/loop-bound-injection when iterating with .length on a
user-controlled value. Guard with Array.isArray() so non-array inputs
(e.g. JSON objects with a spoofed .length) are returned unchanged.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sonarqubecloud

sonarqubecloud Bot commented May 6, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@sirdeggen sirdeggen merged commit 5f95c80 into main May 6, 2026
9 of 10 checks passed
sirdeggen added a commit that referenced this pull request May 6, 2026
Add Jest unit tests for the four exports of
packages/wallet/wallet-toolbox/src/storage/remoting/entityValidationHelpers.ts
to recover SonarCloud's new-coverage gate after PR #55.

Covers validateDate (Date / ISO string / number / epoch / unparsable),
validateEntity (string-to-Date coercion, null-to-undefined, Uint8Array and
Buffer to number[] equivalence, custom dateFields, in-place mutation),
validateEntities (non-array passthrough, empty / single / multi, dateFields
propagation), and validateSyncChunkEntities (empty chunk, user-only,
all-arrays-populated, undefined / empty array handling).

100% statement / branch / function / line coverage on the file.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
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.

2 participants