Files
PleinR/tests/image-info.test.ts
MichaelandClaude Opus 5 e59ae3a908 Espace adhérent : taille et format de l'image annoncés au dépôt
L'application publie le fichier tel quel sur Facebook et LinkedIn : aucun
recadrage, aucune marge. Un visuel qui n'est pas au format attendu est donc
recadré — ou entouré de bandes de couleur prises dans l'image — par la
plateforme, ce qui ne se voit nulle part côté site puisque les cartes
recadrent en `cover`.

Le formulaire de dépôt affiche maintenant les dimensions, le format et le
poids du fichier choisi, et dit ce que les réseaux en feront : format non
carré, image trop petite pour un fil d'actualité, fichier trop lourd pour
l'envoi. Purement informatif, rien n'est bloqué.

`src/lib/image-info.ts` est pur et verrouillé par `tests/image-info.test.ts`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MS61rkgTUGgPYH88tuVx9U
2026-09-14 05:52:13 +00:00

92 lines
3.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { strict as assert } from "node:assert";
import { describe, it } from "node:test";
import {
aspectLabel,
formatBytes,
imageNotes,
imageSummary,
isSquare,
orientationLabel,
IDEAL_SIDE,
} from "../src/lib/image-info";
describe("Poids d'un fichier", () => {
it("passe des octets aux kilo-octets puis aux méga-octets", () => {
assert.equal(formatBytes(640), "640 o");
assert.equal(formatBytes(420_000), "420 Ko");
assert.equal(formatBytes(1_200_000), "1,2 Mo");
});
it("ne rend rien pour une valeur absurde", () => {
assert.equal(formatBytes(-1), "");
assert.equal(formatBytes(Number.NaN), "");
});
});
describe("Format d'une image", () => {
it("reconnaît les formats courants", () => {
assert.equal(aspectLabel(1080, 1080), "1:1");
assert.equal(aspectLabel(1080, 1350), "4:5");
assert.equal(aspectLabel(1920, 1080), "16:9");
});
it("tolère un écart de 2 % autour d'un format connu", () => {
assert.equal(aspectLabel(1080, 1078), "1:1");
assert.ok(isSquare({ width: 1080, height: 1078, bytes: 0 }));
});
it("approche les formats inhabituels plutôt que de rendre une fraction illisible", () => {
assert.equal(aspectLabel(1000, 730), "1,37:1");
assert.equal(aspectLabel(730, 1000), "1:1,37");
});
it("nomme l'orientation", () => {
assert.equal(orientationLabel({ width: 1080, height: 1080, bytes: 0 }), "carré");
assert.equal(orientationLabel({ width: 1080, height: 1350, bytes: 0 }), "portrait");
assert.equal(orientationLabel({ width: 1350, height: 1080, bytes: 0 }), "paysage");
});
it("résume dimensions, format et poids", () => {
assert.equal(
imageSummary({ width: 1080, height: 1350, bytes: 420_000 }),
"1080 × 1350 px · portrait 4:5 · 420 Ko"
);
});
it("ne résume rien sans dimensions exploitables", () => {
assert.equal(imageSummary({ width: 0, height: 0, bytes: 1000 }), "");
assert.deepEqual(imageNotes({ width: 0, height: 0, bytes: 1000 }), []);
});
});
describe("Remarques sur l'image déposée", () => {
it("félicite une image carrée assez grande et assez légère", () => {
const notes = imageNotes({ width: IDEAL_SIDE, height: IDEAL_SIDE, bytes: 300_000 });
assert.equal(notes.length, 1);
assert.equal(notes[0].tone, "info");
});
it("prévient des bandes de couleur ajoutées par les réseaux hors du carré", () => {
const notes = imageNotes({ width: 1080, height: 1350, bytes: 300_000 });
assert.ok(notes.some((n) => n.tone === "warn" && /bandes de couleur/.test(n.text)));
assert.ok(notes.some((n) => /4:5/.test(n.text)));
});
it("signale une image trop petite", () => {
const notes = imageNotes({ width: 400, height: 400, bytes: 60_000 });
assert.ok(notes.some((n) => /floue/.test(n.text)));
});
it("signale un fichier trop lourd", () => {
const notes = imageNotes({ width: 1080, height: 1080, bytes: 3_000_000 });
assert.ok(notes.some((n) => /Fichier lourd/.test(n.text)));
});
it("cumule les remarques quand tout se ligue", () => {
const notes = imageNotes({ width: 600, height: 900, bytes: 3_000_000 });
assert.equal(notes.length, 3);
assert.ok(notes.every((n) => n.tone === "warn"));
});
});