examples/storefront Public
// One storefront per test: a fresh database, a fixed clock and predictable ids.import { createApp } from "../../src/app.ts";import type { Env } from "../../src/env.ts";import { testDb } from "./d1.ts";import { FakePayments } from "./payments.ts";import { testBucket } from "./r2.ts";export const ADMIN_TOKEN = "test-admin-token";export const NOW = Date.UTC(2026, 9, 1, 12, 0, 0);export function setup() { const env: Env = { DB: testDb(), IMAGES: testBucket(), ADMIN_TOKEN, STRIPE_SECRET_KEY: "sk_test_unused" }; const payments = new FakePayments(); let n = 0; const app = createApp(env, { now: () => NOW, newId: () => `id-${++n}`, payments: () => payments }); async function call(method: string, path: string, body?: unknown, headers: Record<string, string> = {}) { const init: RequestInit = { method, headers: { ...headers } }; if (body instanceof Uint8Array) { init.body = body; } else if (body !== undefined) { init.body = JSON.stringify(body); (init.headers as Record<string, string>)["content-type"] = "application/json"; } const res = await app.fetch(new Request(`https://shop.test${path}`, init)); const text = await res.text(); const isJson = (res.headers.get("content-type") ?? "").startsWith("application/json"); return { status: res.status, headers: res.headers, body: isJson && text ? JSON.parse(text) : null, text }; } const admin = { authorization: `Bearer ${ADMIN_TOKEN}` }; async function addProduct(id: string, fields: Record<string, unknown> = {}) { const res = await call("PUT", `/admin/products/${id}`, { name: id, price_cents: 1200, stock: 5, ...fields }, admin); if (res.status !== 200) throw new Error(`adding ${id}: ${JSON.stringify(res.body)}`); return res.body.product; } return { env, app, call, admin, addProduct, payments };}