|
| 1 | +using System.Text.Json.Nodes; |
| 2 | +using GsdOrchestrator.Checkpointing; |
| 3 | +using GsdOrchestrator.Mcp; |
| 4 | +using GsdOrchestrator.Workflows; |
| 5 | +using GsdOrchestrator.Workflows.Models; |
| 6 | +using GsdOrchestrator.Workflows.States; |
| 7 | +using Microsoft.Extensions.Logging.Abstractions; |
| 8 | +using NSubstitute; |
| 9 | +using Polly.Registry; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace GsdOrchestrator.Tests; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Additional tests covering GsdStateMachine gaps: GetState, PostFailureCommentAsync. |
| 16 | +/// </summary> |
| 17 | +public class GsdStateMachineAdditionalTests |
| 18 | +{ |
| 19 | + private static GsdStateMachine BuildSut( |
| 20 | + ICheckpointStore checkpoints, |
| 21 | + IWorkflowState[] states, |
| 22 | + IMcpClient? mcpClient = null) |
| 23 | + { |
| 24 | + var client = mcpClient ?? Substitute.For<IMcpClient>(); |
| 25 | + var registry = new ResiliencePipelineRegistry<string>(); |
| 26 | + registry.TryAddBuilder("mcp-tools", (b, _) => { }); |
| 27 | + var dispatcher = new McpToolDispatcher( |
| 28 | + client, registry, NullLogger<McpToolDispatcher>.Instance); |
| 29 | + return new GsdStateMachine( |
| 30 | + checkpoints, dispatcher, states, NullLogger<GsdStateMachine>.Instance); |
| 31 | + } |
| 32 | + |
| 33 | + private static IWorkflowState MakeState(WorkflowState from, WorkflowState to) |
| 34 | + { |
| 35 | + var s = Substitute.For<IWorkflowState>(); |
| 36 | + s.State.Returns(from); |
| 37 | + s.ExecuteAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 38 | + .Returns(ci => Task.FromResult(ci.Arg<GsdWorkflowContext>() with { CurrentState = to })); |
| 39 | + return s; |
| 40 | + } |
| 41 | + |
| 42 | + // SM-GETSTATE-01: GetState returns registered state handler |
| 43 | + [Fact] |
| 44 | + public void GetState_RegisteredState_ReturnsHandler() |
| 45 | + { |
| 46 | + var checkpoints = Substitute.For<ICheckpointStore>(); |
| 47 | + var idleState = MakeState(WorkflowState.Idle, WorkflowState.Done); |
| 48 | + var sut = BuildSut(checkpoints, [idleState]); |
| 49 | + |
| 50 | + var handler = sut.GetState(WorkflowState.Idle); |
| 51 | + |
| 52 | + Assert.NotNull(handler); |
| 53 | + Assert.Equal(WorkflowState.Idle, handler.State); |
| 54 | + } |
| 55 | + |
| 56 | + // SM-GETSTATE-02: GetState for unregistered state throws InvalidOperationException |
| 57 | + [Fact] |
| 58 | + public void GetState_UnregisteredState_ThrowsInvalidOperationException() |
| 59 | + { |
| 60 | + var checkpoints = Substitute.For<ICheckpointStore>(); |
| 61 | + var sut = BuildSut(checkpoints, []); |
| 62 | + |
| 63 | + var ex = Assert.Throws<InvalidOperationException>( |
| 64 | + () => sut.GetState(WorkflowState.Idle)); |
| 65 | + Assert.Contains("Idle", ex.Message); |
| 66 | + } |
| 67 | + |
| 68 | + // SM-FAILURE-01: on failure, add_issue_comment is called with failure details |
| 69 | + [Fact] |
| 70 | + public async Task RunAsync_StateThrowsException_PostsFailureComment() |
| 71 | + { |
| 72 | + var checkpoints = Substitute.For<ICheckpointStore>(); |
| 73 | + checkpoints.SaveAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 74 | + .Returns(Task.CompletedTask); |
| 75 | + |
| 76 | + var mcp = Substitute.For<IMcpClient>(); |
| 77 | + mcp.CallToolAsync(Arg.Any<string>(), Arg.Any<JsonObject>(), Arg.Any<CancellationToken>()) |
| 78 | + .Returns(Task.FromResult(new McpToolResult("", false))); |
| 79 | + |
| 80 | + var failingState = Substitute.For<IWorkflowState>(); |
| 81 | + failingState.State.Returns(WorkflowState.Idle); |
| 82 | + failingState.ExecuteAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 83 | + .Returns<Task<GsdWorkflowContext>>(_ => throw new InvalidOperationException("boom")); |
| 84 | + |
| 85 | + var sut = BuildSut(checkpoints, [failingState], mcp); |
| 86 | + |
| 87 | + var ctx = await sut.RunAsync("owner", "repo", 1, triageModeOnly: false, CancellationToken.None); |
| 88 | + |
| 89 | + Assert.Equal(WorkflowState.Failed, ctx.CurrentState); |
| 90 | + // Verify add_issue_comment was called for the failure notification |
| 91 | + await mcp.Received().CallToolAsync( |
| 92 | + Arg.Is<string>("add_issue_comment"), |
| 93 | + Arg.Any<JsonObject>(), |
| 94 | + Arg.Any<CancellationToken>()); |
| 95 | + } |
| 96 | + |
| 97 | + // SM-FAILURE-02: when Issue is null, PostFailureCommentAsync is a no-op (no MCP call) |
| 98 | + [Fact] |
| 99 | + public async Task RunAsync_FailureWithNullIssue_DoesNotCallMcp() |
| 100 | + { |
| 101 | + // Create a state machine with NO states and override so failure happens with no issue context |
| 102 | + var checkpoints = Substitute.For<ICheckpointStore>(); |
| 103 | + checkpoints.SaveAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 104 | + .Returns(Task.CompletedTask); |
| 105 | + |
| 106 | + var mcp = Substitute.For<IMcpClient>(); |
| 107 | + |
| 108 | + var failingState = Substitute.For<IWorkflowState>(); |
| 109 | + failingState.State.Returns(WorkflowState.Idle); |
| 110 | + failingState.ExecuteAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 111 | + .Returns<Task<GsdWorkflowContext>>(ci => |
| 112 | + { |
| 113 | + // Return a failed context with no issue |
| 114 | + var ctx = ci.Arg<GsdWorkflowContext>() with |
| 115 | + { |
| 116 | + Issue = null, |
| 117 | + FailureReason = "no issue", |
| 118 | + CurrentState = WorkflowState.Failed |
| 119 | + }; |
| 120 | + return Task.FromResult(ctx); |
| 121 | + }); |
| 122 | + |
| 123 | + var sut = BuildSut(checkpoints, [failingState], mcp); |
| 124 | + var result = await sut.RunAsync("owner", "repo", 1, triageModeOnly: false, CancellationToken.None); |
| 125 | + |
| 126 | + Assert.Equal(WorkflowState.Failed, result.CurrentState); |
| 127 | + // No add_issue_comment should be fired when Issue is null |
| 128 | + await mcp.DidNotReceive().CallToolAsync( |
| 129 | + Arg.Is<string>("add_issue_comment"), |
| 130 | + Arg.Any<JsonObject>(), |
| 131 | + Arg.Any<CancellationToken>()); |
| 132 | + } |
| 133 | + |
| 134 | + // SM-FAILURE-03: PostFailureCommentAsync McpException is swallowed (logged, not rethrown) |
| 135 | + [Fact] |
| 136 | + public async Task RunAsync_FailureCommentThrows_DoesNotRethrow() |
| 137 | + { |
| 138 | + var checkpoints = Substitute.For<ICheckpointStore>(); |
| 139 | + checkpoints.SaveAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 140 | + .Returns(Task.CompletedTask); |
| 141 | + |
| 142 | + var mcp = Substitute.For<IMcpClient>(); |
| 143 | + mcp.CallToolAsync(Arg.Any<string>(), Arg.Any<JsonObject>(), Arg.Any<CancellationToken>()) |
| 144 | + .Returns<Task<McpToolResult>>(_ => throw new McpException("mcp dead", isTransient: false)); |
| 145 | + |
| 146 | + var failingState = Substitute.For<IWorkflowState>(); |
| 147 | + failingState.State.Returns(WorkflowState.Idle); |
| 148 | + failingState.ExecuteAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 149 | + .Returns<Task<GsdWorkflowContext>>(_ => throw new InvalidOperationException("state exploded")); |
| 150 | + |
| 151 | + var sut = BuildSut(checkpoints, [failingState], mcp); |
| 152 | + |
| 153 | + // Should not throw even when MCP call for comment also fails |
| 154 | + var ctx = await sut.RunAsync("owner", "repo", 1, triageModeOnly: false, CancellationToken.None); |
| 155 | + Assert.Equal(WorkflowState.Failed, ctx.CurrentState); |
| 156 | + } |
| 157 | +} |
0 commit comments