fix(downloads): a GIF's size warning judges the size it shows

This commit is contained in:
2026-11-11 02:19:56 +01:00
parent 27ccf253f5
commit 0b92b40db5
2 changed files with 22 additions and 5 deletions
+17 -3
View File
@@ -1,5 +1,11 @@
import { describe, expect, it } from "vitest";
import { estimateGif, GIF_MAX_BYTES, GIF_PREVIEW_MAX_BYTES, gifVerdict } from "./gif";
import {
estimateGif,
gifEstimateText,
GIF_MAX_BYTES,
GIF_PREVIEW_MAX_BYTES,
gifVerdict,
} from "./gif";
const MB = 1024 * 1024;
@@ -34,9 +40,17 @@ describe("gifVerdict", () => {
});
it("warns past the server's cap, then past the link preview's", () => {
expect(gifVerdict(10, { hi: GIF_MAX_BYTES + 1 })).toBe("may_fail");
expect(gifVerdict(10, { hi: GIF_MAX_BYTES + 0.6 * MB })).toBe("may_fail");
expect(gifVerdict(10, { hi: GIF_MAX_BYTES })).toBe("no_preview");
expect(gifVerdict(10, { hi: GIF_PREVIEW_MAX_BYTES + 1 })).toBe("no_preview");
expect(gifVerdict(10, { hi: GIF_PREVIEW_MAX_BYTES + 0.6 * MB })).toBe("no_preview");
expect(gifVerdict(10, { hi: GIF_PREVIEW_MAX_BYTES })).toBe("ok");
});
it("judges the size the reader is shown: a high end shown as 30 MB is not past 30 MB", () => {
// 30 s at 480 lands a hair over 30 MiB, and reads "about 12–30 MB".
const hi = GIF_MAX_BYTES + 0.4 * MB;
expect(gifEstimateText({ lo: 12 * MB, hi }, "en").hi).toBe("30");
expect(gifVerdict(30, { hi })).toBe("no_preview");
expect(gifVerdict(10, { hi: GIF_PREVIEW_MAX_BYTES + 0.4 * MB })).toBe("ok");
});
});
+5 -2
View File
@@ -54,7 +54,10 @@ export type GifVerdict = "too_long" | "may_fail" | "no_preview" | "ok";
export function gifVerdict(seconds: number, est: { hi: number }): GifVerdict {
if (seconds > GIF_MAX_S + 0.0005) return "too_long";
if (est.hi > GIF_MAX_BYTES) return "may_fail";
if (est.hi > GIF_PREVIEW_MAX_BYTES) return "no_preview";
// Judged on the high end as shown (whole megabytes): "about 12–30 MB" beside "it may pass 30 MB"
// would contradict itself, and the estimate is no finer than that anyway.
const hi = Math.round(est.hi / MB) * MB;
if (hi > GIF_MAX_BYTES) return "may_fail";
if (hi > GIF_PREVIEW_MAX_BYTES) return "no_preview";
return "ok";
}