Open History to see the last commit
1import type { ReactNode } from "react";
2import { act } from "react";
3import { createRoot } from "react-dom/client";
4import { expect, it, vi } from "vite-plus/test";
5
6const { harness, stub } = vi.hoisted(() => ({
7 harness: { appBars: [] as Array<{ menus?: Array<{ items: Array<{ id: string }> }> }> },
8 stub: () => null,
9}));
10
11vi.mock("react-native", () => ({
12 Platform: { OS: "ios" },
13 View: ({ children }: { children?: ReactNode }) => <div>{children}</div>,
14}));
15vi.mock("react-native-svg", () => ({
16 default: stub,
17 Defs: stub,
18 LinearGradient: stub,
19 Rect: stub,
20 Stop: stub,
21}));
22vi.mock("@react-navigation/native", () => ({
23 StackActions: { replace: () => ({}), push: () => ({}) },
24 useNavigation: () => ({ canGoBack: () => true, goBack: () => {}, dispatch: () => {} }),
25 usePreventRemove: () => {},
26}));
27vi.mock("@effect/atom-react", () => ({ useAtomValue: () => ({ _tag: "Initial" }) }));
28vi.mock("@t3tools/client-runtime/state/filesystem", () => ({
29 resolveFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }),
30}));
31vi.mock("../../state/session", () => ({
32 environmentSession: { sessionStateAtom: () => "session" },
33}));
34vi.mock("../../state/query", () => ({
35 useEnvironmentQuery: (atom: unknown) =>
36 atom === "metadata"
37 ? { data: { kind: "file" }, error: null, isSuccess: true, failure: null }
38 : { data: null, error: null, isSuccess: false, failure: null, refresh: async () => {} },
39}));
40vi.mock("../../state/presentation", () => ({
41 useEnvironmentPresentation: () => ({ isReady: true, presentation: null }),
42}));
43vi.mock("../../state/projects", () => ({
44 projectEnvironment: { fileMetadata: () => "metadata", readFile: () => "read" },
45}));
46vi.mock("../../state/use-thread-selection", () => ({
47 useThreadSelection: () => ({
48 selectedThread: { environmentId: "env" },
49 selectedThreadProject: { title: "App", workspaceRoot: "/repo" },
50 }),
51}));
52vi.mock("../../state/use-selected-thread-worktree", () => ({
53 useSelectedThreadWorktree: () => ({ selectedThreadCwd: "/repo" }),
54}));
55vi.mock("../../state/assets", () => ({ useFreshAssetUrl: () => ({ _tag: "Initial" }) }));
56vi.mock("../../state/mediaActions", () => ({ useMediaActions: () => ({ actions: [] }) }));
57vi.mock("../layout/AdaptiveWorkspaceLayout", () => ({
58 Inspector: stub,
59 useAdaptiveWorkspaceLayout: () => ({ toggleAuxiliaryPane: () => {} }),
60 useWorkspacePaneState: () => ({
61 fileInspector: { supported: false },
62 panes: { auxiliaryPaneVisible: false },
63 }),
64}));
65vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({
66 useAppearancePreferences: () => ({
67 appearance: { codeWordBreak: false },
68 setCodeWordBreak: () => {},
69 themeAppearance: "dark",
70 }),
71}));
72vi.mock("../threads/ThreadRouteScreen", () => ({ ThreadRouteScreen: stub }));
73vi.mock("./workspaceFileAssetUrl", () => ({
74 useWorkspaceFileAssetUrlState: (input: { relativePath: string | null }) =>
75 input.relativePath === null
76 ? { _tag: "Initial", resource: null, refresh: async () => null }
77 : {
78 _tag: "Success",
79 url: `https://env.test/api/assets/token/${encodeURIComponent(input.relativePath)}`,
80 resource: { _tag: "media-file", threadId: "thread", path: input.relativePath },
81 refresh: async () => null,
82 },
83}));
84vi.mock("./useFullFileText", () => ({
85 MOBILE_FULL_FILE_MAX_BYTES: 1,
86 useFullFileText: () => null,
87}));
88vi.mock("./useFileTreeEntries", () => ({ useFileTreeEntries: () => ({ refresh: () => {} }) }));
89vi.mock("../../components/ui/AppBar", () => ({
90 AppBar: (props: (typeof harness.appBars)[number]) => {
91 harness.appBars.push(props);
92 return null;
93 },
94}));
95vi.mock("../../components/ui/useBarInset", () => ({ useContentTopInset: () => 0 }));
96vi.mock("../../components/ui/EmptyState", () => ({ EmptyState: stub }));
97vi.mock("../../components/ui/FilePreviewModal", () => ({ FilePreviewModal: stub }));
98vi.mock("../../components/LoadingScreen", () => ({ LoadingScreen: stub }));
99vi.mock("../../lib/attachmentDocument", () => ({
100 attachmentTextAtom: () => null,
101 noAttachmentText: null,
102}));
103vi.mock("../../lib/copyTextWithHaptic", () => ({ copyTextWithHaptic: () => {} }));
104vi.mock("./DelimitedFilePreview", () => ({ DelimitedFilePreview: stub }));
105vi.mock("./AudioFilePreview", () => ({ AudioFilePreview: stub }));
106vi.mock("./EpubFilePreview", () => ({ EpubFilePreview: stub }));
107vi.mock("./FilePreviewFeedback", () => ({ FilePreviewLoading: stub, FilePreviewNotice: stub }));
108vi.mock("./FileMarkdownPreview", () => ({ FileMarkdownPreview: stub }));
109vi.mock("./FileTreeBrowser", () => ({ FileTreeBrowser: stub }));
110vi.mock("./preload-workspace-file", () => ({ preloadWorkspaceFileContents: () => {} }));
111vi.mock("./SourceFileSurface", () => ({ SourceFileSurface: stub }));
112vi.mock("./WorkspaceFileImagePreview", () => ({ WorkspaceFileImagePreview: stub }));
113vi.mock("./WorkspaceFilePreviewError", () => ({ WorkspaceFilePreviewError: stub }));
114vi.mock("./WorkspaceFileVideoPreview", () => ({ WorkspaceFileVideoPreview: stub }));
115vi.mock("./WorkspaceFileWebPreview", () => ({ WorkspaceFileWebPreview: stub }));
116
117import { ThreadFileScreen } from "./ThreadFilesRouteScreen";
118
119function fileMenuIds(path: readonly string[]) {
120 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
121 const container = document.body.appendChild(document.createElement("div"));
122 const root = createRoot(container);
123 harness.appBars.length = 0;
124 try {
125 const route = { params: { environmentId: "env", threadId: "thread", path: [...path] } };
126 const props = { route } as unknown as Parameters<typeof ThreadFileScreen>[0];
127 act(() => root.render(<ThreadFileScreen {...props} />));
128 return harness.appBars.at(-1)?.menus?.[0]?.items.map((item) => item.id) ?? [];
129 } finally {
130 act(() => root.unmount());
131 container.remove();
132 }
133}
134
135it("offers Open PDF for a workspace PDF with # in its name", () => {
136 expect(fileMenuIds(["docs", "Invoice #42.pdf"])).toContain("open-pdf");
137});