|
| 1 | +/** @jsxImportSource @opentui/solid */ |
| 2 | +import { afterEach, beforeEach, expect, test } from "bun:test" |
| 3 | +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises" |
| 4 | +import os from "node:os" |
| 5 | +import path from "node:path" |
| 6 | +import { SyntaxStyle } from "@opentui/core" |
| 7 | +import { ManualClock } from "@opentui/core/testing" |
| 8 | +import { testRender, useRenderer } from "@opentui/solid" |
| 9 | +import { linkDirectoryMarkdown } from "../../src/ui/directory-markdown" |
| 10 | +import { clickedLink } from "../../src/ui/link" |
| 11 | + |
| 12 | +const syntaxStyle = SyntaxStyle.fromStyles({ default: { fg: "#ffffff" } }) |
| 13 | +let root: string |
| 14 | + |
| 15 | +beforeEach(async () => { |
| 16 | + root = await mkdtemp(path.join(os.tmpdir(), "directory-markdown-")) |
| 17 | + await mkdir(path.join(root, "worktree")) |
| 18 | + await writeFile(path.join(root, "file.txt"), "") |
| 19 | +}) |
| 20 | + |
| 21 | +afterEach(() => rm(root, { recursive: true, force: true })) |
| 22 | + |
| 23 | +test("inline code holding an existing absolute directory is a clickable link; files and missing paths are not", async () => { |
| 24 | + const directory = path.join(root, "worktree") |
| 25 | + const file = path.join(root, "file.txt") |
| 26 | + const missing = path.join(root, "missing") |
| 27 | + const opened: string[] = [] |
| 28 | + function Transcript() { |
| 29 | + const renderer = useRenderer() |
| 30 | + return ( |
| 31 | + <box |
| 32 | + onMouseUp={(event) => { |
| 33 | + const link = clickedLink(renderer, event) |
| 34 | + if (link) opened.push(link) |
| 35 | + }} |
| 36 | + > |
| 37 | + <markdown |
| 38 | + ref={linkDirectoryMarkdown} |
| 39 | + syntaxStyle={syntaxStyle} |
| 40 | + conceal |
| 41 | + content={`Dir \`${directory}\`\n\nFile \`${file}\`\n\nGone \`${missing}\``} |
| 42 | + /> |
| 43 | + </box> |
| 44 | + ) |
| 45 | + } |
| 46 | + const app = await testRender(() => <Transcript />, { width: 120, height: 10, clock: new ManualClock() }) |
| 47 | + |
| 48 | + try { |
| 49 | + for (let attempt = 0; attempt < 50 && !app.captureCharFrame().includes("Gone"); attempt++) { |
| 50 | + await app.renderOnce() |
| 51 | + await Bun.sleep(10) |
| 52 | + } |
| 53 | + await Bun.sleep(50) |
| 54 | + await app.renderOnce() |
| 55 | + const rows = app.captureCharFrame().split("\n") |
| 56 | + const at = (label: string) => { |
| 57 | + const y = rows.findIndex((row) => row.includes(label)) |
| 58 | + return [rows[y]!.indexOf("/"), y] as const |
| 59 | + } |
| 60 | + |
| 61 | + await app.mockMouse.click(...at("Dir")) |
| 62 | + await app.mockMouse.click(...at("File")) |
| 63 | + await app.mockMouse.click(...at("Gone")) |
| 64 | + |
| 65 | + expect(opened).toEqual([directory]) |
| 66 | + } finally { |
| 67 | + app.renderer.destroy() |
| 68 | + } |
| 69 | +}) |
0 commit comments