The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | import * as Cause from "effect/Cause"; |
| 3 | import { AsyncResult } from "effect/reactivity"; |
| 4 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 5 | |
| 6 | import type { OpenPreviewMutation } from "~/browser/openFileInPreview"; |
| 7 | import * as previewRuntime from "~/browser/previewRuntime"; |
| 8 | import { openTerminalLinkInPreview } from "./openTerminalLinkInPreview"; |
| 9 | |
| 10 | vi.mock("~/previewStateStore", () => ({ |
| 11 | applyPreviewServerSnapshot: vi.fn(), |
| 12 | isPreviewSupportedInRuntime: () => true, |
| 13 | })); |
| 14 | |
| 15 | vi.mock("~/rightPanelStore", () => ({ |
| 16 | useRightPanelStore: { |
| 17 | getState: () => ({ openBrowser: vi.fn() }), |
| 18 | }, |
| 19 | })); |
| 20 | |
| 21 | const browserDefaultsMocks = vi.hoisted(() => ({ |
| 22 | resolve: vi.fn(), |
| 23 | })); |
| 24 | |
| 25 | vi.mock("~/browser/browserDefaults", () => ({ |
| 26 | resolveBrowserDefaults: browserDefaultsMocks.resolve, |
| 27 | })); |
| 28 | |
| 29 | const linkTargetMocks = vi.hoisted(() => ({ |
| 30 | preference: vi.fn<() => "system" | "app">(), |
| 31 | })); |
| 32 | |
| 33 | vi.mock("~/browser/browserLinkTarget", () => ({ |
| 34 | resolveBrowserLinkTargetPreference: async () => linkTargetMocks.preference(), |
| 35 | isWebUrl: (url: string) => /^https?:/u.test(url), |
| 36 | })); |
| 37 | |
| 38 | const hydratedDefaults = { |
| 39 | viewport: { _tag: "fixed", width: 1280, height: 720 } as const, |
| 40 | profileId: "work", |
| 41 | zoomFactor: 0.8, |
| 42 | appearance: "dark", |
| 43 | }; |
| 44 | |
| 45 | const threadRef = { |
| 46 | environmentId: "local" as ScopedThreadRef["environmentId"], |
| 47 | threadId: "thread-1" as ScopedThreadRef["threadId"], |
| 48 | }; |
| 49 | |
| 50 | beforeEach(() => { |
| 51 | browserDefaultsMocks.resolve.mockReset(); |
| 52 | browserDefaultsMocks.resolve.mockResolvedValue(hydratedDefaults); |
| 53 | linkTargetMocks.preference.mockReturnValue("app"); |
| 54 | }); |
| 55 | |
| 56 | afterEach(() => { |
| 57 | vi.restoreAllMocks(); |
| 58 | }); |
| 59 | |
| 60 | describe("openTerminalLinkInPreview", () => { |
| 61 | it("does not report or fall back when opening the preview is interrupted", async () => { |
| 62 | const fallbackToBrowser = vi.fn(); |
| 63 | const reportError = vi.spyOn(console, "error").mockImplementation(() => undefined); |
| 64 | |
| 65 | await openTerminalLinkInPreview({ |
| 66 | url: "http://localhost:5173/", |
| 67 | threadRef, |
| 68 | openPreview: async () => AsyncResult.failure(Cause.interrupt()), |
| 69 | fallbackToBrowser, |
| 70 | forceBrowser: false, |
| 71 | }); |
| 72 | |
| 73 | expect(reportError).not.toHaveBeenCalled(); |
| 74 | expect(fallbackToBrowser).not.toHaveBeenCalled(); |
| 75 | }); |
| 76 | |
| 77 | it("opens a server tab with the default zoom and appearance", async () => { |
| 78 | vi.spyOn(previewRuntime, "previewRuntimeFor").mockReturnValue("server"); |
| 79 | const openPreview = vi.fn<OpenPreviewMutation>(async () => |
| 80 | AsyncResult.failure(Cause.interrupt()), |
| 81 | ); |
| 82 | |
| 83 | await openTerminalLinkInPreview({ |
| 84 | url: "https://t3.chat/", |
| 85 | threadRef, |
| 86 | openPreview, |
| 87 | fallbackToBrowser: vi.fn(), |
| 88 | forceBrowser: false, |
| 89 | }); |
| 90 | |
| 91 | expect(openPreview).toHaveBeenCalledExactlyOnceWith({ |
| 92 | environmentId: threadRef.environmentId, |
| 93 | input: expect.objectContaining({ runtime: "server", zoomFactor: 0.8, colorScheme: "dark" }), |
| 94 | }); |
| 95 | }); |
| 96 | }); |