Allow previews to open readable files (#1214)

* Allow previews to open readable files

* Fix Windows file preview path test

* Address file preview review comments
This commit is contained in:
Mohamed Boudra
2026-05-29 11:35:38 +08:00
committed by GitHub
parent 2e1b907c63
commit 3585c80266
11 changed files with 361 additions and 132 deletions
@@ -1,4 +1,4 @@
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it } from "vitest";
import {
classifyForResolution,
fetchDaemonResolution,
@@ -35,27 +35,22 @@ function suggestionsFromMap(entriesByQuery: Record<string, DirectorySuggestionEn
matchMode: "suffix";
limit: number;
}> = [];
const getDirectorySuggestions = vi.fn<GetDirectorySuggestions>(
async (input: {
query: string;
cwd: string;
includeFiles: true;
includeDirectories: false;
matchMode: "suffix";
limit: number;
}) => {
searches.push({
query: input.query,
cwd: input.cwd,
matchMode: input.matchMode,
limit: input.limit,
});
return resolvedSuggestions(entriesByQuery[input.query] ?? []);
},
);
const getDirectorySuggestions: GetDirectorySuggestions = async (input) => {
searches.push({
query: input.query,
cwd: input.cwd,
matchMode: input.matchMode,
limit: input.limit,
});
return resolvedSuggestions(entriesByQuery[input.query] ?? []);
};
return { getDirectorySuggestions, searches };
}
const unavailableSuggestions: GetDirectorySuggestions = async () => {
throw new Error("daemon unavailable");
};
describe("classifyForResolution", () => {
it("returns the directFile target synchronously", () => {
const result = classifyForResolution({ href: "src/components/message.tsx#L33" }, CONTEXT);
@@ -119,6 +114,40 @@ describe("classifyForResolution", () => {
});
});
it("keeps absolute paths outside the workspace as direct file targets", () => {
const result = classifyForResolution({ href: "/tmp/outside.txt" }, CONTEXT);
expect(result).toEqual({
kind: "resolved",
value: {
kind: "file",
target: {
raw: "/tmp/outside.txt",
path: "/tmp/outside.txt",
lineStart: undefined,
lineEnd: undefined,
},
},
});
});
it("keeps tilde paths as direct file targets", () => {
const result = classifyForResolution({ href: "~/.paseo/plans/file-preview.md" }, CONTEXT);
expect(result).toEqual({
kind: "resolved",
value: {
kind: "file",
target: {
raw: "~/.paseo/plans/file-preview.md",
path: "~/.paseo/plans/file-preview.md",
lineStart: undefined,
lineEnd: undefined,
},
},
});
});
it("keeps auto-linkified normal domains external", () => {
const result = classifyForResolution(
{ href: "http://google.com", text: "google.com", markup: "linkify" },
@@ -193,10 +222,6 @@ describe("fetchDaemonResolution", () => {
});
it("throws a typed unresolved error when the daemon throws", async () => {
const getDirectorySuggestions = vi.fn<GetDirectorySuggestions>(async () => {
throw new Error("daemon unavailable");
});
await expect(
fetchDaemonResolution({
ambiguousQuery: "dumm.md",
@@ -208,7 +233,7 @@ describe("fetchDaemonResolution", () => {
lineEnd: undefined,
},
workspaceRoot: "/Users/test/project",
getDirectorySuggestions,
getDirectorySuggestions: unavailableSuggestions,
}),
).rejects.toEqual(new UnresolvedFileLinkError("dumm.md"));
});