fix(notify,esg): classify dropped SMTP connections, OAuth2 token-endpoint outages and body-read connection loss as transient (NTF-2, ESG-1, WP4 task 4.6)

This commit is contained in:
Joseph Doherty
2026-12-03 09:58:10 -05:00
parent 5f7b850af2
commit 11992075e8
8 changed files with 210 additions and 14 deletions
@@ -159,6 +159,55 @@ public class ExternalSystemClientTests : IDisposable
Assert.Contains("Transient error", result.ErrorMessage);
}
/// <summary>
/// ESG-1 regression: the external system sends 200 headers, then drops the connection
/// mid-body. The call is a transient failure — not a raw IOException escaping into the
/// script.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Call_ConnectionLostReadingBody_ReturnsTransientError()
{
var system = new ExternalSystemDefinition("TestAPI", "https://api.example.com", "none") { Id = 1 };
var method = new ExternalSystemMethod("getData", "GET", "/data") { Id = 1, ExternalSystemDefinitionId = 1 };
StubResolution(system, method);
var httpClient = new HttpClient(new BodyFailsHandler(HttpStatusCode.OK, new IOException("connection reset mid-body")));
_httpClientFactory.CreateClient(Arg.Any<string>()).Returns(httpClient);
var client = new ExternalSystemClient(
_httpClientFactory, _repository,
NullLogger<ExternalSystemClient>.Instance);
var result = await client.CallAsync("TestAPI", "getData");
Assert.False(result.Success);
Assert.Contains("Transient error", result.ErrorMessage);
}
/// <summary>ESG-1: a permanent status whose error body is unreadable stays permanent.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Call_Permanent400WithUnreadableBody_StaysPermanent()
{
var system = new ExternalSystemDefinition("TestAPI", "https://api.example.com", "none") { Id = 1 };
var method = new ExternalSystemMethod("getData", "GET", "/data") { Id = 1, ExternalSystemDefinitionId = 1 };
StubResolution(system, method);
var httpClient = new HttpClient(new BodyFailsHandler(HttpStatusCode.BadRequest, new HttpIOException(HttpRequestError.ResponseEnded)));
_httpClientFactory.CreateClient(Arg.Any<string>()).Returns(httpClient);
var client = new ExternalSystemClient(
_httpClientFactory, _repository,
NullLogger<ExternalSystemClient>.Instance);
var result = await client.CallAsync("TestAPI", "getData");
Assert.False(result.Success);
Assert.DoesNotContain("Transient error", result.ErrorMessage);
Assert.Contains("400", result.ErrorMessage);
}
/// <summary>Verifies that an HTTP 400 response is returned as a permanent error.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
@@ -1239,6 +1288,30 @@ public class ExternalSystemClientTests : IDisposable
/// <summary>
/// Test helper: mock HTTP message handler.
/// </summary>
/// <summary>Answers with the given status, then fails the body read with <c>error</c>.</summary>
private sealed class BodyFailsHandler(HttpStatusCode statusCode, Exception error) : HttpMessageHandler
{
/// <inheritdoc />
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
=> Task.FromResult(new HttpResponseMessage(statusCode) { Content = new StreamContent(new FailingStream(error)) });
private sealed class FailingStream(Exception error) : Stream
{
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
public override long Position { get => 0; set => throw new NotSupportedException(); }
public override void Flush() { }
public override int Read(byte[] buffer, int offset, int count) => throw error;
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
=> ValueTask.FromException<int>(error);
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
}
}
private class MockHttpMessageHandler : HttpMessageHandler
{
private readonly HttpStatusCode _statusCode;