feat: add limit size to download flow

This commit is contained in:
2026-10-08 17:42:21 +05:00
parent 7361a82c18
commit 8dcc263461
11 changed files with 247 additions and 15 deletions
+40 -1
View File
@@ -1,5 +1,9 @@
import { describe, expect, it } from "vitest";
import { COMPRESSION_LEVELS, findMaxColorsWithin } from "./compress";
import {
COMPRESSION_LEVELS,
findMaxColorsWithin,
findQualityWithin,
} from "./compress";
describe("COMPRESSION_LEVELS", () => {
it("presets ordered by descending colors", () => {
@@ -38,3 +42,38 @@ describe("findMaxColorsWithin", () => {
expect(calls).toBeLessThanOrEqual(12); // log2(254)+probe(2)
});
});
describe("findQualityWithin", () => {
const sizeOf = (q: number) => 500 + q * 50; // size grows with quality
it("picks max quality fitting the target", async () => {
// target 5000 -> q<=90 fits
const q = await findQualityWithin(5000, async (qq) => sizeOf(qq));
expect(q).toBe(90);
expect(sizeOf(q)).toBeLessThanOrEqual(5000);
});
it("everything fits - returns max quality", async () => {
const q = await findQualityWithin(999999, async (qq) => sizeOf(qq));
expect(q).toBe(100);
});
it("target unreachable even at minimum - returns min", async () => {
const q = await findQualityWithin(10, async (qq) => sizeOf(qq));
expect(q).toBe(1);
});
it("encodeSize null treated as failure", async () => {
const q = await findQualityWithin(999999, async () => null);
expect(q).toBe(1);
});
it("binary search converges faster than full scan", async () => {
let calls = 0;
await findQualityWithin(3000, async (qq) => {
calls++;
return sizeOf(qq);
});
expect(calls).toBeLessThanOrEqual(10); // log2(99)+2 probes
});
});