The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | |
| 3 | import { cursorFetchRange, isCursorCacheFresh, mergeCursorFetch } from "./accountCache.ts"; |
| 4 | import type { UsageRecord } from "@t3tools/provider-core/server/usage"; |
| 5 | |
| 6 | const DAY_MS = 24 * 60 * 60 * 1000; |
| 7 | |
| 8 | const record = (timestampMs: number): UsageRecord => ({ |
| 9 | provider: "cursor", |
| 10 | timestampMs, |
| 11 | model: "claude-fable-5", |
| 12 | sessionId: "conversation-1", |
| 13 | totals: { |
| 14 | uncachedInputTokens: 0, |
| 15 | cachedInputTokens: 0, |
| 16 | cacheCreationTokens: 0, |
| 17 | outputTokens: 1, |
| 18 | reasoningTokens: 0, |
| 19 | }, |
| 20 | reportedCostUsd: null, |
| 21 | speed: "standard", |
| 22 | dedupeKey: `cursor-account:a:${timestampMs}:0`, |
| 23 | }); |
| 24 | |
| 25 | describe("cursorAccountCache", () => { |
| 26 | it("reads only the history the cache is missing", () => { |
| 27 | const cache = { |
| 28 | accountKey: "a", |
| 29 | sinceMs: 0, |
| 30 | untilMs: 10 * DAY_MS, |
| 31 | fetchedAtMs: 10 * DAY_MS, |
| 32 | records: [record(5 * DAY_MS)], |
| 33 | }; |
| 34 | assert.isTrue(isCursorCacheFresh(cache, 10 * DAY_MS, 0)); |
| 35 | const narrow = { ...cache, sinceMs: DAY_MS }; |
| 36 | assert.isFalse(isCursorCacheFresh(narrow, 10 * DAY_MS, 0)); |
| 37 | const widenedRange = cursorFetchRange(narrow, 0, 10 * DAY_MS); |
| 38 | assert.deepStrictEqual(widenedRange, { sinceMs: 0, untilMs: DAY_MS }); |
| 39 | const widened = mergeCursorFetch(narrow, "a", widenedRange, cache.records, 10 * DAY_MS, 0); |
| 40 | assert.isTrue(isCursorCacheFresh(widened.cache, 10 * DAY_MS, 0)); |
| 41 | assert.deepStrictEqual(cursorFetchRange(widened.cache, 0, 11 * DAY_MS), { |
| 42 | sinceMs: 10 * DAY_MS - 60 * 60 * 1000, |
| 43 | untilMs: 11 * DAY_MS, |
| 44 | }); |
| 45 | const nowMs = 100 * DAY_MS; |
| 46 | const range = cursorFetchRange(cache, 10 * DAY_MS, nowMs); |
| 47 | assert.deepStrictEqual(range, { sinceMs: 10 * DAY_MS, untilMs: nowMs }); |
| 48 | |
| 49 | const { cache: merged } = mergeCursorFetch( |
| 50 | cache, |
| 51 | "a", |
| 52 | range, |
| 53 | [record(50 * DAY_MS)], |
| 54 | nowMs, |
| 55 | 10 * DAY_MS, |
| 56 | ); |
| 57 | assert.deepStrictEqual( |
| 58 | merged.records.map((entry) => entry.timestampMs), |
| 59 | [50 * DAY_MS], |
| 60 | ); |
| 61 | assert.deepStrictEqual(cursorFetchRange(merged, 11 * DAY_MS, nowMs + DAY_MS), { |
| 62 | sinceMs: nowMs - 60 * 60 * 1000, |
| 63 | untilMs: nowMs + DAY_MS, |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | it("reports an edge that only confirms the cache as unchanged", () => { |
| 68 | const cache = { |
| 69 | accountKey: "a", |
| 70 | sinceMs: 0, |
| 71 | untilMs: 10 * DAY_MS, |
| 72 | fetchedAtMs: 10 * DAY_MS, |
| 73 | records: [record(DAY_MS), record(10 * DAY_MS)], |
| 74 | }; |
| 75 | const range = cursorFetchRange(cache, 0, 11 * DAY_MS); |
| 76 | assert.isFalse( |
| 77 | mergeCursorFetch(cache, "a", range, [record(10 * DAY_MS)], 11 * DAY_MS, 0).changed, |
| 78 | ); |
| 79 | const pruned = mergeCursorFetch( |
| 80 | cache, |
| 81 | "a", |
| 82 | range, |
| 83 | [record(10 * DAY_MS)], |
| 84 | 11 * DAY_MS, |
| 85 | 2 * DAY_MS, |
| 86 | ); |
| 87 | assert.isTrue(pruned.changed); |
| 88 | assert.deepStrictEqual(pruned.cache.records, [record(10 * DAY_MS)]); |
| 89 | assert.isTrue( |
| 90 | mergeCursorFetch(cache, "a", range, [record(10 * DAY_MS)], 11 * DAY_MS, 2 * DAY_MS).changed, |
| 91 | ); |
| 92 | assert.isTrue( |
| 93 | mergeCursorFetch( |
| 94 | { ...cache, sinceMs: DAY_MS, records: [] }, |
| 95 | "a", |
| 96 | { sinceMs: 0, untilMs: 11 * DAY_MS }, |
| 97 | [], |
| 98 | 11 * DAY_MS, |
| 99 | 0, |
| 100 | ).changed, |
| 101 | ); |
| 102 | assert.isTrue( |
| 103 | mergeCursorFetch( |
| 104 | cache, |
| 105 | "a", |
| 106 | range, |
| 107 | [record(10 * DAY_MS), record(10.5 * DAY_MS)], |
| 108 | 11 * DAY_MS, |
| 109 | 0, |
| 110 | ).changed, |
| 111 | ); |
| 112 | }); |
| 113 | }); |