210 lines
13 KiB
Java
210 lines
13 KiB
Java
package com.moyi.controlplane.gateway;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.moyi.controlplane.catalog.CatalogService;
|
|
import com.moyi.controlplane.common.ApiException;
|
|
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
|
import com.moyi.controlplane.task.AsyncTaskService;
|
|
import com.moyi.controlplane.usage.UsageAuditService;
|
|
import com.moyi.controlplane.wallet.ChatBillingService;
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.data.redis.core.script.RedisScript;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.mockito.ArgumentMatchers.*;
|
|
import static org.mockito.Mockito.*;
|
|
|
|
class GatewayDeploymentTest {
|
|
@Test
|
|
void capacitySnapshotReadsBothLiveAndWaitingBucketsAndDoesNotInventZeroOnFailure() {
|
|
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
|
doReturn(List.of(2L, 3L, 0L, 1L)).when(redis).execute(any(RedisScript.class), anyList());
|
|
GatewayGuard guard = new GatewayGuard(redis, 60, 300, 4, "moyi:test:");
|
|
var snapshot = guard.capacitySnapshot(List.of("model-a", "model-b"));
|
|
assertTrue(snapshot.available());
|
|
assertEquals(new GatewayGuard.Capacity(2, 3), snapshot.groups().get("model-a"));
|
|
assertEquals(new GatewayGuard.Capacity(0, 1), snapshot.groups().get("model-b"));
|
|
verify(redis).execute(any(RedisScript.class), eq(List.of(
|
|
"moyi:test:gateway:slots:model:model-a", "moyi:test:gateway:waiting:model:model-a",
|
|
"moyi:test:gateway:slots:model:model-b", "moyi:test:gateway:waiting:model:model-b")));
|
|
doThrow(new org.springframework.dao.DataAccessResourceFailureException("offline")).when(redis).execute(any(RedisScript.class), anyList());
|
|
assertFalse(guard.capacitySnapshot(List.of("model-a")).available());
|
|
assertTrue(guard.capacitySnapshot(List.of("model-a")).groups().isEmpty());
|
|
}
|
|
|
|
@Test
|
|
void queueTimeoutCleansItsWaitingEntry() {
|
|
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
|
doReturn(0L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
|
|
GatewayGuard guard = new GatewayGuard(redis, 60, 300, 1, 1, 15, 10, "moyi:test:");
|
|
assertThrows(GatewayLimitException.class, () -> guard.acquire(Map.of("id", "model-a"), UUID.randomUUID(), 15));
|
|
verify(redis).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:waiting:model:model-a")), any(Object[].class));
|
|
}
|
|
|
|
@Test
|
|
void redisRateKeysAreEnvironmentScoped() {
|
|
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
|
doReturn(0L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
|
|
UUID keyId = UUID.randomUUID();
|
|
new GatewayGuard(redis, 60, 300, 4, "moyi:test:").checkRate(keyId, false);
|
|
verify(redis).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:rate:invoke:" + keyId)), any(Object[].class));
|
|
}
|
|
|
|
@Test
|
|
void keyConcurrencyUsesDedicatedRedisBucket() {
|
|
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
|
doReturn(1L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
|
|
UUID keyId = UUID.randomUUID();
|
|
try (GatewayGuard.Slot ignored = new GatewayGuard(redis, 60, 300, 4, 2, "moyi:test:").acquireKey(keyId)) {
|
|
// The lease is intentionally held for the duration of this assertion.
|
|
}
|
|
verify(redis, atLeastOnce()).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:slots:key:" + keyId)), any(Object[].class));
|
|
}
|
|
|
|
@Test
|
|
void modelsKeepRouteIdAndExposeDisplayMetadata() {
|
|
CatalogService catalog = mock(CatalogService.class);
|
|
GatewayGuard guard = mock(GatewayGuard.class);
|
|
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
|
|
Map<String, Object> route = Map.of(
|
|
"id", "route-1",
|
|
"providerId", "provider-apihub",
|
|
"modelKey", "deepseek-v4-flash",
|
|
"publicName", "DeepSeek V4 Flash",
|
|
"vendorName", "DeepSeek",
|
|
"sourceLabel", "ApiHub 国内",
|
|
"upstreamModel", "deepseek-v4-flash",
|
|
"type", "text",
|
|
"mode", "sync"
|
|
);
|
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
|
when(catalog.listModels(false)).thenReturn(List.of(route));
|
|
when(catalog.keyCanInvoke(key, "route-1")).thenReturn(true);
|
|
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
|
|
|
Map<String, Object> response = controller.models("Bearer test-key");
|
|
@SuppressWarnings("unchecked")
|
|
Map<String, Object> item = ((List<Map<String, Object>>) response.get("data")).getFirst();
|
|
assertEquals("route-1", item.get("id"));
|
|
assertEquals("deepseek-v4-flash", item.get("model_key"));
|
|
assertEquals("DeepSeek V4 Flash", item.get("public_name"));
|
|
assertEquals("DeepSeek", item.get("vendor_name"));
|
|
assertEquals("ApiHub 国内", item.get("source_label"));
|
|
assertEquals("deepseek-v4-flash", item.get("upstream_model"));
|
|
}
|
|
|
|
@Test
|
|
void invalidRedisPrefixIsRejected() {
|
|
assertThrows(IllegalArgumentException.class, () -> new GatewayGuard(mock(StringRedisTemplate.class), 60, 300, 4, ""));
|
|
}
|
|
|
|
@Test
|
|
void unconfiguredRealProviderNeverReturnsMockSuccess() {
|
|
CatalogService catalog = mock(CatalogService.class);
|
|
GatewayGuard guard = mock(GatewayGuard.class);
|
|
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
|
|
UsageAuditService usage = mock(UsageAuditService.class);
|
|
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
|
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync"));
|
|
when(catalog.keyCanInvoke(key, "model-real")).thenReturn(true);
|
|
GatewayController controller = new GatewayController(catalog, provider, guard, mock(ChatBillingService.class),new ObjectMapper());
|
|
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real"),null,new MockHttpServletResponse()));
|
|
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
|
|
assertEquals(503, error.getStatus());
|
|
verify(provider, never()).chat(anyString(), anyString(), anyMap());
|
|
}
|
|
|
|
@Test
|
|
void explicitMockProviderStillWorks() {
|
|
CatalogService catalog = mock(CatalogService.class);
|
|
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
|
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync"));
|
|
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
|
|
var billing=mock(ChatBillingService.class);
|
|
when(billing.reserve(anyMap(),anyMap(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
|
|
when(billing.settle(any(),anyMap(),anyInt(),any())).thenReturn(java.math.BigDecimal.ZERO);
|
|
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), billing,new ObjectMapper());
|
|
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock","messages",List.of(Map.of("role","user","content","test"))),null,new MockHttpServletResponse()).getStatusCode().value());
|
|
}
|
|
|
|
@Test
|
|
void syncResponsePreservesReasoningAndFinishReasonWhenContentIsEmpty() {
|
|
CatalogService catalog = mock(CatalogService.class);
|
|
GatewayGuard guard = mock(GatewayGuard.class);
|
|
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
|
|
ChatBillingService billing = mock(ChatBillingService.class);
|
|
String userId = UUID.randomUUID().toString();
|
|
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
|
|
Map<String, Object> model = Map.of("id", "reasoning-route", "providerId", "provider-relay", "upstreamModel", "reasoning-model", "mode", "sync");
|
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
|
when(catalog.activeModel("reasoning-route")).thenReturn(model);
|
|
when(catalog.keyCanInvoke(key, "reasoning-route")).thenReturn(true);
|
|
when(provider.isConfigured("provider-relay")).thenReturn(true);
|
|
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
|
|
"id", "upstream-response",
|
|
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", "", "reasoning_content", "private reasoning"), "finish_reason", "length")),
|
|
"usage", Map.of("prompt_tokens", 2, "completion_tokens", 16, "total_tokens", 18)
|
|
));
|
|
when(provider.chat(eq("provider-relay"), eq("reasoning-model"), anyMap())).thenReturn(upstreamResponse);
|
|
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
|
when(billing.settle(any(), anyMap(), anyInt(), isNull())).thenReturn(java.math.BigDecimal.ZERO);
|
|
|
|
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
|
|
var response = controller.chatCompletions("Bearer test-key", Map.of("model", "reasoning-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse());
|
|
assertEquals(200, response.getStatusCode().value());
|
|
Map<?, ?> choice = (Map<?, ?>)((List<?>)response.getBody().get("choices")).getFirst();
|
|
Map<?, ?> message = (Map<?, ?>)choice.get("message");
|
|
assertEquals("", message.get("content"));
|
|
assertEquals("private reasoning", message.get("reasoning_content"));
|
|
assertEquals("length", choice.get("finish_reason"));
|
|
}
|
|
|
|
@Test
|
|
void trulyEmptySyncResponseIsRejectedBeforeSettlement() {
|
|
CatalogService catalog = mock(CatalogService.class);
|
|
GatewayGuard guard = mock(GatewayGuard.class);
|
|
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
|
|
ChatBillingService billing = mock(ChatBillingService.class);
|
|
String userId = UUID.randomUUID().toString();
|
|
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
|
|
Map<String, Object> model = Map.of("id", "empty-route", "providerId", "provider-relay", "upstreamModel", "empty-model", "mode", "sync");
|
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
|
when(catalog.activeModel("empty-route")).thenReturn(model);
|
|
when(catalog.keyCanInvoke(key, "empty-route")).thenReturn(true);
|
|
when(provider.isConfigured("provider-relay")).thenReturn(true);
|
|
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
|
|
"id", "upstream-empty",
|
|
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", ""), "finish_reason", "stop"))
|
|
));
|
|
when(provider.chat(eq("provider-relay"), eq("empty-model"), anyMap())).thenReturn(upstreamResponse);
|
|
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
|
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
|
|
|
|
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "empty-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse()));
|
|
assertEquals("UPSTREAM_RESPONSE_INVALID", error.getCode());
|
|
verify(billing, never()).settle(any(), anyMap(), anyInt(), any());
|
|
verify(billing).fail(any(), eq("UPSTREAM_RESPONSE_INVALID"), anyString(), anyInt());
|
|
}
|
|
|
|
@Test
|
|
void unimplementedMediaProviderDoesNotCreateFakeTask() {
|
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
|
CatalogService catalog = mock(CatalogService.class);
|
|
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
|
|
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
|
when(catalog.activeModel("tripo")).thenReturn(Map.of("id", "tripo", "providerId", "provider-vast", "type", "3d", "mode", "async"));
|
|
when(catalog.keyCanInvoke(key, "tripo")).thenReturn(true);
|
|
AsyncTaskService tasks = new AsyncTaskService(jdbc, new ObjectMapper(), catalog, 100, mock(GatewayGuard.class), mock(UsageAuditService.class));
|
|
ApiException error = assertThrows(ApiException.class, () -> tasks.create("Bearer test-key", "3d", Map.of("model", "tripo", "prompt", "test")));
|
|
assertEquals(501, error.getStatus());
|
|
verifyNoInteractions(jdbc);
|
|
}
|
|
}
|