| 1 | import { render } from "ink-testing-library"; |
| 2 | import React from "react"; |
| 3 | |
| 4 | import { createUITestContext } from "../../test-helpers/ui-test-context.js"; |
| 5 | import { AppRoot } from "../AppRoot.js"; |
| 6 | |
| 7 | export type TestMode = "local" | "remote"; |
| 8 | |
| 9 | // Dummy test to satisfy Vitest |
| 10 | describe("TUIChat dual mode helper", () => { |
| 11 | it("exports helper functions", () => { |
| 12 | expect(testBothModes).toBeDefined(); |
| 13 | expect(renderInMode).toBeDefined(); |
| 14 | expect(testSingleMode).toBeDefined(); |
| 15 | }); |
| 16 | }); |
| 17 | |
| 18 | /** |
| 19 | * Helper to run a test in both local and remote modes |
| 20 | * This creates two test suites - one for each mode |
| 21 | */ |
| 22 | export function testBothModes( |
| 23 | name: string, |
| 24 | testFn: (mode: TestMode) => void | Promise<void>, |
| 25 | ) { |
| 26 | describe(`${name} [LOCAL MODE]`, () => { |
| 27 | let context: any; |
| 28 | |
| 29 | beforeEach(() => { |
| 30 | context = createUITestContext({ |
| 31 | allServicesReady: true, |
| 32 | serviceState: "ready", |
| 33 | }); |
| 34 | }); |
| 35 | |
| 36 | afterEach(() => { |
| 37 | context.cleanup(); |
| 38 | }); |
| 39 | |
| 40 | it("works in local mode", async () => { |
| 41 | await testFn("local"); |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | describe(`${name} [REMOTE MODE]`, () => { |
| 46 | let context: any; |
| 47 | |
| 48 | beforeEach(() => { |
| 49 | context = createUITestContext({ |
| 50 | allServicesReady: true, |
| 51 | serviceState: "ready", |
| 52 | }); |
| 53 | }); |
| 54 | |
| 55 | afterEach(() => { |
| 56 | context.cleanup(); |
| 57 | }); |
| 58 | |
| 59 | it("works in remote mode", async () => { |
| 60 | await testFn("remote"); |
| 61 | }); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Helper to render TUIChat in the specified mode |
| 67 | */ |
| 68 | export function renderInMode(mode: TestMode, props?: any) { |
| 69 | return render( |
| 70 | mode === "remote" |
| 71 | ? React.createElement(AppRoot, { |
| 72 | remoteUrl: "http://localhost:3000", |
| 73 | ...props, |
| 74 | }) |
| 75 | : React.createElement(AppRoot, props), |
| 76 | ) as ReturnType<typeof render>; // Explicit type to avoid TypeScript issues |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Helper to run a test in only one mode |
| 81 | */ |
| 82 | export function testSingleMode( |
| 83 | name: string, |
| 84 | mode: TestMode, |
| 85 | testFn: () => void | Promise<void>, |
| 86 | ) { |
| 87 | describe(`${name} [${mode.toUpperCase()} MODE ONLY]`, () => { |
| 88 | let context: any; |
| 89 | |
| 90 | beforeEach(() => { |
| 91 | context = createUITestContext({ |
| 92 | allServicesReady: true, |
| 93 | serviceState: "ready", |
| 94 | }); |
| 95 | }); |
| 96 | |
| 97 | afterEach(() => { |
| 98 | context.cleanup(); |
| 99 | }); |
| 100 | |
| 101 | it(`works in ${mode} mode`, async () => { |
| 102 | await testFn(); |
| 103 | }); |
| 104 | }); |
| 105 | } |