Fix verified image pricing and settle async image usage
This commit is contained in:
1 parent
4c4f5ad68d
commit
8a13ef9c02
22 files changed
+506
-40
No files matched your search
@@ -0,0 +1,20 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class DeferredModelTest {
|
||||
@Test void deferredRouteIsRejectedByPublicIdLegacyIdAndAliasEvenAfterPricing() {
|
||||
var catalog=mock(CatalogService.class);
|
||||
var key=Map.<String,Object>of("id","test");
|
||||
var model=Map.<String,Object>of("id","deferred","upstreamModel","SparkDesk","integrationDeferred",true,"pricing",Map.of("input",1,"output",1,"unit","CNY / 1M tokens"));
|
||||
when(catalog.listModels(false)).thenReturn(List.of(model));
|
||||
when(catalog.keyCanInvoke(key,"deferred")).thenReturn(true);
|
||||
when(catalog.activeModel(anyString())).thenReturn(null);
|
||||
when(catalog.activeModel("deferred")).thenReturn(model);
|
||||
for(String id:List.of("deferred","SparkDesk",GatewayModelIds.publicId(model))) assertEquals("MODEL_ONBOARDING_DEFERRED",assertThrows(ApiException.class,()->GatewayModelIds.resolve(catalog,key,id)).getCode());
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AsyncPriceScheduleTest {
|
||||
private Map<String,Object> image() { return new LinkedHashMap<>(Map.of("unit","CNY / 1M tokens","input",20.16,"imageInput",32.256,"output",120.96,"taskBilling","openai-image-tokens")); }
|
||||
@Test void inputModalitiesUseDifferentRatesAndOnlyActualUsageIsCharged() {
|
||||
var pricing=image();
|
||||
var result=Map.<String,Object>of("usage",Map.of("input_tokens",50,"output_tokens",50,"input_tokens_details",Map.of("text_tokens",10,"image_tokens",40)));
|
||||
assertEquals(new BigDecimal("0.00753984"),AsyncPriceSchedule.bill(pricing,result).amount());
|
||||
assertTrue(AsyncPriceSchedule.reserve(pricing,Map.of("prompt","draw")).compareTo(AsyncPriceSchedule.bill(pricing,result).amount())>0);
|
||||
}
|
||||
@Test void missingOrInvalidUsageCannotBecomeAnEstimatedCharge() {
|
||||
assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(image(),Map.of("base64","png")));
|
||||
for(Object invalid:List.of(-1,0.5,"NaN",100_000_001)) assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(image(),Map.of("usage",Map.of("input_tokens",invalid,"output_tokens",50))));
|
||||
assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(image(),Map.of("usage",Map.of("input_tokens",50,"output_tokens",50,"input_tokens_details",Map.of("text_tokens",50,"image_tokens",1)))));
|
||||
}
|
||||
@Test void ambiguousCachedModalitiesAreNotDoubleCounted() {
|
||||
var price=image(); price.put("cachedInput",8.064);
|
||||
var usage=Map.<String,Object>of("input_tokens",50,"output_tokens",50,"input_tokens_details",Map.of("text_tokens",10,"image_tokens",40,"cached_tokens",10));
|
||||
assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(price,Map.of("usage",usage)));
|
||||
}
|
||||
@Test void geminiUsageIncludesThinkingAndRequiresPromptAndCandidateCounts() {
|
||||
var price=Map.<String,Object>of("unit","CNY / 1M tokens","input",8.064,"output",483.84,"taskBilling","gemini-image-tokens");
|
||||
var bill=AsyncPriceSchedule.bill(price,Map.of("usageMetadata",Map.of("promptTokenCount",100,"candidatesTokenCount",1000,"thoughtsTokenCount",20)));
|
||||
assertEquals(new BigDecimal("0.49432320"),bill.amount());
|
||||
assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(price,Map.of("usageMetadata",Map.of("totalTokenCount",1120))));
|
||||
}
|
||||
@Test void fixedCnyPricesAreSingleImageAndUnsupportedUnitsAreRejected() {
|
||||
var price=Map.<String,Object>of("unit","CNY / image","price",0.4032);
|
||||
assertEquals(new BigDecimal("0.4032"),AsyncPriceSchedule.reserve(price,Map.of("n",1)));
|
||||
assertThrows(ApiException.class,()->AsyncPriceSchedule.reserve(price,Map.of("n",2)));
|
||||
for(String unit:List.of("USD / task","CNY / second","CNY / 1M tokens")) assertFalse(AsyncPriceSchedule.ready(Map.of("unit",unit,"input",1,"output",1,"price",1)));
|
||||
}
|
||||
}
|
||||
+23
@@ -24,6 +24,29 @@ import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
class AsyncProviderClientTest {
|
||||
@Test void imagesPreserveUsageAndGeminiUsesItsNativeEndpoint() throws Exception {
|
||||
HttpServer server=HttpServer.create(new InetSocketAddress("127.0.0.1",0),0);
|
||||
var path=new AtomicReference<String>();
|
||||
server.createContext("/",exchange->{
|
||||
path.set(exchange.getRequestURI().getPath());
|
||||
String body=path.get().contains("generateContent")
|
||||
? "{\"candidates\":[{\"content\":{\"parts\":[{\"inlineData\":{\"mimeType\":\"image/png\",\"data\":\"cG5n\"}}]}}],\"usageMetadata\":{\"promptTokenCount\":20,\"candidatesTokenCount\":100}}"
|
||||
: "{\"data\":[{\"b64_json\":\"cG5n\"}],\"usage\":{\"input_tokens\":10,\"output_tokens\":50}}";
|
||||
byte[] bytes=body.getBytes(StandardCharsets.UTF_8); exchange.sendResponseHeaders(200,bytes.length);
|
||||
try(var out=exchange.getResponseBody()){out.write(bytes);}
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
var jdbc=mock(JdbcTemplate.class); var cipher=new SecretCipher("test-encryption");
|
||||
when(jdbc.queryForMap(any(String.class),eq("relay"))).thenReturn(Map.of("code","apipro","protocol","openai-compatible","base_url","http://127.0.0.1:"+server.getAddress().getPort()+"/v1","credential_ciphertext",cipher.encrypt("test-key")));
|
||||
var client=new AsyncProviderClient(jdbc,cipher,new ObjectMapper(),mock(Environment.class));
|
||||
var image=(Map<?,?>)client.submit("relay","gpt-image-2","image",Map.of("prompt","cat")).get("_moyiInlineResult");
|
||||
assertEquals("/v1/images/generations",path.get()); assertTrue(image.containsKey("usage"));
|
||||
var gemini=(Map<?,?>)client.submit("relay","gemini-3-pro-image-preview","image",Map.of("prompt","cat")).get("_moyiInlineResult");
|
||||
assertEquals("/v1beta/models/gemini-3-pro-image-preview:generateContent",path.get());
|
||||
assertEquals("cG5n",gemini.get("base64")); assertTrue(gemini.containsKey("usageMetadata"));
|
||||
} finally { server.stop(0); }
|
||||
}
|
||||
@Test
|
||||
void upstreamErrorDoesNotExposeProviderMessage() throws Exception {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
|
||||
+28
@@ -186,4 +186,32 @@ class AsyncTaskPostgresTest {
|
||||
verify(provider,never()).poll(anyString(),anyString());
|
||||
assertEquals("running",status());
|
||||
}
|
||||
|
||||
@Test void imageTokensSettleSnapshotOnceAndReleaseUnusedReservation() throws Exception {
|
||||
var pricing=Map.of("unit","CNY / 1M tokens","input",20.16,"imageInput",32.256,"output",120.96,"taskBilling","openai-image-tokens");
|
||||
jdbc.update("UPDATE model_tasks SET pricing_snapshot=CAST(? AS jsonb) WHERE id=?",json.writeValueAsString(pricing),taskId);
|
||||
var result=Map.<String,Object>of("url","https://example.test/image.png","usage",Map.of("input_tokens",50,"output_tokens",50,"input_tokens_details",Map.of("text_tokens",10,"image_tokens",40)));
|
||||
ReflectionTestUtils.invokeMethod(tasks,"finishTask",taskId,"succeeded",100,result,null,null);
|
||||
ReflectionTestUtils.invokeMethod(tasks,"finishTask",taskId,"succeeded",100,result,null,null);
|
||||
fail();
|
||||
assertEquals(new BigDecimal("0.00753984"),cost());
|
||||
assertEquals(new BigDecimal("9.99246016"),wallet("balance"));
|
||||
assertEquals(0,wallet("reserved_balance").signum());
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId));
|
||||
assertEquals(50L,jdbc.queryForObject("SELECT input_units FROM usage_events WHERE provider_task_id=?",Long.class,taskId));
|
||||
}
|
||||
|
||||
@Test void completedImageWithoutUsageIsReturnedAndHeldForReconciliationWithoutCharging() throws Exception {
|
||||
var pricing=Map.of("unit","CNY / 1M tokens","input",1,"output",10,"taskBilling","gemini-image-tokens");
|
||||
jdbc.update("UPDATE model_tasks SET pricing_snapshot=CAST(? AS jsonb) WHERE id=?",json.writeValueAsString(pricing),taskId);
|
||||
var result=Map.<String,Object>of("base64","png");
|
||||
ReflectionTestUtils.invokeMethod(tasks,"finishTask",taskId,"succeeded",100,result,null,null);
|
||||
assertEquals("succeeded",status());
|
||||
assertNull(cost());
|
||||
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
|
||||
assertEquals(0,wallet("reserved_balance").signum());
|
||||
assertEquals("pending",jdbc.queryForObject("SELECT cost_status FROM usage_events WHERE provider_task_id=?",String.class,taskId));
|
||||
assertEquals("TASK_USAGE_PENDING",jdbc.queryForObject("SELECT error_code FROM usage_events WHERE provider_task_id=?",String.class,taskId));
|
||||
assertTrue(jdbc.queryForObject("SELECT result::text FROM model_tasks WHERE id=?",String.class,taskId).contains("png"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user