The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, expect, it } from "vite-plus/test"; |
| 2 | import { DEFAULT_CLIENT_SETTINGS, type DesktopSnapShotState } from "@t3tools/contracts"; |
| 3 | |
| 4 | import { |
| 5 | createRecordingRequestTracker, |
| 6 | snapShotStatus, |
| 7 | snapShotShortcutStatus, |
| 8 | snapShotSetupSummary, |
| 9 | snapShotSetupButtonLabel, |
| 10 | snapShotSetupComplete, |
| 11 | } from "./SnapShotSettings.logic"; |
| 12 | |
| 13 | it("ignores a stale request after a newer request starts", () => { |
| 14 | const requests = createRecordingRequestTracker(); |
| 15 | const firstRequest = requests.tryBegin(); |
| 16 | assert(firstRequest); |
| 17 | |
| 18 | requests.clear(); |
| 19 | const secondRequest = requests.tryBegin(); |
| 20 | assert(secondRequest); |
| 21 | |
| 22 | expect(requests.owns(firstRequest)).toBe(false); |
| 23 | expect(requests.owns(secondRequest)).toBe(true); |
| 24 | expect(requests.tryBegin()).toBeNull(); |
| 25 | }); |
| 26 | |
| 27 | it.each(["gnome-extension", "picker"] as const)( |
| 28 | "waits for opt-in before presenting %s setup requirements", |
| 29 | (linuxBackend) => { |
| 30 | const state: DesktopSnapShotState = { |
| 31 | mode: "portal", |
| 32 | linuxBackend, |
| 33 | shortcut: DEFAULT_CLIENT_SETTINGS.snapShotShortcut, |
| 34 | shortcutRegistered: false, |
| 35 | shortcutMessage: "Shortcut permission needed", |
| 36 | message: "Capture needs attention", |
| 37 | gnomeExtension: { status: "not-installed", message: "Install the extension" }, |
| 38 | }; |
| 39 | |
| 40 | expect(DEFAULT_CLIENT_SETTINGS.snapShotEnabled).toBe(false); |
| 41 | expect(snapShotStatus(state, false)).toBe("Turn this on to set up snapshots."); |
| 42 | expect(snapShotStatus(state, true)).toBe("Capture needs attention"); |
| 43 | }, |
| 44 | ); |
| 45 | |
| 46 | it("distinguishes saved shortcuts from observed delivery without making users repeat setup", () => { |
| 47 | const state: DesktopSnapShotState = { |
| 48 | mode: "portal", |
| 49 | linuxBackend: "gnome-extension", |
| 50 | shortcut: DEFAULT_CLIENT_SETTINGS.snapShotShortcut, |
| 51 | shortcutRegistered: true, |
| 52 | shortcutMessage: "Requested", |
| 53 | message: null, |
| 54 | gnomeExtension: { status: "enabled", message: "Running" }, |
| 55 | }; |
| 56 | expect(snapShotSetupSummary(state, true)).toBe("Shortcut saved"); |
| 57 | expect(snapShotSetupButtonLabel(state)).toBe("Manage capture"); |
| 58 | expect(snapShotSetupSummary({ ...state, shortcutVerified: true }, true)).toBe("Ready to capture"); |
| 59 | expect(snapShotSetupButtonLabel({ ...state, shortcutVerified: true })).toBe("Manage capture"); |
| 60 | expect(snapShotSetupButtonLabel({ ...state, shortcutRegistered: false })).toBe("Manage capture"); |
| 61 | expect( |
| 62 | snapShotSetupButtonLabel({ |
| 63 | ...state, |
| 64 | gnomeExtension: { status: "disabled", message: "Enable the extension" }, |
| 65 | }), |
| 66 | ).toBe("Set up GNOME capture"); |
| 67 | expect(snapShotSetupSummary({ ...state, shortcutVerified: true }, false)).toContain( |
| 68 | "Enable capture", |
| 69 | ); |
| 70 | expect( |
| 71 | snapShotSetupSummary( |
| 72 | { |
| 73 | ...state, |
| 74 | gnomeExtension: { status: "restart-required", message: "Sign out" }, |
| 75 | shortcutVerified: true, |
| 76 | }, |
| 77 | true, |
| 78 | ), |
| 79 | ).toBe("Set up active-window snapshots"); |
| 80 | expect( |
| 81 | snapShotSetupSummary( |
| 82 | { ...state, linuxBackend: "niri", gnomeExtension: undefined, shortcutRegistered: false }, |
| 83 | true, |
| 84 | ), |
| 85 | ).toBe("Finish shortcut setup"); |
| 86 | expect( |
| 87 | snapShotSetupSummary( |
| 88 | { ...state, linuxBackend: "picker", gnomeExtension: undefined, shortcutVerified: true }, |
| 89 | true, |
| 90 | ), |
| 91 | ).toBe("Manual capture only. You'll choose a window each time"); |
| 92 | expect( |
| 93 | snapShotSetupSummary( |
| 94 | { |
| 95 | ...state, |
| 96 | linuxBackend: "screenshot-portal", |
| 97 | gnomeExtension: { status: "not-installed", message: "Optional extension" }, |
| 98 | shortcutVerified: true, |
| 99 | }, |
| 100 | true, |
| 101 | ), |
| 102 | ).toBe("Ready to capture"); |
| 103 | }); |
| 104 | |
| 105 | it("reports pending, denied, and assigned shortcuts without inferring consent from saved keys", () => { |
| 106 | const state: DesktopSnapShotState = { |
| 107 | mode: "portal", |
| 108 | linuxBackend: "screenshot-portal", |
| 109 | shortcut: DEFAULT_CLIENT_SETTINGS.snapShotShortcut, |
| 110 | shortcutRegistered: false, |
| 111 | shortcutPending: true, |
| 112 | shortcutMessage: null, |
| 113 | message: null, |
| 114 | }; |
| 115 | expect(snapShotStatus(state, true)).toContain("Waiting for shortcut permission"); |
| 116 | expect(snapShotShortcutStatus(state)).toContain("Approve the shortcut permission prompt"); |
| 117 | const denied = { ...state, shortcutPending: false, shortcutMessage: "Permission wasn't granted" }; |
| 118 | expect(snapShotShortcutStatus(denied)).toBe("Permission wasn't granted"); |
| 119 | const approved = { |
| 120 | ...denied, |
| 121 | shortcutRegistered: true, |
| 122 | shortcutLabel: "Press <Shift><Control>2", |
| 123 | shortcutMessage: "Desktop shortcut: Press <Shift><Control>2", |
| 124 | }; |
| 125 | expect(snapShotStatus(approved, true)).toBe("Ready to capture"); |
| 126 | expect(snapShotShortcutStatus(approved)).toBeNull(); |
| 127 | expect(snapShotShortcutStatus({ ...approved, shortcutPending: true })).toContain( |
| 128 | "Approve the shortcut permission prompt", |
| 129 | ); |
| 130 | expect( |
| 131 | snapShotShortcutStatus({ |
| 132 | ...approved, |
| 133 | shortcutRegistered: false, |
| 134 | shortcutMessage: "Permission wasn't granted", |
| 135 | }), |
| 136 | ).toBe("Permission wasn't granted"); |
| 137 | }); |
| 138 | |
| 139 | it("hides macOS setup only while permissions and the shortcut are all in place", () => { |
| 140 | const ready: DesktopSnapShotState = { |
| 141 | mode: "direct", |
| 142 | shortcut: DEFAULT_CLIENT_SETTINGS.snapShotShortcut, |
| 143 | shortcutRegistered: true, |
| 144 | shortcutMessage: null, |
| 145 | message: null, |
| 146 | macPermissions: { screenRecording: true, accessibility: true }, |
| 147 | }; |
| 148 | expect(snapShotSetupComplete(ready, true)).toBe(true); |
| 149 | expect(snapShotSetupComplete({ ...ready, macPermissions: undefined }, true)).toBe(false); |
| 150 | expect(snapShotSetupComplete({ ...ready, shortcutRegistered: false }, true)).toBe(false); |
| 151 | const revoked = { |
| 152 | ...ready, |
| 153 | macPermissions: { screenRecording: true, accessibility: false }, |
| 154 | message: "Allow Accessibility in System Settings, then restart T3 Code.", |
| 155 | }; |
| 156 | expect(snapShotSetupComplete(revoked, true)).toBe(false); |
| 157 | expect(snapShotStatus(revoked, true)).toBe("Capture needs attention"); |
| 158 | expect(snapShotSetupButtonLabel(revoked)).toBe("Continue setup"); |
| 159 | expect(snapShotSetupComplete({ ...revoked, message: null }, false)).toBe(true); |
| 160 | expect( |
| 161 | snapShotSetupComplete( |
| 162 | { ...ready, windows: true, macPermissions: undefined, shortcutRegistered: false }, |
| 163 | true, |
| 164 | ), |
| 165 | ).toBe(true); |
| 166 | }); |