[Refactor] Screenshot generator: Better state management via mocks#860
Merged
newtonick merged 3 commits intoSeedSigner:devfrom Feb 16, 2026
Merged
Conversation
5 tasks
alvroble
reviewed
Jan 18, 2026
| # Set up mocks to provide whatever temporary data/state a particular screenshot | ||
| # might need. | ||
| @contextmanager | ||
| def mock_load_psbt(base64_psbt: str, seed: Seed = seed_12b): |
Contributor
There was a problem hiding this comment.
Maybe this is not the case, but if in the future a context manager needs to declare more variables, this could be a good alternative to avoid the withnesting:
from contextlib import ExitStack
with ExitStack() as stack:
stack.enter_context(patch.object(controller, 'psbt', decoder.get_psbt()))
stack.enter_context(patch.object(controller, 'psbt_seed', seed))
stack.enter_context(patch.object(controller, 'psbt_parser', PSBTParser(p=controller.psbt, seed=seed)))
yield
Contributor
Author
There was a problem hiding this comment.
Interesting. Diving further into python techniques I've never used!
For the moment I like the with patch... convention (even with the nesting), because it directly parallels the same technique used elsewhere in the test suite.
Contributor
|
I understood the changes and I think it's a good QOL improvement. ACK tested. |
fc6fa4f to
b447edd
Compare
Contributor
Author
|
Rebased to current |
Collaborator
|
ACK and tested |
7 tasks
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.
Motivation
The screenshot generator's
ScreenshotConfig.run_beforeparam currently provides a way to inject data into theControllerstate just before a given screenshot is rendered.The downside is that these state changes remain in place for all subsequent screenshots that follow in the
screenshot_list.Some screenshots depend on a prior state change and try to document this with
# Relies on callback abovecomments.A few other screenshots also depend on prior state changes but do NOT document the dependency.
This
run_beforemethod and its aftereffects is not ideal.And when more complex contexts are required for new screenshots (see #858) the weaknesses here look even worse.
Solution
This PR refactors
run_beforeto use temporaryunittest.mock.patchcalls instead. The patches are defined using the slightly less straightforwardcontextmanagerpython approach (I had not been terribly familiar withyield) and passed intoScreenshotConfigvia a newmock_context_managerparam. But the tradeoff win is that this approach brings familiar test suite mocking patterns into the screenshot generator.Simplified example:
The screenshots that were relying on prior state changes being present now just call whichever
mock_context_managerthey need. Those dependencies on a particular expected internal state are now explicit in all cases.Interestingly, there were also some DRY wins.
This pull request is categorized as a:
Checklist
pytestand made sure all unit tests pass before submitting the PRIf you modified or added functionality/workflow, did you add new unit tests?
I have tested this PR on the following platforms/os: