fix(downloads): a GIF's size warning judges the size it shows
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
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;
|
const MB = 1024 * 1024;
|
||||||
|
|
||||||
@@ -34,9 +40,17 @@ describe("gifVerdict", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("warns past the server's cap, then past the link preview's", () => {
|
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_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");
|
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");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -54,7 +54,10 @@ export type GifVerdict = "too_long" | "may_fail" | "no_preview" | "ok";
|
|||||||
|
|
||||||
export function gifVerdict(seconds: number, est: { hi: number }): GifVerdict {
|
export function gifVerdict(seconds: number, est: { hi: number }): GifVerdict {
|
||||||
if (seconds > GIF_MAX_S + 0.0005) return "too_long";
|
if (seconds > GIF_MAX_S + 0.0005) return "too_long";
|
||||||
if (est.hi > GIF_MAX_BYTES) return "may_fail";
|
// Judged on the high end as shown (whole megabytes): "about 12–30 MB" beside "it may pass 30 MB"
|
||||||
if (est.hi > GIF_PREVIEW_MAX_BYTES) return "no_preview";
|
// 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";
|
return "ok";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user