UNPKG

acp-handler

Version:

Vercel handler for Agentic Commerce Protocol (ACP) - Build checkout APIs that AI agents like ChatGPT can use to complete purchases

140 lines (139 loc) 3.1 kB
//#region src/test/index.ts /** * In-memory KV store for testing */ function createMemoryStore() { const data = /* @__PURE__ */ new Map(); return { async get(key) { const entry = data.get(key); if (!entry) return null; if (entry.expires && Date.now() > entry.expires) { data.delete(key); return null; } return entry.value; }, async set(key, value, ttlSec) { data.set(key, { value, expires: ttlSec ? Date.now() + ttlSec * 1e3 : void 0 }); }, async setnx(key, value, ttlSec) { if (data.has(key)) return false; await this.set(key, value, ttlSec); return true; } }; } /** * Mock products handler that returns configurable pricing */ function createMockProducts(config) { const pricePerItem = config?.pricePerItem ?? 1e3; const ready = config?.ready ?? true; const messages = config?.messages ?? []; return { price: async (input) => { const items = input.items.map((item) => ({ id: item.id, title: `Product ${item.id}`, quantity: item.quantity, unit_price: { amount: pricePerItem, currency: "USD" } })); const subtotal = items.reduce((sum, item) => sum + item.unit_price.amount * item.quantity, 0); return { items, totals: { subtotal: { amount: subtotal, currency: "USD" }, grand_total: { amount: subtotal, currency: "USD" } }, fulfillment: { options: [{ id: "standard", label: "Standard Shipping", price: { amount: 0, currency: "USD" } }], selected_id: "standard" }, messages, ready }; } }; } /** * Mock payments handler with configurable behavior */ function createMockPayments(config) { const authorizeSucceed = config?.shouldAuthorizeSucceed ?? true; const captureSucceed = config?.shouldCaptureSucceed ?? true; const calls = { authorize: 0, capture: 0 }; return { authorize: async () => { calls.authorize++; if (!authorizeSucceed) return { ok: false, reason: config?.authorizeReason ?? "Authorization failed" }; return { ok: true, intent_id: `pi_test_${crypto.randomUUID()}` }; }, capture: async (_intentId) => { calls.capture++; if (!captureSucceed) return { ok: false, reason: config?.captureReason ?? "Capture failed" }; return { ok: true }; }, _calls: calls }; } /** * Mock webhooks handler that tracks calls */ function createMockWebhooks() { const calls = []; return { orderUpdated: async (evt) => { calls.push(evt); }, _calls: calls }; } /** * Helper to create a mock Request object */ function createRequest(url, options) { const headers = new Headers({ "content-type": "application/json", "request-id": `req_${crypto.randomUUID()}`, "idempotency-key": `idem_${crypto.randomUUID()}`, ...options?.headers }); return new Request(url, { method: options?.method ?? "GET", headers, body: options?.body ? JSON.stringify(options.body) : void 0 }); } //#endregion export { createMemoryStore, createMockPayments, createMockProducts, createMockWebhooks, createRequest }; //# sourceMappingURL=index.js.map