feat: extend enterprise gateway policies billing protocols and operations
This commit is contained in:
1 parent
1528b71882
commit
af43947b5d
67 files changed
+1822
-69
No files matched your search
+5
-1
@@ -82,6 +82,7 @@ class PlatformAdminPostgresTest {
|
||||
context.registerBean(DataSource.class, () -> source);
|
||||
context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source));
|
||||
context.registerBean(AuthService.class, () -> new AuthService(jdbc, new PasswordHasher("test-pepper"), json, 168));
|
||||
context.registerBean(TotpService.class, () -> new TotpService(jdbc,new SecretCipher("test-mfa")));
|
||||
context.registerBean(CatalogService.class, () -> new CatalogService(jdbc, json, new SecretCipher("test-encryption")));
|
||||
context.registerBean(WalletService.class, () -> new WalletService(jdbc, context.getBean(AuthService.class)));
|
||||
context.registerBean(UsageAuditService.class, () -> new UsageAuditService(jdbc,json));
|
||||
@@ -371,6 +372,7 @@ class PlatformAdminPostgresTest {
|
||||
ChatFixture chatFixture() {
|
||||
var model=new LinkedHashMap<>(catalog.listModels(false).stream().filter(m -> "provider-deepseek".equals(m.get("providerId"))).findFirst().orElseThrow());
|
||||
model.put("pricing",Map.of("input",2,"output",4,"cachedInput",1,"unit","CNY / 1M tokens"));
|
||||
jdbc.update("UPDATE model_catalog SET pricing='{"+"\"input\":2,\"output\":4,\"cachedInput\":1,\"unit\":\"CNY / 1M tokens\"}'::jsonb WHERE id=?",model.get("id"));
|
||||
var created=catalog.createKey(memberToken,Map.of("name","Local billing test","modelIds",List.of(model.get("id"))),auth);
|
||||
var key=catalog.authenticateKey(created.get("key").toString());
|
||||
var input=ChatMeter.request(Map.of("model",model.get("id"),"messages",List.of(Map.of("role","user","content","你好")),"max_tokens",16),json);
|
||||
@@ -444,9 +446,11 @@ class PlatformAdminPostgresTest {
|
||||
var f=chatFixture();
|
||||
assertEquals("INSUFFICIENT_BALANCE",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||
f.model().remove("pricing");
|
||||
jdbc.update("UPDATE model_catalog SET pricing=NULL WHERE id=?",f.model().get("id"));
|
||||
assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||
f.model().put("pricing",Map.of("input",1,"output",2,"unit","USD / 1M tokens"));
|
||||
assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||
jdbc.update("UPDATE model_catalog SET pricing='{"+"\"input\":1,\"output\":2,\"unit\":\"USD / 1M tokens\"}'::jsonb WHERE id=?",f.model().get("id"));
|
||||
assertEquals("BILLING_UNIT_UNSUPPORTED",assertThrows(ApiException.class,()->f.reserve(null)).getCode());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM chat_billing_requests",Integer.class));
|
||||
}
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ class AuthPostgresTest {
|
||||
context.registerBean(DataSource.class, () -> source);
|
||||
context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source));
|
||||
context.registerBean(AuthService.class, () -> new AuthService(jdbc, hasher, json, 168));
|
||||
context.registerBean(TotpService.class, () -> new TotpService(jdbc,new com.moyi.controlplane.security.SecretCipher("test-mfa")));
|
||||
context.refresh();
|
||||
AuthService auth = context.getBean(AuthService.class);
|
||||
try (var threads = Executors.newFixedThreadPool(2)) {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
class TotpServiceTest {
|
||||
@Test void rfc6238Sha1VectorsTruncatedToSixDigits() {
|
||||
String secret=TotpService.base32("12345678901234567890".getBytes(StandardCharsets.US_ASCII));
|
||||
assertEquals("287082",TotpService.totp(secret,59/30));
|
||||
assertEquals("081804",TotpService.totp(secret,1111111109L/30));
|
||||
assertEquals("050471",TotpService.totp(secret,1111111111L/30));
|
||||
assertEquals("353130",TotpService.totp(secret,20000000000L/30));
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import java.util.List;
|
||||
class EnterprisePoliciesTest {
|
||||
@Test void ipv4AndIpv6CidrAreMatchedWithoutDns() {
|
||||
assertTrue(EnterprisePolicies.matches("192.0.2.23","192.0.2.0/24"));
|
||||
assertFalse(EnterprisePolicies.matches("192.0.3.23","192.0.2.0/24"));
|
||||
assertTrue(EnterprisePolicies.matches("2001:db8::4","2001:db8::/32"));
|
||||
assertFalse(EnterprisePolicies.matches("2001:db9::4","2001:db8::/32"));
|
||||
assertFalse(EnterprisePolicies.matches("127.0.0.1","localhost"));
|
||||
assertFalse(EnterprisePolicies.matches("127.0.0.1","127.0.0.1/33"));
|
||||
assertThrows(RuntimeException.class,()->EnterprisePolicies.validateRanges(List.of("127.1")));
|
||||
}
|
||||
@Test void nullBudgetIsUnlimitedAndZeroBudgetBlocksSpending() {
|
||||
assertNull(EnterprisePolicyController.budget(""));
|
||||
assertEquals(0,EnterprisePolicyController.budget("0").signum());
|
||||
assertThrows(RuntimeException.class,()->EnterprisePolicyController.budget("-1"));
|
||||
assertThrows(RuntimeException.class,()->EnterprisePolicyController.integer("1.5",100));
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.auth.TotpService;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import com.moyi.controlplane.catalog.PriceManagementController;
|
||||
import com.moyi.controlplane.usage.OperationsController;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import com.moyi.controlplane.provider.UpstreamHealth;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import javax.sql.DataSource;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.*;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@EnabledIfEnvironmentVariable(named="MOYI_AUTH_TEST_JDBC_URL",matches="jdbc:postgresql://127\\.0\\.0\\.1:[0-9]+/.+")
|
||||
class EnterprisePostgresTest {
|
||||
@Configuration @EnableTransactionManagement(proxyTargetClass=true) static class Transactions {}
|
||||
JdbcTemplate jdbc;DriverManagerDataSource base;AnnotationConfigApplicationContext context;String schema,model;
|
||||
UUID user=UUID.randomUUID(),key=UUID.randomUUID();ObjectMapper json=new ObjectMapper();ChatBillingService billing;
|
||||
Map<String,Object> keyMap,modelMap;
|
||||
@BeforeEach void setup() {
|
||||
String url=System.getenv("MOYI_AUTH_TEST_JDBC_URL");schema="enterprise_"+UUID.randomUUID().toString().replace("-","");
|
||||
base=new DriverManagerDataSource(url,"moyi_role_test","");var source=new DriverManagerDataSource(url+"?currentSchema="+schema,"moyi_role_test","");
|
||||
Flyway.configure().dataSource(base).schemas(schema).defaultSchema(schema).load().migrate();jdbc=new JdbcTemplate(source);
|
||||
context=new AnnotationConfigApplicationContext();context.register(Transactions.class);
|
||||
context.registerBean(DataSource.class,()->source);context.registerBean(DataSourceTransactionManager.class,()->new DataSourceTransactionManager(source));
|
||||
context.registerBean(ChatBillingService.class,()->new ChatBillingService(jdbc,json,new UsageAuditService(jdbc,json)));
|
||||
context.registerBean(TotpService.class,()->new TotpService(jdbc,new SecretCipher("isolated-secret")));context.refresh();billing=context.getBean(ChatBillingService.class);
|
||||
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'enterprise-test','username','not-a-login','test')",user);
|
||||
jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'key','test',?)",key,user,"e".repeat(64));
|
||||
jdbc.update("INSERT INTO user_wallets(user_id,balance) VALUES (?,100)",user);
|
||||
model=jdbc.queryForObject("SELECT id FROM model_catalog WHERE provider_id<>'provider-mock' LIMIT 1",String.class);
|
||||
jdbc.update("UPDATE model_catalog SET pricing=?::jsonb,procurement_pricing=?::jsonb WHERE id=?","{\"unit\":\"CNY / 1M tokens\",\"input\":1,\"output\":2}","{\"unit\":\"CNY / 1M tokens\",\"input\":0.5,\"output\":0.8}",model);
|
||||
keyMap=Map.of("id",key,"userId",user);modelMap=Map.of("id",model,"providerId",jdbc.queryForObject("SELECT provider_id FROM model_catalog WHERE id=?",String.class,model));
|
||||
}
|
||||
@AfterEach void cleanup() {if(context!=null) context.close();if(base!=null) new JdbcTemplate(base).execute("DROP SCHEMA IF EXISTS "+schema+" CASCADE");}
|
||||
ChatMeter.Request request() {return new ChatMeter.Request(Map.of("messages",List.of(Map.of("role","user","content","test"))),100,10);}
|
||||
@Test void concurrentAdmissionsRespectBudgetAndReleaseFailures() throws Exception {
|
||||
jdbc.update("INSERT INTO gateway_user_policies(user_id,monthly_budget) VALUES (?,0.0002)",user);
|
||||
var gate=new CountDownLatch(1);var tickets=new CopyOnWriteArrayList<ChatBillingService.Ticket>();
|
||||
try(var pool=Executors.newFixedThreadPool(2)) {
|
||||
var jobs=new ArrayList<Future<String>>();for(int i=0;i<2;i++) jobs.add(pool.submit(()->{gate.await();try {tickets.add(billing.reserve(keyMap,modelMap,request(),null));return "accepted";} catch(ApiException e) {return e.getCode();}}));
|
||||
gate.countDown();var outcomes=new ArrayList<String>();for(var job:jobs) outcomes.add(job.get(10,TimeUnit.SECONDS));
|
||||
assertEquals(1,Collections.frequency(outcomes,"accepted"));assertEquals(1,Collections.frequency(outcomes,"BUDGET_EXCEEDED"));
|
||||
}
|
||||
billing.fail(tickets.getFirst(),"TEST","interrupted",10);
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM gateway_budget_holds",Integer.class));
|
||||
assertEquals("pending",jdbc.queryForObject("SELECT cost_status FROM usage_events WHERE id=?",String.class,tickets.getFirst().id()));
|
||||
assertNotNull(billing.reserve(keyMap,modelMap,request(),null));
|
||||
}
|
||||
@Test void editsCannotChangeInflightRetailOrProcurementSnapshots() {
|
||||
var ticket=billing.reserve(keyMap,modelMap,request(),"one");
|
||||
jdbc.update("UPDATE model_catalog SET pricing='{"+"\"unit\":\"CNY / 1M tokens\",\"input\":10,\"output\":20}'::jsonb,procurement_pricing=NULL WHERE id=?",model);
|
||||
var response=Map.<String,Object>of("usage",Map.of("prompt_tokens",100,"completion_tokens",10));
|
||||
assertEquals(new BigDecimal("0.00012000"),billing.settle(ticket,response,100,null));
|
||||
assertEquals(new BigDecimal("0.00012000"),billing.settle(ticket,response,100,null));
|
||||
assertEquals(new BigDecimal("0.00005800"),jdbc.queryForObject("SELECT procurement_cost FROM usage_events WHERE id=?",BigDecimal.class,ticket.id()));
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE request_id=?",Integer.class,ticket.id()));
|
||||
assertThrows(ApiException.class,()->billing.reserve(keyMap,modelMap,request(),"one"));
|
||||
}
|
||||
@Test void mfaRejectsReplayAndRecoveryCodesAreSingleUse() {
|
||||
var mfa=context.getBean(TotpService.class);var setup=mfa.begin(user,"enterprise-test");
|
||||
String secret=setup.get("secret").toString();String code=ReflectionTestUtils.invokeMethod(TotpService.class,"totp",secret,System.currentTimeMillis()/30000);
|
||||
var recovery=mfa.confirm(user,code);assertEquals(10,recovery.size());
|
||||
assertThrows(ApiException.class,()->mfa.verifyLogin(user,code));
|
||||
mfa.verifyLogin(user,recovery.getFirst());assertThrows(ApiException.class,()->mfa.verifyLogin(user,recovery.getFirst()));
|
||||
assertEquals(9,mfa.status(user).get("recoveryRemaining"));
|
||||
assertFalse(jdbc.queryForObject("SELECT secret_ciphertext FROM user_totp WHERE user_id=?",String.class,user).contains(secret));
|
||||
for(int i=0;i<4;i++) assertThrows(ApiException.class,()->mfa.verifyLogin(user,"wrong"));
|
||||
assertEquals("MFA_RATE_LIMITED",assertThrows(ApiException.class,()->mfa.verifyLogin(user,recovery.get(1))).getCode());
|
||||
}
|
||||
@Test void healthCooldownExcludesRejectedAccount() {
|
||||
var health=new UpstreamHealth(jdbc);health.record(model,"upstream-test-a",429,100);
|
||||
assertEquals("upstream-test-b",health.select(model,List.of("upstream-test-a","upstream-test-b"),Set.of(),0));
|
||||
assertNull(health.select(model,List.of("upstream-test-a"),Set.of(),0));
|
||||
health.record(model,"upstream-test-a",200,50);
|
||||
assertEquals("upstream-test-a",health.select(model,List.of("upstream-test-a"),Set.of(),0));
|
||||
}
|
||||
@Test void removedPriceCannotFallBackToPreviouslyReadCatalog() {
|
||||
var stale=new HashMap<>(modelMap);stale.put("pricing",Map.of("unit","CNY / 1M tokens","input",1,"output",2));
|
||||
jdbc.update("UPDATE model_catalog SET pricing=NULL WHERE id=?",model);
|
||||
assertEquals("MODEL_NOT_PRICED",assertThrows(ApiException.class,()->billing.reserve(keyMap,stale,request(),null)).getCode());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM gateway_budget_holds",Integer.class));
|
||||
}
|
||||
@Test void priceEditsAcceptEquivalentNumbersAndRejectStaleVersions() throws Exception {
|
||||
var auth=mock(AuthService.class);when(auth.requireAdmin(any())).thenReturn(new AuthService.UserView(user,"admin","username","test","super_admin","active",null,null));
|
||||
var controller=new PriceManagementController(jdbc,auth,json);var request=new MockHttpServletRequest();
|
||||
jdbc.update("UPDATE model_catalog SET pricing=NULL,procurement_pricing=NULL WHERE id=?",model);
|
||||
var payload=new HashMap<String,Object>();payload.put("pricing",Map.of("unit","CNY / 1M tokens","input",new BigDecimal("2.50"),"output",new BigDecimal("10.00")));
|
||||
payload.put("expectedPricing",null);payload.put("expectedProcurementPricing",null);
|
||||
controller.save(model,payload,request);
|
||||
assertEquals("PRICE_CHANGED",assertThrows(ApiException.class,()->controller.save(model,payload,request)).getCode());
|
||||
payload.put("expectedPricing",Map.of("unit","CNY / 1M tokens","input",2.5,"output",10));
|
||||
payload.put("pricing",Map.of("unit","CNY / 1M tokens","input",3,"output",10));
|
||||
controller.save(model,payload,request);
|
||||
assertEquals(new BigDecimal("3"),jdbc.queryForObject("SELECT (pricing->>'input')::numeric FROM model_catalog WHERE id=?",BigDecimal.class,model));
|
||||
}
|
||||
@Test void alertCooldownAndRecoveryPersistWithoutSendingMail() {
|
||||
jdbc.update("UPDATE ops_alert_rules SET minimum_requests=1,error_percent=50");
|
||||
var ticket=billing.reserve(keyMap,modelMap,request(),null);billing.fail(ticket,"TEST","isolated failure",1);
|
||||
var ops=new OperationsController(jdbc,mock(AuthService.class));ops.evaluate();ops.evaluate();
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM ops_alert_events",Integer.class));
|
||||
assertEquals("disabled",jdbc.queryForObject("SELECT notification_status FROM ops_alert_events",String.class));
|
||||
jdbc.update("UPDATE usage_events SET updated_at=now()-interval '1 day'");ops.evaluate();
|
||||
assertEquals("resolved",jdbc.queryForObject("SELECT status FROM ops_alert_events",String.class));
|
||||
}
|
||||
}
|
||||
+17
-4
@@ -20,6 +20,19 @@ import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class GatewayDeploymentTest {
|
||||
@Test void failedTaskClaimReleasesRedisLeaseAfterDatabaseRollback() {
|
||||
var redis=mock(StringRedisTemplate.class);doReturn(1L).when(redis).execute(any(RedisScript.class),anyList(),any(Object[].class));
|
||||
var policies=mock(EnterprisePolicies.class);UUID key=UUID.randomUUID(),user=UUID.randomUUID();
|
||||
when(policies.taskLimit(key)).thenReturn(new EnterprisePolicies.UserLimit(user,120,16));
|
||||
var guard=new GatewayGuard(redis,60,300,4,"moyi:test:");guard.setEnterprisePolicies(policies);
|
||||
org.springframework.transaction.support.TransactionSynchronizationManager.initSynchronization();
|
||||
try {
|
||||
assertTrue(guard.tryTaskUserSlot(key,"isolated-task",3600000));
|
||||
clearInvocations(redis);
|
||||
for(var callback:org.springframework.transaction.support.TransactionSynchronizationManager.getSynchronizations()) callback.afterCompletion(org.springframework.transaction.support.TransactionSynchronization.STATUS_ROLLED_BACK);
|
||||
verify(redis).execute(any(RedisScript.class),eq(List.of("moyi:test:gateway:slots:user:"+user)),eq("isolated-task"));
|
||||
} finally {org.springframework.transaction.support.TransactionSynchronizationManager.clearSynchronization();}
|
||||
}
|
||||
@Test
|
||||
void capacitySnapshotReadsBothLiveAndWaitingBucketsAndDoesNotInventZeroOnFailure() {
|
||||
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
||||
@@ -221,7 +234,7 @@ class GatewayDeploymentTest {
|
||||
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.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync", "type", "text"));
|
||||
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()));
|
||||
@@ -235,7 +248,7 @@ class GatewayDeploymentTest {
|
||||
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.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync", "type", "text"));
|
||||
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)));
|
||||
@@ -252,7 +265,7 @@ class GatewayDeploymentTest {
|
||||
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");
|
||||
Map<String, Object> model = Map.of("id", "reasoning-route", "providerId", "provider-relay", "upstreamModel", "reasoning-model", "mode", "sync", "type", "text");
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("reasoning-route")).thenReturn(model);
|
||||
when(catalog.keyCanInvoke(key, "reasoning-route")).thenReturn(true);
|
||||
@@ -284,7 +297,7 @@ class GatewayDeploymentTest {
|
||||
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");
|
||||
Map<String, Object> model = Map.of("id", "empty-route", "providerId", "provider-relay", "upstreamModel", "empty-model", "mode", "sync", "type", "text");
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("empty-route")).thenReturn(model);
|
||||
when(catalog.keyCanInvoke(key, "empty-route")).thenReturn(true);
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.*;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
class GatewayFailoverTest {
|
||||
Map<String,Object> route(String id,String logical) {return Map.of("id",id,"logicalModelId",logical,"providerId","p", "upstreamModel","same-model","type","text","mode","sync","capabilities",Map.of("failover",true));}
|
||||
@Test void onlyAuthorizedEquivalentRoutesAreSelectedAndRetailTicketIsReused() {
|
||||
var catalog=mock(CatalogService.class);var provider=mock(OpenAiCompatibleProvider.class);var guard=mock(GatewayGuard.class);var billing=mock(ChatBillingService.class);
|
||||
Map<String,Object> primary=route("one","logical"),backup=route("two","logical"),forbidden=route("no-access","logical"),different=route("other","other-model");
|
||||
var key=Map.<String,Object>of("id",UUID.randomUUID());when(catalog.listModels(false)).thenReturn(List.of(primary,backup,forbidden,different));
|
||||
when(catalog.keyCanInvoke(eq(key),anyString())).thenAnswer(call->!call.getArgument(1).equals("no-access"));when(provider.isConfigured(anyString(),anyString(),anyString())).thenReturn(true);
|
||||
var service=new GatewayFailover(catalog,guard,billing,provider);assertEquals(List.of(primary,backup),service.candidates(key,primary,"chat"));
|
||||
var ticket=new ChatBillingService.Ticket(UUID.randomUUID(),UUID.randomUUID(),ChatMeter.rates(null,true));
|
||||
when(provider.chat(eq("p"),eq("same-model"),eq("one"),anyMap())).thenThrow(new UpstreamRejectedException(new ApiException("UPSTREAM_RATE_LIMITED","rejected",429)));
|
||||
when(provider.chat(eq("p"),eq("same-model"),eq("two"),anyMap())).thenReturn(Map.of("ok",true));
|
||||
assertEquals(Map.of("ok",true),service.invoke(key,primary,"chat",Map.of(),ticket));verify(billing).actualRoute(ticket,backup);verify(billing,never()).reserve(any(),any(),any(),any());
|
||||
}
|
||||
@Test void ambiguousTimeoutIsNeverRetried() {
|
||||
var catalog=mock(CatalogService.class);var provider=mock(OpenAiCompatibleProvider.class);var guard=mock(GatewayGuard.class);var billing=mock(ChatBillingService.class);
|
||||
Map<String,Object> primary=route("one","logical"),backup=route("two","logical");var key=Map.<String,Object>of("id",UUID.randomUUID());
|
||||
when(catalog.listModels(false)).thenReturn(List.of(primary,backup));when(catalog.keyCanInvoke(anyMap(),anyString())).thenReturn(true);when(provider.isConfigured(anyString(),anyString(),anyString())).thenReturn(true);
|
||||
when(provider.chat(eq("p"),eq("same-model"),eq("one"),anyMap())).thenThrow(new ApiException("UPSTREAM_PROVIDER_TIMEOUT","unknown",504));
|
||||
assertThrows(ApiException.class,()->new GatewayFailover(catalog,guard,billing,provider).invoke(key,primary,"chat",Map.of(),null));
|
||||
verify(provider,never()).chat(eq("p"),eq("same-model"),eq("two"),anyMap());
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import java.util.*;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
class NativeProtocolMeterTest {
|
||||
@Test void claudeCacheTokensAreAddedOnceAndHourlyWritesPreserved() {
|
||||
var body=Map.<String,Object>of("stop_reason","end_turn","usage",Map.of("input_tokens",10,"output_tokens",20,"cache_read_input_tokens",100,"cache_creation_input_tokens",50,"cache_creation",Map.of("ephemeral_1h_input_tokens",30,"ephemeral_5m_input_tokens",20)));
|
||||
var tokens=ChatMeter.tokens(NativeProtocolMeter.normalize("messages",body));
|
||||
assertEquals(new ChatMeter.Tokens(160,20,100,20,30),tokens);
|
||||
}
|
||||
@Test void geminiThoughtTokensAreBillableOutput() {
|
||||
var tokens=ChatMeter.tokens(NativeProtocolMeter.normalize("gemini",Map.of("candidates",List.of(Map.of("finishReason","STOP")),"usageMetadata",Map.of("promptTokenCount",100,"candidatesTokenCount",20,"thoughtsTokenCount",30))));
|
||||
assertEquals(50,tokens.completion());
|
||||
}
|
||||
@Test void messagesStreamRequiresStopAndMergesCumulativeUsage() {
|
||||
var collector=new NativeProtocolMeter.Collector("messages");
|
||||
collector.accept(Map.of("type","message_start","message",Map.of("usage",Map.of("input_tokens",100,"output_tokens",1))));
|
||||
collector.accept(Map.of("type","message_delta","delta",Map.of("stop_reason","tool_use"),"usage",Map.of("output_tokens",25)));
|
||||
assertThrows(RuntimeException.class,collector::result);
|
||||
collector.accept(Map.of("type","message_stop"));
|
||||
assertEquals(25,ChatMeter.tokens(collector.result()).completion());
|
||||
}
|
||||
@Test void responsesStateCannotCrossCustomerBoundary() {
|
||||
assertThrows(RuntimeException.class,()->NativeProtocolMeter.request("responses",Map.of("input","hello","previous_response_id","resp_other_customer"),new ObjectMapper()));
|
||||
assertEquals(false,NativeProtocolMeter.request("responses",Map.of("input","hello"),new ObjectMapper()).body().get("store"));
|
||||
}
|
||||
@Test void embeddingsHaveNoOutputCharge() {
|
||||
assertEquals(0,ChatMeter.tokens(NativeProtocolMeter.normalize("embeddings",Map.of("data",List.of(Map.of("embedding",List.of(0.1))),"usage",Map.of("prompt_tokens",5)))).completion());
|
||||
assertThrows(RuntimeException.class,()->NativeProtocolMeter.normalize("embeddings",Map.of("data",List.of(Map.of("error","no vector")),"usage",Map.of("prompt_tokens",5))));
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class TokenPriceScheduleTest {
|
||||
private Map<String,Object> base() { return new HashMap<>(Map.of("unit","CNY / 1M tokens","input",2,"output",8,"cachedInput",0.2,"cacheWrite5m",2.5,"cacheWrite1h",4)); }
|
||||
@Test void inputIncludesCacheButEachCategoryIsBilledOnlyOnce() {
|
||||
var rates=ChatMeter.rates(base(),false);
|
||||
assertEquals(new BigDecimal("0.00287000"),rates.cost(new ChatMeter.Tokens(1000,100,100,100,100)));
|
||||
}
|
||||
@Test void tierBoundaryIsStrictAndSnapshotIsIndependent() {
|
||||
var raw=base(); raw.put("tiers",List.of(Map.of("aboveInputTokens",1000,"input",4,"output",12)));
|
||||
var rates=ChatMeter.rates(raw,false); raw.put("input",999);
|
||||
assertEquals(new BigDecimal("0.00200000"),rates.cost(1000,0,0));
|
||||
assertEquals(new BigDecimal("0.00400400"),rates.cost(1001,0,0));
|
||||
assertEquals(new BigDecimal("0.01600000"),rates.hold(1000,1000));
|
||||
assertEquals(rates.cost(1001,0,0),ChatMeter.rates(rates.snapshot(),false).cost(1001,0,0));
|
||||
}
|
||||
@Test void rejectsAmbiguousTiersAndInvalidCacheBreakdown() {
|
||||
var raw=base(); raw.put("tiers",List.of(Map.of("aboveInputTokens",1000,"input",3),Map.of("aboveInputTokens",1000,"output",5)));
|
||||
assertThrows(RuntimeException.class,()->ChatMeter.rates(raw,false));
|
||||
assertThrows(RuntimeException.class,()->ChatMeter.tokens(Map.of("usage",Map.of("prompt_tokens",10,"completion_tokens",1,"cache_creation_input_tokens",11))));
|
||||
}
|
||||
@Test void explicitZeroCacheWritePriceRemainsFree() {
|
||||
var raw=base(); raw.put("cacheWrite1h",0);
|
||||
assertEquals(new BigDecimal("0.00000000"),ChatMeter.rates(raw,false).cost(new ChatMeter.Tokens(1000,0,0,0,1000)));
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.moyi.controlplane.provider;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
class NativeProviderTest {
|
||||
@Test void nativeEndpointsKeepToolsHeadersAndSseEvents() throws Exception {
|
||||
var server=HttpServer.create(new InetSocketAddress("127.0.0.1",0),0);var json=new ObjectMapper();var seen=new ArrayList<String>();
|
||||
server.createContext("/",exchange->{
|
||||
seen.add(exchange.getRequestURI().toString());var body=json.readTree(exchange.getRequestBody());
|
||||
assertEquals("Bearer test-upstream",exchange.getRequestHeaders().getFirst("Authorization"));
|
||||
if(exchange.getRequestURI().getPath().endsWith("messages")) {assertEquals("2023-06-01",exchange.getRequestHeaders().getFirst("anthropic-version"));assertEquals("function",body.path("tools").get(0).path("type").asText());}
|
||||
byte[] output=(body.path("stream").asBoolean()?"event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n":"{\"usage\":{\"prompt_tokens\":5},\"data\":[{\"embedding\":[0.1]}]}").getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().set("Content-Type",body.path("stream").asBoolean()?"text/event-stream":"application/json");exchange.sendResponseHeaders(200,output.length);exchange.getResponseBody().write(output);exchange.close();
|
||||
});server.start();
|
||||
var env=new MockEnvironment().withProperty("moyi.providers.deepseek.api-key","test-upstream").withProperty("moyi.providers.deepseek.base-url","http://127.0.0.1:"+server.getAddress().getPort()+"/v1");
|
||||
var provider=new OpenAiCompatibleProvider(json,env,mock(JdbcTemplate.class),new SecretCipher("test"));
|
||||
try {provider.invoke("provider-deepseek","embedding","route",Map.of("input","hello"),"embeddings");
|
||||
try(var stream=provider.nativeStream("provider-deepseek","claude","route",Map.of("tools",List.of(Map.of("type","function"))),"messages")) {var chunks=new ArrayList<Map<String,Object>>();stream.events(chunks::add,false);assertEquals("message_stop",chunks.getFirst().get("type"));}
|
||||
provider.invoke("provider-deepseek","gemini-model","route",Map.of("contents",List.of()),"gemini");
|
||||
assertEquals(List.of("/v1/embeddings","/v1/messages","/v1beta/models/gemini-model:generateContent"),seen);
|
||||
} finally {provider.close();server.stop(0);}
|
||||
}
|
||||
@Test void rejectedKeyIsReplacedButSuccessfulRequestIsNotDuplicated() throws Exception {
|
||||
var calls=new AtomicInteger();var server=HttpServer.create(new InetSocketAddress("127.0.0.1",0),0);
|
||||
server.createContext("/v1/chat/completions",exchange->{calls.incrementAndGet();boolean reject=exchange.getRequestHeaders().getFirst("Authorization").equals("Bearer first");byte[] output=(reject?"{\"error\":\"rate limit\"}":"{\"choices\":[]}").getBytes(StandardCharsets.UTF_8);exchange.sendResponseHeaders(reject?429:200,output.length);exchange.getResponseBody().write(output);exchange.close();});server.start();
|
||||
var env=new MockEnvironment().withProperty("moyi.providers.deepseek.api-key","first").withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_1","first|second").withProperty("moyi.providers.deepseek.base-url","http://127.0.0.1:"+server.getAddress().getPort()+"/v1");var provider=new OpenAiCompatibleProvider(new ObjectMapper(),env,mock(JdbcTemplate.class),new SecretCipher("test"));
|
||||
ReflectionTestUtils.setField(provider,"health",new UpstreamHealth(mock(JdbcTemplate.class)));
|
||||
try {provider.chat("provider-deepseek","model","route-1",Map.of());assertEquals(2,calls.get());} finally {provider.close();server.stop(0);}
|
||||
}
|
||||
}
|
||||
+12
-9
@@ -46,8 +46,10 @@ class AsyncTaskPostgresTest {
|
||||
jdbc = new JdbcTemplate(source);
|
||||
usage = spy(new UsageAuditService(jdbc, json));
|
||||
provider = mock(AsyncProviderClient.class);
|
||||
GatewayGuard guard=mock(GatewayGuard.class);
|
||||
when(guard.tryTaskUserSlot(any(),anyString(),anyLong())).thenReturn(true);
|
||||
tasks = new AsyncTaskService(jdbc, json, mock(CatalogService.class), 100, 4, 4, 120_000,
|
||||
mock(GatewayGuard.class), usage, provider, new DataSourceTransactionManager(source));
|
||||
guard, usage, provider, new DataSourceTransactionManager(source));
|
||||
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'async-test','username','not-a-password','Test')", userId);
|
||||
jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'Test','test-prefix',?)", keyId, userId, "b".repeat(64));
|
||||
jdbc.update("INSERT INTO user_wallets(user_id,balance,reserved_balance) VALUES (?,10,1.44)", userId);
|
||||
@@ -64,8 +66,9 @@ class AsyncTaskPostgresTest {
|
||||
ReflectionTestUtils.invokeMethod(tasks,"failTask",taskId,"PROVIDER_NOT_CONFIGURED","异步渠道尚未配置 API Key");
|
||||
}
|
||||
private void poll() {
|
||||
Map<String,Object> row = jdbc.queryForMap("SELECT id,provider_task_id,provider_deadline_at,created_at FROM model_tasks WHERE id=?", taskId);
|
||||
Map<String,Object> row = jdbc.queryForMap("SELECT id,model_id,provider_task_id,provider_deadline_at,created_at,provider_key_fingerprint FROM model_tasks WHERE id=?", taskId);
|
||||
row.put("provider_id", "provider-vast");
|
||||
row.put("upstream_model","v3.1-20260211");
|
||||
ReflectionTestUtils.invokeMethod(tasks,"pollProviderTask",row);
|
||||
}
|
||||
private BigDecimal wallet(String column) {
|
||||
@@ -82,7 +85,7 @@ class AsyncTaskPostgresTest {
|
||||
@ValueSource(strings={"PROVIDER_NOT_CONFIGURED", "UPSTREAM_AUTH_FAILED"})
|
||||
void submissionFailureReleasesExactlyOnceWithoutDebitingBalance(String code) {
|
||||
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL WHERE id=?",taskId);
|
||||
when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenThrow(new ApiException(code,"上游任务未接受",503));
|
||||
when(provider.submit(anyString(),anyString(),anyString(),anyString(),anyString())).thenThrow(new ApiException(code,"上游任务未接受",503));
|
||||
tasks.completeMockTasks();
|
||||
tasks.completeMockTasks();
|
||||
assertEquals("failed",status());
|
||||
@@ -91,7 +94,7 @@ class AsyncTaskPostgresTest {
|
||||
assertEquals(0,cost().signum());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT cost_amount FROM usage_events WHERE provider_task_id=?",BigDecimal.class,taskId).signum());
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId));
|
||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString());
|
||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString(),anyString());
|
||||
}
|
||||
|
||||
@Test void refundAndTaskStateRollBackTogetherWhenAuditFails() {
|
||||
@@ -108,7 +111,7 @@ class AsyncTaskPostgresTest {
|
||||
}
|
||||
|
||||
@Test void successfulSettlementRollsBackForRetryAndCannotBeRefundedByLateFailure() {
|
||||
when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","success","progress",100,"output",Map.of("model_url","https://example.test/model.glb"))));
|
||||
when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null)).thenReturn(Map.of("data",Map.of("status","success","progress",100,"output",Map.of("model_url","https://example.test/model.glb"))));
|
||||
doThrow(new IllegalStateException("simulated database write failure")).when(usage).updateTask(eq(taskId),eq("succeeded"),anyMap(),isNull(),isNull());
|
||||
assertThrows(IllegalStateException.class,this::poll);
|
||||
assertEquals("running",status());
|
||||
@@ -128,7 +131,7 @@ class AsyncTaskPostgresTest {
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings={"UPSTREAM_PROVIDER_ERROR", "UPSTREAM_AUTH_FAILED", "UPSTREAM_RATE_LIMITED"})
|
||||
void pollingErrorsKeepTheHoldAndClearOnRecovery(String code) {
|
||||
when(provider.poll("provider-vast","upstream-task"))
|
||||
when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null))
|
||||
.thenThrow(new ApiException(code,"private upstream details",502))
|
||||
.thenReturn(Map.of("data",Map.of("status","running","progress",75)));
|
||||
poll();
|
||||
@@ -160,7 +163,7 @@ class AsyncTaskPostgresTest {
|
||||
}
|
||||
|
||||
@Test void confirmedUpstreamFailureReleasesHoldImmediately() {
|
||||
when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","failed")));
|
||||
when(provider.poll("provider-vast","model-mock-3d","v3.1-20260211","upstream-task",null)).thenReturn(Map.of("data",Map.of("status","failed")));
|
||||
poll();
|
||||
assertEquals("failed",status());
|
||||
assertEquals(0,wallet("reserved_balance").signum());
|
||||
@@ -173,13 +176,13 @@ class AsyncTaskPostgresTest {
|
||||
OffsetDateTime migratedDeadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||
assertEquals(Duration.ofHours(1),Duration.between(createdAt,migratedDeadline));
|
||||
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL,provider_deadline_at=NULL WHERE id=?",taskId);
|
||||
when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenReturn(Map.of("data",Map.of("task_id","upstream-task")));
|
||||
when(provider.submit(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn(Map.of("data",Map.of("task_id","upstream-task")));
|
||||
tasks.completeMockTasks();
|
||||
OffsetDateTime submittedAt = jdbc.queryForObject("SELECT updated_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||
OffsetDateTime deadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
|
||||
assertEquals(Duration.ofMinutes(2),Duration.between(submittedAt,deadline));
|
||||
tasks.completeMockTasks();
|
||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString());
|
||||
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString(),anyString());
|
||||
verify(provider,never()).poll(anyString(),anyString());
|
||||
assertEquals("running",status());
|
||||
}
|
||||
|
||||
+1
@@ -51,6 +51,7 @@ class UsageCapacityPostgresTest {
|
||||
@Test void liveCapacitySeparatesQueuedWorkAndKeepsEveryGroupAndLongRunningTask() {
|
||||
GatewayGuard guard = mock(GatewayGuard.class);
|
||||
when(guard.defaultModelConcurrency()).thenReturn(7);
|
||||
when(guard.modelConcurrencyEnabled()).thenReturn(true);
|
||||
when(guard.defaultKeyConcurrency()).thenReturn(3);
|
||||
when(guard.queueWaitMs()).thenReturn(4500L);
|
||||
when(guard.capacitySnapshot(anyList())).thenAnswer(invocation -> {
|
||||
|
||||
Reference in new issue
Block a user