-
Notifications
You must be signed in to change notification settings - Fork 4
fix: keep transfer unit tests out of the app's UserDefaults #756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jvsena42
merged 6 commits into
master
from
fix/733-transfer-test-userdefaults-pollution
Sep 21, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c37ce5b
refactor: make TransferStorage's UserDefaults injectable
jvsena42 5757e0d
refactor: let TransferViewModel scope transfer persistence
jvsena42 7db4064
test: add shared app-state isolation helpers
jvsena42 8b14255
fix: keep transfer unit tests out of the app's UserDefaults
jvsena42 de059da
test: put the suite's remaining view models on the isolated store
jvsena42 a721ea0
fix: keep TransferViewModel's default on the shared transfer storage
jvsena42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import Foundation | ||
| import XCTest | ||
|
|
||
| /// Helpers that keep a test suite off the host app's own state. | ||
| /// | ||
| /// `BitkitTests` is hosted in the Bitkit app (`TEST_HOST` in the project settings), so | ||
| /// `UserDefaults.standard` *is* the app's preferences and anything a test writes there lands in the | ||
| /// developer's wallet. Issue #733 is what that looks like in practice: mock transfer records left | ||
| /// behind by a test run pinned a permanent "TRANSFER IN PROGRESS" banner on the real wallet. | ||
| /// | ||
| /// Reach for these in order of preference: | ||
| /// 1. `makeIsolatedDefaults()` when the code under test accepts injected defaults — nothing touches | ||
| /// the app's domain at all. | ||
| /// 2. `snapshotAppDefaults(_:)` when it does not, so the keys are put back afterwards. | ||
| /// 3. `guardAppDefaults(_:)` on suites that should write nothing, to keep it that way. | ||
| extension XCTestCase { | ||
| /// A `UserDefaults` suite unique to this test, emptied before it runs and removed afterwards. | ||
| func makeIsolatedDefaults(_ label: String = #function, file: StaticString = #filePath, line: UInt = #line) throws -> UserDefaults { | ||
| let suiteName = "\(type(of: self)).\(label).\(UUID().uuidString)" | ||
| let defaults = try XCTUnwrap(UserDefaults(suiteName: suiteName), "Could not open suite \(suiteName)", file: file, line: line) | ||
| defaults.removePersistentDomain(forName: suiteName) | ||
| addTeardownBlock { defaults.removePersistentDomain(forName: suiteName) } | ||
| return defaults | ||
| } | ||
|
|
||
| /// Restores `keys` in `UserDefaults.standard` when the test ends, removing any that are absent | ||
| /// now. Use when the code under test has no seam for injected defaults. | ||
| func snapshotAppDefaults(_ keys: String...) { | ||
| let defaults = UserDefaults.standard | ||
| let snapshot = keys.map { (key: $0, value: defaults.object(forKey: $0)) } | ||
| addTeardownBlock { | ||
| for entry in snapshot { | ||
| if let value = entry.value { | ||
| defaults.set(value, forKey: entry.key) | ||
| } else { | ||
| defaults.removeObject(forKey: entry.key) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Fails the test if it leaves any of `keys` in `UserDefaults.standard` changed. The regression | ||
| /// guard for #733: a suite that should be writing to an isolated suite goes red here instead of | ||
| /// silently corrupting the wallet on the simulator. | ||
| func guardAppDefaults(_ keys: String..., file: StaticString = #filePath, line: UInt = #line) { | ||
| let defaults = UserDefaults.standard | ||
| let before = keys.map { (key: $0, value: defaults.object(forKey: $0) as? NSObject) } | ||
| addTeardownBlock { | ||
| for entry in before where defaults.object(forKey: entry.key) as? NSObject != entry.value { | ||
| // Deliberately not interpolating the values: these keys hold large encoded blobs, and | ||
| // dumping both of them buries the one line that says what to do about it. | ||
| XCTFail( | ||
| """ | ||
| '\(entry.key)' in UserDefaults.standard was modified by this test, which writes to the host app's \ | ||
| own preferences. Inject an isolated suite with makeIsolatedDefaults(), or snapshot the key with \ | ||
| snapshotAppDefaults(_:) if the code under test has no seam for it. | ||
| """, | ||
| file: file, | ||
| line: line | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.