Skip to content

feat(state): implemented fluent state pattern#55

Merged
JerrettDavis merged 1 commit intomainfrom
feature/25-fluent-create-state-pattern
Oct 12, 2025
Merged

feat(state): implemented fluent state pattern#55
JerrettDavis merged 1 commit intomainfrom
feature/25-fluent-create-state-pattern

Conversation

@JerrettDavis
Copy link
Copy Markdown
Owner

No description provided.

- AsyncStateMachine<TState,TEvent> API redesigned to avoid ref parameters (not allowed on async):
  - TryTransitionAsync(TState state, TEvent event, CancellationToken) -> (bool handled, TState state)
  - TransitionAsync(TState state, TEvent event, CancellationToken) -> TState
  - Preserves ordering: Exit -> Effect -> Enter; Stay runs effect only; per-state default supported.
- Updated unit tests for async state machine to new API and added coverage for ordering, default handling, and unhandled throws.
- Added Async Connection demo:
  - src/PatternKit.Examples/AsyncStateDemo/AsyncStateDemo.cs
  - test/PatternKit.Examples.Tests/AsyncStateDemo/AsyncStateDemoTests.cs (TinyBDD overloads disambiguated)
- Documentation:
  - docs/patterns/behavioral/state/state.md explains both sync and async flavors with quick starts and API summary.
  - docs/examples/async-state-machine.md example page; linked in docs/examples/toc.yml.

Build & tests:
- Verified with `dotnet test -v minimal`: all projects compile, and all tests pass across target frameworks.
@JerrettDavis JerrettDavis requested a review from Copilot October 11, 2025 04:18
@JerrettDavis JerrettDavis self-assigned this Oct 11, 2025
@JerrettDavis JerrettDavis added the enhancement New feature or request label Oct 11, 2025
@JerrettDavis JerrettDavis linked an issue Oct 11, 2025 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a fluent state pattern providing both synchronous and asynchronous state machines with entry/exit hooks, transition effects, and predictable first-match rule ordering.

  • Adds StateMachine<TState, TEvent> for synchronous state transitions with ref state parameter updates
  • Adds AsyncStateMachine<TState, TEvent> for asynchronous operations returning state tuples
  • Includes comprehensive test coverage and example demos for order and connection lifecycles

Reviewed Changes

Copilot reviewed 15 out of 16 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/PatternKit.Core/Behavioral/State/StateMachine.cs Core synchronous state machine implementation with fluent builder API
src/PatternKit.Core/Behavioral/State/AsyncStateMachine.cs Asynchronous variant with ValueTask-based predicates and effects
test/PatternKit.Tests/Behavioral/State/StateMachineTests.cs Unit tests for synchronous state machine behavior and transitions
test/PatternKit.Tests/Behavioral/State/AsyncStateMachineTests.cs Unit tests for asynchronous state machine operations
src/PatternKit.Examples/StateDemo/StateDemo.cs Example order lifecycle demo using synchronous state machine
src/PatternKit.Examples/AsyncStateDemo/AsyncStateDemo.cs Example connection lifecycle demo using async state machine
test/PatternKit.Examples.Tests/StateDemo/StateDemoTests.cs Tests for order lifecycle demo scenarios
test/PatternKit.Examples.Tests/AsyncStateDemo/AsyncStateDemoTests.cs Tests for async connection demo scenarios
docs/patterns/behavioral/state/state.md Documentation page for state pattern implementation
docs/examples/state-machine.md Example documentation for order lifecycle demo
docs/examples/async-state-machine.md Example documentation for async connection demo
docs/patterns/toc.yml Table of contents update to include state pattern
docs/patterns/index.md Main patterns index update with state machine references
docs/examples/toc.yml Examples table of contents update
docs/examples/index.md Examples index update with state machine demos
README.md Root readme update marking state pattern as completed

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread docs/examples/async-state-machine.md
@github-actions
Copy link
Copy Markdown
Contributor

Code Coverage

Summary
  Generated on: 10/11/2025 - 04:19:56
  Coverage date: 10/11/2025 - 04:19:38 - 10/11/2025 - 04:19:52
  Parser: MultiReport (6x Cobertura)
  Assemblies: 3
  Classes: 163
  Files: 70
  Line coverage: 82.5%
  Covered lines: 4005
  Uncovered lines: 846
  Coverable lines: 4851
  Total lines: 14554
  Branch coverage: 72.4% (1379 of 1903)
  Covered branches: 1379
  Total branches: 1903
  Method coverage: 88.7% (1037 of 1168)
  Full method coverage: 81.7% (955 of 1168)
  Covered methods: 1037
  Fully covered methods: 955
  Total methods: 1168

PatternKit.Core                                                                    85%
  PatternKit.Behavioral.Chain.ActionChain<T>                                     84.7%
  PatternKit.Behavioral.Chain.ResultChain<T1, T2>                                 100%
  PatternKit.Behavioral.Command.Command<T>                                       54.9%
  PatternKit.Behavioral.Iterator.AsyncFlow<T>                                     100%
  PatternKit.Behavioral.Iterator.AsyncFlowExtensions                              100%
  PatternKit.Behavioral.Iterator.AsyncReplayBuffer<T>                              75%
  PatternKit.Behavioral.Iterator.Flow<T>                                         94.4%
  PatternKit.Behavioral.Iterator.FlowExtensions                                  21.4%
  PatternKit.Behavioral.Iterator.ReplayableSequence<T>                           80.7%
  PatternKit.Behavioral.Iterator.ReplayableSequenceExtensions                     100%
  PatternKit.Behavioral.Iterator.SharedAsyncFlow<T>                               100%
  PatternKit.Behavioral.Iterator.SharedFlow<T>                                   63.1%
  PatternKit.Behavioral.Iterator.WindowSequence                                  92.3%
  PatternKit.Behavioral.Mediator.Mediator                                        88.2%
  PatternKit.Behavioral.Mediator.MediatorHelpers                                   50%
  PatternKit.Behavioral.Mediator.TaskExtensions                                     0%
  PatternKit.Behavioral.Memento.Memento<T>                                       85.3%
  PatternKit.Behavioral.Observer.AsyncObserver<T>                                87.8%
  PatternKit.Behavioral.Observer.Observer<T>                                     96.5%
  PatternKit.Behavioral.State.AsyncStateMachine<T1, T2>                            68%
  PatternKit.Behavioral.State.StateMachine<T1, T2>                                 74%
  PatternKit.Behavioral.Strategy.ActionStrategy<T>                                 97%
  PatternKit.Behavioral.Strategy.AsyncStrategy<T1, T2>                           93.5%
  PatternKit.Behavioral.Strategy.Strategy<T1, T2>                                 100%
  PatternKit.Behavioral.Strategy.TryStrategy<T1, T2>                             95.8%
  PatternKit.Common.Option<T>                                                      80%
  PatternKit.Common.Throw                                                          80%
  PatternKit.Common.TryHandlerExtensions                                         33.3%
  PatternKit.Creational.Builder.BranchBuilder<T1, T2>                             100%
  PatternKit.Creational.Builder.BuilderExtensions                                 100%
  PatternKit.Creational.Builder.ChainBuilder<T>                                   100%
  PatternKit.Creational.Builder.Composer<T1, T2>                                  100%
  PatternKit.Creational.Builder.MutableBuilder<T>                                 100%
  PatternKit.Creational.Factory.Factory<T1, T2>                                  76.9%
  PatternKit.Creational.Factory.Factory<T1, T2, T3>                              76.9%
  PatternKit.Creational.Prototype.Prototype<T>                                    100%
  PatternKit.Creational.Prototype.Prototype<T1, T2>                                90%
  PatternKit.Creational.Singleton.Singleton<T>                                   96.5%
  PatternKit.Structural.Adapter.Adapter<T1, T2>                                  82.2%
  PatternKit.Structural.Bridge.Bridge<T1, T2, T3>                                77.5%
  PatternKit.Structural.Composite.Composite<T1, T2>                              97.3%
  PatternKit.Structural.Decorator.Decorator<T1, T2>                              97.6%
  PatternKit.Structural.Facade.Facade<T1, T2>                                    88.8%
  PatternKit.Structural.Facade.TypedFacade<T>                                    79.4%
  PatternKit.Structural.Facade.TypedFacadeDispatchProxy<T>                       81.8%
  PatternKit.Structural.Facade.TypedFacadeProxyFactory<T>                         100%
  PatternKit.Structural.Flyweight.Flyweight<T1, T2>                              83.3%
  PatternKit.Structural.Proxy.Proxy<T1, T2>                                      98.8%

PatternKit.Examples                                                              80.4%
  PatternKit.Examples.ApiGateway.Demo                                            97.9%
  PatternKit.Examples.ApiGateway.MiniRouter                                      96.6%
  PatternKit.Examples.ApiGateway.Request                                           75%
  PatternKit.Examples.ApiGateway.Response                                         100%
  PatternKit.Examples.ApiGateway.Responses                                        100%
  PatternKit.Examples.AsyncStateDemo.ConnectionStateDemo                         93.5%
  PatternKit.Examples.Chain.AuthLoggingDemo                                      95.2%
  PatternKit.Examples.Chain.CardProcessors                                        100%
  PatternKit.Examples.Chain.CardTenderStrategy                                      0%
  PatternKit.Examples.Chain.CashTenderStrategy                                      0%
  PatternKit.Examples.Chain.ChainStage                                            100%
  PatternKit.Examples.Chain.CharityRoundUpRule                                     20%
  PatternKit.Examples.Chain.ConfigDriven.Bundle1OffEach                          14.2%
  PatternKit.Examples.Chain.ConfigDriven.CardTender                              72.2%
  PatternKit.Examples.Chain.ConfigDriven.Cash2Pct                                16.6%
  PatternKit.Examples.Chain.ConfigDriven.CashTender                              90.9%
  PatternKit.Examples.Chain.ConfigDriven.CharityRoundUp                           100%
  PatternKit.Examples.Chain.ConfigDriven.ConfigDrivenPipelineBuilderExtensions   94.2%
  PatternKit.Examples.Chain.ConfigDriven.ConfigDrivenPipelineDemo                 100%
  PatternKit.Examples.Chain.ConfigDriven.Loyalty5Pct                               20%
  PatternKit.Examples.Chain.ConfigDriven.NickelCashOnly                          77.7%
  PatternKit.Examples.Chain.ConfigDriven.PipelineOptions                          100%
  PatternKit.Examples.Chain.Customer                                              100%
  PatternKit.Examples.Chain.DeviceBus                                             100%
  PatternKit.Examples.Chain.GenericProcessor                                      100%
  PatternKit.Examples.Chain.HttpRequest                                           100%
  PatternKit.Examples.Chain.IRoundingRule                                         100%
  PatternKit.Examples.Chain.LineItem                                              100%
  PatternKit.Examples.Chain.MediatedTransactionPipelineDemo                       100%
  PatternKit.Examples.Chain.NickelCashOnlyRule                                    100%
  PatternKit.Examples.Chain.NoopCharityTracker                                      0%
  PatternKit.Examples.Chain.RoundingPipeline                                      100%
  PatternKit.Examples.Chain.Tender                                                100%
  PatternKit.Examples.Chain.TenderRouterFactory                                  91.3%
  PatternKit.Examples.Chain.TransactionContext                                    100%
  PatternKit.Examples.Chain.TransactionPipeline                                   100%
  PatternKit.Examples.Chain.TransactionPipelineBuilder                           92.3%
  PatternKit.Examples.Chain.TxResult                                              100%
  PatternKit.Examples.FacadeDemo.FacadeDemo                                      62.9%
  PatternKit.Examples.FlyweightDemo.FlyweightDemo                                96.9%
  PatternKit.Examples.Generators.IntParser                                         60%
  PatternKit.Examples.Generators.OrderRouter                                     95.2%
  PatternKit.Examples.Generators.ScoreLabeler                                     100%
  PatternKit.Examples.MediatorDemo.AppMediator                                    100%
  PatternKit.Examples.MediatorDemo.AuditLogHandler                                100%
  PatternKit.Examples.MediatorDemo.BoxHelper                                       25%
  PatternKit.Examples.MediatorDemo.CountUpCmd                                     100%
  PatternKit.Examples.MediatorDemo.CountUpHandler                                 100%
  PatternKit.Examples.MediatorDemo.EchoCmd                                        100%
  PatternKit.Examples.MediatorDemo.EchoHandler                                    100%
  PatternKit.Examples.MediatorDemo.LoggingBehavior<T1, T2>                        100%
  PatternKit.Examples.MediatorDemo.MediatorAssemblyScanner                        100%
  PatternKit.Examples.MediatorDemo.MediatorDemoSink                               100%
  PatternKit.Examples.MediatorDemo.MediatorRegistry                               100%
  PatternKit.Examples.MediatorDemo.PingCmd                                        100%
  PatternKit.Examples.MediatorDemo.PingHandler                                    100%
  PatternKit.Examples.MediatorDemo.ServiceCollectionExtensions                   83.3%
  PatternKit.Examples.MediatorDemo.SumCmd                                         100%
  PatternKit.Examples.MediatorDemo.SumCmdBehavior                                 100%
  PatternKit.Examples.MediatorDemo.SumHandler                                     100%
  PatternKit.Examples.MediatorDemo.UserCreated                                    100%
  PatternKit.Examples.MediatorDemo.WelcomeEmailHandler                            100%
  PatternKit.Examples.MementoDemo.MementoDemo                                    83.5%
  PatternKit.Examples.ObserverDemo.EventHub<T>                                      0%
  PatternKit.Examples.ObserverDemo.LineItem                                       100%
  PatternKit.Examples.ObserverDemo.ObservableList<T>                               47%
  PatternKit.Examples.ObserverDemo.ObservableVar<T>                               100%
  PatternKit.Examples.ObserverDemo.ProfileViewModel                               100%
  PatternKit.Examples.ObserverDemo.PropertyChangedHub                             100%
  PatternKit.Examples.ObserverDemo.ReactiveTransaction                           95.4%
  PatternKit.Examples.ObserverDemo.UserEvent                                        0%
  PatternKit.Examples.PointOfSale.CustomerInfo                                    100%
  PatternKit.Examples.PointOfSale.Demo                                              0%
  PatternKit.Examples.PointOfSale.OrderLineItem                                   100%
  PatternKit.Examples.PointOfSale.PaymentProcessorDemo                           94.8%
  PatternKit.Examples.PointOfSale.PaymentReceipt                                  100%
  PatternKit.Examples.PointOfSale.PromotionConfig                                 100%
  PatternKit.Examples.PointOfSale.PurchaseOrder                                   100%
  PatternKit.Examples.PointOfSale.ReceiptLineItem                                 100%
  PatternKit.Examples.PointOfSale.StoreLocation                                   100%
  PatternKit.Examples.Pricing.ApiPricingSource                                    100%
  PatternKit.Examples.Pricing.CharityRoundUpRule                                  100%
  PatternKit.Examples.Pricing.Coupon                                              100%
  PatternKit.Examples.Pricing.DbPricingSource                                     100%
  PatternKit.Examples.Pricing.DefaultSourceRouting                               83.3%
  PatternKit.Examples.Pricing.FilePricingSource                                   100%
  PatternKit.Examples.Pricing.LineItem                                            100%
  PatternKit.Examples.Pricing.Location                                            100%
  PatternKit.Examples.Pricing.LoyaltyMembership                                   100%
  PatternKit.Examples.Pricing.NickelCashOnlyRule                                  100%
  PatternKit.Examples.Pricing.PercentLoyaltyRule                                  100%
  PatternKit.Examples.Pricing.PricingContext                                      100%
  PatternKit.Examples.Pricing.PricingDemo                                        56.7%
  PatternKit.Examples.Pricing.PricingPipeline                                     100%
  PatternKit.Examples.Pricing.PricingPipelineBuilder                              100%
  PatternKit.Examples.Pricing.PricingResult                                       100%
  PatternKit.Examples.Pricing.RegionCategoryTaxPolicy                             100%
  PatternKit.Examples.Pricing.Sku                                                85.7%
  PatternKit.Examples.Pricing.SourceRouter                                       92.3%
  PatternKit.Examples.ProxyDemo.ProxyDemo                                        94.5%
  PatternKit.Examples.Singleton.DeviceRegistry                                    100%
  PatternKit.Examples.Singleton.PosAppState                                       100%
  PatternKit.Examples.Singleton.PosAppStateDemo                                   100%
  PatternKit.Examples.Singleton.PricingCache                                      100%
  PatternKit.Examples.Singleton.StoreConfig                                       100%
  PatternKit.Examples.StateDemo.OrderStateDemo                                    100%
  PatternKit.Examples.Strategies.Coercion.Coercer<T>                             83.3%
  PatternKit.Examples.Strategies.Coercion.CoercerExtensions                       100%
  PatternKit.Examples.Strategies.Composed.ChannelPolicy                           100%
  PatternKit.Examples.Strategies.Composed.ChannelPolicyFactory                    100%
  PatternKit.Examples.Strategies.Composed.ComposedStrategies                     94.2%
  PatternKit.Examples.Strategies.Composed.SendContext                             100%
  PatternKit.Examples.Strategies.Composed.SendResult                              100%

PatternKit.Generators                                                            88.2%
  PatternKit.Generators.GenerateStrategyAttribute                                   0%
  PatternKit.Generators.StrategyGenerator                                        93.9%

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Oct 11, 2025

Codecov Report

❌ Patch coverage is 84.38662% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.51%. Comparing base (61a4dc0) to head (ca9ae05).
⚠️ Report is 82 commits behind head on main.

Files with missing lines Patch % Lines
...ternKit.Core/Behavioral/State/AsyncStateMachine.cs 77.00% 23 Missing ⚠️
...c/PatternKit.Core/Behavioral/State/StateMachine.cs 83.00% 17 Missing ⚠️
...tternKit.Examples/AsyncStateDemo/AsyncStateDemo.cs 93.54% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #55      +/-   ##
==========================================
+ Coverage   77.39%   84.51%   +7.12%     
==========================================
  Files          60       64       +4     
  Lines        4472     4741     +269     
  Branches      678      744      +66     
==========================================
+ Hits         3461     4007     +546     
+ Misses        767      734      -33     
+ Partials      244        0     -244     
Flag Coverage Δ
unittests 84.51% <84.38%> (+7.12%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JerrettDavis JerrettDavis merged commit beda683 into main Oct 12, 2025
4 checks passed
@JerrettDavis JerrettDavis deleted the feature/25-fluent-create-state-pattern branch October 12, 2025 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fluent: Create State Pattern

3 participants