76 lines
4.3 KiB
Java
76 lines
4.3 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 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));
|
||
|
|
}
|
||
|
|
|
||
|
|
@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, usage);
|
||
|
|
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real")));
|
||
|
|
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
|
||
|
|
assertEquals(503, error.getStatus());
|
||
|
|
verify(provider, never()).chat(anyString(), anyString(), anyList());
|
||
|
|
}
|
||
|
|
|
||
|
|
@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);
|
||
|
|
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), mock(UsageAuditService.class));
|
||
|
|
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock")).getStatusCode().value());
|
||
|
|
}
|
||
|
|
|
||
|
|
@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);
|
||
|
|
}
|
||
|
|
}
|