-
Notifications
You must be signed in to change notification settings - Fork 5.6k
feat(web): open file links in the system browser #7641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saphid
wants to merge
1
commit into
pingdotgg:main
Choose a base branch
from
saphid:agent/web-file-menu-external-browser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+364
−55
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| import type { AssetCreateUrlResult, ScopedThreadRef } from "@t3tools/contracts"; | ||
| import * as Cause from "effect/Cause"; | ||
| import { AsyncResult } from "effect/unstable/reactivity"; | ||
| import { describe, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import { | ||
| beginExternalFileOpen, | ||
| isBrowserPreviewFile, | ||
| openFileInExternalBrowser, | ||
| } from "./openFileInPreview"; | ||
|
|
||
| const threadRef = { | ||
| environmentId: "local" as ScopedThreadRef["environmentId"], | ||
| threadId: "thread-1" as ScopedThreadRef["threadId"], | ||
| }; | ||
|
|
||
| const assetResult = (relativeUrl: string): AssetCreateUrlResult => ({ | ||
| relativeUrl, | ||
| expiresAt: 1_750_000_000_000, | ||
| }); | ||
|
|
||
| describe("isBrowserPreviewFile", () => { | ||
| it.each(["report.html", "doc.HTM", "paper.pdf", "nested/page.html?x=1#top"])( | ||
| "accepts %s", | ||
| (path) => expect(isBrowserPreviewFile(path)).toBe(true), | ||
| ); | ||
| it.each(["notes.md", "index.html.bak", "script.js"])("rejects %s", (path) => { | ||
| expect(isBrowserPreviewFile(path)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("beginExternalFileOpen", () => { | ||
| it("uses the desktop shell without opening a web tab", async () => { | ||
| const openExternal = vi.fn(async () => undefined); | ||
| const openWindow = vi.fn(); | ||
| const session = beginExternalFileOpen({ isDesktop: true, openExternal, openWindow }); | ||
|
|
||
| await session.open("http://environment.test/report.pdf"); | ||
|
|
||
| expect(openExternal).toHaveBeenCalledWith("http://environment.test/report.pdf"); | ||
| expect(openWindow).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("reserves and isolates a web tab before navigation", async () => { | ||
| const replace = vi.fn(); | ||
| const close = vi.fn(); | ||
| const tab = { opener: {} as Window | null, location: { replace }, close }; | ||
| const session = beginExternalFileOpen({ | ||
| isDesktop: false, | ||
| openExternal: vi.fn(), | ||
| openWindow: () => tab as unknown as Pick<Window, "close" | "location" | "opener">, | ||
| }); | ||
|
|
||
| expect(tab.opener).toBeNull(); | ||
| await session.open("http://environment.test/report.html"); | ||
| expect(replace).toHaveBeenCalledWith("http://environment.test/report.html"); | ||
| session.cancel(); | ||
| expect(close).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("reports a blocked web tab", () => { | ||
| expect(() => | ||
| beginExternalFileOpen({ | ||
| isDesktop: false, | ||
| openExternal: vi.fn(), | ||
| openWindow: () => null, | ||
| }), | ||
| ).toThrow("The browser blocked the new tab."); | ||
| }); | ||
| }); | ||
|
|
||
| describe("openFileInExternalBrowser", () => { | ||
| it.each([ | ||
| ["/repo/artifacts/report.html", "/repo", "workspace-file"], | ||
| ["/tmp/report.html", "/repo", "media-file"], | ||
| ["/repo-other/report.pdf", "/repo", "media-file"], | ||
| ["C:/repo/report.pdf", "C:/repo", "workspace-file"], | ||
| ["C:/temp/report.pdf", "C:/repo", "media-file"], | ||
| ] as const)( | ||
| "opens %s using %s and the matching resource scope", | ||
| async (filePath, workspaceRoot, resourceTag) => { | ||
| const createAssetUrl = vi.fn(async () => | ||
| AsyncResult.success(assetResult("/api/assets/token/report")), | ||
| ); | ||
| const result = await openFileInExternalBrowser({ | ||
| threadRef, | ||
| filePath, | ||
| workspaceRoot, | ||
| httpBaseUrl: "http://environment.test", | ||
| createAssetUrl, | ||
| beginOpen: () => ({ open: vi.fn(async () => undefined), cancel: vi.fn() }), | ||
| }); | ||
| expect(result._tag).toBe("Success"); | ||
| expect(createAssetUrl).toHaveBeenCalledWith({ | ||
| environmentId: threadRef.environmentId, | ||
| input: { resource: { _tag: resourceTag, threadId: threadRef.threadId, path: filePath } }, | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| it("reserves the browser target before requesting the signed URL", async () => { | ||
| const calls: string[] = []; | ||
| const open = vi.fn(async (url: string): Promise<void> => { | ||
| calls.push(`open:${url}`); | ||
| }); | ||
| const cancel = vi.fn(); | ||
| const createAssetUrl = vi.fn(async () => { | ||
| calls.push("asset"); | ||
| return AsyncResult.success(assetResult("/api/assets/token/report.html")); | ||
| }); | ||
|
|
||
| const result = await openFileInExternalBrowser({ | ||
| threadRef, | ||
| workspaceRoot: "/repo", | ||
| filePath: "/repo/artifacts/report.html", | ||
| httpBaseUrl: "http://environment.test:1234", | ||
| createAssetUrl, | ||
| beginOpen: () => { | ||
| calls.push("begin"); | ||
| return { open, cancel }; | ||
| }, | ||
| }); | ||
|
|
||
| expect(result._tag).toBe("Success"); | ||
| expect(calls).toEqual([ | ||
| "begin", | ||
| "asset", | ||
| "open:http://environment.test:1234/api/assets/token/report.html", | ||
| ]); | ||
| expect(cancel).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("closes the reserved target when signed URL creation fails", async () => { | ||
| const cancel = vi.fn(); | ||
| const result = await openFileInExternalBrowser({ | ||
| threadRef, | ||
| workspaceRoot: "/repo", | ||
| filePath: "/repo/artifacts/report.html", | ||
| httpBaseUrl: "http://environment.test:1234", | ||
| createAssetUrl: vi.fn(async () => | ||
| AsyncResult.failure<AssetCreateUrlResult, Error>(Cause.fail(new Error("missing"))), | ||
| ), | ||
| beginOpen: () => ({ open: vi.fn(), cancel }), | ||
| }); | ||
|
|
||
| expect(result._tag).toBe("Failure"); | ||
| expect(cancel).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("closes the reserved target when signed URL creation rejects", async () => { | ||
| const cancel = vi.fn(); | ||
| const failure = new Error("connection closed"); | ||
| await expect( | ||
| openFileInExternalBrowser({ | ||
| threadRef, | ||
| workspaceRoot: "/repo", | ||
| filePath: "/repo/artifacts/report.html", | ||
| httpBaseUrl: "http://environment.test:1234", | ||
| createAssetUrl: vi.fn(async () => Promise.reject(failure)), | ||
| beginOpen: () => ({ open: vi.fn(), cancel }), | ||
| }), | ||
| ).rejects.toBe(failure); | ||
| expect(cancel).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("closes the reserved target when navigation fails", async () => { | ||
| const cancel = vi.fn(); | ||
| const failure = new Error("blocked"); | ||
| await expect( | ||
| openFileInExternalBrowser({ | ||
| threadRef, | ||
| workspaceRoot: "/repo", | ||
| filePath: "/repo/artifacts/report.pdf", | ||
| httpBaseUrl: "http://environment.test:1234", | ||
| createAssetUrl: vi.fn(async () => | ||
| AsyncResult.success(assetResult("/api/assets/token/report.pdf")), | ||
| ), | ||
| beginOpen: () => ({ | ||
| open: vi.fn(async () => Promise.reject(failure)), | ||
| cancel, | ||
| }), | ||
| }), | ||
| ).rejects.toBe(failure); | ||
| expect(cancel).toHaveBeenCalledOnce(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.