|
| 1 | +using GsdOrchestrator.Checkpointing; |
| 2 | +using GsdOrchestrator.Mcp; |
| 3 | +using GsdOrchestrator.Workflows; |
| 4 | +using GsdOrchestrator.Workflows.Models; |
| 5 | +using GsdOrchestrator.Workflows.States; |
| 6 | +using Microsoft.Extensions.Logging.Abstractions; |
| 7 | +using NSubstitute; |
| 8 | +using NSubstitute.ExceptionExtensions; |
| 9 | +using Polly.Registry; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace GsdOrchestrator.Tests; |
| 13 | + |
| 14 | +public sealed class FaultInjectionTests |
| 15 | +{ |
| 16 | + private static GsdStateMachine BuildSut( |
| 17 | + ICheckpointStore checkpoints, |
| 18 | + IWorkflowState[] states, |
| 19 | + IMcpClient? mcpClient = null) |
| 20 | + { |
| 21 | + var client = mcpClient ?? Substitute.For<IMcpClient>(); |
| 22 | + var registry = new ResiliencePipelineRegistry<string>(); |
| 23 | + registry.TryAddBuilder("mcp-tools", (b, _) => { }); |
| 24 | + var dispatcher = new McpToolDispatcher( |
| 25 | + client, |
| 26 | + registry, |
| 27 | + NullLogger<McpToolDispatcher>.Instance); |
| 28 | + return new GsdStateMachine( |
| 29 | + checkpoints, |
| 30 | + dispatcher, |
| 31 | + states, |
| 32 | + NullLogger<GsdStateMachine>.Instance); |
| 33 | + } |
| 34 | + |
| 35 | + private static IWorkflowState MakeTransitionState(WorkflowState from, WorkflowState to) |
| 36 | + { |
| 37 | + var state = Substitute.For<IWorkflowState>(); |
| 38 | + state.State.Returns(from); |
| 39 | + state.ExecuteAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 40 | + .Returns(ci => Task.FromResult(ci.Arg<GsdWorkflowContext>().Transition(to))); |
| 41 | + return state; |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public async Task RunAsync_McpFailureMidGoal_ProducesTypedFailedStateAndCheckpoint() |
| 46 | + { |
| 47 | + var checkpoints = Substitute.For<ICheckpointStore>(); |
| 48 | + checkpoints.SaveAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 49 | + .Returns(Task.CompletedTask); |
| 50 | + |
| 51 | + var idle = MakeTransitionState(WorkflowState.Idle, WorkflowState.Analyzing); |
| 52 | + var analyzing = MakeTransitionState(WorkflowState.Analyzing, WorkflowState.Editing); |
| 53 | + var editing = Substitute.For<IWorkflowState>(); |
| 54 | + editing.State.Returns(WorkflowState.Editing); |
| 55 | + editing.ExecuteAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 56 | + .ThrowsAsync(new McpException("simulated MCP failure", isTransient: true)); |
| 57 | + |
| 58 | + var sut = BuildSut(checkpoints, [idle, analyzing, editing]); |
| 59 | + |
| 60 | + var ctx = await sut.RunAsync("owner", "repo", 42, triageModeOnly: false, CancellationToken.None); |
| 61 | + |
| 62 | + Assert.Equal(WorkflowState.Failed, ctx.CurrentState); |
| 63 | + Assert.Equal(WorkflowState.Editing, ctx.FailedState); |
| 64 | + Assert.Contains("simulated MCP failure", ctx.FailureReason); |
| 65 | + await checkpoints.Received().SaveAsync( |
| 66 | + Arg.Is<GsdWorkflowContext>(saved => |
| 67 | + saved.CurrentState == WorkflowState.Failed && |
| 68 | + saved.FailedState == WorkflowState.Editing && |
| 69 | + saved.FailureReason != null && |
| 70 | + saved.FailureReason.Contains("simulated MCP failure", StringComparison.Ordinal)), |
| 71 | + Arg.Any<CancellationToken>()); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public async Task ResumeAsync_RetriesFailedStateOnce_ThenHaltsDeterministically() |
| 76 | + { |
| 77 | + var checkpoints = Substitute.For<ICheckpointStore>(); |
| 78 | + checkpoints.SaveAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 79 | + .Returns(Task.CompletedTask); |
| 80 | + |
| 81 | + var firstCheckpoint = FailedCheckpoint("retry-wf", retryCount: 0); |
| 82 | + var secondCheckpoint = FailedCheckpoint("retry-wf", retryCount: 1); |
| 83 | + checkpoints.LoadAsync("retry-wf", Arg.Any<CancellationToken>()) |
| 84 | + .Returns(firstCheckpoint, secondCheckpoint); |
| 85 | + |
| 86 | + var editing = Substitute.For<IWorkflowState>(); |
| 87 | + editing.State.Returns(WorkflowState.Editing); |
| 88 | + editing.ExecuteAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 89 | + .ThrowsAsync(new McpException("editing exploded again", isTransient: true)); |
| 90 | + |
| 91 | + var sut = BuildSut(checkpoints, [editing]); |
| 92 | + |
| 93 | + var firstResult = await sut.ResumeAsync("retry-wf", CancellationToken.None); |
| 94 | + |
| 95 | + Assert.Equal(WorkflowState.Failed, firstResult.CurrentState); |
| 96 | + Assert.Equal(1, firstResult.RetryCount); |
| 97 | + Assert.Equal(WorkflowState.Editing, firstResult.FailedState); |
| 98 | + Assert.Contains("editing exploded again", firstResult.FailureReason); |
| 99 | + await checkpoints.Received().SaveAsync( |
| 100 | + Arg.Is<GsdWorkflowContext>(saved => |
| 101 | + saved.CurrentState == WorkflowState.Failed && |
| 102 | + saved.RetryCount == 1 && |
| 103 | + saved.FailedState == WorkflowState.Editing), |
| 104 | + Arg.Any<CancellationToken>()); |
| 105 | + |
| 106 | + var error = await Assert.ThrowsAsync<InvalidOperationException>( |
| 107 | + () => sut.ResumeAsync("retry-wf", CancellationToken.None)); |
| 108 | + Assert.Contains("recovery retry limit", error.Message, StringComparison.OrdinalIgnoreCase); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public async Task RunAsync_TruncatesFailureReasonTo1024Characters() |
| 113 | + { |
| 114 | + var checkpoints = Substitute.For<ICheckpointStore>(); |
| 115 | + checkpoints.SaveAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 116 | + .Returns(Task.CompletedTask); |
| 117 | + |
| 118 | + var oversizedMessage = new string('x', 1200); |
| 119 | + var idle = Substitute.For<IWorkflowState>(); |
| 120 | + idle.State.Returns(WorkflowState.Idle); |
| 121 | + idle.ExecuteAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 122 | + .ThrowsAsync(new InvalidOperationException(oversizedMessage)); |
| 123 | + |
| 124 | + var sut = BuildSut(checkpoints, [idle]); |
| 125 | + |
| 126 | + var ctx = await sut.RunAsync("owner", "repo", 7, triageModeOnly: false, CancellationToken.None); |
| 127 | + |
| 128 | + Assert.Equal(WorkflowState.Failed, ctx.CurrentState); |
| 129 | + Assert.NotNull(ctx.FailureReason); |
| 130 | + Assert.Equal(1024, ctx.FailureReason!.Length); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public async Task RunAsync_BudgetFailureMessage_SetsBudgetExhaustedStopReason() |
| 135 | + { |
| 136 | + var checkpoints = Substitute.For<ICheckpointStore>(); |
| 137 | + checkpoints.SaveAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 138 | + .Returns(Task.CompletedTask); |
| 139 | + |
| 140 | + var idle = Substitute.For<IWorkflowState>(); |
| 141 | + idle.State.Returns(WorkflowState.Idle); |
| 142 | + idle.ExecuteAsync(Arg.Any<GsdWorkflowContext>(), Arg.Any<CancellationToken>()) |
| 143 | + .ThrowsAsync(new InvalidOperationException("budget exhausted during planning")); |
| 144 | + |
| 145 | + var sut = BuildSut(checkpoints, [idle]); |
| 146 | + |
| 147 | + var ctx = await sut.RunAsync("owner", "repo", 8, triageModeOnly: false, CancellationToken.None); |
| 148 | + |
| 149 | + Assert.Equal(WorkflowState.Failed, ctx.CurrentState); |
| 150 | + Assert.Equal(TerminalStopReason.BudgetExhausted, ctx.StopReason); |
| 151 | + } |
| 152 | + |
| 153 | + private static GsdWorkflowContext FailedCheckpoint(string workflowId, int retryCount) => |
| 154 | + new() |
| 155 | + { |
| 156 | + WorkflowId = workflowId, |
| 157 | + CurrentState = WorkflowState.Failed, |
| 158 | + FailedState = WorkflowState.Editing, |
| 159 | + RetryCount = retryCount, |
| 160 | + FailureReason = "prior failure", |
| 161 | + Issue = new IssueContext(1, "Retry workflow", "", [], "owner", "repo", "main") |
| 162 | + }; |
| 163 | +} |
0 commit comments