feat: publish readable model ids and media task adapters
This commit is contained in:
1 parent
ba98b47618
commit
94eaee7b95
16 files changed
+654
-41
No files matched your search
+86
-2
@@ -67,7 +67,7 @@ class GatewayDeploymentTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void modelsKeepRouteIdAndExposeDisplayMetadata() {
|
||||
void modelsExposeReadableIdAndKeepRouteIdForDiagnostics() {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
GatewayGuard guard = mock(GatewayGuard.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
|
||||
@@ -90,7 +90,9 @@ class GatewayDeploymentTest {
|
||||
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--apihub--" + tag("route-1"), item.get("id"));
|
||||
assertEquals("route-1", item.get("moyi_route_id"));
|
||||
assertEquals("DeepSeek V4 Flash", item.get("name"));
|
||||
assertEquals("deepseek-v4-flash", item.get("model_key"));
|
||||
assertEquals("DeepSeek V4 Flash", item.get("public_name"));
|
||||
assertEquals("DeepSeek", item.get("vendor_name"));
|
||||
@@ -98,6 +100,88 @@ class GatewayDeploymentTest {
|
||||
assertEquals("deepseek-v4-flash", item.get("upstream_model"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateModelKeysAreDisambiguatedBySourceGroup() {
|
||||
var first = Map.<String, Object>of(
|
||||
"id", "route-a", "providerId", "provider-a", "modelKey", "gpt-5.5",
|
||||
"publicName", "GPT-5.5", "sourceLabel", "ApiHub 国内", "upstreamModel", "gpt-5.5");
|
||||
var second = Map.<String, Object>of(
|
||||
"id", "route-b", "providerId", "provider-b", "modelKey", "gpt-5.5",
|
||||
"publicName", "GPT-5.5", "sourceLabel", "APIPro", "upstreamModel", "gpt-5.5");
|
||||
var visible = List.of(first, second);
|
||||
assertEquals("gpt-5.5--apihub--" + tag("route-a"), GatewayModelIds.publicId(first));
|
||||
assertEquals("gpt-5.5--apipro--" + tag("route-b"), GatewayModelIds.publicId(second));
|
||||
}
|
||||
|
||||
@Test
|
||||
void readableIdResolvesOnlyToAnAuthorizedRoute() {
|
||||
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-readable", "providerId", "provider-a", "modelKey", "glm-5",
|
||||
"publicName", "GLM-5", "sourceLabel", "ApiHub 国内", "upstreamModel", "glm-5");
|
||||
when(catalog.activeModel("glm-5--apihub")).thenReturn(null);
|
||||
when(catalog.listModels(false)).thenReturn(List.of(route));
|
||||
when(catalog.keyCanInvoke(key, "route-readable")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
||||
assertSame(route, controller.resolveModel(key, "glm-5--apihub--" + tag("route-readable")));
|
||||
verify(catalog).keyCanInvoke(key, "route-readable");
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyRouteIdRemainsCompatibleAndUnauthorizedRouteIsRejected() {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
|
||||
Map<String, Object> route = Map.of("id", "route-legacy", "modelKey", "gpt-5", "sourceLabel", "APIPro");
|
||||
when(catalog.listModels(false)).thenReturn(List.of());
|
||||
when(catalog.activeModel("route-legacy")).thenReturn(route);
|
||||
when(catalog.keyCanInvoke(key, "route-legacy")).thenReturn(true);
|
||||
assertSame(route, GatewayModelIds.resolve(catalog, key, "route-legacy"));
|
||||
|
||||
Map<String, Object> forbidden = Map.of("id", "route-forbidden", "modelKey", "gpt-5", "sourceLabel", "APIPro");
|
||||
when(catalog.activeModel("route-forbidden")).thenReturn(forbidden);
|
||||
when(catalog.keyCanInvoke(key, "route-forbidden")).thenReturn(false);
|
||||
ApiException error = assertThrows(ApiException.class, () -> GatewayModelIds.resolve(catalog, key, "route-forbidden"));
|
||||
assertEquals("MODEL_FORBIDDEN", error.getCode());
|
||||
assertEquals(403, error.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void routeTagKeepsPreviouslyImportedIdUsableAfterRename() {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
|
||||
Map<String, Object> renamed = Map.of(
|
||||
"id", "route-renamed", "modelKey", "new-model-key", "publicName", "New Model",
|
||||
"sourceLabel", "New Group", "upstreamModel", "new-upstream");
|
||||
String oldPublicId = "old-model--old-group--" + tag("route-renamed");
|
||||
when(catalog.listModels(false)).thenReturn(List.of(renamed));
|
||||
when(catalog.keyCanInvoke(key, "route-renamed")).thenReturn(true);
|
||||
assertSame(renamed, GatewayModelIds.resolve(catalog, key, oldPublicId));
|
||||
verify(catalog, never()).activeModel(oldPublicId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyAliasWithMultipleAuthorizedRoutesIsRejectedAsAmbiguous() {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
|
||||
Map<String, Object> first = Map.of("id", "route-a", "modelKey", "same-model", "upstreamModel", "same-model", "sourceLabel", "A");
|
||||
Map<String, Object> second = Map.of("id", "route-b", "modelKey", "same-model", "upstreamModel", "same-model", "sourceLabel", "B");
|
||||
when(catalog.listModels(false)).thenReturn(List.of(first, second));
|
||||
when(catalog.activeModel("same-model")).thenReturn(null);
|
||||
when(catalog.keyCanInvoke(key, "route-a")).thenReturn(true);
|
||||
when(catalog.keyCanInvoke(key, "route-b")).thenReturn(true);
|
||||
ApiException error = assertThrows(ApiException.class, () -> GatewayModelIds.resolve(catalog, key, "same-model"));
|
||||
assertEquals("MODEL_AMBIGUOUS", error.getCode());
|
||||
assertEquals(400, error.getStatus());
|
||||
}
|
||||
|
||||
private static String tag(String route) {
|
||||
try {
|
||||
return java.util.HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256").digest(route.getBytes(java.nio.charset.StandardCharsets.UTF_8))).substring(0, 16);
|
||||
} catch (java.security.NoSuchAlgorithmException exception) { throw new AssertionError(exception); }
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidRedisPrefixIsRejected() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new GatewayGuard(mock(StringRedisTemplate.class), 60, 300, 4, ""));
|
||||
|
||||
+97
-1
@@ -2,6 +2,7 @@ package com.moyi.controlplane.task;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -43,6 +44,101 @@ class AsyncProviderClientTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void tripoUsesCurrentOfficialV3EndpointAndBearerAuth() throws Exception {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/v3/generation/text-to-model", exchange -> {
|
||||
assertEquals("Bearer test-key", exchange.getRequestHeaders().getFirst("Authorization"));
|
||||
byte[] body = "{\"code\":0,\"data\":{\"task_id\":\"task-1\"}}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, body.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(body); }
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
AsyncProviderClient client = clientFor("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
Map<String,Object> result = client.submit("provider-vast", "v3.1-20260211", "3d", "chair");
|
||||
assertEquals("task-1", ((Map<?,?>) result.get("data")).get("task_id"));
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void tripoImageInputUsesDocumentedInputField() throws Exception {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/v3/generation/image-to-model", exchange -> {
|
||||
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
|
||||
org.junit.jupiter.api.Assertions.assertTrue(body.contains("\"input\":\"https://example.test/image.png\""));
|
||||
org.junit.jupiter.api.Assertions.assertFalse(body.contains("\"image\":\""));
|
||||
byte[] response = "{\"code\":0,\"data\":{\"task_id\":\"task-image\"}}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(response); }
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
AsyncProviderClient client = clientFor("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
Map<String,Object> result = client.submit("provider-vast", "v3.1-20260211", "3d", Map.of("prompt", "chair", "media_url", "https://example.test/image.png"));
|
||||
assertEquals("task-image", ((Map<?,?>) result.get("data")).get("task_id"));
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void openAiCompatibleSpeechStoresBoundedBinaryAudio() throws Exception {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/audio/speech", exchange -> {
|
||||
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
|
||||
org.junit.jupiter.api.Assertions.assertTrue(body.contains("\"voice\":\"alloy\""));
|
||||
exchange.getResponseHeaders().set("Content-Type", "audio/mpeg");
|
||||
byte[] audio = "fake-mp3".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, audio.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(audio); }
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
SecretCipher cipher = new SecretCipher("test-encryption");
|
||||
when(jdbc.queryForMap(any(String.class), eq("provider-audio"))).thenReturn(Map.of(
|
||||
"code", "relay-audio", "protocol", "openai-compatible", "base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", cipher.encrypt("test-key")
|
||||
));
|
||||
AsyncProviderClient client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
|
||||
Map<String,Object> result = client.submit("provider-audio", "tts-1", "audio", Map.of("prompt", "hello"));
|
||||
Map<?,?> inline = (Map<?,?>) result.get("_moyiInlineResult");
|
||||
assertEquals("audio/mpeg", inline.get("mimeType"));
|
||||
assertEquals("ZmFrZS1tcDM=", inline.get("base64"));
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void openAiCompatibleVideoUsesMultipartRequest() throws Exception {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/videos", exchange -> {
|
||||
assertTrue(exchange.getRequestHeaders().getFirst("Content-Type").startsWith("multipart/form-data; boundary="));
|
||||
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
|
||||
assertTrue(body.contains("name=\"model\""));
|
||||
assertTrue(body.contains("sora-2"));
|
||||
byte[] response = "{\"id\":\"video-1\",\"status\":\"queued\"}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(response); }
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
SecretCipher cipher = new SecretCipher("test-encryption");
|
||||
when(jdbc.queryForMap(any(String.class), eq("provider-video"))).thenReturn(Map.of(
|
||||
"code", "relay-video", "protocol", "openai-compatible", "base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", cipher.encrypt("test-key")
|
||||
));
|
||||
AsyncProviderClient client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
|
||||
Map<String,Object> result = client.submit("provider-video", "sora-2", "video", Map.of("prompt", "a cat"));
|
||||
assertEquals("video-1", result.get("id"));
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"401,UPSTREAM_AUTH_FAILED", "403,UPSTREAM_AUTH_FAILED", "402,UPSTREAM_CREDIT_INSUFFICIENT", "429,UPSTREAM_RATE_LIMITED", "503,UPSTREAM_PROVIDER_ERROR"})
|
||||
void httpErrorsExposeOnlySafeCodes(int status, String code) throws Exception {
|
||||
@@ -70,7 +166,7 @@ class AsyncProviderClientTest {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
SecretCipher cipher = new SecretCipher("test-encryption");
|
||||
when(jdbc.queryForMap(any(String.class), eq("provider-vast"))).thenReturn(Map.of(
|
||||
"code", "vast-tripo", "base_url", baseUrl, "credential_ciphertext", cipher.encrypt("test-key")
|
||||
"code", "vast-tripo", "protocol", "http", "base_url", baseUrl, "credential_ciphertext", cipher.encrypt("test-key")
|
||||
));
|
||||
return new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user