ManiCandles/test/product-images.test.ts

25 lines
1.5 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { productImageUrl, productSchema } from "../src/lib/api.ts";
import { adminProduct, prepareProductImage } from "../src/lib/commerce.ts";
const id = "d66c798a-f764-4b88-8d13-ace328ff1010";
const product = { id, name: "Rose", slug: "rose", description: "", variants: [], groups: [] };
test("product images default to none and keep their storefront order", () => {
assert.deepEqual(productSchema.parse(product).images, []);
assert.deepEqual(productSchema.parse({ ...product, images: [{ id }] }).images, [{ id }]);
assert.throws(() => productSchema.parse({ ...product, images: [{ id: "../x" }] }));
assert.equal(productImageUrl(id, id, id), `/api/v1/storefront/${id}/products/${id}/images/${id}`);
const admin = adminProduct.parse({ ...product, status: "DRAFT", images: [{ id, mimeType: "image/webp", size: 10 }] });
assert.equal(admin.images[0].mimeType, "image/webp");
});
test("photo uploads accept only JPEG, PNG or WebP up to 2 MiB", () => {
const body = prepareProductImage(new File(["x"], "a.jpg", { type: "image/jpeg" }));
assert.ok(body.get("file") instanceof File);
assert.throws(() => prepareProductImage(new File(["<svg/>"], "a.svg", { type: "image/svg+xml" })), /JPEG, PNG or WebP/);
assert.throws(() => prepareProductImage(new File([new Uint8Array(2 * 1024 * 1024 + 1)], "a.png", { type: "image/png" })), /2 MiB/);
assert.throws(() => prepareProductImage(null), /Choose/);
});