Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
123 lines
5.3 KiB
TypeScript
123 lines
5.3 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { shrinkImage } from "./shrinkImage";
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
function fakeCanvas(blob: Blob | null) {
|
|
const canvas = {
|
|
width: 0,
|
|
height: 0,
|
|
getContext: () => ({ fillStyle: "", fillRect: vi.fn(), drawImage: vi.fn() }),
|
|
toBlob: (cb: (b: Blob | null) => void) => cb(blob),
|
|
};
|
|
const create = document.createElement.bind(document);
|
|
vi.spyOn(document, "createElement").mockImplementation(((tag: string) =>
|
|
tag === "canvas" ? canvas : create(tag)) as typeof document.createElement);
|
|
return canvas;
|
|
}
|
|
|
|
describe("shrinkImage", () => {
|
|
it("re-encodes a large photo as a JPEG that fits 2000px", async () => {
|
|
vi.stubGlobal("createImageBitmap", vi.fn(async () => ({ width: 4000, height: 3000, close: vi.fn() })));
|
|
const canvas = fakeCanvas(new Blob(["j"], { type: "image/jpeg" }));
|
|
const original = new File([new Uint8Array(8 * 1024 * 1024)], "IMG_1.png", { type: "image/png" });
|
|
|
|
const out = await shrinkImage(original);
|
|
|
|
expect(out.type).toBe("image/jpeg");
|
|
expect(out.name).toBe("IMG_1.jpg");
|
|
expect(out.size).toBe(1);
|
|
expect([canvas.width, canvas.height]).toEqual([2000, 1500]);
|
|
});
|
|
|
|
it("paints a white background first so transparent PNG pixels do not turn black", async () => {
|
|
vi.stubGlobal("createImageBitmap", vi.fn(async () => ({ width: 3000, height: 3000, close: vi.fn() })));
|
|
const calls: string[] = [];
|
|
const ctx = {
|
|
fillStyle: "",
|
|
fillRect: vi.fn(() => calls.push(`fill:${ctx.fillStyle}`)),
|
|
drawImage: vi.fn(() => calls.push("draw")),
|
|
};
|
|
const create = document.createElement.bind(document);
|
|
vi.spyOn(document, "createElement").mockImplementation(((tag: string) =>
|
|
tag === "canvas"
|
|
? { width: 0, height: 0, getContext: () => ctx, toBlob: (cb: (b: Blob) => void) => cb(new Blob(["j"])) }
|
|
: create(tag)) as typeof document.createElement);
|
|
|
|
await shrinkImage(new File([new Uint8Array(2 * 1024 * 1024)], "logo.png", { type: "image/png" }));
|
|
|
|
expect(calls).toEqual(["fill:#fff", "draw"]);
|
|
});
|
|
|
|
it("keeps the original when re-encoding would not make it smaller", async () => {
|
|
vi.stubGlobal("createImageBitmap", vi.fn(async () => ({ width: 100, height: 100, close: vi.fn() })));
|
|
fakeCanvas(new Blob([new Uint8Array(50)], { type: "image/jpeg" }));
|
|
const original = new File([new Uint8Array(10)], "tiny.png", { type: "image/png" });
|
|
|
|
expect(await shrinkImage(original)).toBe(original);
|
|
});
|
|
|
|
it("keeps a PNG already within 900 KB as-is, so screenshot text stays sharp", async () => {
|
|
const decode = vi.fn();
|
|
vi.stubGlobal("createImageBitmap", decode);
|
|
const original = new File([new Uint8Array(900 * 1024)], "screen.png", { type: "image/png" });
|
|
|
|
expect(await shrinkImage(original)).toBe(original);
|
|
expect(decode).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("steps quality, then size, down until the JPEG is within 900 KB", async () => {
|
|
vi.stubGlobal("createImageBitmap", vi.fn(async () => ({ width: 4000, height: 3000, close: vi.fn() })));
|
|
const tries: string[] = [];
|
|
const canvas = {
|
|
width: 0,
|
|
height: 0,
|
|
getContext: () => ({ fillStyle: "", fillRect: vi.fn(), drawImage: vi.fn() }),
|
|
toBlob: (cb: (b: Blob) => void, _type: string, q: number) => {
|
|
tries.push(`${canvas.width}@${q}`);
|
|
// only 1600px or smaller fits the budget in this fake
|
|
cb(new Blob([new Uint8Array(canvas.width > 1600 ? 2 * 1024 * 1024 : 800 * 1024)], { type: "image/jpeg" }));
|
|
},
|
|
};
|
|
const create = document.createElement.bind(document);
|
|
vi.spyOn(document, "createElement").mockImplementation(((tag: string) =>
|
|
tag === "canvas" ? canvas : create(tag)) as typeof document.createElement);
|
|
|
|
const out = await shrinkImage(new File([new Uint8Array(6 * 1024 * 1024)], "IMG_2.jpg", { type: "image/jpeg" }));
|
|
|
|
expect(tries).toEqual(["2000@0.85", "2000@0.7", "1600@0.7"]);
|
|
expect(out.size).toBe(800 * 1024);
|
|
expect([canvas.width, canvas.height]).toEqual([1600, 1200]);
|
|
});
|
|
|
|
it("uses the smallest attempt when no step reaches 900 KB", async () => {
|
|
vi.stubGlobal("createImageBitmap", vi.fn(async () => ({ width: 4000, height: 3000, close: vi.fn() })));
|
|
const canvas = {
|
|
width: 0,
|
|
height: 0,
|
|
getContext: () => ({ fillStyle: "", fillRect: vi.fn(), drawImage: vi.fn() }),
|
|
toBlob: (cb: (b: Blob) => void) => cb(new Blob([new Uint8Array(canvas.width * 1024)], { type: "image/jpeg" })),
|
|
};
|
|
const create = document.createElement.bind(document);
|
|
vi.spyOn(document, "createElement").mockImplementation(((tag: string) =>
|
|
tag === "canvas" ? canvas : create(tag)) as typeof document.createElement);
|
|
|
|
const out = await shrinkImage(new File([new Uint8Array(6 * 1024 * 1024)], "IMG_3.jpg", { type: "image/jpeg" }));
|
|
|
|
expect(out.size).toBe(1024 * 1024); // the 1024px attempt
|
|
});
|
|
|
|
it("returns the file unchanged where the browser cannot decode it", async () => {
|
|
const original = new File(["x"], "a.png", { type: "image/png" });
|
|
vi.stubGlobal("createImageBitmap", undefined);
|
|
expect(await shrinkImage(original)).toBe(original);
|
|
|
|
vi.stubGlobal("createImageBitmap", vi.fn(async () => { throw new Error("decode failed"); }));
|
|
expect(await shrinkImage(original)).toBe(original);
|
|
});
|
|
});
|