|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const { mockWriteText, mockGet } = vi.hoisted(() => ({ |
| 4 | + mockWriteText: vi.fn(), |
| 5 | + mockGet: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock("@/app/store/wshclientapi", () => ({ RpcApi: {} })); |
| 9 | +vi.mock("@/app/store/wshrpcutil", () => ({ TabRpcClient: {} })); |
| 10 | +vi.mock("@/store/services", () => ({})); |
| 11 | +vi.mock("@/store/global", () => ({ |
| 12 | + getApi: vi.fn(), |
| 13 | + getBlockMetaKeyAtom: vi.fn(), |
| 14 | + getBlockTermDurableAtom: vi.fn(), |
| 15 | + getOverrideConfigAtom: vi.fn((_blockId: string, key: string) => ({ key })), |
| 16 | + globalStore: { get: mockGet }, |
| 17 | + recordTEvent: vi.fn(), |
| 18 | + WOS: {}, |
| 19 | +})); |
| 20 | +vi.mock("@/util/util", () => ({ |
| 21 | + base64ToString: (data: string) => Buffer.from(data, "base64").toString("utf8"), |
| 22 | + fireAndForget: (fn: () => Promise<void>) => { |
| 23 | + void fn(); |
| 24 | + }, |
| 25 | + isSshConnName: vi.fn(), |
| 26 | + isWslConnName: vi.fn(), |
| 27 | +})); |
| 28 | + |
| 29 | +import { handleOsc52Command } from "./osc-handlers"; |
| 30 | + |
| 31 | +describe("handleOsc52Command", () => { |
| 32 | + beforeEach(() => { |
| 33 | + vi.clearAllMocks(); |
| 34 | + mockWriteText.mockResolvedValue(undefined); |
| 35 | + Object.defineProperty(globalThis, "navigator", { |
| 36 | + configurable: true, |
| 37 | + value: { clipboard: { writeText: mockWriteText } }, |
| 38 | + }); |
| 39 | + Object.defineProperty(globalThis, "document", { |
| 40 | + configurable: true, |
| 41 | + value: { hasFocus: () => true }, |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("rejects unfocused block when term:osc52 is focus", () => { |
| 46 | + mockGet.mockImplementation((atom: { key?: string } | undefined) => { |
| 47 | + if (atom?.key === "term:osc52") { |
| 48 | + return "focus"; |
| 49 | + } |
| 50 | + return false; |
| 51 | + }); |
| 52 | + |
| 53 | + handleOsc52Command("c;SGVsbG8=", "block-1", true, { nodeModel: { isFocused: {} } } as any); |
| 54 | + |
| 55 | + expect(mockWriteText).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("allows unfocused block when term:osc52 is always", async () => { |
| 59 | + mockGet.mockImplementation((atom: { key?: string } | undefined) => { |
| 60 | + if (atom?.key === "term:osc52") { |
| 61 | + return "always"; |
| 62 | + } |
| 63 | + return false; |
| 64 | + }); |
| 65 | + |
| 66 | + handleOsc52Command("c;SGVsbG8=", "block-1", true, { nodeModel: { isFocused: {} } } as any); |
| 67 | + await Promise.resolve(); |
| 68 | + |
| 69 | + expect(mockWriteText).toHaveBeenCalledWith("Hello"); |
| 70 | + }); |
| 71 | +}); |
0 commit comments