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 { DesktopSshPasswordPromptRequest } from "@t3tools/contracts"; |
| 2 | import { expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { render, stubGlobal } from "../../../test/render"; |
| 5 | import { SshPasswordPromptDialog, startSshPasswordPromptQueue } from "./SshPasswordPromptDialog"; |
| 6 | |
| 7 | function stubPromptBridge() { |
| 8 | const listeners: Array<(request: DesktopSshPasswordPromptRequest) => void> = []; |
| 9 | const settledListeners: Array<(requestId: string) => void> = []; |
| 10 | const bridge = stubGlobal("desktopBridge", { |
| 11 | onSshPasswordPrompt: (listener: (request: DesktopSshPasswordPromptRequest) => void) => { |
| 12 | listeners.push(listener); |
| 13 | return () => listeners.splice(listeners.indexOf(listener), 1); |
| 14 | }, |
| 15 | onSshPasswordPromptSettled: (listener: (requestId: string) => void) => { |
| 16 | settledListeners.push(listener); |
| 17 | return () => settledListeners.splice(settledListeners.indexOf(listener), 1); |
| 18 | }, |
| 19 | resolveSshPasswordPrompt: async () => undefined, |
| 20 | }); |
| 21 | return { |
| 22 | prompt: (requestId: string, destination: string) => { |
| 23 | for (const listener of listeners) { |
| 24 | listener({ |
| 25 | requestId, |
| 26 | destination, |
| 27 | username: "julius", |
| 28 | prompt: `julius@${destination}'s password:`, |
| 29 | expiresAt: "2999-01-01T00:00:00.000Z", |
| 30 | }); |
| 31 | } |
| 32 | }, |
| 33 | settle: (requestId: string) => { |
| 34 | for (const listener of settledListeners) listener(requestId); |
| 35 | }, |
| 36 | [Symbol.dispose]: () => bridge[Symbol.dispose](), |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | it("drops a prompt the desktop ended, whether it is on screen or still waiting", async () => { |
| 41 | using bridge = stubPromptBridge(); |
| 42 | const stopQueue = startSshPasswordPromptQueue(); |
| 43 | using _queue = { [Symbol.dispose]: stopQueue }; |
| 44 | await using screen = await render(<SshPasswordPromptDialog />); |
| 45 | |
| 46 | bridge.prompt("ssh-1", "first-host"); |
| 47 | bridge.prompt("ssh-1", "first-host"); |
| 48 | bridge.prompt("ssh-2", "second-host"); |
| 49 | bridge.prompt("ssh-3", "third-host"); |
| 50 | await expect.element(screen.getByText("julius@first-host")).toBeVisible(); |
| 51 | |
| 52 | // ssh-2 ended while it waited behind ssh-1, and ssh-1 ended on screen. |
| 53 | bridge.settle("ssh-2"); |
| 54 | bridge.settle("ssh-1"); |
| 55 | await expect.element(screen.getByText("julius@third-host")).toBeVisible(); |
| 56 | |
| 57 | bridge.settle("ssh-3"); |
| 58 | await expect.element(screen.getByText("SSH verification required")).not.toBeInTheDocument(); |
| 59 | }); |
| 60 | |
| 61 | it("shows a prompt that arrived before the dialog mounted", async () => { |
| 62 | using bridge = stubPromptBridge(); |
| 63 | const stopQueue = startSshPasswordPromptQueue(); |
| 64 | using _queue = { [Symbol.dispose]: stopQueue }; |
| 65 | |
| 66 | // The desktop asks while the app is still on a page without the dialog (pairing, sign-in). |
| 67 | bridge.prompt("ssh-1", "devbox"); |
| 68 | |
| 69 | await using screen = await render(<SshPasswordPromptDialog />); |
| 70 | await expect.element(screen.getByText("SSH verification required")).toBeVisible(); |
| 71 | }); |