refactor(test): dedupe env setup across suites

This commit is contained in:
Peter Steinberger
2026-02-21 13:12:53 +00:00
parent f903603722
commit 7724abeee0
5 changed files with 43 additions and 122 deletions

View File

@@ -3,6 +3,7 @@ import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { withEnvAsync } from "../test-utils/env.js";
import {
discoverAllSessions,
loadCostUsageSummary,
@@ -12,6 +13,9 @@ import {
} from "./session-cost-usage.js";
describe("session cost usage", () => {
const withStateDir = async <T>(stateDir: string, fn: () => Promise<T>): Promise<T> =>
await withEnvAsync({ OPENCLAW_STATE_DIR: stateDir }, fn);
it("aggregates daily totals with log cost and pricing fallback", async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-cost-"));
const sessionsDir = path.join(root, "agents", "main", "sessions");
@@ -98,20 +102,12 @@ describe("session cost usage", () => {
},
} as unknown as OpenClawConfig;
const originalState = process.env.OPENCLAW_STATE_DIR;
process.env.OPENCLAW_STATE_DIR = root;
try {
await withStateDir(root, async () => {
const summary = await loadCostUsageSummary({ days: 30, config });
expect(summary.daily.length).toBe(1);
expect(summary.totals.totalTokens).toBe(50);
expect(summary.totals.totalCost).toBeCloseTo(0.03003, 5);
} finally {
if (originalState === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = originalState;
}
}
});
});
it("summarizes a single session file", async () => {
@@ -225,22 +221,14 @@ describe("session cost usage", () => {
const now = Date.now();
await fs.utimes(sessionFile, now / 1000, now / 1000);
const originalState = process.env.OPENCLAW_STATE_DIR;
process.env.OPENCLAW_STATE_DIR = root;
try {
await withStateDir(root, async () => {
const sessions = await discoverAllSessions({
startMs: now - 7 * 24 * 60 * 60 * 1000,
endMs: now - 24 * 60 * 60 * 1000,
});
expect(sessions.length).toBe(1);
expect(sessions[0]?.sessionId).toBe("sess-late");
} finally {
if (originalState === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = originalState;
}
}
});
});
it("resolves non-main absolute sessionFile using explicit agentId for cost summary", async () => {
@@ -270,9 +258,7 @@ describe("session cost usage", () => {
"utf-8",
);
const originalState = process.env.OPENCLAW_STATE_DIR;
process.env.OPENCLAW_STATE_DIR = root;
try {
await withStateDir(root, async () => {
const summary = await loadSessionCostSummary({
sessionId: "sess-worker-1",
sessionEntry: {
@@ -284,13 +270,7 @@ describe("session cost usage", () => {
});
expect(summary?.totalTokens).toBe(18);
expect(summary?.totalCost).toBeCloseTo(0.01, 5);
} finally {
if (originalState === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = originalState;
}
}
});
});
it("resolves non-main absolute sessionFile using explicit agentId for timeseries", async () => {
@@ -316,9 +296,7 @@ describe("session cost usage", () => {
"utf-8",
);
const originalState = process.env.OPENCLAW_STATE_DIR;
process.env.OPENCLAW_STATE_DIR = root;
try {
await withStateDir(root, async () => {
const timeseries = await loadSessionUsageTimeSeries({
sessionId: "sess-worker-2",
sessionEntry: {
@@ -330,13 +308,7 @@ describe("session cost usage", () => {
});
expect(timeseries?.points.length).toBe(1);
expect(timeseries?.points[0]?.totalTokens).toBe(8);
} finally {
if (originalState === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = originalState;
}
}
});
});
it("resolves non-main absolute sessionFile using explicit agentId for logs", async () => {
@@ -360,9 +332,7 @@ describe("session cost usage", () => {
"utf-8",
);
const originalState = process.env.OPENCLAW_STATE_DIR;
process.env.OPENCLAW_STATE_DIR = root;
try {
await withStateDir(root, async () => {
const logs = await loadSessionLogs({
sessionId: "sess-worker-3",
sessionEntry: {
@@ -375,13 +345,7 @@ describe("session cost usage", () => {
expect(logs).toHaveLength(1);
expect(logs?.[0]?.content).toContain("hello worker");
expect(logs?.[0]?.role).toBe("user");
} finally {
if (originalState === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = originalState;
}
}
});
});
it("strips inbound and untrusted metadata blocks from session usage logs", async () => {