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 { |
| 2 | AuthFilesystemReadScope, |
| 3 | EnvironmentId, |
| 4 | ThreadId, |
| 5 | type AuthSessionState, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { AsyncResult } from "effect/reactivity"; |
| 8 | import { beforeEach, expect, it, vi } from "vite-plus/test"; |
| 9 | |
| 10 | const state = vi.hoisted(() => ({ |
| 11 | sessions: new Map<string, Pick<AuthSessionState, "authenticated" | "scopes">>(), |
| 12 | refresh: vi.fn(), |
| 13 | download: vi.fn(), |
| 14 | downloadPreview: vi.fn(), |
| 15 | saveImage: vi.fn(), |
| 16 | shareLocal: vi.fn(), |
| 17 | shareDraft: vi.fn(), |
| 18 | navigate: vi.fn(), |
| 19 | copy: vi.fn(), |
| 20 | })); |
| 21 | |
| 22 | vi.mock("react", () => ({ |
| 23 | useEffect: () => {}, |
| 24 | useRef: <A>(current: A) => ({ current }), |
| 25 | useState: <A>(initial: A) => [initial, () => {}], |
| 26 | })); |
| 27 | vi.mock("react-native", () => ({ Alert: { alert: vi.fn() } })); |
| 28 | vi.mock("gt-react-native", async (importOriginal) => { |
| 29 | const gt = await importOriginal<typeof import("gt-react-native")>(); |
| 30 | return { ...gt, useGT: () => gt.mFallback, useMessages: () => gt.mFallback }; |
| 31 | }); |
| 32 | vi.mock("@react-navigation/native", () => ({ |
| 33 | useNavigation: () => ({ navigate: state.navigate }), |
| 34 | })); |
| 35 | vi.mock("./markdownLinks", () => ({ |
| 36 | normalizeNativeMarkdownUrl: (uri: string) => uri, |
| 37 | })); |
| 38 | vi.mock("../state/assets", () => ({ |
| 39 | useRefreshAssetUrl: (environmentId: string, resource: unknown) => () => |
| 40 | state.refresh(environmentId, resource), |
| 41 | })); |
| 42 | vi.mock("../state/session", () => ({ |
| 43 | environmentSession: { sessionStateAtom: (environmentId: string) => environmentId }, |
| 44 | })); |
| 45 | vi.mock("../state/query", () => ({ |
| 46 | useEnvironmentQuery: (environmentId: string) => ({ |
| 47 | data: state.sessions.get(environmentId) ?? null, |
| 48 | error: null, |
| 49 | }), |
| 50 | })); |
| 51 | vi.mock("../state/atom-registry", () => ({ |
| 52 | appAtomRegistry: { |
| 53 | get: (environmentId: string) => { |
| 54 | const session = state.sessions.get(environmentId); |
| 55 | return session === undefined ? AsyncResult.initial() : AsyncResult.success(session); |
| 56 | }, |
| 57 | }, |
| 58 | })); |
| 59 | vi.mock("./attachmentDownload", () => ({ |
| 60 | downloadAndShareAttachment: state.download, |
| 61 | downloadAttachmentForPreview: state.downloadPreview, |
| 62 | shareLocalAttachment: state.shareLocal, |
| 63 | })); |
| 64 | vi.mock("./saveImageToLibrary", () => ({ |
| 65 | canSaveImageToLibrary: (mimeType: string) => mimeType === "image/png", |
| 66 | saveImageToLibrary: state.saveImage, |
| 67 | })); |
| 68 | vi.mock("expo-haptics", () => ({ |
| 69 | notificationAsync: async () => {}, |
| 70 | NotificationFeedbackType: { Success: "success" }, |
| 71 | })); |
| 72 | vi.mock("./copyTextWithHaptic", () => ({ copyTextWithHaptic: state.copy })); |
| 73 | vi.mock("./localAttachmentPreview", () => ({ |
| 74 | loadLocalAttachmentPreview: async () => ({ share: state.shareDraft, dispose: vi.fn() }), |
| 75 | })); |
| 76 | |
| 77 | import { useMediaActions } from "../state/mediaActions"; |
| 78 | import type { MediaActionsSource } from "./mediaActionsSource"; |
| 79 | |
| 80 | const environmentId = EnvironmentId.make("media-environment"); |
| 81 | const otherEnvironmentId = EnvironmentId.make("other-environment"); |
| 82 | const threadId = ThreadId.make("media-thread"); |
| 83 | const granted: Pick<AuthSessionState, "authenticated" | "scopes"> = { |
| 84 | authenticated: true, |
| 85 | scopes: [AuthFilesystemReadScope], |
| 86 | }; |
| 87 | const denied: Pick<AuthSessionState, "authenticated" | "scopes"> = { |
| 88 | authenticated: true, |
| 89 | scopes: [], |
| 90 | }; |
| 91 | |
| 92 | function hostSource(_tag: "workspace-file" | "media-file" = "media-file"): MediaActionsSource { |
| 93 | return { |
| 94 | environmentId, |
| 95 | threadId, |
| 96 | resource: { _tag, threadId, path: "/repo/image.png" }, |
| 97 | reference: { kind: "file", path: "/repo/image.png", relativePath: "image.png" }, |
| 98 | name: "image.png", |
| 99 | mimeType: "image/png", |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | beforeEach(() => { |
| 104 | state.sessions.clear(); |
| 105 | state.sessions.set(environmentId, denied); |
| 106 | state.refresh.mockReset().mockResolvedValue("https://host.test/image.png"); |
| 107 | state.download.mockReset().mockResolvedValue(undefined); |
| 108 | state.downloadPreview.mockReset(); |
| 109 | state.saveImage.mockReset().mockResolvedValue(undefined); |
| 110 | state.shareLocal.mockReset().mockResolvedValue(undefined); |
| 111 | state.shareDraft.mockReset().mockResolvedValue(undefined); |
| 112 | state.navigate.mockReset(); |
| 113 | state.copy.mockReset(); |
| 114 | }); |
| 115 | |
| 116 | it.each(["workspace-file", "media-file"] as const)( |
| 117 | "blocks denied %s sharing and file opening while preserving path copying", |
| 118 | async (_tag) => { |
| 119 | const media = useMediaActions(hostSource(_tag)); |
| 120 | await media.share(); |
| 121 | media.actions.find(({ id }) => id === "open-file")!.run(); |
| 122 | media.actions.find(({ id }) => id === "copy-full-path")!.run(); |
| 123 | |
| 124 | expect(state.refresh).not.toHaveBeenCalled(); |
| 125 | expect(state.download).not.toHaveBeenCalled(); |
| 126 | expect(state.navigate).not.toHaveBeenCalled(); |
| 127 | expect(state.copy).toHaveBeenCalledWith("/repo/image.png"); |
| 128 | expect(media.actions.find(({ id }) => id === "save")?.disabled).toBe(true); |
| 129 | expect(media.actions.find(({ id }) => id === "save-image")?.disabled).toBe(true); |
| 130 | expect(media.actions.find(({ id }) => id === "open-file")?.disabled).toBe(true); |
| 131 | }, |
| 132 | ); |
| 133 | |
| 134 | it("rechecks a retained menu action after revocation", async () => { |
| 135 | state.sessions.set(environmentId, granted); |
| 136 | const media = useMediaActions(hostSource()); |
| 137 | state.sessions.set(environmentId, denied); |
| 138 | |
| 139 | await media.share(); |
| 140 | media.actions.find(({ id }) => id === "open-file")!.run(); |
| 141 | |
| 142 | expect(state.refresh).not.toHaveBeenCalled(); |
| 143 | expect(state.navigate).not.toHaveBeenCalled(); |
| 144 | }); |
| 145 | |
| 146 | it("reenables sharing after gaining access while preserving a retained callback", async () => { |
| 147 | const media = useMediaActions(hostSource()); |
| 148 | state.sessions.set(environmentId, granted); |
| 149 | |
| 150 | await media.share(); |
| 151 | |
| 152 | expect(state.download).toHaveBeenCalledOnce(); |
| 153 | expect(useMediaActions(hostSource()).actions.find(({ id }) => id === "save")?.disabled).toBe( |
| 154 | false, |
| 155 | ); |
| 156 | }); |
| 157 | |
| 158 | it.each([false, true])("uses the media environment's grant (allowed: %s)", async (allowed) => { |
| 159 | state.sessions.set(environmentId, allowed ? granted : denied); |
| 160 | state.sessions.set(otherEnvironmentId, allowed ? denied : granted); |
| 161 | |
| 162 | await useMediaActions(hostSource()).share(); |
| 163 | |
| 164 | expect(state.refresh).toHaveBeenCalledTimes(allowed ? 1 : 0); |
| 165 | expect(state.download).toHaveBeenCalledTimes(allowed ? 1 : 0); |
| 166 | }); |
| 167 | |
| 168 | it("stops before downloading if access is revoked while the URL is refreshed", async () => { |
| 169 | state.sessions.set(environmentId, granted); |
| 170 | state.refresh.mockImplementation(async () => { |
| 171 | state.sessions.set(environmentId, denied); |
| 172 | return "https://host.test/image.png"; |
| 173 | }); |
| 174 | |
| 175 | await useMediaActions(hostSource()).share(); |
| 176 | |
| 177 | expect(state.refresh).toHaveBeenCalledOnce(); |
| 178 | expect(state.download).not.toHaveBeenCalled(); |
| 179 | }); |
| 180 | |
| 181 | it("lets an unresolved grant be authorized by an explicit server request", async () => { |
| 182 | state.sessions.delete(environmentId); |
| 183 | |
| 184 | await useMediaActions(hostSource()).share(); |
| 185 | |
| 186 | expect(state.refresh).toHaveBeenCalledOnce(); |
| 187 | expect(state.download).toHaveBeenCalledOnce(); |
| 188 | }); |
| 189 | |
| 190 | it.each(["https://cdn.test/image.png", "file:///device/image.png"])( |
| 191 | "shares direct media without a host grant: %s", |
| 192 | async (uri) => { |
| 193 | await useMediaActions({ uri, name: "image.png", mimeType: "image/png" }).share(); |
| 194 | |
| 195 | expect(state.refresh).not.toHaveBeenCalled(); |
| 196 | expect(uri.startsWith("file:") ? state.shareLocal : state.download).toHaveBeenCalledOnce(); |
| 197 | }, |
| 198 | ); |
| 199 | |
| 200 | it("saves a host image from its downloaded file, then removes the file", async () => { |
| 201 | state.sessions.set(environmentId, granted); |
| 202 | const disposed = Promise.withResolvers<void>(); |
| 203 | state.downloadPreview.mockResolvedValue({ |
| 204 | uri: "file:///cache/image.png", |
| 205 | dispose: disposed.resolve, |
| 206 | }); |
| 207 | |
| 208 | useMediaActions(hostSource()) |
| 209 | .actions.find(({ id }) => id === "save-image")! |
| 210 | .run(); |
| 211 | await disposed.promise; |
| 212 | |
| 213 | expect(state.downloadPreview).toHaveBeenCalledWith( |
| 214 | expect.objectContaining({ url: "https://host.test/image.png" }), |
| 215 | ); |
| 216 | expect(state.saveImage).toHaveBeenCalledWith("file:///cache/image.png", "image/png", "image.png"); |
| 217 | }); |