@pplethai/cli 0.22.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -14
- package/dist/bin.js +109 -109
- package/package.json +3 -6
- package/templates/golden-path/.dev.vars.example +0 -33
- package/templates/golden-path/.env.example +0 -10
- package/templates/golden-path/.github/workflows/ci.yml +0 -22
- package/templates/golden-path/AGENTS.md +0 -1
- package/templates/golden-path/CLAUDE.md +0 -191
- package/templates/golden-path/README.md +0 -121
- package/templates/golden-path/gitignore +0 -17
- package/templates/golden-path/index.html +0 -16
- package/templates/golden-path/migrations/0001_init.sql +0 -10
- package/templates/golden-path/overlay-mtime.mjs +0 -30
- package/templates/golden-path/package-lock.json +0 -4318
- package/templates/golden-path/package.json +0 -41
- package/templates/golden-path/scripts/assert-dev-compiled-out.mjs +0 -45
- package/templates/golden-path/shared/api.ts +0 -325
- package/templates/golden-path/src/auth.ts +0 -230
- package/templates/golden-path/src/call-service.ts +0 -147
- package/templates/golden-path/src/dataset-query.ts +0 -470
- package/templates/golden-path/src/dataset-schema.ts +0 -398
- package/templates/golden-path/src/db.ts +0 -32
- package/templates/golden-path/src/dev-call-service.ts +0 -392
- package/templates/golden-path/src/dev-personas.ts +0 -186
- package/templates/golden-path/src/dev-read-dataset.ts +0 -444
- package/templates/golden-path/src/dev-registry.ts +0 -294
- package/templates/golden-path/src/email.ts +0 -49
- package/templates/golden-path/src/entry-name.ts +0 -114
- package/templates/golden-path/src/env.ts +0 -38
- package/templates/golden-path/src/files.ts +0 -38
- package/templates/golden-path/src/identity-header.ts +0 -17
- package/templates/golden-path/src/index.ts +0 -282
- package/templates/golden-path/src/legal-identity.ts +0 -180
- package/templates/golden-path/src/levels.ts +0 -132
- package/templates/golden-path/src/notification-response.ts +0 -55
- package/templates/golden-path/src/notify.ts +0 -311
- package/templates/golden-path/src/party-email.ts +0 -183
- package/templates/golden-path/src/payment.ts +0 -676
- package/templates/golden-path/src/read-dataset.ts +0 -490
- package/templates/golden-path/src/registry.ts +0 -128
- package/templates/golden-path/src/vite-env.d.ts +0 -27
- package/templates/golden-path/test/apply-migrations.ts +0 -4
- package/templates/golden-path/test/call-service.test.ts +0 -288
- package/templates/golden-path/test/dev-call-service.test.ts +0 -455
- package/templates/golden-path/test/dev-read-dataset.test.ts +0 -379
- package/templates/golden-path/test/dev-registry.test.ts +0 -404
- package/templates/golden-path/test/effective-sub.test.ts +0 -101
- package/templates/golden-path/test/email-offline.test.ts +0 -51
- package/templates/golden-path/test/email.test.ts +0 -117
- package/templates/golden-path/test/env.d.ts +0 -17
- package/templates/golden-path/test/fixtures/overlay/branch-list/entry.md +0 -85
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/audience.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/named.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/offices.json +0 -6
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/roster.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/summary.json +0 -1
- package/templates/golden-path/test/fixtures/overlay/catalog/entry.md +0 -8
- package/templates/golden-path/test/fixtures/overlay/catalog/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/entry.md +0 -11
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/election/results/entry.md +0 -24
- package/templates/golden-path/test/fixtures/overlay/election/results/mocks/turnout.json +0 -1
- package/templates/golden-path/test/fixtures/overlay/orders/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/orders/mocks/responses.json +0 -36
- package/templates/golden-path/test/fixtures/overlay/platform/email/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/platform/email/mocks/responses.json +0 -11
- package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/entry.md +0 -8
- package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/mocks/responses.json +0 -13
- package/templates/golden-path/test/fixtures/overlay/platform/payments/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/platform/payments/mocks/responses.json +0 -98
- package/templates/golden-path/test/fixtures/overlay/platform/sms/entry.md +0 -10
- package/templates/golden-path/test/fixtures/overlay/platform/sms/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/score/entry.md +0 -10
- package/templates/golden-path/test/fixtures/overlay/score/mocks/responses.json +0 -7
- package/templates/golden-path/test/harness.ts +0 -153
- package/templates/golden-path/test/interaction.test.ts +0 -230
- package/templates/golden-path/test/legal-identity.test.ts +0 -323
- package/templates/golden-path/test/membership.test.ts +0 -105
- package/templates/golden-path/test/notification-response.test.ts +0 -79
- package/templates/golden-path/test/payment-app.test.ts +0 -211
- package/templates/golden-path/test/payment-offline.test.ts +0 -91
- package/templates/golden-path/test/payment.test.ts +0 -518
- package/templates/golden-path/test/payments-offline.ts +0 -33
- package/templates/golden-path/test/read-dataset.test.ts +0 -387
- package/templates/golden-path/test/service-call.test.ts +0 -166
- package/templates/golden-path/test/smoke.test.ts +0 -464
- package/templates/golden-path/tsconfig.json +0 -20
- package/templates/golden-path/tsconfig.worker.json +0 -21
- package/templates/golden-path/vite.config.ts +0 -12
- package/templates/golden-path/vitest.config.ts +0 -28
- package/templates/golden-path/web/App.tsx +0 -478
- package/templates/golden-path/web/app.css +0 -83
- package/templates/golden-path/web/main.tsx +0 -51
- package/templates/golden-path/web/pple.ts +0 -124
- package/templates/golden-path/web/vite-env.d.ts +0 -10
- package/templates/golden-path/wrangler.jsonc +0 -89
- package/templates/payment-bridge/.github/workflows/ci.yml +0 -21
- package/templates/payment-bridge/CLAUDE.md +0 -9
- package/templates/payment-bridge/README.md +0 -57
- package/templates/payment-bridge/docs/verify-webhook.md +0 -80
- package/templates/payment-bridge/docs/verify-webhook.ts +0 -29
- package/templates/payment-bridge/gitignore +0 -5
- package/templates/payment-bridge/package-lock.json +0 -4198
- package/templates/payment-bridge/package.json +0 -21
- package/templates/payment-bridge/src/bridge-key.ts +0 -18
- package/templates/payment-bridge/src/env.ts +0 -14
- package/templates/payment-bridge/src/index.ts +0 -39
- package/templates/payment-bridge/src/platform.ts +0 -49
- package/templates/payment-bridge/src/webhook.ts +0 -86
- package/templates/payment-bridge/test/docs.test.ts +0 -9
- package/templates/payment-bridge/test/fixtures/payments-responses.json +0 -98
- package/templates/payment-bridge/test/harness.ts +0 -78
- package/templates/payment-bridge/test/inbound.test.ts +0 -138
- package/templates/payment-bridge/test/webhook.test.ts +0 -174
- package/templates/payment-bridge/tsconfig.json +0 -22
- package/templates/payment-bridge/vitest.config.ts +0 -8
- package/templates/payment-bridge/wrangler.jsonc +0 -15
- package/templates/payment-bridge.version +0 -1
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
// How to test a Notification Response handler (docs/adr/0035). The platform
|
|
2
|
-
// wrapper hands your handler the parsed event and your env, so a test calls
|
|
3
|
-
// `notificationResponse` on the default export directly with a sample event.
|
|
4
|
-
// There is no request to build and no secret to send. The wrapper's own work
|
|
5
|
-
// (the status mapping, 404 for no handler) is the platform's, not yours to test.
|
|
6
|
-
|
|
7
|
-
import { afterEach, describe, expect, it } from "vitest";
|
|
8
|
-
import worker from "../src/index";
|
|
9
|
-
import type { NotificationResponseEvent } from "../src/notification-response";
|
|
10
|
-
import { makeHarness, testExecutionContext } from "./harness";
|
|
11
|
-
|
|
12
|
-
const sample = (overrides: Partial<NotificationResponseEvent> = {}): NotificationResponseEvent => ({
|
|
13
|
-
type: "notification.response",
|
|
14
|
-
id: "clx1",
|
|
15
|
-
notificationId: "clw1",
|
|
16
|
-
ref: "leave:123",
|
|
17
|
-
app: { slug: "leave-request" },
|
|
18
|
-
responder: { sub: "sub-1" },
|
|
19
|
-
action: "reject",
|
|
20
|
-
values: { note: "ไม่ว่าง" },
|
|
21
|
-
respondedAt: "2026-11-10T07:03:00.000Z",
|
|
22
|
-
attempt: 1,
|
|
23
|
-
...overrides,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
afterEach(() => {
|
|
27
|
-
delete worker.notificationResponse;
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe("a Notification Response handler", () => {
|
|
31
|
-
it("is not defined until you add one, which the platform answers with 404 so answers keep coming", () => {
|
|
32
|
-
expect(worker.notificationResponse).toBeUndefined();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it("is called with the event, and counts an answer once however often it is delivered", async () => {
|
|
36
|
-
const decisions = new Map<string, string>();
|
|
37
|
-
worker.notificationResponse = async (event) => {
|
|
38
|
-
// At-least-once delivery: the same id may come again, so key on it.
|
|
39
|
-
if (decisions.has(event.id)) return;
|
|
40
|
-
decisions.set(event.id, `${event.ref}:${event.action}`);
|
|
41
|
-
};
|
|
42
|
-
const h = makeHarness();
|
|
43
|
-
const ctx = testExecutionContext();
|
|
44
|
-
|
|
45
|
-
// Returning nothing is a 204: delivered.
|
|
46
|
-
expect(await worker.notificationResponse(sample(), h.testEnv, ctx)).toBeUndefined();
|
|
47
|
-
await worker.notificationResponse(sample({ attempt: 2 }), h.testEnv, ctx);
|
|
48
|
-
|
|
49
|
-
expect([...decisions]).toEqual([["clx1", "leave:123:reject"]]);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it("can opt out of answers by returning a 410", async () => {
|
|
53
|
-
worker.notificationResponse = () => new Response(null, { status: 410 });
|
|
54
|
-
const h = makeHarness();
|
|
55
|
-
|
|
56
|
-
const result = await worker.notificationResponse(sample(), h.testEnv, testExecutionContext());
|
|
57
|
-
|
|
58
|
-
expect((result as Response).status).toBe(410);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it("can leave slow work to waitUntil, after the answer is acknowledged", async () => {
|
|
62
|
-
const background: Promise<unknown>[] = [];
|
|
63
|
-
const ctx = { ...testExecutionContext(), waitUntil: (p: Promise<unknown>) => void background.push(p) };
|
|
64
|
-
let notified = false;
|
|
65
|
-
worker.notificationResponse = (_event, _env, handlerCtx) => {
|
|
66
|
-
handlerCtx.waitUntil(
|
|
67
|
-
(async () => {
|
|
68
|
-
notified = true;
|
|
69
|
-
})(),
|
|
70
|
-
);
|
|
71
|
-
};
|
|
72
|
-
const h = makeHarness();
|
|
73
|
-
|
|
74
|
-
await worker.notificationResponse(sample(), h.testEnv, ctx as unknown as ExecutionContext);
|
|
75
|
-
await Promise.all(background);
|
|
76
|
-
|
|
77
|
-
expect(notified).toBe(true);
|
|
78
|
-
});
|
|
79
|
-
});
|
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
// A fresh template app takes a fake payment end to end, offline (ADR-0037,
|
|
2
|
-
// ADR-0026): the Payer asks the app for a Payment, is sent to the Checkout
|
|
3
|
-
// address, and the platform calls `payment()` when it is paid, which verifies
|
|
4
|
-
// the event and stops there: issuing a legal Receipt is the Builder's decision,
|
|
5
|
-
// never a default (see "the two patterns" below). The platform is the registry's own mocks,
|
|
6
|
-
// as the signed-in Persona. Offline there is no Checkout Page and no platform
|
|
7
|
-
// callback, so this stands in for both: it hands the handler the event the
|
|
8
|
-
// platform would send. A real checkout is a deployed Beta app's.
|
|
9
|
-
|
|
10
|
-
import { describe, expect, it } from "vitest";
|
|
11
|
-
import { createPaymentHandler } from "../src/index";
|
|
12
|
-
import { issueReceipt, paymentHandler, rejectPayment, verifyPaymentEvent, type PaymentPaidEvent, type PaymentReceiptIssuedEvent } from "../src/payment";
|
|
13
|
-
import { makeHarness, testExecutionContext } from "./harness";
|
|
14
|
-
import { offlinePayments } from "./payments-offline";
|
|
15
|
-
|
|
16
|
-
const SERVICES = { PPLE_SERVICES_URL: "https://services.test", PPLE_APP_KEY: "app-key" };
|
|
17
|
-
|
|
18
|
-
const post = (body: unknown) => ({
|
|
19
|
-
method: "POST",
|
|
20
|
-
headers: { "content-type": "application/json" },
|
|
21
|
-
body: JSON.stringify(body),
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
/** What the platform sends when `pay_offline-mock` is paid. */
|
|
25
|
-
const PAID_EVENT: PaymentPaidEvent = {
|
|
26
|
-
type: "payment.paid",
|
|
27
|
-
id: "evt_1",
|
|
28
|
-
payment: {
|
|
29
|
-
id: "pay_offline-mock",
|
|
30
|
-
kind: "donation",
|
|
31
|
-
amountSatang: 50_000,
|
|
32
|
-
description: "รายการทดสอบ",
|
|
33
|
-
reference: "offline-mock",
|
|
34
|
-
expiresAt: null,
|
|
35
|
-
mode: "test",
|
|
36
|
-
},
|
|
37
|
-
payer: { name: "บุคคลทดสอบ หนึ่ง", maskedCitizenId: "x-xxxx-xxxxx-xx-0" },
|
|
38
|
-
voucher: { number: "TEST-PV-000001", nameTh: "…", isReceipt: false },
|
|
39
|
-
paidAt: "2026-01-01T00:00:00.000Z",
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
describe("a fresh template app, offline", () => {
|
|
43
|
-
it.each(["hq-staff", "member", "kyc-verified-nonmember", "delegate"])(
|
|
44
|
-
"takes a donation as %s: create, pay, verified, and no Receipt issued until the app decides",
|
|
45
|
-
async (persona) => {
|
|
46
|
-
const h = makeHarness({ services: offlinePayments(persona) }, { ...SERVICES, DEV_PERSONA: persona });
|
|
47
|
-
|
|
48
|
-
// 1. The Payer asks for a Payment and gets the address to pay at.
|
|
49
|
-
const created = await h.request("/api/payments", post({ amountSatang: 50_000 }));
|
|
50
|
-
expect(created.status).toBe(201);
|
|
51
|
-
const { id, checkoutUrl } = (await created.json()) as { id: string; checkoutUrl: string };
|
|
52
|
-
expect(id).toBe("pay_offline-mock");
|
|
53
|
-
expect(checkoutUrl).toMatch(/^https:\/\//);
|
|
54
|
-
|
|
55
|
-
// 2. Paid: the platform calls `payment()`. The handler verifies and does not issue:
|
|
56
|
-
// a legal Receipt is the Builder's decision, so a fresh app never issues one by default (P4).
|
|
57
|
-
const handler = createPaymentHandler({ fetchImpl: h.fetchImpl });
|
|
58
|
-
expect(await handler(PAID_EVENT, h.testEnv, testExecutionContext())).toBeUndefined();
|
|
59
|
-
expect(h.calls.services.map((c) => `${c.method} ${c.path}`)).toEqual([
|
|
60
|
-
"POST /platform/payments",
|
|
61
|
-
"GET /platform/payments/pay_offline-mock",
|
|
62
|
-
]);
|
|
63
|
-
|
|
64
|
-
// 3. The app can read the Payment back for its page.
|
|
65
|
-
const read = await h.request(`/api/payments/${id}`);
|
|
66
|
-
expect(read.status).toBe(200);
|
|
67
|
-
expect(await read.json()).toMatchObject({ payment: { state: "paid", mode: "test" } });
|
|
68
|
-
},
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
it.each(["hq-staff", "member", "kyc-verified-nonmember", "delegate"])(
|
|
72
|
-
"sends %s to checkout with a Checkout Pass when they tap to pay (ADR-0043)",
|
|
73
|
-
async (persona) => {
|
|
74
|
-
const h = makeHarness({ services: offlinePayments(persona) }, { ...SERVICES, DEV_PERSONA: persona });
|
|
75
|
-
|
|
76
|
-
// The Payer taps to pay: the app mints a pass now, not at create time.
|
|
77
|
-
const res = await h.request("/api/payments/pay_offline-mock/checkout", { method: "POST" });
|
|
78
|
-
|
|
79
|
-
expect(res.status).toBe(201);
|
|
80
|
-
expect(await res.json()).toEqual({ checkoutUrl: "https://pay.example.invalid/pay_offline-mock", pass: "offline-mock-pass" });
|
|
81
|
-
expect(h.calls.services.map((c) => `${c.method} ${c.path}`)).toEqual([
|
|
82
|
-
"POST /platform/payments/pay_offline-mock/checkout-pass",
|
|
83
|
-
]);
|
|
84
|
-
},
|
|
85
|
-
);
|
|
86
|
-
|
|
87
|
-
it("sends the Payer's own token with the pass call, and no other call carries it", async () => {
|
|
88
|
-
const tokens: Record<string, string | null> = {};
|
|
89
|
-
const h = makeHarness(
|
|
90
|
-
{
|
|
91
|
-
services: (req) => {
|
|
92
|
-
tokens[new URL(req.url).pathname] = req.headers.get("x-pple-user-token");
|
|
93
|
-
return Response.json({ checkoutUrl: "https://pay.example.test/pay_1", pass: "p" }, { status: 201 });
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
{ ...SERVICES, DEV_PERSONA: "member" },
|
|
97
|
-
);
|
|
98
|
-
const res = await h.request("/api/payments/pay_1/checkout", { method: "POST" }, "payer-token");
|
|
99
|
-
|
|
100
|
-
expect(res.status).toBe(201);
|
|
101
|
-
expect(tokens).toEqual({ "/platform/payments/pay_1/checkout-pass": "payer-token" });
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it("tells the Payer when the Payment belongs to someone else (409)", async () => {
|
|
105
|
-
const h = makeHarness(
|
|
106
|
-
{ services: () => Response.json({ error: "payment_tied_to_another_payer" }, { status: 409 }) },
|
|
107
|
-
{ ...SERVICES, DEV_PERSONA: "member" },
|
|
108
|
-
);
|
|
109
|
-
|
|
110
|
-
const res = await h.request("/api/payments/pay_1/checkout", { method: "POST" });
|
|
111
|
-
|
|
112
|
-
expect(res.status).toBe(409);
|
|
113
|
-
expect(await res.json()).toEqual({ error: "not_your_payment" });
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
// The Checkout Page turns an unverified Payer away, so the app says so first.
|
|
117
|
-
// The mocks cannot: the Service never sees who is signed in.
|
|
118
|
-
it.each(["unverified", "kyc-pending"])("does not start a payment for %s: they must verify first", async (persona) => {
|
|
119
|
-
const h = makeHarness({ services: offlinePayments(persona) }, { ...SERVICES, DEV_PERSONA: persona });
|
|
120
|
-
|
|
121
|
-
const res = await h.request("/api/payments", post({ amountSatang: 50_000 }));
|
|
122
|
-
|
|
123
|
-
expect(res.status).toBe(403);
|
|
124
|
-
expect(await res.json()).toEqual({ error: "kyc_required" });
|
|
125
|
-
expect(h.calls.services).toEqual([]);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it("refuses an amount that is not a positive whole number of satang", async () => {
|
|
129
|
-
const h = makeHarness({ services: offlinePayments("hq-staff") }, SERVICES);
|
|
130
|
-
|
|
131
|
-
for (const amountSatang of [0, -5, 10.5, "100", null]) {
|
|
132
|
-
const res = await h.request("/api/payments", post({ amountSatang }));
|
|
133
|
-
expect(res.status).toBe(400);
|
|
134
|
-
}
|
|
135
|
-
expect(h.calls.services).toEqual([]);
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
describe("the two patterns, which a Builder writes into their own `payment` handler", () => {
|
|
139
|
-
// The same helpers, wired the way payments.md shows. The template's default
|
|
140
|
-
// handler is neither: it only verifies.
|
|
141
|
-
it("issue at payment: verify the event, then issue the Receipt", async () => {
|
|
142
|
-
const h = makeHarness({ services: offlinePayments("hq-staff") }, SERVICES);
|
|
143
|
-
const handler = paymentHandler({
|
|
144
|
-
"payment.paid": async (event, env) => {
|
|
145
|
-
const verified = await verifyPaymentEvent(env, event, h.fetchImpl);
|
|
146
|
-
if (!verified.ok) return verified.reason === "mismatch" ? undefined : new Response(null, { status: 503 });
|
|
147
|
-
const issued = await issueReceipt(env, event.payment.id, h.fetchImpl);
|
|
148
|
-
if (!issued.ok && issued.reason !== "not_paid") return new Response(null, { status: 503 });
|
|
149
|
-
},
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
expect(await handler(PAID_EVENT, h.testEnv, testExecutionContext())).toBeUndefined();
|
|
153
|
-
expect(h.calls.services.map((c) => `${c.method} ${c.path}`)).toEqual([
|
|
154
|
-
"GET /platform/payments/pay_offline-mock",
|
|
155
|
-
"POST /platform/payments/pay_offline-mock/receipt",
|
|
156
|
-
]);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it("verify first: the app's own check decides between issuing and rejecting", async () => {
|
|
160
|
-
const h = makeHarness({ services: offlinePayments("hq-staff") }, SERVICES);
|
|
161
|
-
const outcome = (stockOk: boolean) =>
|
|
162
|
-
paymentHandler({
|
|
163
|
-
"payment.paid": async (event, env) => {
|
|
164
|
-
const verified = await verifyPaymentEvent(env, event, h.fetchImpl);
|
|
165
|
-
if (!verified.ok) return verified.reason === "mismatch" ? undefined : new Response(null, { status: 503 });
|
|
166
|
-
if (stockOk) await issueReceipt(env, event.payment.id, h.fetchImpl);
|
|
167
|
-
else await rejectPayment(env, event.payment.id, h.fetchImpl);
|
|
168
|
-
},
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
await outcome(true)(PAID_EVENT, h.testEnv, testExecutionContext());
|
|
172
|
-
await outcome(false)(PAID_EVENT, h.testEnv, testExecutionContext());
|
|
173
|
-
expect(h.calls.services.map((c) => c.path.split("/").pop())).toEqual(["pay_offline-mock", "receipt", "pay_offline-mock", "reject"]);
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
it("does not issue a Receipt for an event the platform does not agree with", async () => {
|
|
178
|
-
const h = makeHarness({ services: offlinePayments("hq-staff") }, SERVICES);
|
|
179
|
-
const forged = { ...PAID_EVENT, payment: { ...PAID_EVENT.payment, amountSatang: 99_999_900 } };
|
|
180
|
-
|
|
181
|
-
const handler = createPaymentHandler({ fetchImpl: h.fetchImpl });
|
|
182
|
-
|
|
183
|
-
// Answered 2xx so it is not retried, and nothing was issued.
|
|
184
|
-
expect(await handler(forged, h.testEnv, testExecutionContext())).toBeUndefined();
|
|
185
|
-
expect(h.calls.services.map((c) => c.method)).toEqual(["GET"]);
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
it("is told to try again when the platform cannot be reached, so the Receipt is not lost", async () => {
|
|
189
|
-
const h = makeHarness({ services: () => new Response(null, { status: 502 }) }, SERVICES);
|
|
190
|
-
|
|
191
|
-
const handler = createPaymentHandler({ fetchImpl: h.fetchImpl });
|
|
192
|
-
|
|
193
|
-
const answer = await handler(PAID_EVENT, h.testEnv, testExecutionContext());
|
|
194
|
-
expect((answer as Response).status).toBe(503);
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
it("ignores the other outcomes, and the next time the handler hears of a Receipt it only acknowledges", async () => {
|
|
198
|
-
const h = makeHarness({ services: offlinePayments("hq-staff") }, SERVICES);
|
|
199
|
-
const handler = createPaymentHandler({ fetchImpl: h.fetchImpl });
|
|
200
|
-
const issued = {
|
|
201
|
-
type: "payment.receipt_issued",
|
|
202
|
-
id: "evt_receipt_issued_rcp_1",
|
|
203
|
-
payment: PAID_EVENT.payment,
|
|
204
|
-
payer: PAID_EVENT.payer!,
|
|
205
|
-
receipt: { id: "rcp_1", number: "TEST-2569-000001", isTest: true, issuedAt: "2026-01-01T00:00:00.000Z", replaces: null },
|
|
206
|
-
} satisfies PaymentReceiptIssuedEvent;
|
|
207
|
-
|
|
208
|
-
expect(await handler(issued, h.testEnv, testExecutionContext())).toBeUndefined();
|
|
209
|
-
expect(h.calls.services).toEqual([]);
|
|
210
|
-
});
|
|
211
|
-
});
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
// Offline payments (ADR-0026, docs/adr/0037). The registry's own mocks for
|
|
2
|
-
// `platform/payments` (registry/payments/mocks/responses.json) are the
|
|
3
|
-
// answers `npm run dev` gives, as the signed-in Persona. This drives the real
|
|
4
|
-
// files through the same matcher a Builder's `callService` uses offline, with
|
|
5
|
-
// the helpers on top, so what the Skill promises is what runs.
|
|
6
|
-
//
|
|
7
|
-
// There is no Checkout Page or Gateway offline: the Payment is simply paid.
|
|
8
|
-
|
|
9
|
-
import { describe, expect, it } from "vitest";
|
|
10
|
-
import { DEV_PERSONAS } from "../src/dev-personas";
|
|
11
|
-
import { createCheckoutPass, createPayment, getPayment, issueReceipt, rejectPayment } from "../src/payment";
|
|
12
|
-
import { offlinePayments } from "./payments-offline";
|
|
13
|
-
|
|
14
|
-
/** `callService` as `npm run dev` runs it for this Persona. */
|
|
15
|
-
const offlineAs =
|
|
16
|
-
(persona: string): typeof fetch =>
|
|
17
|
-
async (input, init) =>
|
|
18
|
-
offlinePayments(persona)(new Request(input as RequestInfo, init));
|
|
19
|
-
|
|
20
|
-
const env = { PPLE_SERVICES_URL: "https://services.test", PPLE_APP_KEY: "k", DEV_PERSONA: "none" };
|
|
21
|
-
const INPUT = {
|
|
22
|
-
kind: "donation" as const,
|
|
23
|
-
amountSatang: 50_000,
|
|
24
|
-
description: "บริจาค",
|
|
25
|
-
reference: "order-1",
|
|
26
|
-
returnUrl: "http://localhost:5173/thanks",
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// The entry is `userToken: none`, so the Service never sees who is signed in
|
|
30
|
-
// and the registry refuses a Persona on its mocks. Every Persona gets the same
|
|
31
|
-
// answers: the Payer got through checkout. Whether a Persona may start a
|
|
32
|
-
// payment at all is the app's own gate on `actor.kyc.verified`
|
|
33
|
-
// (test/payment-app.test.ts), which is where the platform's KYC rule is
|
|
34
|
-
// visible to a Builder offline.
|
|
35
|
-
const personas = Object.keys(DEV_PERSONAS);
|
|
36
|
-
|
|
37
|
-
describe("offline payments", () => {
|
|
38
|
-
it("covers Personas that are and are not KYC-verified", () => {
|
|
39
|
-
const kyc = personas.map((name) => DEV_PERSONAS[name]!.actor.kyc.verified);
|
|
40
|
-
expect(kyc).toContain(true);
|
|
41
|
-
expect(kyc).toContain(false);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it.each([...personas, "intern"])("%s: a Payment is created, paid, receipted and can be rejected", async (persona) => {
|
|
45
|
-
const fetchImpl = offlineAs(persona);
|
|
46
|
-
|
|
47
|
-
const created = await createPayment(env, INPUT, fetchImpl);
|
|
48
|
-
expect(created).toMatchObject({ ok: true, id: "pay_offline-mock", state: "created" });
|
|
49
|
-
|
|
50
|
-
const read = await getPayment(env, "pay_offline-mock", fetchImpl);
|
|
51
|
-
expect(read).toMatchObject({ ok: true, payment: { state: "paid", mode: "test", receipt: null } });
|
|
52
|
-
if (!read.ok) throw new Error("unreachable");
|
|
53
|
-
// The Voucher says it is not a receipt, and the Payer is masked.
|
|
54
|
-
expect(read.payment.voucher).toMatchObject({ isReceipt: false });
|
|
55
|
-
expect(read.payment.payer?.maskedCitizenId).toMatch(/x/);
|
|
56
|
-
|
|
57
|
-
expect(await issueReceipt(env, "pay_offline-mock", fetchImpl)).toMatchObject({
|
|
58
|
-
ok: true,
|
|
59
|
-
issuedNow: true,
|
|
60
|
-
receipt: { isTest: true, number: expect.stringMatching(/^TEST-/) },
|
|
61
|
-
});
|
|
62
|
-
expect(await rejectPayment(env, "pay_offline-mock", fetchImpl)).toMatchObject({
|
|
63
|
-
ok: true,
|
|
64
|
-
payment: { state: "refunded", refund: { status: "refunded" } },
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// A signed-in Payer's app mints a pass the Checkout Page would redeem
|
|
69
|
-
// (ADR-0043). Offline there is no Checkout Page: the mock answers the address.
|
|
70
|
-
it.each([...personas, "intern"])("%s: a Checkout Pass is minted and answers an address to go to", async (persona) => {
|
|
71
|
-
const fetchImpl = offlineAs(persona);
|
|
72
|
-
|
|
73
|
-
const pass = await createCheckoutPass(env, "pay_offline-mock", "payer-token", fetchImpl);
|
|
74
|
-
|
|
75
|
-
expect(pass).toEqual({ ok: true, checkoutUrl: expect.stringMatching(/^https:\/\/.+\/pay_offline-mock$/), pass: "offline-mock-pass" });
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it("mints a pass with no token while a Persona is signed in, as `npm run dev` does", async () => {
|
|
79
|
-
// `npm run dev` signs the Payer in as the Persona and sends no PPLE ID token,
|
|
80
|
-
// so a blank token is fine offline and refused against the real platform.
|
|
81
|
-
const fetchImpl = offlineAs("member");
|
|
82
|
-
|
|
83
|
-
expect(await createCheckoutPass({ ...env, DEV_PERSONA: "member" }, "pay_offline-mock", "", fetchImpl)).toMatchObject({
|
|
84
|
-
ok: true,
|
|
85
|
-
});
|
|
86
|
-
expect(await createCheckoutPass(env, "pay_offline-mock", "", fetchImpl)).toEqual({
|
|
87
|
-
ok: false,
|
|
88
|
-
reason: "user_token_invalid",
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
});
|