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