settlekit/apps/api/scripts/pg-smoke.mjs
caelum0x 286b1d4adc feat(api): Postgres persistence + real integration/secrets wiring
Persistence (selected by DATABASE_URL; in-memory fallback otherwise):
- document-projection pattern (codec): canonical entity in metadata.__doc,
  typed columns projected for querying — lossless round-trip despite schema drift.
- async EntityStore<T> (in-memory + Postgres) for products/prices/customers/
  delivery-runs/webhooks/github+discord records.
- Pg adapters for packaged stores: checkout/payment/subscription repositories,
  entitlements, license keys, api keys, saas plans, bundles, agent services.
- seedDefaults() upserts the default org+merchant to satisfy merchant_id FKs.
- context.ts selects Pg vs in-memory; routes awaited end-to-end.
- drizzle migration 0000_init.sql (48 tables) + db:generate/migrate scripts.

Integrations (config/env.ts + config/integrations.ts + wiring/delivery-clients.ts):
- env-driven secrets for Arc/Circle/GitHub/Discord/Resend/S3, partial-group
  validation via ConfigError.
- real delivery clients + Arc x402 verifier when configured; in-memory fallback.

Verified: full purchase->entitlement flow run against a live Postgres 16
(rows persisted + documents round-trip); 590 in-memory tests still pass; 0 TS errors.

x402: pinned-nonce support already added earlier enables stateless verify.
2026-06-15 11:50:50 +03:00

77 lines
3.1 KiB
JavaScript

/**
* Live Postgres smoke test (not a unit test): drives the real API end-to-end
* against a running Postgres via the built dist, then leaves the rows behind for
* external verification with psql. Run with DATABASE_URL + API_BOOTSTRAP_KEY set.
*/
import { createApp } from "../dist/app.js";
import { createContext } from "../dist/context.js";
const KEY = process.env.API_BOOTSTRAP_KEY ?? "smoke-key";
async function main() {
const ctx = await createContext();
const app = createApp(ctx);
async function call(method, path, body) {
const res = await app.request(path, {
method,
headers: { authorization: `Bearer ${KEY}`, "content-type": "application/json" },
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
});
const json = await res.json();
return { status: res.status, json };
}
const log = (label, r) => {
console.log(`${label}: ${r.status} ${r.json.error ? JSON.stringify(r.json.error) : "ok"}`);
if (r.status >= 400) throw new Error(`${label} failed: ${JSON.stringify(r.json)}`);
};
console.log(`persistent=${ctx.persistent}`);
const product = await call("POST", "/v1/products", {
merchantId: "mch_1", organizationId: "org_1", name: "Pro Repo Access",
description: "Private repo", type: "github_repo_access", deliveryMode: "github_invite",
});
log("product", product);
const productId = product.json.data.id;
const price = await call("POST", `/v1/products/${productId}/prices`, { amount: "25.00", interval: "one_time" });
log("price", price);
const priceId = price.json.data.id;
const customer = await call("POST", "/v1/customers", {
organizationId: "org_1", email: "buyer@example.com", githubUsername: "octocat",
});
log("customer", customer);
const customerId = customer.json.data.id;
const checkout = await call("POST", "/v1/checkout-sessions", {
organizationId: "org_1", merchantId: "mch_1", customerId,
items: [{ priceId, productId, quantity: 1 }], payToAddress: "0xMerchantWallet", network: "base",
});
log("checkout", checkout);
const sessionId = checkout.json.data.id;
console.log(` checkout amount=${checkout.json.data.amount.amount}`);
const payment = await call("POST", "/v1/payments", { checkoutSessionId: sessionId });
log("payment", payment);
const paymentId = payment.json.data.id;
const confirmed = await call("POST", `/v1/payments/${paymentId}/confirm`, { txHash: "0xabc123", confirmations: 3 });
log("confirm", confirmed);
console.log(` payment status=${confirmed.json.data.payment.status} entitlements=${confirmed.json.data.entitlements.length}`);
const verify = await call("POST", "/v1/entitlements/verify", { customerId, productId });
log("verify", verify);
console.log(` allowed=${verify.json.data.allowed}`);
const session = await call("GET", `/v1/checkout-sessions/${sessionId}`);
console.log(` session status=${session.json.data.status}`);
console.log("\nIDS", JSON.stringify({ productId, priceId, customerId, sessionId, paymentId }));
console.log("SMOKE_OK");
}
main().catch((e) => { console.error("SMOKE_FAIL", e); process.exit(1); });