| 1 | # TUI Testing Framework |
| 2 | |
| 3 | This directory contains a comprehensive testing framework for the Continue CLI's Terminal User Interface (TUI) that supports running tests in both normal (local) and remote modes. |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | The testing framework ensures that the TUI behaves consistently whether running locally or connected to a remote `cn serve` instance. Most tests run in both modes automatically, verifying feature parity between local and remote operation. |
| 8 | |
| 9 | ## Key Components |
| 10 | |
| 11 | ### 1. Mock Remote Server (`mockRemoteServer.ts`) |
| 12 | |
| 13 | A mock Express server that simulates the `cn serve` endpoints: |
| 14 | |
| 15 | - `GET /state` - Returns current chat state |
| 16 | - `POST /message` - Receives user messages and can simulate responses |
| 17 | |
| 18 | ### 2. Test Helper (`TUIChat.testHelper.ts`) |
| 19 | |
| 20 | Provides utilities for writing tests that work in both modes: |
| 21 | |
| 22 | - `runTest()` - Run a single test in specified mode(s) |
| 23 | - `runTestSuite()` - Run a test suite in specified mode(s) |
| 24 | - Helper functions for common operations |
| 25 | |
| 26 | ### 3. Test Files |
| 27 | |
| 28 | - `TUIChat.basic.test.tsx` - Basic UI and layout tests |
| 29 | - `TUIChat.messages.test.tsx` - Message handling tests |
| 30 | - `TUIChat.remote.test.tsx` - Remote-specific behavior tests |
| 31 | - Additional test files for other features... |
| 32 | |
| 33 | ## Writing Tests |
| 34 | |
| 35 | ### Basic Test (runs in both modes) |
| 36 | |
| 37 | ```typescript |
| 38 | import { runTest } from "./TUIChat.testHelper.js"; |
| 39 | |
| 40 | runTest("my test name", async (ctx) => { |
| 41 | const { renderResult, mode, server } = ctx; |
| 42 | |
| 43 | // Your test code here |
| 44 | const frame = renderResult.lastFrame(); |
| 45 | expect(frame).toContain("Expected content"); |
| 46 | |
| 47 | // Mode-specific logic if needed |
| 48 | if (mode === "remote" && server) { |
| 49 | // Remote-specific assertions |
| 50 | } |
| 51 | }); |
| 52 | ``` |
| 53 | |
| 54 | ### Test Suite (runs all tests in both modes) |
| 55 | |
| 56 | ```typescript |
| 57 | import { runTestSuite } from "./TUIChat.testHelper.js"; |
| 58 | |
| 59 | runTestSuite("My Test Suite", () => { |
| 60 | runTest("test 1", async (ctx) => { |
| 61 | // Test code |
| 62 | }); |
| 63 | |
| 64 | runTest("test 2", async (ctx) => { |
| 65 | // Test code |
| 66 | }); |
| 67 | }); |
| 68 | ``` |
| 69 | |
| 70 | ### Mode-Specific Tests |
| 71 | |
| 72 | ```typescript |
| 73 | // Remote-only test |
| 74 | runTest( |
| 75 | "remote-specific behavior", |
| 76 | async (ctx) => { |
| 77 | // This only runs in remote mode |
| 78 | }, |
| 79 | { mode: "remote" }, |
| 80 | ); |
| 81 | |
| 82 | // Normal-only test |
| 83 | runTest( |
| 84 | "local-specific behavior", |
| 85 | async (ctx) => { |
| 86 | // This only runs in normal mode |
| 87 | }, |
| 88 | { mode: "normal" }, |
| 89 | ); |
| 90 | ``` |
| 91 | |
| 92 | ## Helper Functions |
| 93 | |
| 94 | ### `sendMessage(ctx, message, waitTime?)` |
| 95 | |
| 96 | Sends a message and waits for it to be processed: |
| 97 | |
| 98 | ```typescript |
| 99 | await sendMessage(ctx, "Hello world"); |
| 100 | ``` |
| 101 | |
| 102 | ### `waitForServerState(server, predicate, timeout?)` |
| 103 | |
| 104 | Waits for server state to match a condition (remote mode only): |
| 105 | |
| 106 | ```typescript |
| 107 | await waitForServerState(server, (state) => state.messages.length > 0, 5000); |
| 108 | ``` |
| 109 | |
| 110 | ### `expectRemoteMode(frame)` / `expectNormalMode(frame)` |
| 111 | |
| 112 | Verify mode-specific UI indicators: |
| 113 | |
| 114 | ```typescript |
| 115 | if (mode === "remote") { |
| 116 | expectRemoteMode(frame); |
| 117 | } else { |
| 118 | expectNormalMode(frame); |
| 119 | } |
| 120 | ``` |
| 121 | |
| 122 | ## Server Setup |
| 123 | |
| 124 | For tests that need specific server behavior: |
| 125 | |
| 126 | ```typescript |
| 127 | runTest( |
| 128 | "test with custom server", |
| 129 | async (ctx) => { |
| 130 | // Test code |
| 131 | }, |
| 132 | { |
| 133 | serverSetup: (server) => { |
| 134 | server.onMessage((msg) => { |
| 135 | // Custom response logic |
| 136 | server.simulateResponse(`Echo: ${msg}`); |
| 137 | }); |
| 138 | }, |
| 139 | }, |
| 140 | ); |
| 141 | ``` |
| 142 | |
| 143 | ## Running Tests |
| 144 | |
| 145 | ```bash |
| 146 | # Run all tests |
| 147 | npm test |
| 148 | |
| 149 | # Run specific test file |
| 150 | npm test TUIChat.basic.test.tsx |
| 151 | |
| 152 | # Run with coverage |
| 153 | npm test -- --coverage |
| 154 | ``` |
| 155 | |
| 156 | ## Best Practices |
| 157 | |
| 158 | 1. **Write mode-agnostic tests by default** - Most functionality should work the same in both modes |
| 159 | 2. **Use helper functions** - They handle mode-specific details automatically |
| 160 | 3. **Test server state in remote mode** - Verify both UI and server state for comprehensive coverage |
| 161 | 4. **Handle async operations properly** - Use appropriate wait times and state checks |
| 162 | 5. **Clean up resources** - The framework handles server lifecycle automatically |
| 163 | |
| 164 | ## Debugging |
| 165 | |
| 166 | - Tests output the current mode in brackets: `[NORMAL MODE]` or `[REMOTE MODE]` |
| 167 | - Server state can be inspected with `server.getState()` |
| 168 | - Use `console.log(renderResult.lastFrame())` to see the current UI state |
| 169 | - Mock server runs on a random port to avoid conflicts |