Path: redb-route/tests/redb.Route.Tests.Llm/TestHelpers/StreamingFakeProvider.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Llm/TestHelpers/StreamingFakeProvider.cs
----
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
namespace redb.Route.Tests.Llm.TestHelpers;
///
/// Plays a script as a stream: thinking and text word by word, then the last chunk with the
/// whole answer (). Counts which kind of call the engine made.
///
public sealed class StreamingFakeProvider(FakeProvider script) : ILlmProvider
{
private int _streamCalls;
private int _plainCalls;
/// Streamed calls made so far.
public int StreamCalls => Volatile.Read(ref _streamCalls);
/// Plain calls made so far.
public int PlainCalls => Volatile.Read(ref _plainCalls);
///
public string ProviderId => "fake-stream";
///
public string ModelId => script.ModelId;
///
public Task CompleteAsync(LlmRequest request, CancellationToken ct = default)
{
Interlocked.Increment(ref _plainCalls);
return script.CompleteAsync(request, ct);
}
///
public async IAsyncEnumerable StreamAsync(
LlmRequest request, [EnumeratorCancellation] CancellationToken ct = default)
{
Interlocked.Increment(ref _streamCalls);
var response = await script.CompleteAsync(request, ct);
foreach (var block in response.Content)
{
if (block is LlmThinkingBlock thinking)
foreach (var piece in Words(thinking.Text))
yield return new LlmStreamChunk([new LlmThinkingBlock(piece)], null, null);
else if (block is LlmTextBlock text)
foreach (var piece in Words(text.Text))
yield return new LlmStreamChunk([new LlmTextBlock(piece)], null, null);
}
yield return new LlmStreamChunk(
[.. response.Content.OfType()], response.StopReason, response.Usage)
{
Response = response
};
}
private static IEnumerable Words(string text) =>
Regex.Split(text, "(?<= )").Where(w => w.Length > 0);
}