mirror of
https://github.com/storytold/autocraft.git
synced 2026-12-01 02:24:06 +01:00
53 lines
2.8 KiB
TypeScript
53 lines
2.8 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { mkdtempSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { test } from "node:test";
|
|
import { askJev, runJev, saveUsage, setRunner } from "../src/classify/run.ts";
|
|
import { SCRATCH } from "./scratch.ts";
|
|
import { connectDb } from "../src/db/open.ts";
|
|
import { sqliteOnly } from "./test-db.ts";
|
|
|
|
const answers = {
|
|
type: { choice: "performance", confidence: 0.8, probabilities: { performance: 0.8, bugfix: 0.2 } },
|
|
security: { noul: 0.7 }, needs_decision: { noul: 0.1 },
|
|
};
|
|
const fresh = async () => await connectDb(join(mkdtempSync(join(SCRATCH, "jevcost-")), "t.db"));
|
|
const cost = async (db: Awaited<ReturnType<typeof fresh>>, agent: string) =>
|
|
(await db.get(db.k.selectFrom("token_usage").select("cost_usd as c").where("agent", "=", agent)))!.c;
|
|
|
|
test("a Jev call is priced by its input tokens", async () => {
|
|
const db = await fresh();
|
|
const restore = setRunner(async () => ({ code: 0, stderr: "", stdout: JSON.stringify({ answers, usage: { input_tokens: 1_000_000, output_tokens: 500 } }) }));
|
|
try {
|
|
const item = { repo: "a", number: 1, kind: "pr" as const, title: "t", body: "b", lines_changed: 1, files_changed: 1, author_association: null };
|
|
await runJev(db, "run1", [item], 1);
|
|
} finally { restore(); }
|
|
assert.ok(Math.abs((await cost(db, "jev-a#1"))! - 0.042) < 1e-9);
|
|
});
|
|
|
|
test("the migration prices unpriced Jev rows and leaves other models alone", { skip: sqliteOnly("SQLite data migration in src/db.ts migrate(); Postgres starts from schema.pg.sql and a copy of a migrated SQLite database") }, async () => {
|
|
const dir = mkdtempSync(join(SCRATCH, "jevcost-"));
|
|
const path = join(dir, "t.db");
|
|
const db = await connectDb(path);
|
|
const u = { input: 2_000_000, output: 5, cacheRead: 0, cacheWrite: 0, cost: null };
|
|
await saveUsage(db, "r", "jev-x", "classify", "jev-latest", 1, u, "s", "e");
|
|
await saveUsage(db, "r", "haiku-x", "classify", "haiku", 1, u, "s", "e");
|
|
await db.close();
|
|
const again = await connectDb(path);
|
|
assert.ok(Math.abs((await cost(again, "jev-x"))! - 0.084) < 1e-9);
|
|
assert.equal(await cost(again, "haiku-x"), null);
|
|
});
|
|
|
|
test("priority and dupes Jev calls are saved under their own stage", async () => {
|
|
const db = await fresh();
|
|
const restore = setRunner(async () => ({ code: 0, stderr: "", stdout: JSON.stringify({ answers: {}, usage: { input_tokens: 500_000, output_tokens: 3 } }) }));
|
|
try {
|
|
const item = { repo: "a", number: 1, kind: "pr" as const, title: "t", body: "b", lines_changed: 1, files_changed: 1, author_association: null };
|
|
await askJev("priority.jev", item, db);
|
|
} finally { restore(); }
|
|
const row = (await db.get(db.k.selectFrom("token_usage").select(["stage", "model", "cost_usd as c"])))!;
|
|
assert.equal(row.stage, "priority");
|
|
assert.equal(row.model, "jev-latest");
|
|
assert.ok(Math.abs(row.c! - 0.021) < 1e-9);
|
|
});
|