| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 1 | import { html, fixture, expect } from "@open-wc/testing"; |
| 2 | import "./sketch-container-status"; |
| 3 | import type { SketchContainerStatus } from "./sketch-container-status"; |
| 4 | import { State } from "../types"; |
| 5 | |
| 6 | describe("SketchContainerStatus", () => { |
| 7 | // Mock complete state for testing |
| 8 | const mockCompleteState: State = { |
| 9 | hostname: "test-host", |
| 10 | working_dir: "/test/dir", |
| 11 | initial_commit: "abcdef1234567890", |
| 12 | message_count: 42, |
| 13 | os: "linux", |
| 14 | title: "Test Session", |
| 15 | total_usage: { |
| 16 | input_tokens: 1000, |
| 17 | output_tokens: 2000, |
| 18 | cache_read_input_tokens: 300, |
| 19 | cache_creation_input_tokens: 400, |
| 20 | total_cost_usd: 0.25 |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | it("renders with complete state data", async () => { |
| 25 | const el: SketchContainerStatus = await fixture(html` |
| 26 | <sketch-container-status .state=${mockCompleteState}></sketch-container-status> |
| 27 | `); |
| 28 | |
| 29 | // Check that all expected elements exist |
| 30 | expect(el.shadowRoot!.querySelector("#hostname")).to.exist; |
| 31 | expect(el.shadowRoot!.querySelector("#workingDir")).to.exist; |
| 32 | expect(el.shadowRoot!.querySelector("#initialCommit")).to.exist; |
| 33 | expect(el.shadowRoot!.querySelector("#messageCount")).to.exist; |
| 34 | expect(el.shadowRoot!.querySelector("#inputTokens")).to.exist; |
| 35 | expect(el.shadowRoot!.querySelector("#outputTokens")).to.exist; |
| 36 | expect(el.shadowRoot!.querySelector("#cacheReadInputTokens")).to.exist; |
| 37 | expect(el.shadowRoot!.querySelector("#cacheCreationInputTokens")).to.exist; |
| 38 | expect(el.shadowRoot!.querySelector("#totalCost")).to.exist; |
| 39 | |
| 40 | // Verify content of displayed elements |
| 41 | expect(el.shadowRoot!.querySelector("#hostname")!.textContent).to.equal("test-host"); |
| 42 | expect(el.shadowRoot!.querySelector("#workingDir")!.textContent).to.equal("/test/dir"); |
| 43 | expect(el.shadowRoot!.querySelector("#initialCommit")!.textContent).to.equal("abcdef12"); // Only first 8 chars |
| 44 | expect(el.shadowRoot!.querySelector("#messageCount")!.textContent).to.equal("42"); |
| 45 | expect(el.shadowRoot!.querySelector("#inputTokens")!.textContent).to.equal("1000"); |
| 46 | expect(el.shadowRoot!.querySelector("#outputTokens")!.textContent).to.equal("2000"); |
| 47 | expect(el.shadowRoot!.querySelector("#cacheReadInputTokens")!.textContent).to.equal("300"); |
| 48 | expect(el.shadowRoot!.querySelector("#cacheCreationInputTokens")!.textContent).to.equal("400"); |
| 49 | expect(el.shadowRoot!.querySelector("#totalCost")!.textContent).to.equal("$0.25"); |
| 50 | }); |
| 51 | |
| 52 | it("renders with undefined state", async () => { |
| 53 | const el: SketchContainerStatus = await fixture(html` |
| 54 | <sketch-container-status></sketch-container-status> |
| 55 | `); |
| 56 | |
| 57 | // Elements should exist but be empty |
| 58 | expect(el.shadowRoot!.querySelector("#hostname")!.textContent).to.equal(""); |
| 59 | expect(el.shadowRoot!.querySelector("#workingDir")!.textContent).to.equal(""); |
| 60 | expect(el.shadowRoot!.querySelector("#initialCommit")!.textContent).to.equal(""); |
| 61 | expect(el.shadowRoot!.querySelector("#messageCount")!.textContent).to.equal(""); |
| 62 | expect(el.shadowRoot!.querySelector("#inputTokens")!.textContent).to.equal(""); |
| 63 | expect(el.shadowRoot!.querySelector("#outputTokens")!.textContent).to.equal(""); |
| 64 | expect(el.shadowRoot!.querySelector("#totalCost")!.textContent).to.equal("$0.00"); |
| 65 | }); |
| 66 | |
| 67 | it("renders with partial state data", async () => { |
| 68 | const partialState: Partial<State> = { |
| 69 | hostname: "partial-host", |
| 70 | message_count: 10, |
| 71 | os: "linux", |
| 72 | title: "Partial Test", |
| 73 | total_usage: { |
| 74 | input_tokens: 500 |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | const el: SketchContainerStatus = await fixture(html` |
| 79 | <sketch-container-status .state=${partialState as State}></sketch-container-status> |
| 80 | `); |
| 81 | |
| 82 | // Check that elements with data are properly populated |
| 83 | expect(el.shadowRoot!.querySelector("#hostname")!.textContent).to.equal("partial-host"); |
| 84 | expect(el.shadowRoot!.querySelector("#messageCount")!.textContent).to.equal("10"); |
| 85 | expect(el.shadowRoot!.querySelector("#inputTokens")!.textContent).to.equal("500"); |
| 86 | |
| 87 | // Check that elements without data are empty |
| 88 | expect(el.shadowRoot!.querySelector("#workingDir")!.textContent).to.equal(""); |
| 89 | expect(el.shadowRoot!.querySelector("#initialCommit")!.textContent).to.equal(""); |
| 90 | expect(el.shadowRoot!.querySelector("#outputTokens")!.textContent).to.equal(""); |
| 91 | expect(el.shadowRoot!.querySelector("#totalCost")!.textContent).to.equal("$0.00"); |
| 92 | }); |
| 93 | |
| 94 | it("handles cost formatting correctly", async () => { |
| 95 | // Test with different cost values |
| 96 | const testCases = [ |
| 97 | { cost: 0, expected: "$0.00" }, |
| 98 | { cost: 0.1, expected: "$0.10" }, |
| 99 | { cost: 1.234, expected: "$1.23" }, |
| 100 | { cost: 10.009, expected: "$10.01" } |
| 101 | ]; |
| 102 | |
| 103 | for (const testCase of testCases) { |
| 104 | const stateWithCost = { |
| 105 | ...mockCompleteState, |
| 106 | total_usage: { |
| 107 | ...mockCompleteState.total_usage, |
| 108 | total_cost_usd: testCase.cost |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | const el: SketchContainerStatus = await fixture(html` |
| 113 | <sketch-container-status .state=${stateWithCost}></sketch-container-status> |
| 114 | `); |
| 115 | |
| 116 | expect(el.shadowRoot!.querySelector("#totalCost")!.textContent).to.equal(testCase.expected); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | it("truncates commit hash to 8 characters", async () => { |
| 121 | const stateWithLongCommit = { |
| 122 | ...mockCompleteState, |
| 123 | initial_commit: "1234567890abcdef1234567890abcdef12345678" |
| 124 | }; |
| 125 | |
| 126 | const el: SketchContainerStatus = await fixture(html` |
| 127 | <sketch-container-status .state=${stateWithLongCommit}></sketch-container-status> |
| 128 | `); |
| 129 | |
| 130 | expect(el.shadowRoot!.querySelector("#initialCommit")!.textContent).to.equal("12345678"); |
| 131 | }); |
| 132 | |
| 133 | it("has correct link elements", async () => { |
| 134 | const el: SketchContainerStatus = await fixture(html` |
| 135 | <sketch-container-status .state=${mockCompleteState}></sketch-container-status> |
| 136 | `); |
| 137 | |
| 138 | const links = Array.from(el.shadowRoot!.querySelectorAll('a')); |
| 139 | expect(links.length).to.equal(2); |
| 140 | |
| 141 | // Check for logs link |
| 142 | const logsLink = links.find(link => link.textContent === 'Logs'); |
| 143 | expect(logsLink).to.exist; |
| 144 | expect(logsLink!.getAttribute('href')).to.equal('logs'); |
| 145 | |
| 146 | // Check for download link |
| 147 | const downloadLink = links.find(link => link.textContent === 'Download'); |
| 148 | expect(downloadLink).to.exist; |
| 149 | expect(downloadLink!.getAttribute('href')).to.equal('download'); |
| 150 | }); |
| 151 | }); |