examples/storefront Public
import assert from "node:assert/strict";import { test } from "node:test";import { setup } from "./helpers/app.ts";test("lists active products by name", async () => { const s = setup(); await s.addProduct("wool-socks", { name: "Wool socks", price_cents: 1800 }); await s.addProduct("canvas-tote", { name: "Canvas tote", price_cents: 2400 }); await s.addProduct("old-mug", { name: "Old mug", active: false }); const res = await s.call("GET", "/products"); assert.equal(res.status, 200); assert.deepEqual(res.body.products.map((p: { id: string }) => p.id), ["canvas-tote", "wool-socks"]);});test("an inactive product is not found by shoppers", async () => { const s = setup(); await s.addProduct("old-mug", { active: false }); assert.equal((await s.call("GET", "/products/old-mug")).status, 404);});test("updating a product keeps its id and changes its fields", async () => { const s = setup(); await s.addProduct("wool-socks", { price_cents: 1800 }); await s.addProduct("wool-socks", { price_cents: 1500, stock: 40 }); const { body } = await s.call("GET", "/products/wool-socks"); assert.equal(body.product.price_cents, 1500); assert.equal(body.product.stock, 40);});test("merchant routes need the admin token", async () => { const s = setup(); const res = await s.call("PUT", "/admin/products/wool-socks", { name: "Wool socks", price_cents: 1800 }, { authorization: "Bearer wrong" }); assert.equal(res.status, 401); assert.equal(res.body.error, "unauthorized");});test("rejects a price that is not whole cents", async () => { const s = setup(); const res = await s.call("PUT", "/admin/products/wool-socks", { name: "Wool socks", price_cents: 18.5 }, s.admin); assert.equal(res.status, 400); assert.equal(res.body.error, "bad_price");});test("rejects an id that is not a slug", async () => { const s = setup(); const res = await s.call("PUT", "/admin/products/Wool%20Socks", { name: "Wool socks", price_cents: 1800 }, s.admin); assert.equal(res.body.error, "bad_id");});test("unknown routes and methods", async () => { const s = setup(); assert.equal((await s.call("GET", "/nope")).status, 404); assert.equal((await s.call("DELETE", "/products")).status, 405); assert.equal((await s.call("GET", "/products/%E0%A4%A")).status, 400);});