Skip to content

Commit 1963a2f

Browse files
committed
fix(web): copy public GitHub images while disconnected
1 parent f4ca44e commit 1963a2f

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎apps/web/src/components/media/MediaActions.test.tsx‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function deferred<T>() {
1919

2020
const state = vi.hoisted(() => ({
2121
sessions: new Map<string, Pick<AuthSessionState, "authenticated" | "scopes">>(),
22+
connection: vi.fn(),
2223
mint: vi.fn(),
2324
download: vi.fn(),
2425
png: vi.fn(),
@@ -43,7 +44,7 @@ vi.mock("../../state/assets", () => ({ assetEnvironment: { createUrl: {} } }));
4344
vi.mock("../../state/use-atom-query-runner", () => ({ useAtomQueryRunner: () => state.mint }));
4445
vi.mock("../../state/session", () => ({
4546
environmentSession: { sessionStateAtom: (environmentId: string) => environmentId },
46-
readPreparedConnection: () => ({ httpBaseUrl: "https://host.test" }),
47+
readPreparedConnection: (environmentId: string) => state.connection(environmentId),
4748
}));
4849
vi.mock("../../state/query", () => ({
4950
useEnvironmentQuery: (environmentId: string) => ({
@@ -122,6 +123,7 @@ function openMenu(source: MediaActionSource) {
122123
beforeEach(() => {
123124
state.sessions.clear();
124125
state.sessions.set(environmentId, denied);
126+
state.connection.mockReset().mockReturnValue({ httpBaseUrl: "https://host.test" });
125127
state.mint
126128
.mockReset()
127129
.mockResolvedValue(AsyncResult.success({ relativeUrl: "/api/assets/image.png", expiresAt: 1 }));
@@ -242,6 +244,40 @@ it("remints GitHub media before using its public display URL", async () => {
242244
expect(await item.items["image/png"]).toEqual(png);
243245
});
244246

247+
it("copies a readable public GitHub image while its environment is disconnected", async () => {
248+
const { readMediaPng } = await vi.importActual<typeof import("./mediaContent")>("./mediaContent");
249+
state.png.mockImplementation(readMediaPng);
250+
state.connection.mockReturnValue(null);
251+
const png = new Blob(["public GitHub image"], { type: "image/png" });
252+
using fetch = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(png));
253+
const url = "https://raw.githubusercontent.com/example/repo/main/image.png";
254+
255+
await useMediaActions({
256+
kind: "image",
257+
name: "image.png",
258+
src: url,
259+
environmentId,
260+
asset: { environmentId, resource: { _tag: "github-media", cwd: "/repo", url } },
261+
}).copyImage();
262+
263+
expect(state.mint).not.toHaveBeenCalled();
264+
expect(fetch).toHaveBeenCalledExactlyOnceWith(url);
265+
const [item] = state.clipboard.mock.calls[0]![0];
266+
expect(await item.items["image/png"]).toEqual(png);
267+
});
268+
269+
it("does not bypass host-file assets through their display URL when disconnected", async () => {
270+
state.connection.mockReturnValue(null);
271+
state.sessions.set(environmentId, granted);
272+
const source = { ...hostSource(), src: "https://images.example.com/image.png" };
273+
274+
await expect(useMediaActions(source).copyImage()).rejects.toThrow("Reconnect");
275+
await expect(useMediaActions(source).save()).rejects.toThrow("Reconnect");
276+
expect(state.mint).not.toHaveBeenCalled();
277+
expect(state.png).not.toHaveBeenCalled();
278+
expect(state.download).not.toHaveBeenCalled();
279+
});
280+
245281
it.each(["workspace-file", "media-file"] as const)(
246282
"disables denied %s byte actions and prevents imperative requests",
247283
async (_tag) => {

‎apps/web/src/components/media/MediaActions.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ export function useMediaActions(source: MediaActionSource) {
8787
}
8888
const { environmentId, resource } = asset;
8989
const connection = readPreparedConnection(environmentId);
90-
if (!connection) throw new Error("Reconnect to this environment and try again.");
90+
if (!connection) {
91+
if (resource._tag === "github-media") return resource.url;
92+
throw new Error("Reconnect to this environment and try again.");
93+
}
9194
const result = await createAssetUrl({ environmentId, input: { resource } });
9295
if (result._tag === "Failure") {
9396
if (resource._tag === "github-media") return resource.url;

0 commit comments

Comments
 (0)