VNOX-Web/tests/crop.test.ts
loki5512344 76f8371a7e
feat(web): let users set their own photo on the account page
Pick a PNG/JPEG/WebP file, crop it to a square on a canvas (drag, wheel,
slider or keyboard), export a 256x256 PNG and send it to Hub. Adds remove
photo, error states, RU/EN copy and privacy policy text about the stored
photo and 30-day backups.

Claude-Session: https://claude.ai/code/session_01GDjxLvBcnJqniFQGUGb1V4
2026-10-24 21:48:05 +02:00

40 lines
1.2 KiB
TypeScript

import { test } from "bun:test";
import assert from "node:assert/strict";
import {
MAX_ZOOM,
clampCrop,
initialCrop,
panBy,
sourceRect,
zoomTo,
} from "../src/crop";
test("the first frame is centred and fills the square", () => {
const rect = sourceRect(initialCrop(400, 200));
assert.deepEqual(rect, { sx: 100, sy: 0, side: 200 });
});
test("panning never leaves empty edges and never stretches", () => {
let state = initialCrop(400, 200);
state = panBy(state, -10_000, 10_000, 280);
let rect = sourceRect(state);
assert.equal(rect.sx + rect.side, 400);
assert.equal(rect.sy, 0);
state = zoomTo(state, 2);
state = panBy(state, 10_000, -10_000, 280);
rect = sourceRect(state);
assert.equal(rect.side, 100);
assert.equal(rect.sx, 0);
assert.equal(rect.sy, 100);
});
test("zoom is clamped and keeps the area inside the image", () => {
const state = clampCrop({
...initialCrop(300, 300),
zoom: 99,
x: -5,
y: 900,
});
assert.equal(state.zoom, MAX_ZOOM);
const { sx, sy, side } = sourceRect(state);
assert.ok(sx >= 0 && sy >= 0 && sx + side <= 300 && sy + side <= 300);
assert.equal(zoomTo(initialCrop(300, 300), 0.2).zoom, 1);
});