|
| 1 | +import { render } from "ink"; |
| 2 | +import { PassThrough, Writable } from "node:stream"; |
| 3 | + |
| 4 | +import type React from "react"; |
| 5 | + |
| 6 | +export class TestInput extends PassThrough { |
| 7 | + readonly isTTY = true; |
| 8 | + |
| 9 | + setRawMode(_value: boolean): void {} |
| 10 | + |
| 11 | + ref(): this { |
| 12 | + return this; |
| 13 | + } |
| 14 | + |
| 15 | + unref(): this { |
| 16 | + return this; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +export class TestOutput extends Writable { |
| 21 | + readonly isTTY = true; |
| 22 | + readonly columns: number; |
| 23 | + readonly rows = 40; |
| 24 | + private readonly chunks: string[] = []; |
| 25 | + |
| 26 | + constructor(columns: number = 120) { |
| 27 | + super(); |
| 28 | + this.columns = columns; |
| 29 | + } |
| 30 | + |
| 31 | + override _write( |
| 32 | + chunk: string | Uint8Array, |
| 33 | + _encoding: BufferEncoding, |
| 34 | + callback: (error?: Error | null) => void, |
| 35 | + ): void { |
| 36 | + const text = typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8"); |
| 37 | + this.chunks.push(text); |
| 38 | + callback(); |
| 39 | + } |
| 40 | + |
| 41 | + readAll(): string { |
| 42 | + return this.chunks.join(""); |
| 43 | + } |
| 44 | + |
| 45 | + clear(): void { |
| 46 | + this.chunks.length = 0; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const instances: Array<{ unmount: () => void; cleanup: () => void }> = []; |
| 51 | + |
| 52 | +export function cleanupRenderedInstances(): void { |
| 53 | + while (instances.length > 0) { |
| 54 | + const instance = instances.pop(); |
| 55 | + instance?.unmount(); |
| 56 | + instance?.cleanup(); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export async function settle(): Promise<void> { |
| 61 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 62 | +} |
| 63 | + |
| 64 | +export async function waitForRenders(cycles: number = 6): Promise<void> { |
| 65 | + for (let index = 0; index < cycles; index++) { |
| 66 | + await settle(); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export function stripAnsi(text: string): string { |
| 71 | + return text.replace(/\x1b\[[0-9;]*m/g, ""); |
| 72 | +} |
| 73 | + |
| 74 | +export function countPromptPlaceholders(text: string): number { |
| 75 | + return (stripAnsi(text).match(/Ask anything…/g) ?? []).length; |
| 76 | +} |
| 77 | + |
| 78 | +export async function typeAndSubmit(stdin: TestInput, text: string): Promise<void> { |
| 79 | + stdin.write(text); |
| 80 | + await settle(); |
| 81 | + stdin.write("\r"); |
| 82 | +} |
| 83 | + |
| 84 | +export async function insertModifiedReturn(stdin: TestInput): Promise<void> { |
| 85 | + stdin.write("\x1b\r"); |
| 86 | + await settle(); |
| 87 | +} |
| 88 | + |
| 89 | +export function renderForTest( |
| 90 | + node: React.ReactElement, |
| 91 | + options?: { readonly columns?: number }, |
| 92 | +): { |
| 93 | + readonly stdout: TestOutput; |
| 94 | + readonly stdin: TestInput; |
| 95 | + readonly rerender: (tree: React.ReactElement) => void; |
| 96 | + readonly unmount: () => void; |
| 97 | + readonly cleanup: () => void; |
| 98 | +} { |
| 99 | + const stdout = new TestOutput(options?.columns); |
| 100 | + const stdin = new TestInput(); |
| 101 | + const stderr = new TestOutput(options?.columns); |
| 102 | + const instance = render(node, { |
| 103 | + stdout: stdout as unknown as NodeJS.WriteStream, |
| 104 | + stdin: stdin as unknown as NodeJS.ReadStream, |
| 105 | + stderr: stderr as unknown as NodeJS.WriteStream, |
| 106 | + debug: true, |
| 107 | + exitOnCtrlC: false, |
| 108 | + patchConsole: false, |
| 109 | + }); |
| 110 | + instances.push(instance); |
| 111 | + return { |
| 112 | + stdout, |
| 113 | + stdin, |
| 114 | + rerender: instance.rerender, |
| 115 | + unmount: instance.unmount, |
| 116 | + cleanup: instance.cleanup, |
| 117 | + }; |
| 118 | +} |
0 commit comments