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