Skip to content

Commit 0de78e3

Browse files
allensquareSquirtle Squadclaude
committed
fix(github): keep renewing signed media and keep shown panels on failed reads
Review follow-ups for private-repo body media: - A renewal read that failed or changed nothing blocked every later renewal. Allow one read in flight and clear it when the read settles; media that failed and has not loaded since still does not read again. - Adopting renewed HTML remounted every media element, because react-markdown got new component types on every render, restarting playing videos and reloading loaded images. Make the components stable (they read the media renderer from context) and have each element keep the signed link it shows until that link fails, so a renewal swaps only the media that failed. This replaces the body-wide HTML pinning. - Reading an issue panel again for an expired image replaced the shown issue with an error when the read failed. A failed read now keeps a shown object; a first read's error is unchanged. Co-authored-by: Squirtle Squad <ec57954349ef49ce554b9cda062c34933dd3a085cf82278bb0e955efea492c7e@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Allen Tsai <[email protected]>
1 parent 98c74d9 commit 0de78e3

3 files changed

Lines changed: 209 additions & 148 deletions

File tree

‎src/bundled/github/GitHubBody.test.tsx‎

Lines changed: 101 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -215,60 +215,89 @@ it("keeps original links when media fails before loading", () => {
215215
}
216216
});
217217

218-
it("shows private media from signed links and reads the body again once when they fail", () => {
219-
const signed = (jwt: string) =>
220-
`https://private-user-images.githubusercontent.com/1888043/1-cd0773ee-9861-46d9-8a0f-352ae951ec7f.png?jwt=${jwt}`;
221-
const html = (jwt: string) =>
222-
`<p><a href="${signed(jwt)}"><img src="${signed(jwt)}" alt="Screenshot"></a></p><video src="${signedBefore.replace("expired", jwt)}"></video>`;
223-
const refresh = vi.fn();
224-
const body = `![Screenshot](${image})\n\n${before}`;
218+
const signedImage = (jwt: string) =>
219+
`https://private-user-images.githubusercontent.com/1888043/1-cd0773ee-9861-46d9-8a0f-352ae951ec7f.png?jwt=${jwt}`;
220+
const signedVideo = (jwt: string) => signedBefore.replace("expired", jwt);
221+
const signedHtml = (jwt: string) =>
222+
`<p><a href="${signedImage(jwt)}"><img src="${signedImage(jwt)}" alt="Screenshot"></a></p><video src="${signedVideo(jwt)}"></video>`;
223+
const signedBody = `![Screenshot](${image})\n\n${before}`;
224+
225+
it("renews only failed private media from signed links, keeping media that works", async () => {
226+
const read = deferred<void>();
227+
const refresh = vi.fn(() => read.promise);
225228
const { container, rerender } = render(
226-
<GitHubBody url={url} body={body} bodyHtml={html("1")} refresh={refresh} />,
229+
<GitHubBody
230+
url={url}
231+
body={signedBody}
232+
bodyHtml={signedHtml("1")}
233+
refresh={refresh}
234+
/>,
227235
);
228-
const sources = () =>
229-
[...container.querySelectorAll("img, video")].map((media) =>
230-
media.getAttribute("src"),
231-
);
232-
expect(sources()).toEqual([
233-
signed("1"),
234-
signedBefore.replace("expired", "1"),
235-
]);
236-
// Failing together, they read the body once; the original links remain.
236+
const video = container.querySelector("video") as HTMLVideoElement;
237+
expect(screen.getByAltText("Screenshot")).toHaveAttribute(
238+
"src",
239+
signedImage("1"),
240+
);
241+
expect(video).toHaveAttribute("src", signedVideo("1"));
242+
fireEvent.loadedData(video);
243+
// The original link remains while the failed image renews.
237244
fireEvent.error(screen.getByAltText("Screenshot"));
238-
fireEvent.error(container.querySelector("video") as HTMLVideoElement);
239245
expect(refresh).toHaveBeenCalledOnce();
240246
expect(screen.getByRole("link", { name: "Screenshot" })).toHaveAttribute(
241247
"href",
242248
image,
243249
);
244-
expect(screen.getByRole("link", { name: before })).toHaveAttribute(
245-
"href",
246-
before,
247-
);
248-
rerender(
249-
<GitHubBody url={url} body={body} bodyHtml={html("2")} refresh={refresh} />,
250-
);
251-
expect(sources()).toEqual([
252-
signed("2"),
253-
signedBefore.replace("expired", "2"),
254-
]);
255-
// Working media keeps its source when the body is read again.
256-
fireEvent.load(screen.getByAltText("Screenshot"));
257250
rerender(
258-
<GitHubBody url={url} body={body} bodyHtml={html("3")} refresh={refresh} />,
251+
<GitHubBody
252+
url={url}
253+
body={signedBody}
254+
bodyHtml={signedHtml("2")}
255+
refresh={refresh}
256+
/>,
259257
);
260-
expect(screen.getByAltText("Screenshot")).toHaveAttribute("src", signed("2"));
261-
// A renewed source that fails before loading does not read again.
262-
fireEvent.error(container.querySelector("video") as HTMLVideoElement);
258+
expect(screen.getByAltText("Screenshot")).toHaveAttribute(
259+
"src",
260+
signedImage("2"),
261+
);
262+
// The loaded video keeps its element and link, so it does not restart.
263+
expect(container.querySelector("video")).toBe(video);
264+
expect(video).toHaveAttribute("src", signedVideo("1"));
265+
await act(async () => read.resolve());
266+
// A renewed link that fails before loading does not read again.
267+
fireEvent.error(screen.getByAltText("Screenshot"));
263268
expect(refresh).toHaveBeenCalledOnce();
264-
// Media that loaded and later expires reads again, and meanwhile takes
265-
// the links already read.
269+
});
270+
271+
it("reads again after a failed renewal, one read at a time", async () => {
272+
const signedAfter =
273+
"https://private-user-images.githubusercontent.com/1888043/2-dd1f7e4f-e2a4-4aec-b661-cb45032f29c5.mp4?jwt=1";
274+
const first = deferred<void>();
275+
const refresh = vi
276+
.fn<() => Promise<void>>()
277+
.mockReturnValueOnce(first.promise)
278+
.mockResolvedValue(undefined);
279+
const { container } = render(
280+
<GitHubBody
281+
url={url}
282+
body={`${signedBody}\n\n${after}`}
283+
bodyHtml={`${signedHtml("1")}<video src="${signedAfter}"></video>`}
284+
refresh={refresh}
285+
/>,
286+
);
287+
const [video, later] = container.querySelectorAll("video");
288+
if (!video || !later) throw new Error("Expected both videos");
289+
expect(later).toHaveAttribute("src", signedAfter);
290+
fireEvent.loadedData(video);
291+
fireEvent.loadedData(later);
266292
fireEvent.error(screen.getByAltText("Screenshot"));
293+
// Media failing while a read is in flight waits for that read.
294+
fireEvent.error(video);
295+
expect(refresh).toHaveBeenCalledOnce();
296+
await act(async () => first.reject(new Error("offline")));
297+
// The read failed and nothing changed; media that expires later still
298+
// reads again.
299+
fireEvent.error(later);
267300
expect(refresh).toHaveBeenCalledTimes(2);
268-
expect(sources()).toEqual([
269-
signed("3"),
270-
signedBefore.replace("expired", "3"),
271-
]);
272301
});
273302

274303
it("keeps public and raw media on their own links without reading again", () => {
@@ -557,10 +586,12 @@ it.each([
557586

558587
function deferred<T>() {
559588
let resolve!: (value: T) => void;
560-
const promise = new Promise<T>((done) => {
589+
let reject!: (error: unknown) => void;
590+
const promise = new Promise<T>((done, fail) => {
561591
resolve = done;
592+
reject = fail;
562593
});
563-
return { promise, resolve };
594+
return { promise, resolve, reject };
564595
}
565596

566597
it("ignores retired PR loads, retries errors, and clears media when navigating", async () => {
@@ -664,3 +695,30 @@ it("reads the pull request again when its signed image fails", async () => {
664695
);
665696
expect(fetch).toHaveBeenCalledTimes(2);
666697
});
698+
699+
it("keeps a shown issue when reading it again for its signed image fails", async () => {
700+
const issue = "https://github.com/block/buzz-app/issues/5";
701+
const fetch = vi
702+
.fn()
703+
.mockResolvedValueOnce(
704+
new Response(
705+
JSON.stringify({
706+
title: "Private issue",
707+
body: `![Screenshot](${image})`,
708+
body_html: `<img src="${signedImage("1")}" alt="Screenshot">`,
709+
}),
710+
),
711+
)
712+
.mockResolvedValueOnce(new Response(null, { status: 404 }));
713+
vi.stubGlobal("fetch", fetch);
714+
render(<GitHubPanel target={issue} close={() => {}} />);
715+
fireEvent.error(await screen.findByAltText("Screenshot"));
716+
await waitFor(() => expect(fetch).toHaveBeenCalledTimes(2));
717+
await act(async () => {});
718+
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
719+
expect(screen.getByRole("heading", { name: "Private issue" })).toBeVisible();
720+
expect(screen.getByRole("link", { name: "Screenshot" })).toHaveAttribute(
721+
"href",
722+
image,
723+
);
724+
});

0 commit comments

Comments
 (0)