blob: db11a4e9e7831ea01f179d5c66fe42f34c5514f2 [file] [log] [blame]
Sean McCulloughb29f8912025-04-20 15:39:11 -07001import { test, expect } from "@sand4rt/experimental-ct-web";
2import { SketchContainerStatus } from "./sketch-container-status";
Sean McCullough86b56862025-04-18 13:04:03 -07003import { State } from "../types";
4
Sean McCulloughb29f8912025-04-20 15:39:11 -07005// Mock complete state for testing
6const mockCompleteState: State = {
7 hostname: "test-host",
8 working_dir: "/test/dir",
9 initial_commit: "abcdef1234567890",
10 message_count: 42,
11 os: "linux",
12 title: "Test Session",
13 total_usage: {
14 input_tokens: 1000,
15 output_tokens: 2000,
16 cache_read_input_tokens: 300,
17 cache_creation_input_tokens: 400,
18 total_cost_usd: 0.25,
Sean McCulloughd9f13372025-04-21 15:08:49 -070019 start_time: "",
20 messages: 0,
21 tool_uses: {},
Sean McCulloughb29f8912025-04-20 15:39:11 -070022 },
23};
24
25test("render props", async ({ mount }) => {
26 const component = await mount(SketchContainerStatus, {
27 props: {
28 state: mockCompleteState,
29 },
30 });
31 await expect(component.locator("#hostname")).toContainText(
32 mockCompleteState.hostname,
33 );
34 // Check that all expected elements exist
35 await expect(component.locator("#workingDir")).toContainText(
36 mockCompleteState.working_dir,
37 );
38 await expect(component.locator("#initialCommit")).toContainText(
39 mockCompleteState.initial_commit.substring(0, 8),
40 );
41
42 await expect(component.locator("#messageCount")).toContainText(
43 mockCompleteState.message_count + "",
44 );
45 await expect(component.locator("#inputTokens")).toContainText(
46 mockCompleteState.total_usage.input_tokens + "",
47 );
48 await expect(component.locator("#outputTokens")).toContainText(
49 mockCompleteState.total_usage.output_tokens + "",
50 );
51
52 await expect(component.locator("#cacheReadInputTokens")).toContainText(
53 mockCompleteState.total_usage.cache_read_input_tokens + "",
54 );
55 await expect(component.locator("#cacheCreationInputTokens")).toContainText(
56 mockCompleteState.total_usage.cache_creation_input_tokens + "",
57 );
58 await expect(component.locator("#totalCost")).toContainText(
59 "$" + mockCompleteState.total_usage.total_cost_usd.toFixed(2),
60 );
61});
62
63test("renders with undefined state", async ({ mount }) => {
64 const component = await mount(SketchContainerStatus, {});
65
66 // Elements should exist but be empty
67 await expect(component.locator("#hostname")).toContainText("");
68 await expect(component.locator("#workingDir")).toContainText("");
69 await expect(component.locator("#initialCommit")).toContainText("");
70 await expect(component.locator("#messageCount")).toContainText("");
71 await expect(component.locator("#inputTokens")).toContainText("");
72 await expect(component.locator("#outputTokens")).toContainText("");
73 await expect(component.locator("#totalCost")).toContainText("$0.00");
74});
75
76test("renders with partial state data", async ({ mount }) => {
77 const partialState: Partial<State> = {
78 hostname: "partial-host",
79 message_count: 10,
Sean McCullough86b56862025-04-18 13:04:03 -070080 os: "linux",
Sean McCulloughb29f8912025-04-20 15:39:11 -070081 title: "Partial Test",
Sean McCullough86b56862025-04-18 13:04:03 -070082 total_usage: {
Sean McCulloughb29f8912025-04-20 15:39:11 -070083 input_tokens: 500,
Sean McCulloughd9f13372025-04-21 15:08:49 -070084 start_time: "",
85 messages: 0,
86 output_tokens: 0,
87 cache_read_input_tokens: 0,
88 cache_creation_input_tokens: 0,
89 total_cost_usd: 0,
90 tool_uses: {},
Sean McCullough71941bd2025-04-18 13:31:48 -070091 },
Sean McCullough86b56862025-04-18 13:04:03 -070092 };
93
Sean McCulloughb29f8912025-04-20 15:39:11 -070094 const component = await mount(SketchContainerStatus, {
95 props: {
96 state: partialState as State,
97 },
Sean McCullough86b56862025-04-18 13:04:03 -070098 });
99
Sean McCulloughb29f8912025-04-20 15:39:11 -0700100 // Check that elements with data are properly populated
101 await expect(component.locator("#hostname")).toContainText("partial-host");
102 await expect(component.locator("#messageCount")).toContainText("10");
103 await expect(component.locator("#inputTokens")).toContainText("500");
Sean McCullough86b56862025-04-18 13:04:03 -0700104
Sean McCulloughb29f8912025-04-20 15:39:11 -0700105 // Check that elements without data are empty
106 await expect(component.locator("#workingDir")).toContainText("");
107 await expect(component.locator("#initialCommit")).toContainText("");
108 await expect(component.locator("#outputTokens")).toContainText("");
109 await expect(component.locator("#totalCost")).toContainText("$0.00");
110});
Sean McCullough86b56862025-04-18 13:04:03 -0700111
Sean McCulloughb29f8912025-04-20 15:39:11 -0700112test("handles cost formatting correctly", async ({ mount }) => {
113 // Test with different cost values
114 const testCases = [
115 { cost: 0, expected: "$0.00" },
116 { cost: 0.1, expected: "$0.10" },
117 { cost: 1.234, expected: "$1.23" },
118 { cost: 10.009, expected: "$10.01" },
119 ];
120
121 for (const testCase of testCases) {
122 const stateWithCost = {
123 ...mockCompleteState,
Sean McCullough86b56862025-04-18 13:04:03 -0700124 total_usage: {
Sean McCulloughb29f8912025-04-20 15:39:11 -0700125 ...mockCompleteState.total_usage,
126 total_cost_usd: testCase.cost,
Sean McCullough71941bd2025-04-18 13:31:48 -0700127 },
Sean McCullough86b56862025-04-18 13:04:03 -0700128 };
129
Sean McCulloughb29f8912025-04-20 15:39:11 -0700130 const component = await mount(SketchContainerStatus, {
131 props: {
132 state: stateWithCost,
133 },
134 });
135 await expect(component.locator("#totalCost")).toContainText(
136 testCase.expected,
137 );
138 await component.unmount();
139 }
140});
Sean McCullough86b56862025-04-18 13:04:03 -0700141
Sean McCulloughb29f8912025-04-20 15:39:11 -0700142test("truncates commit hash to 8 characters", async ({ mount }) => {
143 const stateWithLongCommit = {
144 ...mockCompleteState,
145 initial_commit: "1234567890abcdef1234567890abcdef12345678",
146 };
Sean McCullough71941bd2025-04-18 13:31:48 -0700147
Sean McCulloughb29f8912025-04-20 15:39:11 -0700148 const component = await mount(SketchContainerStatus, {
149 props: {
150 state: stateWithLongCommit,
151 },
Sean McCullough86b56862025-04-18 13:04:03 -0700152 });
153
Sean McCulloughb29f8912025-04-20 15:39:11 -0700154 await expect(component.locator("#initialCommit")).toContainText("12345678");
155});
Sean McCullough86b56862025-04-18 13:04:03 -0700156
Sean McCulloughb29f8912025-04-20 15:39:11 -0700157test("has correct link elements", async ({ mount }) => {
158 const component = await mount(SketchContainerStatus, {
159 props: {
160 state: mockCompleteState,
161 },
Sean McCullough86b56862025-04-18 13:04:03 -0700162 });
163
Sean McCulloughb29f8912025-04-20 15:39:11 -0700164 // Check for logs link
165 const logsLink = component.locator("a").filter({ hasText: "Logs" });
166 await expect(logsLink).toHaveAttribute("href", "logs");
Sean McCullough86b56862025-04-18 13:04:03 -0700167
Sean McCulloughb29f8912025-04-20 15:39:11 -0700168 // Check for download link
169 const downloadLink = component.locator("a").filter({ hasText: "Download" });
170 await expect(downloadLink).toHaveAttribute("href", "download");
Sean McCullough86b56862025-04-18 13:04:03 -0700171});