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 { AuthPairingLink } from "@t3tools/contracts"; |
| 2 | import * as DateTime from "effect/DateTime"; |
| 3 | import { describe, expect, it } from "vite-plus/test"; |
| 4 | |
| 5 | import { connectDeviceApprovalPrompts, createApprovalPromptQueue } from "./connectDeviceApproval"; |
| 6 | |
| 7 | const link = (id: string, createdAtMs: number, request?: AuthPairingLink["connectDeviceRequest"]) => |
| 8 | ({ |
| 9 | id, |
| 10 | scopes: [], |
| 11 | subject: "cloud-connect", |
| 12 | createdAt: DateTime.makeUnsafe(createdAtMs), |
| 13 | expiresAt: DateTime.makeUnsafe(createdAtMs + 600_000), |
| 14 | ...(request ? { connectDeviceRequest: request } : {}), |
| 15 | }) as unknown as AuthPairingLink; |
| 16 | |
| 17 | describe("connectDeviceApprovalPrompts", () => { |
| 18 | it("prompts once per waiting device, oldest first, and skips ordinary pairing links", () => { |
| 19 | const pairingLinks = [ |
| 20 | link("newer", 2_000, { verificationCode: "WXYZ-2345" }), |
| 21 | link("plain", 500), |
| 22 | link("older", 1_000, { |
| 23 | verificationCode: "ABCD-EFGH", |
| 24 | client: { deviceType: "desktop", browser: "Chrome", os: "macOS" }, |
| 25 | }), |
| 26 | ]; |
| 27 | |
| 28 | const prompts = connectDeviceApprovalPrompts({ |
| 29 | environmentLabel: "Mac mini", |
| 30 | pairingLinks, |
| 31 | shownIds: new Set(["newer"]), |
| 32 | }); |
| 33 | |
| 34 | expect(prompts.map((prompt) => prompt.pairingLinkId)).toEqual(["older"]); |
| 35 | expect(prompts[0]?.message).toContain("Chrome on macOS wants to connect to Mac mini"); |
| 36 | expect(prompts[0]?.message).toContain("ABCD-EFGH"); |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | describe("createApprovalPromptQueue", () => { |
| 41 | it("shows one prompt at a time and offers a failed one again before the rest", () => { |
| 42 | const shown: string[] = []; |
| 43 | const answers: Array<(outcome: "done" | "retry" | "later") => void> = []; |
| 44 | const queue = createApprovalPromptQueue<{ pairingLinkId: string }>((prompt, answer) => { |
| 45 | shown.push(prompt.pairingLinkId); |
| 46 | answers.push(answer); |
| 47 | }); |
| 48 | |
| 49 | queue.offer([{ pairingLinkId: "a" }, { pairingLinkId: "b" }]); |
| 50 | queue.offer([{ pairingLinkId: "a" }]); |
| 51 | expect(shown).toEqual(["a"]); |
| 52 | |
| 53 | answers.at(-1)!("retry"); |
| 54 | expect(shown).toEqual(["a", "a"]); |
| 55 | answers.at(-1)!("done"); |
| 56 | expect(shown).toEqual(["a", "a", "b"]); |
| 57 | }); |
| 58 | |
| 59 | it("waits for the next offer when another dialog replaced the prompt", () => { |
| 60 | const shown: string[] = []; |
| 61 | const answers: Array<(outcome: "done" | "retry" | "later") => void> = []; |
| 62 | const queue = createApprovalPromptQueue<{ pairingLinkId: string }>((prompt, answer) => { |
| 63 | shown.push(prompt.pairingLinkId); |
| 64 | answers.push(answer); |
| 65 | }); |
| 66 | |
| 67 | queue.offer([{ pairingLinkId: "a" }]); |
| 68 | answers.at(-1)!("later"); |
| 69 | expect(shown).toEqual(["a"]); |
| 70 | queue.offer([]); |
| 71 | expect(shown).toEqual(["a", "a"]); |
| 72 | }); |
| 73 | |
| 74 | it("offers a dropped host's on-screen prompt again once the host is back", () => { |
| 75 | const shown: string[] = []; |
| 76 | const answers: Array<(outcome: "done" | "retry" | "later") => void> = []; |
| 77 | const queue = createApprovalPromptQueue<{ pairingLinkId: string; host: string }>( |
| 78 | (prompt, answer) => { |
| 79 | shown.push(prompt.pairingLinkId); |
| 80 | answers.push(answer); |
| 81 | }, |
| 82 | ); |
| 83 | |
| 84 | queue.offer([{ pairingLinkId: "a", host: "mini" }]); |
| 85 | queue.drop((prompt) => prompt.host === "mini"); |
| 86 | answers.at(-1)!("retry"); |
| 87 | expect(shown).toEqual(["a"]); |
| 88 | queue.offer([{ pairingLinkId: "a", host: "mini" }]); |
| 89 | expect(shown).toEqual(["a", "a"]); |
| 90 | }); |
| 91 | |
| 92 | it("drops a host's waiting prompts so they cannot hold up other hosts", () => { |
| 93 | const shown: string[] = []; |
| 94 | const answers: Array<(outcome: "done" | "retry" | "later") => void> = []; |
| 95 | const queue = createApprovalPromptQueue<{ pairingLinkId: string; host: string }>( |
| 96 | (prompt, answer) => { |
| 97 | shown.push(prompt.pairingLinkId); |
| 98 | answers.push(answer); |
| 99 | }, |
| 100 | ); |
| 101 | |
| 102 | queue.offer([ |
| 103 | { pairingLinkId: "a", host: "mini" }, |
| 104 | { pairingLinkId: "b", host: "mini" }, |
| 105 | { pairingLinkId: "c", host: "laptop" }, |
| 106 | ]); |
| 107 | queue.drop((prompt) => prompt.host === "mini"); |
| 108 | answers.at(-1)!("done"); |
| 109 | expect(shown).toEqual(["a", "c"]); |
| 110 | // A dropped host's request can be offered again once the host is back. |
| 111 | expect(queue.known.has("b")).toBe(false); |
| 112 | }); |
| 113 | }); |