Skip to content

Collapse XJogStateChange action-type stubs into a plain-action union (C6) - #68

Merged
juhovuo merged 1 commit into
mainfrom
fix/xjog-state-change-action-stubs
Jul 5, 2026
Merged

juhovuo merged 1 commit into
mainfrom
fix/xjog-state-change-action-stubs

Conversation

@juhovuo

@juhovuo juhovuo commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Resolves TODO item C6, which asked for a design proposal before coding. Proposal and rationale below.

Design proposal

packages/util/src/XJogStateChange.ts declared 18 per-action-type variants plus Unknown, but 15 of them were empty // TODO ? stubs whose only field was type. The TODO offered two options: fill in payloads, or collapse to a smaller union.

Chosen: collapse to a smaller union.

Rationale:

  • Nothing reads per-variant payloads. The only consumer of XJogStateChangeState.actions is the journal writer, which passes the array to JournalPersistenceAdapter.record() where it is JSON-serialized as-is. No code narrows on the stub variants — none of the 15 stub type names were referenced anywhere outside the union itself.
  • Filling in payloads is not generally possible. Several resolved xstate v4 action objects carry functions (assign assignments, pure/choose/log expressions), which cannot be JSON-serialized into the journal. Populating the serializable subset would be speculative API surface with no consumer, and a behavior change to persisted journal rows.
  • The collapse is behavior-preserving. mapActions already mapped all 15 stub types to a bare { type }; the serialized shape of journaled actions is byte-for-byte unchanged.

New shape: XJogStateChangeAction = Send | Cancel | Start | Stop (the four variants mapActions actually populates) | XJogStateChangePlainAction | Unknown, where XJogStateChangePlainAction is { type: XJogStateChangePlainActionType } and XJogStateChangePlainActionType is the union of the 15 built-in action types recorded by type only. Discriminated-union narrowing on the payload variants still works.

Bugs fixed along the way

  • XJogActionTypes was a declare enum, so it emitted no runtime value: XJogActionTypes.Unknown was a crash/never-match for any consumer (which is why mapActions hardcoded 'xjog.unknown' behind a commented-out line). It is now a real enum with the same member and value. The new regression test fails before this fix (TypeError: Cannot read properties of undefined (reading 'Unknown')).
  • mapActions dropped its blanket // @ts-expect-error; the switch is now fully type-checked, uses XJogActionTypes.Unknown (same runtime string), and the dead commented-out line is gone.

Tests

Added a mapActions suite in resolveXJogStateChange.test.ts: send/cancel/start/stop payload mapping, all 15 plain built-ins mapping to { type } only (payload fields dropped), custom types mapping to the runtime xjog.unknown marker, and one end-to-end case through a real machine transition.

Breaking change note (types only)

The 15 removed type aliases (e.g. XJogStateChangeLogAction) were exported from @telia-oss/xjog-util; external code importing them should switch to XJogStateChangePlainAction. Runtime behavior and persisted data are unchanged. Changeset: minor for xjog-util, patch for xjog.

Verification

  • pnpm run build — 15/15 tasks pass
  • pnpm run test — 13/13 tasks pass (11 tests in the touched suite)
  • pnpm run lint — clean

🤖 Generated with Claude Code

15 of the 18 XJogStateChange*Action variants were empty '// TODO ?' stubs
whose only field was 'type', and mapActions never populated anything else
for them. Collapse them into a single XJogStateChangePlainAction with a
narrowed XJogStateChangePlainActionType union; the serialized shape of
journaled actions is unchanged.

Also make XJogActionTypes a real enum (it was 'declare enum', so
XJogActionTypes.Unknown did not exist at runtime), use it in mapActions
instead of a hardcoded 'xjog.unknown' literal, drop the blanket
@ts-expect-error now that the mapping type-checks, and add unit tests
covering every branch of mapActions.

Resolves TODO item C6.
@juhovuo
juhovuo force-pushed the fix/xjog-state-change-action-stubs branch from f5e8dda to 8d0631a Compare July 5, 2026 00:41
@juhovuo
juhovuo merged commit c61b5fc into main Jul 5, 2026
1 check passed
@juhovuo
juhovuo deleted the fix/xjog-state-change-action-stubs branch July 5, 2026 00:41
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.

1 participant