Skip to content

fix(hottesting): mock state and list-state inputs where the state is read - #3216

Merged
rajamatt merged 10 commits into
mainfrom
dev/mara/mock-list-state-inputs
Oct 14, 2026
Merged

rajamatt merged 10 commits into
mainfrom
dev/mara/mock-list-state-inputs

Conversation

@rajamatt

@rajamatt rajamatt commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue (If applicable): related to https://github.com/unoplatform/studio.live/issues/4074

PR Type

  • Bugfix

What is the current behavior?

A model input declared as a state is not mocked where the state itself is read:

  • IListState<T> (ListState.Async(this, …)), the usual shape for a list edited elsewhere: {Vm}Mock.Create with ListFeedMock.Value(...) never reaches the view-model. The list's only message has no data and a NullReferenceException from the real loader, which runs against the null-injected service. Every preview of that list shows the error template.
  • IState<T>: feeds derived from the state see the mock, but reading the state itself, as its binding does, runs the real loader.

Cause: every reader of a state ends up at the state's own subscription, over its UpdateFeed. SwapFeed swapped only the context's subscription to the state, which no reader goes through. SwapListFeed swapped the context's subscription keyed on the ListStateImpl, which the list state's own reads, its bindable's included, never go through either. This was a regression of spec 013 v17.

What is the new behavior?

  • For a state input, what is swapped is the feed its UpdateFeed reads, at the context's subscription to it. That subscription is already swappable under mocking, and hot reload uses the same mechanism.
    • The real loader never runs.
    • The state keeps its own updates, so an edit applies over the mock, as it would over a service result in a live app. The bindable and the feeds derived from the state see it.
  • Each SetMock clears the state's updates (UpdateFeed.Clear), so the state starts over from its mock, even when the same mock is re-applied ({Model}Mock.Empty is a single shared instance).
  • A mock that is itself a state also takes the member's writes. Passing the member's own state restores its real loader.
  • A list state is swapped through the state it wraps.
  • Plain feed inputs are unchanged.
  • Rejected alternatives:
    • swapping the state's own subscription straight to the mock makes writes hang;
    • reading a separate state built over the mock for each swap (an earlier commit of this PR) adds a second replacement point next to the shared one, a process-wide table of the built states, and a fire-and-forget dispose.

Reviewer's guide

  • Start here: MockingService.SwapStateSource.
  • Core changes, internal only:
    • UpdateFeed.Clear drops the active updates, and UpdateFeed.Source exposes the feed the updates apply on;
    • StateImpl.Source and StateImpl.ClearUpdates expose them on the state;
    • ListStateImpl.Implementation exposes the state a list state wraps.
  • Commit history: 0f8e3dadb (a state built over the mock for each swap) is superseded by 7cb36e01b.
  • Tests that prove it:
    • the first commit, a43842163, adds tests only: 9 of its 10 tests fail without the fix, with no data and a NullReferenceException. They cover a list state carrying mocked items, re-swap, Loading and Error, restore, an edit kept over the mock, another list state as the mock, and a scalar state read directly. The 10th, a feed derived from a list state, already passed and is kept because the fix changes the path it reads through;
    • 3e9da13a4 adds re-applying the same mock drops an earlier edit (list and scalar), an edit reaches the derived feed, and a scalar re-swap;
    • d243e1d6e covers UpdateFeed.Clear and StateImpl.ClearUpdates directly.
  • Docs: spec 013 architecture, history v19 and decision D6; a paragraph in testing.md.

PR Checklist

  • Unit Tests for the changes have been added: 16 tests (Given_GeneratedMock, Given_SingleProjectMock, Given_UpdateFeed).
  • Contains NO breaking changes: no public API change. Behaviour change: an edit made through a mocked state now shows on the page and in feeds derived from it, as in a live app, and each SetMock starts a state input over from its mock; before, the edit was dropped from what the page showed.
  • Tested code with current supported SDKs: unit tests (rebased on main with fix(hottesting): give mocked view models a messenger #3214: Uno.Extensions.Reactive.Tests, 1486 pass, 19 skipped; Uno.HotTesting.Reactive.Tests, 38 pass; mocking, UpdateFeed and hot-reload tests stable over 3 runs). UI runtime tests not run locally, and not yet checked in a Studio preview.
  • Associated with an issue

Other information

🤖 Generated with Claude Code

@rajamatt rajamatt self-assigned this Oct 8, 2026
@rajamatt
rajamatt requested a review from dr1rrb October 8, 2026 18:19
@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 238299 has failed on uno.extensions.

@rajamatt
rajamatt marked this pull request as ready for review October 13, 2026 16:39
@dr1rrb

dr1rrb commented Oct 14, 2026

Copy link
Copy Markdown
Member

@rajamatt, thanks for tracking this down: the state-input swap did break when the swap moved to the shared FeedSubscription in #3192, and your tests pin it well. We pushed 8f8c602 on top of your branch, which keeps all your tests unchanged and replaces the implementation.

Why. Building a StateImpl over the mock for each swap and reading it through the state's own subscription fixes the preview, but it brings back a second replacement point for state inputs, next to the shared FeedSubscription that hot reload and the other members use (#3192 had just merged the two), and it needs a process-wide ConditionalWeakTable plus a fire-and-forget dispose to manage the built states.

What changed. A state input now keeps its own UpdateFeed; what is swapped is the feed its updates apply on, through the context's subscription to it, which is already swappable under mocking. So the bindable and the feeds derived from the state see the mock with the edits over it, and nothing extra is built per swap. SetMock clears the state's updates (new internal UpdateFeed.Clear), so re-applying the same mock, {Model}Mock.Empty included, starts over from it. Spec 013 v19, the architecture notes and testing.md are updated to match. Uno.Extensions.Reactive.Tests (1483 passed) and Uno.HotTesting.Reactive.Tests (34 passed) are green locally.

One known limit. A state built over another member feed (for example State.FromFeed(this, OtherFeed)) shares that feed's subscription, so mocking the state also mocks OtherFeed.

Could you check that this works for your case in studio.live (unoplatform/studio.live#4074)? In particular, does any model you preview build a state over another member feed, where the limit above would bite?

rajamatt and others added 10 commits October 14, 2026 13:55
- add TasksModel: a service-dependent IListState input and a feed derived from it
- the list state carries mocked items through the view model, re-swaps, pins Loading and Error, restores its own loader, keeps an edit, and accepts another list state as its mock
- a scalar state input read directly carries the mocked value and keeps an edit
- all but the derived-feed test fail on the current code: the state still reads the real, null-injected loader

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- every reader of a state reads the state's own subscription, directly or through the context's subscription to the state; SwapFeed swapped only the latter, and SwapListFeed swapped the context's subscription to the list state, which the list state's own reads (its bindable's included) never go through
- a state input's own subscription now reads a state over the mock, and the member's writes are redirected to that state, so edits apply over the mock and the real loader never runs
- a list state is swapped through the state it wraps; a mock that is the member itself restores its own updates
- StateImpl exposes its subscription and ListStateImpl the state it wraps; ListStateImpl.HotSwap had no caller left and is removed

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Spec 013 architecture: a state input is swapped at the state's own subscription,
  over a state built on the mock that also takes the member's writes

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- re-applying the same mock to a list or scalar state input must drop an earlier edit;
  both fail on the current code, which reuses the state built over that mock instance
- an edit through a mocked list state reaches the feed derived from it
- a scalar state input re-swapped carries the new value
- WaitForMessage joins WaitForData in the shared recorder helpers and reads the count
  once per pass, so a message recorded after the check is not skipped

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- each swap of a state input reads a state built for that swap only, so it starts over
  from its mock even when the instance is reused ({Model}Mock.Empty) or given to two members
- the state built by the previous swap is disposed once nothing reads it
- a mock that is itself a state still takes the writes directly; any other IState
  no longer hits an InvalidCastException

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- architecture: an edit of a state built over a feed input stays local; a state input's
  edit reaches its derived feeds, and its mock state lives for one swap
- history v19 explains the move and why v16's lock-ordering objection no longer applies,
  with the remaining limits; D6 names the state input's anchor
- testing.md notes how a mocked state input behaves

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- the note sat right before the IMPORTANT admonition, and two blockquotes split by a
  blank line fail markdownlint (MD028); it is now a plain paragraph

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The swap of a state input built a state over the mock for each swap and read it through
the state's own subscription. That is a second replacement point next to the shared
FeedSubscription hot reload uses, plus a process-wide table of the built states and a
fire-and-forget dispose.

A state input now keeps its own UpdateFeed, and what is swapped is the feed its updates
apply on, through the context's subscription to it. The bindable and the feeds derived from
the state see the mock with the edits over it. SetMock clears the state's updates through
UpdateFeed.Clear, so re-applying the same mock starts over from it. The tests of this PR are
kept unchanged; spec 013 v19, architecture and testing.md describe the new swap.

Limit: a state built over another member feed shares that feed's subscription, so mocking
the state also mocks that feed.
- Clear drops the active updates; once the source has completed, a compacted update
  is undone only if it can roll itself back, so a state's own edit stays applied
- clearing is meant for a source still running, like a mocked one

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Clear reverts every active update and the feed shows its source again
- a state over a source still running shows that source again once its updates are cleared,
  which is what SetMock relies on

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rajamatt
rajamatt force-pushed the dev/mara/mock-list-state-inputs branch from d52eb28 to d243e1d Compare October 14, 2026 17:56
@mergify

mergify Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@rajamatt
rajamatt merged commit e27c3c8 into main Oct 14, 2026
27 of 28 checks passed
@rajamatt
rajamatt deleted the dev/mara/mock-list-state-inputs branch October 14, 2026 18:32
@rajamatt

Copy link
Copy Markdown
Contributor Author

@Mergifyio backport servicing/7.4

@mergify

mergify Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

backport servicing/7.4

✅ Backports have been created

Details

Cherry-pick of 78d351f has failed:

On branch mergify/bp/servicing/7.4/pr-3216
Your branch is ahead of 'origin/servicing/7.4' by 5 commits.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 78d351f.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   doc/Reference/Reactive/testing.md
	modified:   specs/013-mvux-mocking-previews/architecture.md

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   specs/013-mvux-mocking-previews/history.md

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

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.

3 participants