-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathCostTimeSeriesChart.test.ts
More file actions
156 lines (140 loc) · 3.56 KB
/
Copy pathCostTimeSeriesChart.test.ts
File metadata and controls
156 lines (140 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// @vitest-environment jsdom
import {
afterEach,
beforeEach,
describe,
expect,
it,
} from "vitest";
import { mount, tick, unmount } from "svelte";
// @ts-ignore
import CostTimeSeriesChart from "./CostTimeSeriesChart.svelte";
import { usage } from "../../stores/usage.svelte.js";
import type {
DailyUsageEntry,
UsageSummaryResponse,
} from "../../api/types/usage.js";
const OBSERVED_WIDTH = 1648;
class ImmediateResizeObserver implements ResizeObserver {
private readonly callback: ResizeObserverCallback;
constructor(callback: ResizeObserverCallback) {
this.callback = callback;
}
observe(target: Element): void {
this.callback(
[
{
target,
contentRect: {
width: OBSERVED_WIDTH,
height: 200,
x: 0,
y: 0,
top: 0,
right: OBSERVED_WIDTH,
bottom: 200,
left: 0,
toJSON: () => ({}),
},
} as ResizeObserverEntry,
],
this,
);
}
unobserve(): void {}
disconnect(): void {}
}
function dailyEntry(index: number): DailyUsageEntry {
const date = new Date("2026-06-04T00:00:00");
date.setDate(date.getDate() + index);
const isoDate = date.toISOString().slice(0, 10);
return {
date: isoDate,
inputTokens: 100,
outputTokens: 50,
cacheCreationTokens: 0,
cacheReadTokens: 0,
totalCost: 10,
modelsUsed: ["model"],
projectBreakdowns: [
{
project: "agentsview",
inputTokens: 100,
outputTokens: 50,
cacheCreationTokens: 0,
cacheReadTokens: 0,
cost: 10,
},
],
};
}
function usageSummary(): UsageSummaryResponse {
return {
from: "2026-06-04",
to: "2026-06-18",
totals: {
inputTokens: 1500,
outputTokens: 750,
cacheCreationTokens: 0,
cacheReadTokens: 0,
totalCost: 150,
},
daily: Array.from({ length: 15 }, (_, i) => dailyEntry(i)),
projectTotals: [
{
project: "agentsview",
inputTokens: 1500,
outputTokens: 750,
cacheCreationTokens: 0,
cacheReadTokens: 0,
cost: 150,
},
],
modelTotals: [],
agentTotals: [],
sessionCounts: {
total: 15,
byProject: { agentsview: 15 },
byAgent: {},
},
cacheStats: {
cacheReadTokens: 0,
cacheCreationTokens: 0,
uncachedInputTokens: 1500,
outputTokens: 750,
hitRate: 0,
savingsVsUncached: 0,
},
};
}
describe("CostTimeSeriesChart", () => {
beforeEach(() => {
globalThis.ResizeObserver =
ImmediateResizeObserver as typeof ResizeObserver;
usage.summary = usageSummary();
usage.toggles.timeSeries.groupBy = "project";
});
afterEach(() => {
usage.summary = null;
document.body.innerHTML = "";
});
it("keeps the rightmost date label inside the SVG viewBox", async () => {
const component = mount(CostTimeSeriesChart, {
target: document.body,
});
await tick();
const svg = document.querySelector("svg.chart-svg");
expect(svg).toBeTruthy();
const viewBox = svg!.getAttribute("viewBox")!.split(" ").map(Number);
const viewBoxRight = viewBox[2]!;
const labels = Array.from(
document.querySelectorAll<SVGTextElement>("text.x-label"),
);
const lastLabel = labels.at(-1);
expect(lastLabel).toBeTruthy();
const x = Number(lastLabel!.getAttribute("x"));
const textWidthEstimate = lastLabel!.textContent!.length * 5;
expect(x + textWidthEstimate / 2).toBeLessThanOrEqual(viewBoxRight);
unmount(component);
});
});