|
5 | 5 | type AuthSessionState, |
6 | 6 | } from "@t3tools/contracts"; |
7 | 7 | import { AsyncResult } from "effect/reactivity"; |
| 8 | +import * as Cause from "effect/Cause"; |
8 | 9 | import { createElement, isValidElement, type ReactNode } from "react"; |
9 | 10 | import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
10 | 11 |
|
@@ -180,6 +181,67 @@ it("copies a CORS-blocked image through its source environment while keeping the |
180 | 181 | expect(await item.items["image/png"]).toEqual(png); |
181 | 182 | }); |
182 | 183 |
|
| 184 | +it.each([false, true])( |
| 185 | + "copies a public GitHub fallback after asset minting fails (CORS blocked: %s)", |
| 186 | + async (corsBlocked) => { |
| 187 | + const { readMediaPng } = |
| 188 | + await vi.importActual<typeof import("./mediaContent")>("./mediaContent"); |
| 189 | + state.png.mockImplementation(readMediaPng); |
| 190 | + const url = "https://raw.githubusercontent.com/example/repo/main/image.png"; |
| 191 | + const resource = { _tag: "github-media" as const, cwd: "/repo", url }; |
| 192 | + state.mint.mockResolvedValueOnce( |
| 193 | + AsyncResult.failure(Cause.fail(new Error("GitHub unavailable"))), |
| 194 | + ); |
| 195 | + const png = new Blob(["public image bytes"], { type: "image/png" }); |
| 196 | + using fetch = vi.spyOn(globalThis, "fetch"); |
| 197 | + if (corsBlocked) fetch.mockRejectedValueOnce(new TypeError("Failed to fetch")); |
| 198 | + fetch.mockResolvedValue(new Response(png)); |
| 199 | + |
| 200 | + await useMediaActions({ |
| 201 | + kind: "image", |
| 202 | + name: "image.png", |
| 203 | + src: url, |
| 204 | + environmentId, |
| 205 | + asset: { environmentId, resource }, |
| 206 | + }).copyImage(); |
| 207 | + |
| 208 | + expect(state.mint.mock.calls).toEqual([ |
| 209 | + [{ environmentId, input: { resource } }], |
| 210 | + ...(corsBlocked |
| 211 | + ? [[{ environmentId, input: { resource: { _tag: "remote-image", url } } }]] |
| 212 | + : []), |
| 213 | + ]); |
| 214 | + expect(fetch.mock.calls.map(([url]) => url)).toEqual([ |
| 215 | + url, |
| 216 | + ...(corsBlocked ? ["https://host.test/api/assets/image.png"] : []), |
| 217 | + ]); |
| 218 | + const [item] = state.clipboard.mock.calls[0]![0]; |
| 219 | + expect(await item.items["image/png"]).toEqual(png); |
| 220 | + }, |
| 221 | +); |
| 222 | + |
| 223 | +it("remints GitHub media before using its public display URL", async () => { |
| 224 | + const { readMediaPng } = await vi.importActual<typeof import("./mediaContent")>("./mediaContent"); |
| 225 | + state.png.mockImplementation(readMediaPng); |
| 226 | + const png = new Blob(["authenticated image bytes"], { type: "image/png" }); |
| 227 | + using fetch = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(png)); |
| 228 | + const url = "https://raw.githubusercontent.com/example/repo/main/private.png"; |
| 229 | + const resource = { _tag: "github-media" as const, cwd: "/repo", url }; |
| 230 | + |
| 231 | + await useMediaActions({ |
| 232 | + kind: "image", |
| 233 | + name: "private.png", |
| 234 | + src: url, |
| 235 | + environmentId, |
| 236 | + asset: { environmentId, resource }, |
| 237 | + }).copyImage(); |
| 238 | + |
| 239 | + expect(state.mint).toHaveBeenCalledExactlyOnceWith({ environmentId, input: { resource } }); |
| 240 | + expect(fetch).toHaveBeenCalledExactlyOnceWith("https://host.test/api/assets/image.png"); |
| 241 | + const [item] = state.clipboard.mock.calls[0]![0]; |
| 242 | + expect(await item.items["image/png"]).toEqual(png); |
| 243 | +}); |
| 244 | + |
183 | 245 | it.each(["workspace-file", "media-file"] as const)( |
184 | 246 | "disables denied %s byte actions and prevents imperative requests", |
185 | 247 | async (_tag) => { |
|
0 commit comments