| 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 | }, |
| 23 | }; |
| 24 | |
| 25 | test("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 | |
| 63 | test("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 | |
| 76 | test("renders with partial state data", async ({ mount }) => { |
| 77 | const partialState: Partial<State> = { |
| 78 | hostname: "partial-host", |
| 79 | message_count: 10, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 80 | os: "linux", |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 81 | title: "Partial Test", |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 82 | total_usage: { |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 83 | input_tokens: 500, |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 84 | 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 McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 91 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 94 | const component = await mount(SketchContainerStatus, { |
| 95 | props: { |
| 96 | state: partialState as State, |
| 97 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 98 | }); |
| 99 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 100 | // 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 McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 104 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 105 | // 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 McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 111 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 112 | test("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 McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 124 | total_usage: { |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 125 | ...mockCompleteState.total_usage, |
| 126 | total_cost_usd: testCase.cost, |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 127 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 130 | 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 McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 141 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 142 | test("truncates commit hash to 8 characters", async ({ mount }) => { |
| 143 | const stateWithLongCommit = { |
| 144 | ...mockCompleteState, |
| 145 | initial_commit: "1234567890abcdef1234567890abcdef12345678", |
| 146 | }; |
| Sean McCullough | 71941bd | 2025-04-18 13:31:48 -0700 | [diff] [blame] | 147 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 148 | const component = await mount(SketchContainerStatus, { |
| 149 | props: { |
| 150 | state: stateWithLongCommit, |
| 151 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 152 | }); |
| 153 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 154 | await expect(component.locator("#initialCommit")).toContainText("12345678"); |
| 155 | }); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 156 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 157 | test("has correct link elements", async ({ mount }) => { |
| 158 | const component = await mount(SketchContainerStatus, { |
| 159 | props: { |
| 160 | state: mockCompleteState, |
| 161 | }, |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 162 | }); |
| 163 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 164 | // Check for logs link |
| 165 | const logsLink = component.locator("a").filter({ hasText: "Logs" }); |
| 166 | await expect(logsLink).toHaveAttribute("href", "logs"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 167 | |
| Sean McCullough | b29f891 | 2025-04-20 15:39:11 -0700 | [diff] [blame] | 168 | // Check for download link |
| 169 | const downloadLink = component.locator("a").filter({ hasText: "Download" }); |
| 170 | await expect(downloadLink).toHaveAttribute("href", "download"); |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 171 | }); |