|
| 1 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | +import { isValidElement, type ReactElement, type ReactNode } from "react"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 4 | + |
| 5 | +const testState = vi.hoisted(() => ({ |
| 6 | + useNativeRenderer: false, |
| 7 | + resources: [] as Array<unknown>, |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock("react", async (importOriginal) => ({ |
| 11 | + ...(await importOriginal<typeof import("react")>()), |
| 12 | + useCallback: <Value,>(value: Value) => value, |
| 13 | + useEffect: () => {}, |
| 14 | + useMemo: <Value,>(factory: () => Value) => factory(), |
| 15 | + useState: <Value,>(initial: Value | (() => Value)) => [ |
| 16 | + typeof initial === "function" ? (initial as () => Value)() : initial, |
| 17 | + vi.fn(), |
| 18 | + ], |
| 19 | +})); |
| 20 | +vi.mock("react-native", () => ({ |
| 21 | + ActivityIndicator: "ActivityIndicator", |
| 22 | + Image: "Image", |
| 23 | + RefreshControl: "RefreshControl", |
| 24 | + ScrollView: "ScrollView", |
| 25 | + StyleSheet: { absoluteFill: {} }, |
| 26 | + Text: "Text", |
| 27 | + View: "View", |
| 28 | +})); |
| 29 | +vi.mock("react-native-gesture-handler", () => ({ TouchableOpacity: "TouchableOpacity" })); |
| 30 | +vi.mock("react-native-nitro-markdown", () => ({ |
| 31 | + Markdown: (props: { |
| 32 | + readonly children: string; |
| 33 | + readonly renderers: { |
| 34 | + readonly image: (input: { |
| 35 | + readonly node: { readonly href: string; readonly alt: string; readonly title: null }; |
| 36 | + }) => ReactNode; |
| 37 | + }; |
| 38 | + }) => { |
| 39 | + const image = markdownImage(props.children); |
| 40 | + return image === null ? null : props.renderers.image({ node: image }); |
| 41 | + }, |
| 42 | +})); |
| 43 | +vi.mock("../../components/AppText", () => ({ AppText: "Text" })); |
| 44 | +vi.mock("../../lib/openExternalUrl", () => ({ tryOpenExternalUrl: vi.fn() })); |
| 45 | +vi.mock("../../lib/useFontFamily", () => ({ useFontFamily: () => "System" })); |
| 46 | +vi.mock("../../lib/useUniwindTheme", () => ({ |
| 47 | + useUniwindTheme: () => new Proxy<Record<string, string>>({}, { get: () => "#000000" }), |
| 48 | +})); |
| 49 | +vi.mock("../../native/SelectableMarkdownText", () => ({ |
| 50 | + hasNativeSelectableMarkdownText: () => testState.useNativeRenderer, |
| 51 | + SelectableMarkdownText: (props: { |
| 52 | + readonly markdown: string; |
| 53 | + readonly renderImage: (image: { |
| 54 | + readonly href: string; |
| 55 | + readonly alt: string | null; |
| 56 | + readonly title: string | null; |
| 57 | + }) => ReactNode; |
| 58 | + }) => { |
| 59 | + const image = markdownImage(props.markdown); |
| 60 | + return image === null ? null : props.renderImage(image); |
| 61 | + }, |
| 62 | +})); |
| 63 | +vi.mock("../../state/assets", () => ({ |
| 64 | + useAssetUrlState: (_environmentId: unknown, resource: unknown) => { |
| 65 | + testState.resources.push(resource); |
| 66 | + return { _tag: "Success", url: "https://signed.test/diagram.png" }; |
| 67 | + }, |
| 68 | +})); |
| 69 | +vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({ |
| 70 | + useAppearancePreferences: () => ({ appearance: { baseFontSize: 14 } }), |
| 71 | +})); |
| 72 | + |
| 73 | +import { FileMarkdownPreview } from "./FileMarkdownPreview"; |
| 74 | + |
| 75 | +function markdownImage(markdown: string): { |
| 76 | + readonly href: string; |
| 77 | + readonly alt: string; |
| 78 | + readonly title: null; |
| 79 | +} | null { |
| 80 | + const match = /!\[([^\]]*)]\(([^)]+)\)/.exec(markdown); |
| 81 | + return match?.[2] ? { href: match[2], alt: match[1] ?? "", title: null } : null; |
| 82 | +} |
| 83 | + |
| 84 | +function renderTree(node: ReactNode): void { |
| 85 | + if (Array.isArray(node)) { |
| 86 | + node.forEach(renderTree); |
| 87 | + return; |
| 88 | + } |
| 89 | + if (!isValidElement(node)) return; |
| 90 | + const element = node as ReactElement<Record<string, unknown>>; |
| 91 | + if (typeof element.type === "function") { |
| 92 | + const render = element.type as (props: Record<string, unknown>) => ReactNode; |
| 93 | + renderTree(render(element.props)); |
| 94 | + return; |
| 95 | + } |
| 96 | + renderTree(element.props.children as ReactNode); |
| 97 | +} |
| 98 | + |
| 99 | +function renderPreview(input: { |
| 100 | + readonly cwd: string; |
| 101 | + readonly relativePath: string; |
| 102 | + readonly markdown: string; |
| 103 | +}): void { |
| 104 | + renderTree( |
| 105 | + FileMarkdownPreview({ |
| 106 | + cwd: input.cwd, |
| 107 | + relativePath: input.relativePath, |
| 108 | + markdown: input.markdown, |
| 109 | + environmentId: EnvironmentId.make("environment-1"), |
| 110 | + threadId: ThreadId.make("thread-1"), |
| 111 | + }), |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +describe.each([ |
| 116 | + ["native iOS", true], |
| 117 | + ["JavaScript fallback", false], |
| 118 | +])("FileMarkdownPreview workspace images with the %s renderer", (_label, useNativeRenderer) => { |
| 119 | + beforeEach(() => { |
| 120 | + testState.useNativeRenderer = useNativeRenderer; |
| 121 | + testState.resources = []; |
| 122 | + }); |
| 123 | + |
| 124 | + it.each([ |
| 125 | + ["/workspace/project", "README.md", "/workspace/project/images/diagram.png"], |
| 126 | + ["/workspace/project", "docs/README.md", "/workspace/project/docs/images/diagram.png"], |
| 127 | + [ |
| 128 | + "C:\\Users\\dara\\project", |
| 129 | + "docs\\README.md", |
| 130 | + "C:\\Users\\dara\\project\\docs\\images\\diagram.png", |
| 131 | + ], |
| 132 | + ])("requests an image relative to %s and %s", (cwd, relativePath, expectedPath) => { |
| 133 | + renderPreview({ |
| 134 | + cwd, |
| 135 | + relativePath, |
| 136 | + markdown: "", |
| 137 | + }); |
| 138 | + |
| 139 | + expect(testState.resources).toEqual([ |
| 140 | + { |
| 141 | + _tag: "workspace-file", |
| 142 | + threadId: ThreadId.make("thread-1"), |
| 143 | + path: expectedPath, |
| 144 | + }, |
| 145 | + ]); |
| 146 | + }); |
| 147 | + |
| 148 | + it("leaves remote images with the renderer that already handles them", () => { |
| 149 | + renderPreview({ |
| 150 | + cwd: "/workspace/project", |
| 151 | + relativePath: "docs/README.md", |
| 152 | + markdown: "", |
| 153 | + }); |
| 154 | + |
| 155 | + expect(testState.resources).toEqual([]); |
| 156 | + }); |
| 157 | +}); |
0 commit comments