| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 1 | import { test, expect } from "@sand4rt/experimental-ct-web"; |
| 2 | import { SketchContainerStatus } from "./sketch-container-status"; |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 3 | import { State } from "../types"; |
| 4 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 5 | // Mock complete state for testing |
| 6 | const mockCompleteState: State = { |
| Philip Zeyliger | d03318d | 2025-05-08 13:09:12 -0700 | [diff] [blame] | 7 | state_version: 2, |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 8 | hostname: "test-host", |
| 9 | working_dir: "/test/dir", |
| 10 | initial_commit: "abcdef1234567890", |
| 11 | message_count: 42, |
| 12 | os: "linux", |
| 13 | title: "Test Session", |
| 14 | total_usage: { |
| 15 | input_tokens: 1000, |
| 16 | output_tokens: 2000, |
| 17 | cache_read_input_tokens: 300, |
| 18 | cache_creation_input_tokens: 400, |
| 19 | total_cost_usd: 0.25, |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 20 | start_time: "", |
| 21 | messages: 0, |
| 22 | tool_uses: {}, |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 23 | }, |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 24 | outstanding_llm_calls: 0, |
| 25 | outstanding_tool_calls: [], |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 26 | session_id: "test-session-id", |
| 27 | ssh_available: false, |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 28 | in_container: true, |
| 29 | first_message_index: 0, |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | test("render props", async ({ mount }) => { |
| 33 | const component = await mount(SketchContainerStatus, { |
| 34 | props: { |
| 35 | state: mockCompleteState, |
| 36 | }, |
| 37 | }); |
| 38 | await expect(component.locator("#hostname")).toContainText( |
| 39 | mockCompleteState.hostname, |
| 40 | ); |
| 41 | // Check that all expected elements exist |
| 42 | await expect(component.locator("#workingDir")).toContainText( |
| 43 | mockCompleteState.working_dir, |
| 44 | ); |
| 45 | await expect(component.locator("#initialCommit")).toContainText( |
| 46 | mockCompleteState.initial_commit.substring(0, 8), |
| 47 | ); |
| 48 | |
| Autoformatter | 5c70bfe | 2025-04-25 21:28:00 +0000 | [diff] [blame] | 49 | await expect(component.locator("#messageCount")).toContainText( |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 50 | mockCompleteState.message_count + "", |
| 51 | ); |
| Josh Bleecher Snyder | 3588997 | 2025-04-24 20:48:16 +0000 | [diff] [blame] | 52 | const expectedTotalInputTokens = |
| 53 | mockCompleteState.total_usage.input_tokens + |
| 54 | mockCompleteState.total_usage.cache_read_input_tokens + |
| 55 | mockCompleteState.total_usage.cache_creation_input_tokens; |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 56 | await expect(component.locator("#inputTokens")).toContainText( |
| Josh Bleecher Snyder | a0801ad | 2025-04-25 19:34:53 +0000 | [diff] [blame] | 57 | expectedTotalInputTokens.toLocaleString(), |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 58 | ); |
| 59 | await expect(component.locator("#outputTokens")).toContainText( |
| Josh Bleecher Snyder | a0801ad | 2025-04-25 19:34:53 +0000 | [diff] [blame] | 60 | mockCompleteState.total_usage.output_tokens.toLocaleString(), |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 61 | ); |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 62 | await expect(component.locator("#totalCost")).toContainText( |
| 63 | "$" + mockCompleteState.total_usage.total_cost_usd.toFixed(2), |
| 64 | ); |
| 65 | }); |
| 66 | |
| 67 | test("renders with undefined state", async ({ mount }) => { |
| 68 | const component = await mount(SketchContainerStatus, {}); |
| 69 | |
| 70 | // Elements should exist but be empty |
| 71 | await expect(component.locator("#hostname")).toContainText(""); |
| 72 | await expect(component.locator("#workingDir")).toContainText(""); |
| 73 | await expect(component.locator("#initialCommit")).toContainText(""); |
| 74 | await expect(component.locator("#messageCount")).toContainText(""); |
| Josh Bleecher Snyder | 3588997 | 2025-04-24 20:48:16 +0000 | [diff] [blame] | 75 | await expect(component.locator("#inputTokens")).toContainText("0"); |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 76 | await expect(component.locator("#outputTokens")).toContainText(""); |
| 77 | await expect(component.locator("#totalCost")).toContainText("$0.00"); |
| 78 | }); |
| 79 | |
| 80 | test("renders with partial state data", async ({ mount }) => { |
| 81 | const partialState: Partial<State> = { |
| Philip Zeyliger | d03318d | 2025-05-08 13:09:12 -0700 | [diff] [blame] | 82 | state_version: 2, |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 83 | hostname: "partial-host", |
| 84 | message_count: 10, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 85 | os: "linux", |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 86 | title: "Partial Test", |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 87 | session_id: "partial-session", |
| 88 | ssh_available: false, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 89 | total_usage: { |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 90 | input_tokens: 500, |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 91 | start_time: "", |
| 92 | messages: 0, |
| 93 | output_tokens: 0, |
| 94 | cache_read_input_tokens: 0, |
| 95 | cache_creation_input_tokens: 0, |
| 96 | total_cost_usd: 0, |
| 97 | tool_uses: {}, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 98 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 101 | const component = await mount(SketchContainerStatus, { |
| 102 | props: { |
| 103 | state: partialState as State, |
| 104 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 105 | }); |
| 106 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 107 | // Check that elements with data are properly populated |
| 108 | await expect(component.locator("#hostname")).toContainText("partial-host"); |
| 109 | await expect(component.locator("#messageCount")).toContainText("10"); |
| Josh Bleecher Snyder | 3588997 | 2025-04-24 20:48:16 +0000 | [diff] [blame] | 110 | |
| 111 | const expectedTotalInputTokens = |
| 112 | partialState.total_usage.input_tokens + |
| 113 | partialState.total_usage.cache_read_input_tokens + |
| 114 | partialState.total_usage.cache_creation_input_tokens; |
| 115 | await expect(component.locator("#inputTokens")).toContainText( |
| Josh Bleecher Snyder | a0801ad | 2025-04-25 19:34:53 +0000 | [diff] [blame] | 116 | expectedTotalInputTokens.toLocaleString(), |
| Josh Bleecher Snyder | 3588997 | 2025-04-24 20:48:16 +0000 | [diff] [blame] | 117 | ); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 118 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 119 | // Check that elements without data are empty |
| 120 | await expect(component.locator("#workingDir")).toContainText(""); |
| 121 | await expect(component.locator("#initialCommit")).toContainText(""); |
| Josh Bleecher Snyder | 3588997 | 2025-04-24 20:48:16 +0000 | [diff] [blame] | 122 | await expect(component.locator("#outputTokens")).toContainText("0"); |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 123 | await expect(component.locator("#totalCost")).toContainText("$0.00"); |
| 124 | }); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 125 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 126 | test("handles cost formatting correctly", async ({ mount }) => { |
| 127 | // Test with different cost values |
| 128 | const testCases = [ |
| 129 | { cost: 0, expected: "$0.00" }, |
| 130 | { cost: 0.1, expected: "$0.10" }, |
| 131 | { cost: 1.234, expected: "$1.23" }, |
| 132 | { cost: 10.009, expected: "$10.01" }, |
| 133 | ]; |
| 134 | |
| 135 | for (const testCase of testCases) { |
| 136 | const stateWithCost = { |
| 137 | ...mockCompleteState, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 138 | total_usage: { |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 139 | ...mockCompleteState.total_usage, |
| 140 | total_cost_usd: testCase.cost, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 141 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 144 | const component = await mount(SketchContainerStatus, { |
| 145 | props: { |
| 146 | state: stateWithCost, |
| 147 | }, |
| 148 | }); |
| 149 | await expect(component.locator("#totalCost")).toContainText( |
| 150 | testCase.expected, |
| 151 | ); |
| 152 | await component.unmount(); |
| 153 | } |
| 154 | }); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 155 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 156 | test("truncates commit hash to 8 characters", async ({ mount }) => { |
| 157 | const stateWithLongCommit = { |
| 158 | ...mockCompleteState, |
| 159 | initial_commit: "1234567890abcdef1234567890abcdef12345678", |
| 160 | }; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 161 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 162 | const component = await mount(SketchContainerStatus, { |
| 163 | props: { |
| 164 | state: stateWithLongCommit, |
| 165 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 166 | }); |
| 167 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 168 | await expect(component.locator("#initialCommit")).toContainText("12345678"); |
| 169 | }); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 170 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 171 | test("has correct link elements", async ({ mount }) => { |
| 172 | const component = await mount(SketchContainerStatus, { |
| 173 | props: { |
| 174 | state: mockCompleteState, |
| 175 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 176 | }); |
| 177 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 178 | // Check for logs link |
| 179 | const logsLink = component.locator("a").filter({ hasText: "Logs" }); |
| 180 | await expect(logsLink).toHaveAttribute("href", "logs"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 181 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 182 | // Check for download link |
| 183 | const downloadLink = component.locator("a").filter({ hasText: "Download" }); |
| 184 | await expect(downloadLink).toHaveAttribute("href", "download"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 185 | }); |