|
2 | 2 | // crashing the caller's async iteration. |
3 | 3 | import nodeFs from "node:fs"; |
4 | 4 | import fs from "node:fs/promises"; |
5 | | -import os from "node:os"; |
6 | 5 | import path from "node:path"; |
7 | 6 | import { afterEach, describe, expect, it, vi } from "vitest"; |
8 | | -import { loadSessionLogs } from "./session-cost-usage.js"; |
| 7 | +import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js"; |
| 8 | +import { withEnvAsync } from "../test-utils/env.js"; |
| 9 | +import { |
| 10 | + loadCostUsageSummaryFromCache, |
| 11 | + loadSessionLogs, |
| 12 | + refreshCostUsageCache, |
| 13 | +} from "./session-cost-usage.js"; |
9 | 14 |
|
10 | | -describe("session cost usage stream errors", () => { |
11 | | - let tempDir = ""; |
| 15 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
12 | 16 |
|
13 | | - afterEach(async () => { |
| 17 | +describe("session cost usage stream errors", () => { |
| 18 | + afterEach(() => { |
14 | 19 | vi.restoreAllMocks(); |
15 | | - if (tempDir) { |
16 | | - await fs.rm(tempDir, { recursive: true, force: true }); |
17 | | - tempDir = ""; |
18 | | - } |
19 | 20 | }); |
20 | 21 |
|
21 | 22 | it("does not crash when the transcript stream emits an error mid-read", async () => { |
22 | | - tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-cost-stream-")); |
| 23 | + const tempDir = tempDirs.make("openclaw-session-cost-stream-"); |
23 | 24 | const sessionsDir = path.join(tempDir, "agents", "main", "sessions"); |
24 | 25 | await fs.mkdir(sessionsDir, { recursive: true }); |
25 | 26 | const sessionFile = path.join(sessionsDir, "sess-stream-error.jsonl"); |
@@ -50,4 +51,49 @@ describe("session cost usage stream errors", () => { |
50 | 51 |
|
51 | 52 | expect(logs).toEqual([]); |
52 | 53 | }); |
| 54 | + |
| 55 | + it("does not persist a partial durable cache entry after a stream error", async () => { |
| 56 | + const tempDir = tempDirs.make("openclaw-session-cost-cache-stream-"); |
| 57 | + const sessionsDir = path.join(tempDir, "agents", "main", "sessions"); |
| 58 | + await fs.mkdir(sessionsDir, { recursive: true }); |
| 59 | + const sessionFile = path.join(sessionsDir, "sess-cache-stream-error.jsonl"); |
| 60 | + const usageEntry = (timestamp: string, input: number) => |
| 61 | + JSON.stringify({ |
| 62 | + type: "message", |
| 63 | + timestamp, |
| 64 | + message: { |
| 65 | + role: "assistant", |
| 66 | + usage: { input, output: 0, totalTokens: input, cost: { total: input / 1000 } }, |
| 67 | + }, |
| 68 | + }); |
| 69 | + await fs.writeFile(sessionFile, `${usageEntry("2026-07-06T12:00:00.000Z", 10)}\n`, "utf-8"); |
| 70 | + |
| 71 | + await withEnvAsync({ OPENCLAW_STATE_DIR: tempDir }, async () => { |
| 72 | + await refreshCostUsageCache(); |
| 73 | + const cachePath = path.join(sessionsDir, ".usage-cost-cache.json"); |
| 74 | + const cacheBefore = await fs.readFile(cachePath, "utf-8"); |
| 75 | + |
| 76 | + await fs.appendFile(sessionFile, `${usageEntry("2026-07-06T12:01:00.000Z", 20)}\n`, "utf-8"); |
| 77 | + const originalCreateReadStream = nodeFs.createReadStream; |
| 78 | + vi.spyOn(nodeFs, "createReadStream").mockImplementationOnce((...args: unknown[]) => { |
| 79 | + const stream = originalCreateReadStream.apply(nodeFs, args as never); |
| 80 | + process.nextTick(() => { |
| 81 | + stream.emit("error", new Error("stream read failed")); |
| 82 | + }); |
| 83 | + return stream; |
| 84 | + }); |
| 85 | + |
| 86 | + await expect(refreshCostUsageCache()).rejects.toThrow("stream read failed"); |
| 87 | + expect(await fs.readFile(cachePath, "utf-8")).toBe(cacheBefore); |
| 88 | + |
| 89 | + const summary = await loadCostUsageSummaryFromCache({ |
| 90 | + startMs: Date.UTC(2026, 6, 6), |
| 91 | + endMs: Date.UTC(2026, 6, 7), |
| 92 | + requestRefresh: false, |
| 93 | + }); |
| 94 | + expect(summary.totals.totalTokens).toBe(10); |
| 95 | + expect(summary.cacheStatus?.status).toBe("partial"); |
| 96 | + expect(summary.cacheStatus?.pendingFiles).toBe(1); |
| 97 | + }); |
| 98 | + }); |
53 | 99 | }); |
0 commit comments