Rebasely Beta: data may be purged

examples/storefront Public

TypeScript · 35 lines · 1.4 KiB · at 7b21f05

// 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";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(), ADMIN_TOKEN };  let n = 0;  const app = createApp(env, { now: () => NOW, newId: () => `id-${++n}` });  async function call(method: string, path: string, body?: unknown, headers: Record<string, string> = {}) {    const init: RequestInit = { method, headers: { ...headers } };    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();    return { status: res.status, headers: res.headers, body: text ? JSON.parse(text) : null };  }  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 };}