Skip to content

Commit d8c06f6

Browse files
tellahoCarl
authored andcommitted
fix(link-preview): bound expired preview promotion
Co-authored-by: Carl <acda9e433d19dcd0e6b6840f7f4b98f3a56f1fab98049d444c087019e6d36560@buzz.block.builderlab.xyz> Signed-off-by: Taylor Ho <[email protected]>
1 parent b6e0438 commit d8c06f6

2 files changed

Lines changed: 86 additions & 3 deletions

File tree

‎desktop/src/features/messages/lib/linkPreviewPreparationStore.test.mjs‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import assert from "node:assert/strict";
2-
import test from "node:test";
2+
import { after, before, test } from "node:test";
3+
4+
import { JSDOM } from "jsdom";
35

46
import {
57
__linkPreviewPreparationTest,
@@ -12,6 +14,29 @@ import {
1214
const first = { href: "https://example.com/first" };
1315
const second = { href: "https://example.com/second" };
1416
const firstTag = ["link-preview", "snapshot", first.href];
17+
const dom = new JSDOM("<!doctype html><html><body></body></html>", {
18+
url: "http://localhost",
19+
});
20+
const ipcHandlers = new Map();
21+
22+
before(() => {
23+
Object.assign(globalThis, {
24+
document: dom.window.document,
25+
HTMLElement: dom.window.HTMLElement,
26+
window: dom.window,
27+
});
28+
dom.window.__TAURI_INTERNALS__ = {
29+
invoke: (cmd, args) => {
30+
const handler = ipcHandlers.get(cmd);
31+
return handler
32+
? handler(args)
33+
: Promise.reject(new Error(`unmocked Tauri command: ${cmd}`));
34+
},
35+
transformCallback: () => Math.random(),
36+
};
37+
});
38+
39+
after(() => dom.window.close());
1540

1641
function deferred() {
1742
let resolve;
@@ -41,6 +66,7 @@ function seed(
4166

4267
test.afterEach(() => {
4368
__linkPreviewPreparationTest.reset();
69+
ipcHandlers.clear();
4470
});
4571

4672
test("adopts one in-flight job for the same canonical URL", () => {
@@ -223,6 +249,60 @@ test("Skip after completion cannot replace finalized tags", async () => {
223249
});
224250
});
225251

252+
test("expired settled work replaced by a pending retry keeps deadline and Skip", async () => {
253+
const expiredTag = ["link-preview", "snapshot", first.href, "expired"];
254+
const pendingRetry = deferred();
255+
let fetchCalls = 0;
256+
ipcHandlers.set("fetch_link_preview_metadata", () => {
257+
fetchCalls += 1;
258+
return pendingRetry.promise;
259+
});
260+
ipcHandlers.set("cancel_link_preview_metadata", () => Promise.resolve());
261+
ipcHandlers.set("release_link_preview_metadata", () => Promise.resolve());
262+
263+
const seedExpiredFallback = () =>
264+
seed(
265+
first,
266+
Promise.resolve(expiredTag),
267+
true,
268+
Date.now() - 5 * 60_000,
269+
expiredTag,
270+
expiredTag,
271+
);
272+
273+
seedExpiredFallback();
274+
const timeoutPreparation = prepareBackgroundLinkPreviews([first], 0);
275+
assert.ok(timeoutPreparation);
276+
assert.equal(
277+
__linkPreviewPreparationTest.jobs.get(first.href)?.settled,
278+
false,
279+
);
280+
assert.equal(
281+
fetchCalls,
282+
1,
283+
"the expired job was replaced through production I/O",
284+
);
285+
assert.deepEqual(await timeoutPreparation.promise, {
286+
status: "ready",
287+
tags: [],
288+
});
289+
290+
seedExpiredFallback();
291+
const skipPreparation = prepareBackgroundLinkPreviews([first], 1_000);
292+
assert.ok(skipPreparation);
293+
assert.equal(
294+
__linkPreviewPreparationTest.jobs.get(first.href)?.settled,
295+
false,
296+
);
297+
skipPreparation.skip();
298+
assert.deepEqual(await skipPreparation.promise, {
299+
status: "ready",
300+
tags: [],
301+
});
302+
303+
pendingRetry.resolve(null);
304+
});
305+
226306
test("already-settled partial results contain only successful tags", async () => {
227307
seed(first, Promise.resolve(firstTag), true);
228308
seed(second, Promise.resolve(null), true);

‎desktop/src/features/messages/lib/linkPreviewPreparationStore.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,15 @@ export function prepareBackgroundLinkPreviews(
286286
skip,
287287
});
288288

289+
const preparations = external.map((candidate) =>
290+
prepareLinkPreview(candidate),
291+
);
289292
const pending = external.some(
290293
(candidate) => !jobs.get(candidate.href)?.settled,
291294
);
292295
if (!pending) {
293296
return preparedSend(
294-
Promise.all(external.map(prepareLinkPreview)).then((tags) => ({
297+
Promise.all(preparations).then((tags) => ({
295298
status: "ready" as const,
296299
tags: tags.filter((tag): tag is string[] => tag !== null),
297300
})),
@@ -347,7 +350,7 @@ export function prepareBackgroundLinkPreviews(
347350
() => complete({ status: "ready", tags: availableTags() }, true),
348351
timeoutMs,
349352
);
350-
void Promise.all(external.map(prepareLinkPreview)).then((tags) => {
353+
void Promise.all(preparations).then((tags) => {
351354
complete({
352355
status: "ready",
353356
tags: tags.filter((tag): tag is string[] => tag !== null),

0 commit comments

Comments
 (0)