This commit is contained in:
commit
db82325ed7
216 files changed
+33718
No files matched your search
@@ -0,0 +1,227 @@
|
||||
package run.webup.platform;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
class WalletTaskIntegrationTest {
|
||||
@Autowired IdentityService identity;
|
||||
@Autowired WalletService wallets;
|
||||
@Autowired TaskService tasks;
|
||||
@Autowired ObjectMapper json;
|
||||
@Autowired ReconciliationService reconciliation;
|
||||
@MockitoBean EphemeralStore store;
|
||||
UUID user, project, model, price, provider;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
TestDatabase.clear(identity.db());
|
||||
user = identity.create("billing@test.com", "Tester", "PasswordStrong123", "local");
|
||||
project = UUID.randomUUID();
|
||||
model = UUID.randomUUID();
|
||||
provider = UUID.randomUUID();
|
||||
price = UUID.randomUUID();
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO projects(id,owner_id,name,description,stack,requirements) VALUES"
|
||||
+ " (?,?,?,'','Vue 3','')",
|
||||
project,
|
||||
user,
|
||||
"Test");
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_providers(id,name,base_url,api_type,encrypted_key,enabled,timeout_seconds)"
|
||||
+ " VALUES"
|
||||
+ " (?,'Test','https://api.openai.com/v1','OPENAI','not-a-real-key',TRUE,30)",
|
||||
provider);
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_models(id,provider_id,name,model_id,context_length,max_output,tools_enabled,vision_enabled,enabled)"
|
||||
+ " VALUES (?,?,'Test','test-model',8192,256,TRUE,FALSE,TRUE)",
|
||||
model,
|
||||
provider);
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_model_price_versions(id,model_id,input_price,output_price,cache_read_price,cache_write_price)"
|
||||
+ " VALUES (?,?,1,2,0.1,1)",
|
||||
price,
|
||||
model);
|
||||
identity.db().update("UPDATE ai_models SET current_price_version=? WHERE id=?", price, model);
|
||||
}
|
||||
|
||||
BigDecimal n(String v) {
|
||||
return new BigDecimal(v);
|
||||
}
|
||||
|
||||
void credit() {
|
||||
wallets.adjust(user, n("100"), "credit", "Test credit", user, "local");
|
||||
}
|
||||
|
||||
UUID task() {
|
||||
credit();
|
||||
return UUID.fromString(
|
||||
tasks.create(user, project, model, "Build and test", n("20"), "task").get("id").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void creditIdempotencyAndConflict() {
|
||||
credit();
|
||||
credit();
|
||||
assertEquals(0, n("100").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() -> wallets.adjust(user, n("101"), "credit", "Test credit", user, "local"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reserveSettleReleaseAndReplay() {
|
||||
credit();
|
||||
UUID r = wallets.reserve(user, n("20"), "test");
|
||||
wallets.charge(r, n("13.5"), "usage:test");
|
||||
wallets.charge(r, n("13.5"), "usage:test");
|
||||
wallets.release(r);
|
||||
wallets.release(r);
|
||||
assertEquals(0, n("86.5").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannotOverspendOrDebitReservedFunds() {
|
||||
credit();
|
||||
UUID r = wallets.reserve(user, n("90"), "test");
|
||||
assertThrows(ApiError.Failure.class, () -> wallets.reserve(user, n("11"), "second"));
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() -> wallets.adjust(user, n("-11"), "debit", "test", user, "local"));
|
||||
assertThrows(ApiError.Failure.class, () -> wallets.charge(r, n("91"), "too-much"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentReservationsCannotOverdraw() throws Exception {
|
||||
credit();
|
||||
try (var pool = Executors.newFixedThreadPool(6)) {
|
||||
List<Callable<Boolean>> calls = new ArrayList<>();
|
||||
for (int i = 0; i < 6; i++) {
|
||||
String business = "concurrent-" + i;
|
||||
calls.add(
|
||||
() -> {
|
||||
try {
|
||||
wallets.reserve(user, n("30"), business);
|
||||
return true;
|
||||
} catch (ApiError.Failure f) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
int success = 0;
|
||||
for (Future<Boolean> f : pool.invokeAll(calls)) if (f.get()) success++;
|
||||
assertEquals(3, success);
|
||||
assertEquals(0, n("90").compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void taskReplaySnapshotAndProjectExclusivity() {
|
||||
UUID id = task();
|
||||
assertEquals(
|
||||
id.toString(),
|
||||
tasks.create(user, project, model, "Build and test", n("20"), "task").get("id").toString());
|
||||
assertThrows(
|
||||
ApiError.Failure.class,
|
||||
() -> tasks.create(user, project, model, "Another task", n("20"), "second"));
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"INSERT INTO"
|
||||
+ " ai_model_price_versions(id,model_id,input_price,output_price,cache_read_price,cache_write_price)"
|
||||
+ " VALUES (?,?,99,99,99,99)",
|
||||
UUID.randomUUID(),
|
||||
model);
|
||||
assertEquals(price.toString(), tasks.find(id, user).get("price_version_id").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void pausedCancelledTaskReleasesBudget() {
|
||||
UUID id = task();
|
||||
tasks.action(id, user, "pause");
|
||||
assertEquals("PAUSED", tasks.find(id, user).get("status"));
|
||||
identity.db().update("UPDATE ai_tasks SET step_count=30 WHERE id=?", id);
|
||||
tasks.action(id, user, "resume");
|
||||
assertEquals(60, ((Number) tasks.find(id, user).get("max_steps")).intValue());
|
||||
tasks.action(id, user, "cancel");
|
||||
assertEquals("CANCELLED", tasks.find(id, user).get("status"));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactUsageChargesWithSnapshot() throws Exception {
|
||||
UUID id = task();
|
||||
var claimed = tasks.claim();
|
||||
String lease = claimed.get("lease_token").toString();
|
||||
var usage =
|
||||
new ModelGateway.Usage(
|
||||
1000000,
|
||||
1000000,
|
||||
1000000,
|
||||
0,
|
||||
true,
|
||||
json.readTree("{\"prompt_tokens\":2000000,\"completion_tokens\":1000000}"));
|
||||
var result =
|
||||
new ModelGateway.Result(
|
||||
json.createObjectNode().put("role", "assistant").put("content", "Done"),
|
||||
List.of(),
|
||||
usage,
|
||||
"request");
|
||||
tasks.response(id, lease, result);
|
||||
tasks.finish(id, lease, "COMPLETED", "done", true);
|
||||
assertEquals(0, n("96.9").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingUsageRequiresManualReconciliation() {
|
||||
UUID id = task();
|
||||
var claimed = tasks.claim();
|
||||
var usage = new ModelGateway.Usage(0, 0, 0, 0, false, json.createObjectNode());
|
||||
tasks.response(
|
||||
id,
|
||||
claimed.get("lease_token").toString(),
|
||||
new ModelGateway.Result(
|
||||
json.createObjectNode().put("role", "assistant"), List.of(), usage, "missing"));
|
||||
assertEquals("REVIEW_REQUIRED", tasks.find(id, user).get("status"));
|
||||
assertEquals(0, n("20").compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
reconciliation.resolve(id, user, n("3"), "Reconciled with provider console", "local");
|
||||
assertEquals(0, n("97").compareTo((BigDecimal) wallets.get(user).get("balance")));
|
||||
assertEquals(0, BigDecimal.ZERO.compareTo((BigDecimal) wallets.get(user).get("reserved")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiredLeaseDoesNotReplayOrReleaseFunds() {
|
||||
UUID id = task();
|
||||
tasks.claim();
|
||||
identity
|
||||
.db()
|
||||
.update(
|
||||
"UPDATE ai_tasks SET lease_expires_at=TIMESTAMP '2000-01-01 00:00:00' WHERE id=?", id);
|
||||
assertNull(tasks.claim());
|
||||
assertEquals("REVIEW_REQUIRED", tasks.find(id, user).get("status"));
|
||||
assertThrows(ApiError.Failure.class, () -> tasks.action(id, user, "resume"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user