|
| 1 | +import { render, screen, fireEvent } from "@testing-library/react" |
| 2 | +import ContextCondenseRow from "../ContextCondenseRow" |
| 3 | +import { ContextCondense } from "@roo/schemas" |
| 4 | + |
| 5 | +// Mock the i18n hook |
| 6 | +jest.mock("react-i18next", () => ({ |
| 7 | + useTranslation: () => ({ |
| 8 | + t: (key: string) => { |
| 9 | + // Return mock translations for the keys used in the component |
| 10 | + const translations: Record<string, string> = { |
| 11 | + "chat:contextCondense.title": "Context Condensed", |
| 12 | + "chat:contextCondense.conversationSummary": "Conversation Summary", |
| 13 | + tokens: "tokens", |
| 14 | + } |
| 15 | + return translations[key] || key |
| 16 | + }, |
| 17 | + }), |
| 18 | +})) |
| 19 | + |
| 20 | +// Mock the VSCodeBadge component |
| 21 | +jest.mock("@vscode/webview-ui-toolkit/react", () => ({ |
| 22 | + VSCodeBadge: ({ children, style }: { children: React.ReactNode; style?: React.CSSProperties }) => ( |
| 23 | + <div data-testid="vscode-badge" style={style}> |
| 24 | + {children} |
| 25 | + </div> |
| 26 | + ), |
| 27 | +})) |
| 28 | + |
| 29 | +describe("ContextCondenseRow", () => { |
| 30 | + const defaultProps: ContextCondense = { |
| 31 | + cost: 0.05, |
| 32 | + prevContextTokens: 10000, |
| 33 | + newContextTokens: 5000, |
| 34 | + summary: "This is a test summary of the conversation.", |
| 35 | + } |
| 36 | + |
| 37 | + it("renders with correct token counts", () => { |
| 38 | + render(<ContextCondenseRow {...defaultProps} />) |
| 39 | + |
| 40 | + // Check if the component renders the title |
| 41 | + expect(screen.getByText("Context Condensed")).toBeInTheDocument() |
| 42 | + |
| 43 | + // Check if it displays the token counts correctly |
| 44 | + const tokenText = screen.getByText(/10,000 → 5,000 tokens/) |
| 45 | + expect(tokenText).toBeInTheDocument() |
| 46 | + }) |
| 47 | + |
| 48 | + it("displays cost badge when cost is greater than 0", () => { |
| 49 | + render(<ContextCondenseRow {...defaultProps} />) |
| 50 | + |
| 51 | + const costBadge = screen.getByTestId("vscode-badge") |
| 52 | + expect(costBadge).toBeInTheDocument() |
| 53 | + expect(costBadge).toHaveTextContent("$0.05") |
| 54 | + expect(costBadge).not.toHaveStyle("opacity: 0") |
| 55 | + }) |
| 56 | + |
| 57 | + it("hides cost badge when cost is 0", () => { |
| 58 | + render(<ContextCondenseRow {...defaultProps} cost={0} />) |
| 59 | + |
| 60 | + const costBadge = screen.getByTestId("vscode-badge") |
| 61 | + expect(costBadge).toBeInTheDocument() |
| 62 | + expect(costBadge).toHaveStyle("opacity: 0") |
| 63 | + }) |
| 64 | + |
| 65 | + it("initially renders in collapsed state", () => { |
| 66 | + render(<ContextCondenseRow {...defaultProps} />) |
| 67 | + |
| 68 | + // Check if the chevron is pointing down (collapsed) |
| 69 | + expect(screen.getByText("", { selector: ".codicon-chevron-down" })).toBeInTheDocument() |
| 70 | + |
| 71 | + // Check that the summary is not visible |
| 72 | + expect(screen.queryByText("This is a test summary of the conversation.")).not.toBeInTheDocument() |
| 73 | + }) |
| 74 | + |
| 75 | + it("expands when clicked", () => { |
| 76 | + render(<ContextCondenseRow {...defaultProps} />) |
| 77 | + |
| 78 | + // Click the row to expand it |
| 79 | + fireEvent.click(screen.getByText("Context Condensed")) |
| 80 | + |
| 81 | + // Check if the chevron is pointing up (expanded) |
| 82 | + expect(screen.getByText("", { selector: ".codicon-chevron-up" })).toBeInTheDocument() |
| 83 | + |
| 84 | + // Check if the summary is now visible |
| 85 | + expect(screen.getByText("This is a test summary of the conversation.")).toBeInTheDocument() |
| 86 | + expect(screen.getByText("Conversation Summary")).toBeInTheDocument() |
| 87 | + }) |
| 88 | + |
| 89 | + it("collapses when clicked again", () => { |
| 90 | + render(<ContextCondenseRow {...defaultProps} />) |
| 91 | + |
| 92 | + // Click to expand |
| 93 | + fireEvent.click(screen.getByText("Context Condensed")) |
| 94 | + |
| 95 | + // Click again to collapse |
| 96 | + fireEvent.click(screen.getByText("Context Condensed")) |
| 97 | + |
| 98 | + // Check if the chevron is pointing down again (collapsed) |
| 99 | + expect(screen.getByText("", { selector: ".codicon-chevron-down" })).toBeInTheDocument() |
| 100 | + |
| 101 | + // Check that the summary is not visible again |
| 102 | + expect(screen.queryByText("This is a test summary of the conversation.")).not.toBeInTheDocument() |
| 103 | + }) |
| 104 | + |
| 105 | + it("formats large token numbers with commas", () => { |
| 106 | + const props = { |
| 107 | + ...defaultProps, |
| 108 | + prevContextTokens: 1234567, |
| 109 | + newContextTokens: 567890, |
| 110 | + } |
| 111 | + |
| 112 | + render(<ContextCondenseRow {...props} />) |
| 113 | + |
| 114 | + // Check if the numbers are formatted with commas |
| 115 | + const tokenText = screen.getByText(/1,234,567 → 567,890 tokens/) |
| 116 | + expect(tokenText).toBeInTheDocument() |
| 117 | + }) |
| 118 | + |
| 119 | + it("formats cost to 2 decimal places", () => { |
| 120 | + const props = { |
| 121 | + ...defaultProps, |
| 122 | + cost: 0.12345, |
| 123 | + } |
| 124 | + |
| 125 | + render(<ContextCondenseRow {...props} />) |
| 126 | + |
| 127 | + const costBadge = screen.getByTestId("vscode-badge") |
| 128 | + expect(costBadge).toHaveTextContent("$0.12") |
| 129 | + }) |
| 130 | +}) |
0 commit comments