|
| 1 | +import { vi } from 'vitest'; |
| 2 | +import { createMockRenderer } from './tui'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Test double for `@opentui/core`. |
| 6 | + * |
| 7 | + * The real package bundles its tree-sitter grammars AND its native Zig |
| 8 | + * renderer bindings (`bun:ffi`) into one eagerly-evaluated chunk, so it can |
| 9 | + * only ever load under Bun — there is no way to import any symbol from it |
| 10 | + * under vitest/Node. Suites `vi.mock('@opentui/core', ...)` this module |
| 11 | + * instead of the real package. |
| 12 | + * |
| 13 | + * Fidelity is scoped to what tests/tui/** actually observes: constructor |
| 14 | + * names, constructor-options-become-properties (including on later |
| 15 | + * reassignment), child-list management, the string→StyledText coercion on |
| 16 | + * TextRenderable.content, and RGBA channel maths (mirrored from the real |
| 17 | + * hexToRgb so `.equals()`/`toEqual` behave identically to opentui). |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * Colour-shaped option keys opentui coerces from hex string to RGBA, both at |
| 22 | + * construction and on later reassignment (e.g. panel.ts's box.borderColor = |
| 23 | + * theme.accent). Defined as accessors on the prototype so every set — not |
| 24 | + * just the constructor's initial assignment — goes through parseColor. |
| 25 | + */ |
| 26 | +const COLOUR_KEYS = ['backgroundColor', 'borderColor', 'fg', 'bg', 'color', 'selectedBackgroundColor']; |
| 27 | + |
| 28 | +class BaseRenderable { |
| 29 | + id: string; |
| 30 | + protected colourValues: Record<string, unknown> = {}; |
| 31 | + private children: BaseRenderable[] = []; |
| 32 | + |
| 33 | + constructor(_renderer: unknown, options: Record<string, unknown> = {}) { |
| 34 | + this.id = (options.id as string) ?? `mock-${Math.random().toString(36).slice(2)}`; |
| 35 | + Object.assign(this, options); |
| 36 | + } |
| 37 | + |
| 38 | + add(child: BaseRenderable): void { |
| 39 | + this.children.push(child); |
| 40 | + } |
| 41 | + |
| 42 | + remove(id: string): void { |
| 43 | + this.children = this.children.filter((child) => child.id !== id); |
| 44 | + } |
| 45 | + |
| 46 | + insertBefore(child: BaseRenderable, beforeId: string): void { |
| 47 | + const index = this.children.findIndex((existing) => existing.id === beforeId); |
| 48 | + if (index === -1) { |
| 49 | + this.children.push(child); |
| 50 | + } else { |
| 51 | + this.children.splice(index, 0, child); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + getChildren(): BaseRenderable[] { |
| 56 | + return this.children; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +for (const key of COLOUR_KEYS) { |
| 61 | + Object.defineProperty(BaseRenderable.prototype, key, { |
| 62 | + configurable: true, |
| 63 | + enumerable: true, |
| 64 | + get(this: BaseRenderable) { |
| 65 | + return this.colourValues[key]; |
| 66 | + }, |
| 67 | + set(this: BaseRenderable, value: unknown) { |
| 68 | + this.colourValues[key] = typeof value === 'string' ? parseColor(value) : value; |
| 69 | + }, |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +/** Base class opentui-spinner's SpinnerRenderable extends — construction only, no render loop. */ |
| 74 | +export class Renderable extends BaseRenderable {} |
| 75 | + |
| 76 | +export class BoxRenderable extends BaseRenderable {} |
| 77 | + |
| 78 | +/** Mirrors opentui: assigning a string to `.content` wraps it as StyledText. */ |
| 79 | +export class TextRenderable extends BaseRenderable { |
| 80 | + private _content: { chunks: { text: string }[] }; |
| 81 | + |
| 82 | + constructor(renderer: unknown, options: Record<string, unknown> = {}) { |
| 83 | + const { content, ...rest } = options; |
| 84 | + super(renderer, rest); |
| 85 | + this._content = TextRenderable.toStyledText(content); |
| 86 | + } |
| 87 | + |
| 88 | + get content(): { chunks: { text: string }[] } { |
| 89 | + return this._content; |
| 90 | + } |
| 91 | + |
| 92 | + set content(value: unknown) { |
| 93 | + this._content = TextRenderable.toStyledText(value); |
| 94 | + } |
| 95 | + |
| 96 | + private static toStyledText(value: unknown): { chunks: { text: string }[] } { |
| 97 | + if (value && typeof value === 'object' && 'chunks' in value) { |
| 98 | + return value as { chunks: { text: string }[] }; |
| 99 | + } |
| 100 | + return { chunks: [{ text: (value as string) ?? '' }] }; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export const SelectRenderableEvents = { |
| 105 | + ITEM_SELECTED: 'itemSelected', |
| 106 | + SELECTION_CHANGED: 'selectionChanged', |
| 107 | +} as const; |
| 108 | + |
| 109 | +export const InputRenderableEvents = { |
| 110 | + INPUT: 'input', |
| 111 | + ENTER: 'enter', |
| 112 | +} as const; |
| 113 | + |
| 114 | +export class SelectRenderable extends BaseRenderable { |
| 115 | + on = vi.fn(); |
| 116 | + once = vi.fn(); |
| 117 | + focus = vi.fn(); |
| 118 | + setSelectedIndex = vi.fn(); |
| 119 | + selectCurrent = vi.fn(); |
| 120 | +} |
| 121 | + |
| 122 | +export class InputRenderable extends BaseRenderable { |
| 123 | + on = vi.fn(); |
| 124 | + focus = vi.fn(); |
| 125 | +} |
| 126 | + |
| 127 | +export class TabSelectRenderable extends BaseRenderable { |
| 128 | + on = vi.fn(); |
| 129 | + focus = vi.fn(); |
| 130 | + moveLeft = vi.fn(); |
| 131 | + moveRight = vi.fn(); |
| 132 | +} |
| 133 | + |
| 134 | +export class ASCIIFontRenderable extends BaseRenderable {} |
| 135 | + |
| 136 | +/** |
| 137 | + * Mirrors opentui's real RGBA: a Float32Array-backed colour with r/g/b/a |
| 138 | + * getters. Reproducing the real hexToRgb maths (not just a distinct fake |
| 139 | + * shape) means two independent `RGBA.fromHex(x)` calls satisfy `toEqual` |
| 140 | + * structural equality, matching the real assertions in theme.test.ts. |
| 141 | + */ |
| 142 | +export class RGBA { |
| 143 | + buffer: Float32Array; |
| 144 | + |
| 145 | + constructor(buffer: Float32Array) { |
| 146 | + this.buffer = buffer; |
| 147 | + } |
| 148 | + |
| 149 | + static fromHex(hex: string): RGBA { |
| 150 | + let clean = hex.replace(/^#/, ''); |
| 151 | + if (clean.length === 3) { |
| 152 | + clean = clean[0] + clean[0] + clean[1] + clean[1] + clean[2] + clean[2]; |
| 153 | + } |
| 154 | + const r = parseInt(clean.substring(0, 2), 16) / 255; |
| 155 | + const g = parseInt(clean.substring(2, 4), 16) / 255; |
| 156 | + const b = parseInt(clean.substring(4, 6), 16) / 255; |
| 157 | + const a = clean.length === 8 ? parseInt(clean.substring(6, 8), 16) / 255 : 1; |
| 158 | + return new RGBA(new Float32Array([r, g, b, a])); |
| 159 | + } |
| 160 | + |
| 161 | + get r(): number { |
| 162 | + return this.buffer[0]; |
| 163 | + } |
| 164 | + get g(): number { |
| 165 | + return this.buffer[1]; |
| 166 | + } |
| 167 | + get b(): number { |
| 168 | + return this.buffer[2]; |
| 169 | + } |
| 170 | + get a(): number { |
| 171 | + return this.buffer[3]; |
| 172 | + } |
| 173 | + |
| 174 | + equals(other: RGBA | undefined | null): boolean { |
| 175 | + if (!other) return false; |
| 176 | + return this.r === other.r && this.g === other.g && this.b === other.b && this.a === other.a; |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +/** Mirrors opentui's real parseColor: strings become RGBA, everything else passes through. */ |
| 181 | +export function parseColor(color: unknown): unknown { |
| 182 | + if (typeof color === 'string') { |
| 183 | + if (color.toLowerCase() === 'transparent') { |
| 184 | + return new RGBA(new Float32Array([0, 0, 0, 0])); |
| 185 | + } |
| 186 | + return RGBA.fromHex(color); |
| 187 | + } |
| 188 | + return color; |
| 189 | +} |
| 190 | + |
| 191 | +/** |
| 192 | + * The real resolveRenderLib() loads the native Zig binding via bun:ffi — not |
| 193 | + * reproducible under Node. Nothing in tests/tui/** constructs a renderable |
| 194 | + * that calls into it at runtime (opentui-spinner's SpinnerRenderable is only |
| 195 | + * ever imported, never instantiated, by the suites here), so a throwing stub |
| 196 | + * is correct: it surfaces loudly if that assumption ever changes. |
| 197 | + */ |
| 198 | +export function resolveRenderLib(): never { |
| 199 | + throw new Error( |
| 200 | + 'resolveRenderLib() is not mocked — tests must not instantiate renderables that need the native render lib.' |
| 201 | + ); |
| 202 | +} |
| 203 | + |
| 204 | +/** Styled-text helpers (assets/brand + about.ts) — simple passthroughs. */ |
| 205 | +export function t(strings: TemplateStringsArray, ...values: unknown[]): { chunks: { text: string }[] } { |
| 206 | + const text = strings.reduce((acc, str, i) => acc + str + (values[i] ?? ''), ''); |
| 207 | + return { chunks: [{ text }] }; |
| 208 | +} |
| 209 | + |
| 210 | +export function fg(_color: unknown) { |
| 211 | + return (text: string) => text; |
| 212 | +} |
| 213 | + |
| 214 | +export function link(_url: string) { |
| 215 | + return (text: string) => text; |
| 216 | +} |
| 217 | + |
| 218 | +export function underline(text: string): string { |
| 219 | + return text; |
| 220 | +} |
| 221 | + |
| 222 | +export async function createCliRenderer(_options?: unknown) { |
| 223 | + return createMockRenderer(); |
| 224 | +} |
0 commit comments