feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes

This commit is contained in:
Yun Chan 2026-11-14 11:51:41 +09:00
parent 01ef3293cf
commit 66b6b45ba9
399 changed files with 17828 additions and 11330 deletions

View file

@ -91,6 +91,24 @@ public sealed class SttControllerSecurityTests
Assert.Equal("edge-internal", service.LastUserEmail);
}
[Fact]
public async Task InternalGateway_PropagatesRequestAbortedTokenToSttService()
{
var service = new RecordingSttService();
using var cts = new CancellationTokenSource();
var controller = CreateController(
service,
null,
isAuthenticated: false,
suppliedGatewayToken: GatewayToken,
includeAudioForm: true,
requestAborted: cts.Token);
await controller.TranscribeFromQuotaGateway();
Assert.Equal(cts.Token, service.LastCancellationToken);
}
private static SttController CreateController(
RecordingSttService service,
string? userId,
@ -98,7 +116,8 @@ public sealed class SttControllerSecurityTests
string? email = null,
string configuredGatewayToken = GatewayToken,
string? suppliedGatewayToken = null,
bool includeAudioForm = false)
bool includeAudioForm = false,
CancellationToken requestAborted = default)
{
var claims = new List<Claim>();
if (userId != null) claims.Add(new Claim(ClaimTypes.NameIdentifier, userId));
@ -106,7 +125,8 @@ public sealed class SttControllerSecurityTests
var identity = new ClaimsIdentity(claims, isAuthenticated ? "test-auth" : null);
var context = new DefaultHttpContext
{
User = new ClaimsPrincipal(identity)
User = new ClaimsPrincipal(identity),
RequestAborted = requestAborted
};
if (includeAudioForm)
{
@ -152,6 +172,7 @@ public sealed class SttControllerSecurityTests
public int LastUserId { get; private set; }
public string? LastUserEmail { get; private set; }
public bool LastRecordUsage { get; private set; } = true;
public CancellationToken LastCancellationToken { get; private set; }
public Task<SttTranscribeResponse> TranscribeAsync(
int userId,
@ -160,19 +181,25 @@ public sealed class SttControllerSecurityTests
byte[]? audioBytes = null,
string? contentType = null,
string? fileName = null,
bool recordUsage = true)
bool recordUsage = true,
CancellationToken cancellationToken = default)
{
TranscribeCalls++;
LastUserId = userId;
LastUserEmail = userEmail;
LastRecordUsage = recordUsage;
LastCancellationToken = cancellationToken;
if (Exception != null) throw Exception;
return Task.FromResult(new SttTranscribeResponse(
"real transcript", 0.99, "ko", 1, "test", "test-model", 1, 0));
}
public Task<SttTestResultDto> TestEndpointAsync(int endpointId, string? testApiKey = null, string? testEndpointUrl = null) =>
public Task<SttTestResultDto> TestEndpointAsync(
int endpointId,
string? testApiKey = null,
string? testEndpointUrl = null,
CancellationToken cancellationToken = default) =>
throw new NotSupportedException();
public Task<List<SttProviderEndpointDto>> GetAllEndpointsAsync() =>

View file

@ -121,6 +121,108 @@ public sealed class SttFailClosedTests
Assert.DoesNotContain(apiKey, fixture.Handler.LastRequestUri, StringComparison.Ordinal);
}
[Fact]
public async Task OversizedAudioBase64_IsRejectedBeforeDecodingOrCallingProvider()
{
await using var fixture = await SttFixture.CreateAsync(
apiKey: "configured-key",
_ => throw new InvalidOperationException("HTTP should not be called"));
// Over 26 MiB base64 payload (~36.4M chars), prefixed like a browser data URL.
var oversizedPayload = "data:audio/wav;base64," + new string('A', 36_400_000);
await Assert.ThrowsAsync<ArgumentException>(() =>
fixture.Service.TranscribeAsync(
13,
"user@example.test",
new SttTranscribeRequest(AudioBase64: oversizedPayload, Language: "ko")));
Assert.Equal(0, fixture.Handler.CallCount);
Assert.Empty(await fixture.Db.SttUsageLogs.ToListAsync());
}
[Fact]
public async Task ClientCancellation_StopsFailoverChainBeforeCallingFallbackProvider()
{
using var cts = new CancellationTokenSource();
await using var fixture = await SttFixture.CreateAsync(
apiKey: "configured-key",
req =>
{
cts.Cancel();
throw new OperationCanceledException(cts.Token);
},
providerType: "groq",
endpointUrl: "https://primary.example.test/transcriptions");
fixture.Db.SttProviderEndpoints.Add(new SttProviderEndpoint
{
Name = "Backup provider",
ProviderType = "openai",
EndpointUrl = "https://fallback.example.test/transcriptions",
ApiKey = "backup-key",
ModelId = "whisper-backup",
Method = "multipart",
Language = "ko",
IsDefault = false,
IsActive = true,
FallbackPriority = 2
});
await fixture.Db.SaveChangesAsync();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
fixture.Service.TranscribeAsync(
14,
"user@example.test",
new SttTranscribeRequest(Language: "ko"),
new byte[] { 1, 2, 3 },
"audio/webm",
"recording.webm",
cancellationToken: cts.Token));
Assert.Equal(1, fixture.Handler.CallCount);
Assert.Empty(await fixture.Db.ErrorLogs.ToListAsync());
Assert.Empty(await fixture.Db.SttUsageLogs.ToListAsync());
}
[Fact]
public async Task LlmProxyService_ClientCancellation_PropagatesWithoutMockFallback()
{
await using var fixture = await SttFixture.CreateAsync(
apiKey: "unused",
_ => throw new OperationCanceledException());
fixture.Db.ModelEndpoints.Add(new ServiceModelEndpoint
{
ModelId = "remote-llm",
ModelName = "Remote LLM",
Provider = "OpenAI",
EndpointUrl = "https://llm.example.test/v1/chat/completions",
ApiKey = "configured-llm-key",
CostPer1kPromptTokens = 0.001m,
CostPer1kCompletionTokens = 0.002m,
IsActive = true
});
await fixture.Db.SaveChangesAsync();
using var cts = new CancellationTokenSource();
cts.Cancel();
var llmService = new LlmProxyService(
fixture.Db,
new StubHttpClientFactory(fixture.Handler),
NullLogger<LlmProxyService>.Instance);
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
llmService.GenerateAsync(
15,
"user@example.test",
new LlmGenerateRequest("Hello", Model: "remote-llm"),
cts.Token));
Assert.Equal(0, fixture.Handler.CallCount);
Assert.Empty(await fixture.Db.UsageLogs.ToListAsync());
Assert.Empty(await fixture.Db.ErrorLogs.ToListAsync());
}
private sealed class SttFixture : IAsyncDisposable
{
private readonly SqliteConnection _connection;