File: parent\Microsoft.Extensions.AI.Abstractions.Tests\TestRealtimeClientSession.cs
Project: ..\..\..\test\Libraries\Microsoft.Extensions.AI.Tests\Microsoft.Extensions.AI.Tests.csproj (Microsoft.Extensions.AI.Tests)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
 
namespace Microsoft.Extensions.AI;
 
/// <summary>A test <see cref="IRealtimeClientSession"/> implementation that uses callbacks for verification.</summary>
public sealed class TestRealtimeClientSession : IRealtimeClientSession
{
    /// <summary>Gets or sets the callback to invoke when <see cref="SendAsync"/> is called.</summary>
    public Func<RealtimeClientMessage, CancellationToken, Task>? SendAsyncCallback { get; set; }
 
    /// <summary>Gets or sets the callback to invoke when <see cref="GetStreamingResponseAsync"/> is called.</summary>
    public Func<CancellationToken, IAsyncEnumerable<RealtimeServerMessage>>? GetStreamingResponseAsyncCallback { get; set; }
 
    /// <summary>Gets or sets the callback to invoke when <see cref="GetService"/> is called.</summary>
    public Func<Type, object?, object?>? GetServiceCallback { get; set; }
 
    /// <inheritdoc/>
    public RealtimeSessionOptions? Options { get; set; }
 
    /// <inheritdoc/>
    public Task SendAsync(RealtimeClientMessage message, CancellationToken cancellationToken = default)
    {
        return SendAsyncCallback?.Invoke(message, cancellationToken) ?? Task.CompletedTask;
    }
 
    /// <inheritdoc/>
    public IAsyncEnumerable<RealtimeServerMessage> GetStreamingResponseAsync(
        CancellationToken cancellationToken = default)
    {
        return GetStreamingResponseAsyncCallback?.Invoke(cancellationToken) ?? EmptyAsyncEnumerable();
    }
 
    /// <inheritdoc/>
    public object? GetService(Type serviceType, object? serviceKey = null)
    {
        if (GetServiceCallback is { } callback)
        {
            return callback(serviceType, serviceKey);
        }
 
        return serviceKey is null && serviceType.IsInstanceOfType(this) ? this : null;
    }
 
    /// <inheritdoc/>
    public ValueTask DisposeAsync()
    {
        // No-op for test implementation
        return default;
    }
 
    private static async IAsyncEnumerable<RealtimeServerMessage> EmptyAsyncEnumerable()
    {
        await Task.CompletedTask.ConfigureAwait(false);
        yield break;
    }
}