Rebasely Beta: data may be purged

examples/storefront Public

TypeScript · 44 lines · 2.0 KiB · at 75e565d

// 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 type { OrderEvent } from "../../src/events.ts";import { testDb, testSearchDb } from "./d1.ts";import { FakePayments } from "./payments.ts";import { testQueue } from "./queue.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 events = testQueue<OrderEvent>();  const env: Env = { DB: testDb(), SEARCH: testSearchDb(), IMAGES: testBucket(), ORDER_EVENTS: events, IMAGES_URL: "https://images.shop.test", 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, events };}