test(wallet-context): regression coverage for malformed persisted session JSON - #1171
Open
Ajibose wants to merge 1 commit into
Open
test(wallet-context): regression coverage for malformed persisted session JSON#1171Ajibose wants to merge 1 commit into
Ajibose wants to merge 1 commit into
Conversation
… session JSON readStoredSession already wraps JSON.parse in try/catch, validates shape via isWalletSession, and clears the bad localStorage key on failure, so no production code changes are needed. This adds the missing regression tests: syntactically invalid JSON (JSON.parse throws) and JSON that parses to a non-object value, both asserting hydration completes cleanly and the stale key is removed. Refs LabsCrypt#1082
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1082
Summary
A previous attempt at this issue (#1082) added a
.trim()call toreadStoredSessionand a matching regression test. That PR was correctly closed on review:readStoredSessioninfrontend/src/context/wallet-context.tsxalready wrapsJSON.parsein atry/catch, validates the parsed shape viaisWalletSession, and removes the badlocalStoragekey on both a parse failure and a shape-validation failure — so the hardening called for in #1082 was already in place onmain. The.trim()addition was actually a small regression: a whitespace-only stored value would short-circuit on!rawand returnnullwithout clearing the stored key, which is worse than the existing catch-all behavior.This PR makes no production code changes. It only adds the regression tests that were missing, per the maintainer's note that "a tiny test-only PR is welcome."
What's implemented
No changes to
frontend/src/context/wallet-context.tsx. The existingreadStoredSessionimplementation already satisfies the acceptance criteria in [Backend] frontend/src/context/wallet-context.tsx reducer has no error boundary for malformed persisted session JSON #1082:try/catch, falling back to a cleandisconnected/idlestate via the reducer'shydrateaction withsession: null.localStoragein both the catch block and theisWalletSessionshape-validation failure branch, so the app doesn't loop on the same bad data.frontend/src/context/wallet-context.test.tsx: added two regression tests that were previously missing coverage:should recover from syntactically invalid stored JSON without crashing— seedslocalStoragewith a string that failsJSON.parse('{not valid json,,,'), renders theWalletProvider, and asserts hydration completes without throwing,statusisidle,sessionisnull,errorMessageisnull, and the bad key is removed fromlocalStorage.should discard a stored session that parses to a non-object value— seedslocalStoragewith valid JSON that parses to a primitive ("just-a-string") rather than an object, and asserts the same clean-hydration/clear-storage behavior via theisWalletSessionguard.The existing suite already covered a valid session, an object with an invalid shape, and
mocked !== false, but had no case whereJSON.parseitself throws (the actual "malformed JSON" scenario from the issue title) or where parsing succeeds but yields a non-object. These two tests close that gap.Files changed
frontend/src/context/wallet-context.test.tsxHow to test
All 9 tests in the file pass, including the 2 new ones. Also ran the full frontend suite (
npm run test) — 208/208 tests pass — andnpm run lint, which reports only pre-existing warnings unrelated to this change.