2026-09-10 18:10:31 +08:00
|
|
|
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;
|
2026-09-20 01:36:22 +08:00
|
|
|
import com.moyi.controlplane.wallet.ChatBillingService;
|
|
|
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
2026-09-10 18:10:31 +08:00
|
|
|
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 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));
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-23 17:55:31 +08:00
|
|
|
@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));
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 18:10:31 +08:00
|
|
|
@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);
|
2026-09-20 01:36:22 +08:00
|
|
|
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()));
|
2026-09-10 18:10:31 +08:00
|
|
|
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
|
|
|
|
|
assertEquals(503, error.getStatus());
|
2026-09-20 01:36:22 +08:00
|
|
|
verify(provider, never()).chat(anyString(), anyString(), anyMap());
|
2026-09-10 18:10:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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);
|
2026-09-20 01:36:22 +08:00
|
|
|
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());
|
2026-09-10 18:10:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
}
|