9abea02420
Stats tests set PI_CONFIG_DIR + setAgentDir(home/<config>/agent), which resolves equal to the default agent dir and routes stats.db through $XDG_DATA_HOME/omp/stats.db whenever an XDG var is set, leaking rows across tests. The shared installStatsTestIsolation helper now snapshots and clears XDG_*_HOME with PI_CONFIG_DIR on each test, points the agent dir at a fresh TempDir, closes the DB handle, and restores the prior env after the test.
73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import { getDashboardStats } from "@oh-my-pi/omp-stats/aggregator";
|
|
import { initDb, insertMessageStats } from "@oh-my-pi/omp-stats/db";
|
|
import type { MessageStats } from "@oh-my-pi/omp-stats/types";
|
|
import { installStatsTestIsolation } from "./helpers/temp-agent";
|
|
|
|
installStatsTestIsolation("@pi-stats-db-range-");
|
|
|
|
function makeMessage(timestamp: number, entryId: string): MessageStats {
|
|
return {
|
|
sessionFile: "/tmp/session.jsonl",
|
|
entryId,
|
|
folder: "/tmp/project",
|
|
model: "gpt-5.4",
|
|
provider: "openai-codex",
|
|
api: "openai-codex-responses",
|
|
timestamp,
|
|
duration: 1000,
|
|
ttft: 100,
|
|
stopReason: "stop",
|
|
errorMessage: null,
|
|
usage: {
|
|
input: 1000,
|
|
output: 500,
|
|
cacheRead: 200,
|
|
cacheWrite: 0,
|
|
totalTokens: 1700,
|
|
cost: {
|
|
input: 0,
|
|
output: 0,
|
|
cacheRead: 0,
|
|
cacheWrite: 0,
|
|
total: 0,
|
|
},
|
|
},
|
|
agentType: "main",
|
|
};
|
|
}
|
|
|
|
describe("getDashboardStats time range", () => {
|
|
it("filters dashboard stats by selected range", async () => {
|
|
await initDb();
|
|
|
|
const now = Date.now();
|
|
insertMessageStats([makeMessage(now, "within-24h"), makeMessage(now - 48 * 60 * 60 * 1000, "outside-24h")]);
|
|
|
|
const dayStats = await getDashboardStats("24h");
|
|
expect(dayStats.overall.totalRequests).toBe(1);
|
|
expect(dayStats.byModel[0]).toMatchObject({
|
|
totalRequests: 1,
|
|
model: "gpt-5.4",
|
|
provider: "openai-codex",
|
|
});
|
|
|
|
const weekStats = await getDashboardStats("7d");
|
|
expect(weekStats.overall.totalRequests).toBe(2);
|
|
expect(weekStats.byModel[0]).toMatchObject({ totalRequests: 2, model: "gpt-5.4", provider: "openai-codex" });
|
|
|
|
const allStats = await getDashboardStats("all");
|
|
expect(allStats.overall.totalRequests).toBe(2);
|
|
});
|
|
|
|
it("falls back to 24h for unknown range", async () => {
|
|
await initDb();
|
|
|
|
const now = Date.now();
|
|
insertMessageStats([makeMessage(now, "within-24h"), makeMessage(now - 48 * 60 * 60 * 1000, "outside-24h")]);
|
|
|
|
const stats = await getDashboardStats("last century");
|
|
expect(stats.overall.totalRequests).toBe(1);
|
|
});
|
|
});
|