@pplethai/cli 0.21.0 → 0.23.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.
Files changed (117) hide show
  1. package/README.md +12 -14
  2. package/dist/bin.js +102 -102
  3. package/package.json +3 -6
  4. package/templates/golden-path/.dev.vars.example +0 -33
  5. package/templates/golden-path/.env.example +0 -10
  6. package/templates/golden-path/.github/workflows/ci.yml +0 -22
  7. package/templates/golden-path/AGENTS.md +0 -1
  8. package/templates/golden-path/CLAUDE.md +0 -187
  9. package/templates/golden-path/README.md +0 -118
  10. package/templates/golden-path/gitignore +0 -17
  11. package/templates/golden-path/index.html +0 -16
  12. package/templates/golden-path/migrations/0001_init.sql +0 -10
  13. package/templates/golden-path/overlay-mtime.mjs +0 -30
  14. package/templates/golden-path/package-lock.json +0 -4318
  15. package/templates/golden-path/package.json +0 -41
  16. package/templates/golden-path/scripts/assert-dev-compiled-out.mjs +0 -45
  17. package/templates/golden-path/shared/api.ts +0 -325
  18. package/templates/golden-path/src/auth.ts +0 -230
  19. package/templates/golden-path/src/call-service.ts +0 -147
  20. package/templates/golden-path/src/dataset-query.ts +0 -470
  21. package/templates/golden-path/src/dataset-schema.ts +0 -398
  22. package/templates/golden-path/src/db.ts +0 -32
  23. package/templates/golden-path/src/dev-call-service.ts +0 -392
  24. package/templates/golden-path/src/dev-personas.ts +0 -186
  25. package/templates/golden-path/src/dev-read-dataset.ts +0 -444
  26. package/templates/golden-path/src/dev-registry.ts +0 -294
  27. package/templates/golden-path/src/email.ts +0 -224
  28. package/templates/golden-path/src/entry-name.ts +0 -114
  29. package/templates/golden-path/src/env.ts +0 -38
  30. package/templates/golden-path/src/files.ts +0 -38
  31. package/templates/golden-path/src/identity-header.ts +0 -17
  32. package/templates/golden-path/src/index.ts +0 -276
  33. package/templates/golden-path/src/legal-identity.ts +0 -180
  34. package/templates/golden-path/src/levels.ts +0 -132
  35. package/templates/golden-path/src/notification-response.ts +0 -55
  36. package/templates/golden-path/src/notify.ts +0 -311
  37. package/templates/golden-path/src/payment.ts +0 -554
  38. package/templates/golden-path/src/read-dataset.ts +0 -490
  39. package/templates/golden-path/src/registry.ts +0 -128
  40. package/templates/golden-path/src/vite-env.d.ts +0 -27
  41. package/templates/golden-path/test/apply-migrations.ts +0 -4
  42. package/templates/golden-path/test/call-service.test.ts +0 -288
  43. package/templates/golden-path/test/dev-call-service.test.ts +0 -455
  44. package/templates/golden-path/test/dev-read-dataset.test.ts +0 -379
  45. package/templates/golden-path/test/dev-registry.test.ts +0 -404
  46. package/templates/golden-path/test/effective-sub.test.ts +0 -101
  47. package/templates/golden-path/test/email-offline.test.ts +0 -51
  48. package/templates/golden-path/test/email.test.ts +0 -117
  49. package/templates/golden-path/test/env.d.ts +0 -17
  50. package/templates/golden-path/test/fixtures/overlay/branch-list/entry.md +0 -85
  51. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/audience.json +0 -3
  52. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/named.json +0 -3
  53. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/offices.json +0 -6
  54. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/roster.json +0 -3
  55. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/summary.json +0 -1
  56. package/templates/golden-path/test/fixtures/overlay/catalog/entry.md +0 -8
  57. package/templates/golden-path/test/fixtures/overlay/catalog/mocks/responses.json +0 -7
  58. package/templates/golden-path/test/fixtures/overlay/election/2027/score/entry.md +0 -11
  59. package/templates/golden-path/test/fixtures/overlay/election/2027/score/mocks/responses.json +0 -7
  60. package/templates/golden-path/test/fixtures/overlay/election/results/entry.md +0 -24
  61. package/templates/golden-path/test/fixtures/overlay/election/results/mocks/turnout.json +0 -1
  62. package/templates/golden-path/test/fixtures/overlay/orders/entry.md +0 -12
  63. package/templates/golden-path/test/fixtures/overlay/orders/mocks/responses.json +0 -36
  64. package/templates/golden-path/test/fixtures/overlay/platform/email/entry.md +0 -12
  65. package/templates/golden-path/test/fixtures/overlay/platform/email/mocks/responses.json +0 -11
  66. package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/entry.md +0 -8
  67. package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/mocks/responses.json +0 -13
  68. package/templates/golden-path/test/fixtures/overlay/platform/payments/entry.md +0 -12
  69. package/templates/golden-path/test/fixtures/overlay/platform/payments/mocks/responses.json +0 -71
  70. package/templates/golden-path/test/fixtures/overlay/platform/sms/entry.md +0 -10
  71. package/templates/golden-path/test/fixtures/overlay/platform/sms/mocks/responses.json +0 -7
  72. package/templates/golden-path/test/fixtures/overlay/score/entry.md +0 -10
  73. package/templates/golden-path/test/fixtures/overlay/score/mocks/responses.json +0 -7
  74. package/templates/golden-path/test/harness.ts +0 -153
  75. package/templates/golden-path/test/interaction.test.ts +0 -230
  76. package/templates/golden-path/test/legal-identity.test.ts +0 -323
  77. package/templates/golden-path/test/membership.test.ts +0 -105
  78. package/templates/golden-path/test/notification-response.test.ts +0 -79
  79. package/templates/golden-path/test/payment-app.test.ts +0 -210
  80. package/templates/golden-path/test/payment-offline.test.ts +0 -91
  81. package/templates/golden-path/test/payment.test.ts +0 -427
  82. package/templates/golden-path/test/payments-offline.ts +0 -33
  83. package/templates/golden-path/test/read-dataset.test.ts +0 -387
  84. package/templates/golden-path/test/service-call.test.ts +0 -166
  85. package/templates/golden-path/test/smoke.test.ts +0 -464
  86. package/templates/golden-path/tsconfig.json +0 -20
  87. package/templates/golden-path/tsconfig.worker.json +0 -21
  88. package/templates/golden-path/vite.config.ts +0 -12
  89. package/templates/golden-path/vitest.config.ts +0 -28
  90. package/templates/golden-path/web/App.tsx +0 -478
  91. package/templates/golden-path/web/app.css +0 -83
  92. package/templates/golden-path/web/main.tsx +0 -51
  93. package/templates/golden-path/web/pple.ts +0 -124
  94. package/templates/golden-path/web/vite-env.d.ts +0 -10
  95. package/templates/golden-path/wrangler.jsonc +0 -89
  96. package/templates/payment-bridge/.github/workflows/ci.yml +0 -21
  97. package/templates/payment-bridge/CLAUDE.md +0 -9
  98. package/templates/payment-bridge/README.md +0 -57
  99. package/templates/payment-bridge/docs/verify-webhook.md +0 -80
  100. package/templates/payment-bridge/docs/verify-webhook.ts +0 -29
  101. package/templates/payment-bridge/gitignore +0 -5
  102. package/templates/payment-bridge/package-lock.json +0 -4198
  103. package/templates/payment-bridge/package.json +0 -21
  104. package/templates/payment-bridge/src/bridge-key.ts +0 -18
  105. package/templates/payment-bridge/src/env.ts +0 -14
  106. package/templates/payment-bridge/src/index.ts +0 -39
  107. package/templates/payment-bridge/src/platform.ts +0 -49
  108. package/templates/payment-bridge/src/webhook.ts +0 -86
  109. package/templates/payment-bridge/test/docs.test.ts +0 -9
  110. package/templates/payment-bridge/test/fixtures/payments-responses.json +0 -71
  111. package/templates/payment-bridge/test/harness.ts +0 -78
  112. package/templates/payment-bridge/test/inbound.test.ts +0 -138
  113. package/templates/payment-bridge/test/webhook.test.ts +0 -174
  114. package/templates/payment-bridge/tsconfig.json +0 -22
  115. package/templates/payment-bridge/vitest.config.ts +0 -8
  116. package/templates/payment-bridge/wrangler.jsonc +0 -15
  117. package/templates/payment-bridge.version +0 -1
@@ -1,427 +0,0 @@
1
- // Payment helpers (docs/adr/0037). Each helper wraps one call to the Private
2
- // Platform Service `platform/payments` and turns every answer the Service or
3
- // the gate can give into a named reason, so a Builder switches on `reason`
4
- // and never reads a status. The fake Service below answers as the real one
5
- // does (registry/payments/entry.md).
6
-
7
- import { describe, expect, it } from "vitest";
8
- import {
9
- createCheckoutPass,
10
- createPayment,
11
- getPayment,
12
- issueReceipt,
13
- paymentHandler,
14
- rejectPayment,
15
- verifyPaymentEvent,
16
- type CreatePaymentInput,
17
- type PaymentEvent,
18
- type PaymentPaidEvent,
19
- type PaymentRecord,
20
- } from "../src/payment";
21
- import { makeHarness, serviceRefusal, testExecutionContext } from "./harness";
22
-
23
- const INPUT: CreatePaymentInput = {
24
- kind: "donation",
25
- amountSatang: 50_000,
26
- description: "บริจาคสนับสนุนพรรค",
27
- reference: "order-1",
28
- returnUrl: "https://my-app.ppleth.ai/thanks",
29
- };
30
-
31
- const SERVICES = { PPLE_SERVICES_URL: "https://services.test", PPLE_APP_KEY: "app-key", DEV_PERSONA: "none" };
32
-
33
- describe("createPayment", () => {
34
- it("asks platform/payments for a Payment and answers the Checkout address to send the Payer to", async () => {
35
- let body: unknown;
36
- const h = makeHarness(
37
- {
38
- services: async (req) => {
39
- body = await req.json();
40
- return Response.json(
41
- { id: "pay_1", checkoutUrl: "https://pay.example.test/pay_1", state: "created" },
42
- { status: 201 },
43
- );
44
- },
45
- },
46
- SERVICES,
47
- );
48
-
49
- const result = await createPayment(h.testEnv, INPUT, h.fetchImpl);
50
-
51
- expect(result).toEqual({
52
- ok: true,
53
- id: "pay_1",
54
- checkoutUrl: "https://pay.example.test/pay_1",
55
- state: "created",
56
- });
57
- expect(h.calls.services).toEqual([
58
- expect.objectContaining({ path: "/platform/payments", method: "POST" }),
59
- ]);
60
- expect(body).toEqual(INPUT);
61
- });
62
-
63
- it("sends the optional `test` flag through, so a Live app can ask for a test Payment", async () => {
64
- let body: unknown;
65
- const h = makeHarness(
66
- {
67
- services: async (req) => {
68
- body = await req.json();
69
- return Response.json({ id: "pay_2", checkoutUrl: "https://pay.example.test/pay_2", state: "created" }, { status: 201 });
70
- },
71
- },
72
- SERVICES,
73
- );
74
- const result = await createPayment(h.testEnv, { ...INPUT, test: true }, h.fetchImpl);
75
- expect(result).toMatchObject({ ok: true, id: "pay_2" });
76
- expect(body).toEqual({ ...INPUT, test: true });
77
- });
78
- });
79
-
80
- const PAID: PaymentRecord = {
81
- id: "pay_1",
82
- kind: "donation",
83
- amountSatang: 50_000,
84
- description: "บริจาคสนับสนุนพรรค",
85
- reference: "order-1",
86
- state: "paid",
87
- mode: "test",
88
- payer: { name: "นางสาวสมหญิง ใจดี", maskedCitizenId: "1-1037-xxxxx-56-1" },
89
- voucher: { number: "TEST-PV-000001", nameTh: "หลักฐานการรับชำระเงิน", isReceipt: false },
90
- receipt: null,
91
- paidAt: "2026-11-15T09:00:00.000Z",
92
- refusal: null,
93
- undecided: false,
94
- refund: null,
95
- };
96
-
97
- const RECEIPT = { id: "rcp_1", number: "TEST-2569-000001", isTest: true, issuedAt: "2026-11-15T09:01:00.000Z", replaces: null };
98
-
99
- describe("createCheckoutPass", () => {
100
- it("asks for a pass with the Payer's token, and answers the address to send them to", async () => {
101
- let seen: { token: string | null; auth: string | null; body: string } | undefined;
102
- const h = makeHarness(
103
- {
104
- services: async (req) => {
105
- seen = {
106
- token: req.headers.get("x-pple-user-token"),
107
- auth: req.headers.get("authorization"),
108
- body: await req.text(),
109
- };
110
- return Response.json({ checkoutUrl: "https://pay.example.test/pay_1", pass: "abc" }, { status: 201 });
111
- },
112
- },
113
- SERVICES,
114
- );
115
-
116
- const result = await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl);
117
-
118
- expect(result).toEqual({ ok: true, checkoutUrl: "https://pay.example.test/pay_1", pass: "abc" });
119
- expect(h.calls.services).toEqual([
120
- expect.objectContaining({ path: "/platform/payments/pay_1/checkout-pass", method: "POST" }),
121
- ]);
122
- // The user token travels as the Payer's own; the App Key still names the app.
123
- expect(seen).toEqual({ token: "payer-token", auth: "Bearer app-key", body: "" });
124
- });
125
-
126
- it("sends no token with any other call", async () => {
127
- let token: string | null = "unset";
128
- const h = makeHarness(
129
- {
130
- services: (req) => {
131
- token = req.headers.get("x-pple-user-token");
132
- return Response.json({}, { status: 404 });
133
- },
134
- },
135
- SERVICES,
136
- );
137
-
138
- await getPayment(h.testEnv, "pay_1", h.fetchImpl);
139
-
140
- expect(token).toBeNull();
141
- });
142
-
143
- const refusals: [string, Response, string][] = [
144
- ["a token the platform refuses", serviceRefusal("user_token_invalid", 401), "user_token_invalid"],
145
- ["the Service's own token refusal", Response.json({ error: "user_token_invalid" }, { status: 401 }), "user_token_invalid"],
146
- [
147
- "a Payment that belongs to another Payer",
148
- Response.json({ error: "payment_tied_to_another_payer" }, { status: 409 }),
149
- "not_your_payment",
150
- ],
151
- ["a Payment that is paid, refused or cancelled", Response.json({ error: "payment_ended" }, { status: 409 }), "not_payable"],
152
- ["a bare 409", new Response(null, { status: 409 }), "upstream_unavailable"],
153
- ["a Payment that is not there", Response.json({ error: "not_found" }, { status: 404 }), "not_found"],
154
- ["no Use Grant", serviceRefusal("not_granted", 403), "not_granted"],
155
- ["a Service that answers nonsense", new Response("oops", { status: 500 }), "upstream_unavailable"],
156
- ["a 201 with no address", Response.json({}, { status: 201 }), "upstream_unavailable"],
157
- ["a 201 with no pass", Response.json({ checkoutUrl: "https://pay.example.test/pay_1" }, { status: 201 }), "upstream_unavailable"],
158
- ];
159
-
160
- it.each(refusals)("names %s", async (_name, answer, reason) => {
161
- const h = makeHarness({ services: () => answer.clone() }, SERVICES);
162
-
163
- expect(await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl)).toEqual({ ok: false, reason });
164
- });
165
-
166
- it("does not call the platform without a token", async () => {
167
- const h = makeHarness({}, SERVICES);
168
-
169
- expect(await createCheckoutPass(h.testEnv, "pay_1", " ", h.fetchImpl)).toEqual({
170
- ok: false,
171
- reason: "user_token_invalid",
172
- });
173
- expect(h.calls.services).toEqual([]);
174
- });
175
-
176
- it("says not_configured, and calls nothing, when the platform bindings are missing", async () => {
177
- const h = makeHarness({}, { PPLE_SERVICES_URL: undefined, PPLE_APP_KEY: undefined, DEV_PERSONA: "none" });
178
-
179
- expect(await createCheckoutPass(h.testEnv, "pay_1", "payer-token", h.fetchImpl)).toEqual({
180
- ok: false,
181
- reason: "not_configured",
182
- });
183
- expect(h.calls.services).toEqual([]);
184
- });
185
- });
186
-
187
- describe("every helper names why the platform said no", () => {
188
- const cases: [string, Response, string, string?][] = [
189
- ["the gate refusing the App Key", serviceRefusal("invalid_app_key", 401), "invalid_app_key"],
190
- ["no Use Grant", serviceRefusal("not_granted", 403), "not_granted"],
191
- ["a Draft app that holds the grant", serviceRefusal("needs_beta", 403), "needs_beta"],
192
- ["the gate's other refusals", serviceRefusal("provider_unavailable", 502), "upstream_unavailable"],
193
- [
194
- "a bad field",
195
- Response.json({ error: "invalid_payment", field: "amountSatang" }, { status: 400 }),
196
- "invalid_payment",
197
- "amountSatang",
198
- ],
199
- ["no Checkout Page configured", Response.json({ error: "checkout_unavailable" }, { status: 503 }), "checkout_unavailable"],
200
- ["a Service that answers nonsense", new Response("oops", { status: 500 }), "upstream_unavailable"],
201
- ];
202
-
203
- it.each(cases)("%s", async (_name, answer, reason, field) => {
204
- const h = makeHarness({ services: () => answer.clone() }, SERVICES);
205
- const expected = { ok: false, reason, ...(field ? { field } : {}) };
206
-
207
- expect(await createPayment(h.testEnv, INPUT, h.fetchImpl)).toEqual(expected);
208
- expect(await getPayment(h.testEnv, "pay_1", h.fetchImpl)).toEqual(expected);
209
- expect(await issueReceipt(h.testEnv, "pay_1", h.fetchImpl)).toEqual(expected);
210
- expect(await rejectPayment(h.testEnv, "pay_1", h.fetchImpl)).toEqual(expected);
211
- });
212
-
213
- it("says not_configured, and calls nothing, when the platform bindings are missing", async () => {
214
- const h = makeHarness({}, { PPLE_SERVICES_URL: undefined, PPLE_APP_KEY: undefined, DEV_PERSONA: "none" });
215
-
216
- expect(await createPayment(h.testEnv, INPUT, h.fetchImpl)).toEqual({ ok: false, reason: "not_configured" });
217
- expect(h.calls.services).toEqual([]);
218
- });
219
-
220
- it("says upstream_unavailable when the call itself fails", async () => {
221
- const h = makeHarness({}, SERVICES);
222
- const down: typeof fetch = () => Promise.reject(new Error("offline"));
223
-
224
- expect(await getPayment(h.testEnv, "pay_1", down)).toEqual({ ok: false, reason: "upstream_unavailable" });
225
- });
226
- });
227
-
228
- describe("getPayment", () => {
229
- it("reads the Payment back, the one source of truth for its state", async () => {
230
- const h = makeHarness({ services: () => Response.json(PAID) }, SERVICES);
231
-
232
- expect(await getPayment(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: true, payment: PAID });
233
- expect(h.calls.services).toEqual([expect.objectContaining({ path: "/platform/payments/pay_1", method: "GET" })]);
234
- });
235
-
236
- it("says not_found for another app's Payment, or one that does not exist", async () => {
237
- const h = makeHarness({ services: () => Response.json({ error: "not_found" }, { status: 404 }) }, SERVICES);
238
-
239
- expect(await getPayment(h.testEnv, "pay_other", h.fetchImpl)).toEqual({ ok: false, reason: "not_found" });
240
- });
241
-
242
- it("never lets an id change the path it calls", async () => {
243
- const h = makeHarness({ services: () => Response.json({ error: "not_found" }, { status: 404 }) }, SERVICES);
244
-
245
- await getPayment(h.testEnv, "../x?y=1", h.fetchImpl);
246
-
247
- expect(h.calls.services[0]!.path).toBe("/platform/payments/..%2Fx%3Fy%3D1");
248
- });
249
- });
250
-
251
- describe("issueReceipt", () => {
252
- it("issues the Receipt, and says whether it was issued now or before", async () => {
253
- let answered = 0;
254
- const h = makeHarness(
255
- { services: () => Response.json({ receipt: RECEIPT }, { status: answered++ === 0 ? 201 : 200 }) },
256
- SERVICES,
257
- );
258
-
259
- expect(await issueReceipt(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: true, receipt: RECEIPT, issuedNow: true });
260
- expect(await issueReceipt(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: true, receipt: RECEIPT, issuedNow: false });
261
- expect(h.calls.services[0]).toMatchObject({ path: "/platform/payments/pay_1/receipt", method: "POST" });
262
- });
263
-
264
- it("says not_paid when there is nothing to issue a Receipt for", async () => {
265
- const h = makeHarness({ services: () => Response.json({ error: "not_paid" }, { status: 409 }) }, SERVICES);
266
-
267
- expect(await issueReceipt(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: false, reason: "not_paid" });
268
- });
269
- });
270
-
271
- describe("rejectPayment", () => {
272
- it("rejects a paid Payment and answers it, refunded or with a Refund Owed", async () => {
273
- const owed = { ...PAID, state: "rejected", refund: { status: "owed" } };
274
- const h = makeHarness({ services: () => Response.json(owed) }, SERVICES);
275
-
276
- expect(await rejectPayment(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: true, payment: owed });
277
- expect(h.calls.services[0]).toMatchObject({ path: "/platform/payments/pay_1/reject", method: "POST" });
278
- });
279
-
280
- it("says has_receipt once a Receipt exists: only a Finance Officer can refund it", async () => {
281
- const h = makeHarness({ services: () => Response.json({ error: "has_receipt" }, { status: 409 }) }, SERVICES);
282
-
283
- expect(await rejectPayment(h.testEnv, "pay_1", h.fetchImpl)).toEqual({ ok: false, reason: "has_receipt" });
284
- });
285
- });
286
-
287
- const PAID_EVENT: PaymentPaidEvent = {
288
- type: "payment.paid",
289
- id: "evt_1",
290
- payment: {
291
- id: "pay_1",
292
- kind: "donation",
293
- amountSatang: 50_000,
294
- description: "บริจาคสนับสนุนพรรค",
295
- reference: "order-1",
296
- mode: "test",
297
- },
298
- payer: PAID.payer,
299
- voucher: PAID.voucher,
300
- paidAt: PAID.paidAt,
301
- };
302
-
303
- describe("verifyPaymentEvent", () => {
304
- // The platform authenticates the delivery itself (the wrapper's own secret
305
- // never reaches your code), and your handler is handed the parsed event only.
306
- // To act on money, read the Payment back and compare: what the platform says
307
- // now is the truth, and it is what you get back.
308
- it("reads the Payment back and hands you the platform's version when the event matches it", async () => {
309
- const h = makeHarness({ services: () => Response.json(PAID) }, SERVICES);
310
-
311
- expect(await verifyPaymentEvent(h.testEnv, PAID_EVENT, h.fetchImpl)).toEqual({ ok: true, payment: PAID });
312
- expect(h.calls.services[0]).toMatchObject({ path: "/platform/payments/pay_1", method: "GET" });
313
- });
314
-
315
- it.each([
316
- ["another amount", { amountSatang: 1 }],
317
- ["another reference", { reference: "order-2" }],
318
- ["another kind", { kind: "membership_fee" }],
319
- ["another mode", { mode: "live" }],
320
- ])("refuses an event that claims %s than the Payment has", async (_name, claim) => {
321
- const h = makeHarness({ services: () => Response.json(PAID) }, SERVICES);
322
- const forged = { ...PAID_EVENT, payment: { ...PAID_EVENT.payment, ...claim } } as PaymentEvent;
323
-
324
- expect(await verifyPaymentEvent(h.testEnv, forged, h.fetchImpl)).toEqual({ ok: false, reason: "mismatch" });
325
- });
326
-
327
- it("refuses a paid event for a Payment that is not paid", async () => {
328
- const h = makeHarness({ services: () => Response.json({ ...PAID, state: "created", payer: null }) }, SERVICES);
329
-
330
- expect(await verifyPaymentEvent(h.testEnv, PAID_EVENT, h.fetchImpl)).toEqual({ ok: false, reason: "mismatch" });
331
- });
332
-
333
- it("accepts a late paid event for a Payment that has moved on since", async () => {
334
- const h = makeHarness({ services: () => Response.json({ ...PAID, state: "rejected" }) }, SERVICES);
335
-
336
- const result = await verifyPaymentEvent(h.testEnv, PAID_EVENT, h.fetchImpl);
337
-
338
- expect(result).toMatchObject({ ok: true, payment: { state: "rejected" } });
339
- });
340
-
341
- it.each([
342
- ["payment.refused", ["refused"], ["paid", "created"]],
343
- ["payment.rejected", ["rejected", "refunded"], ["paid"]],
344
- ["payment.refunded", ["refunded"], ["rejected", "paid"]],
345
- ["payment.receipt_issued", ["paid", "refunding", "refunded"], ["created", "refused"]],
346
- ["payment.receipt_voided", ["refunding", "refunded", "paid"], ["created", "refused"]],
347
- ])("checks the state %s implies", async (type, agrees, disagrees) => {
348
- const event = { ...PAID_EVENT, type } as unknown as PaymentEvent;
349
- for (const state of agrees) {
350
- const h = makeHarness({ services: () => Response.json({ ...PAID, state }) }, SERVICES);
351
- expect(await verifyPaymentEvent(h.testEnv, event, h.fetchImpl)).toMatchObject({ ok: true });
352
- }
353
- for (const state of disagrees) {
354
- const h = makeHarness({ services: () => Response.json({ ...PAID, state }) }, SERVICES);
355
- expect(await verifyPaymentEvent(h.testEnv, event, h.fetchImpl)).toEqual({ ok: false, reason: "mismatch" });
356
- }
357
- });
358
-
359
- it("says not_found for a Payment that is not this app's, and passes a refusal on", async () => {
360
- const missing = makeHarness({ services: () => Response.json({ error: "not_found" }, { status: 404 }) }, SERVICES);
361
- const down = makeHarness({ services: () => serviceRefusal("provider_unavailable", 502) }, SERVICES);
362
-
363
- expect(await verifyPaymentEvent(missing.testEnv, PAID_EVENT, missing.fetchImpl)).toEqual({ ok: false, reason: "not_found" });
364
- expect(await verifyPaymentEvent(down.testEnv, PAID_EVENT, down.fetchImpl)).toEqual({ ok: false, reason: "upstream_unavailable" });
365
- });
366
- });
367
-
368
- describe("paymentHandler", () => {
369
- const ctx = testExecutionContext();
370
- const env = makeHarness({}, SERVICES).testEnv;
371
-
372
- it("calls the function for the event's type, with the event typed for it", async () => {
373
- const seen: string[] = [];
374
- const handler = paymentHandler({
375
- "payment.paid": (event) => {
376
- seen.push(`${event.type}:${event.voucher?.number}`);
377
- },
378
- });
379
-
380
- expect(await handler(PAID_EVENT, env, ctx)).toBeUndefined();
381
- expect(seen).toEqual(["payment.paid:TEST-PV-000001"]);
382
- });
383
-
384
- it("hands over a Replacement Receipt's event with the Receipt it replaces", async () => {
385
- const seen: string[] = [];
386
- const handler = paymentHandler({
387
- "payment.receipt_issued": (event) => {
388
- seen.push(`${event.receipt.number} replaces ${event.receipt.replaces?.number ?? "nothing"}`);
389
- },
390
- });
391
- const replacement = {
392
- type: "payment.receipt_issued",
393
- id: "evt_receipt_issued_rcp_2",
394
- payment: PAID_EVENT.payment,
395
- payer: { name: "นางสาวสมหญิง ใจงาม", maskedCitizenId: "x-xxxx-xxxxx-78-9" },
396
- receipt: { ...RECEIPT, id: "rcp_2", number: "TEST-2569-000002", replaces: { id: "rcp_1", number: "TEST-2569-000001" } },
397
- } satisfies PaymentEvent;
398
-
399
- expect(await handler(replacement, env, ctx)).toBeUndefined();
400
- expect(await handler({ ...replacement, receipt: RECEIPT }, env, ctx)).toBeUndefined();
401
- expect(seen).toEqual(["TEST-2569-000002 replaces TEST-2569-000001", "TEST-2569-000001 replaces nothing"]);
402
- });
403
-
404
- it("answers 2xx to a type it does not handle, so a later outcome is not retried forever", async () => {
405
- const handler = paymentHandler({ "payment.paid": () => {} });
406
- const later = { ...PAID_EVENT, type: "payment.voided" } as unknown as PaymentEvent;
407
-
408
- expect(await handler({ ...PAID_EVENT, type: "payment.refunded", refundedAt: null } as PaymentEvent, env, ctx)).toBeUndefined();
409
- expect(await handler(later, env, ctx)).toBeUndefined();
410
- });
411
-
412
- it("keeps what the function returns: a non-2xx Response is 'not delivered' and is sent again", async () => {
413
- const handler = paymentHandler({ "payment.paid": () => new Response(null, { status: 503 }) });
414
-
415
- expect(((await handler(PAID_EVENT, env, ctx)) as Response).status).toBe(503);
416
- });
417
-
418
- it("lets a throw through, which the platform records as 'not delivered'", async () => {
419
- const handler = paymentHandler({
420
- "payment.paid": () => {
421
- throw new Error("db down");
422
- },
423
- });
424
-
425
- await expect(Promise.resolve().then(() => handler(PAID_EVENT, env, ctx))).rejects.toThrow("db down");
426
- });
427
- });
@@ -1,33 +0,0 @@
1
- // The `platform/payments` mocks, served the way `npm run dev` serves them
2
- // (ADR-0026): through the offline matcher, as a Persona. Tests use it as the
3
- // fake Platform Service, so a test runs the answers a Builder sees offline, not
4
- // a hand-written copy of them.
5
-
6
- import { answerFromOverlay } from "../src/dev-call-service";
7
-
8
- // The mocks as a fresh app has them (test/fixtures/overlay/platform/payments),
9
- // laid out as `pple registry` writes them into `.pple/registry/`. They are a
10
- // copy of the registry's own, which a Builder's app does not carry; the repo
11
- // keeps the copy equal to the original (recipes/registry-fixtures.test.ts).
12
- const files: Record<string, unknown> = Object.fromEntries(
13
- Object.entries(
14
- import.meta.glob("./fixtures/overlay/platform/payments/**/*", { eager: true, query: "?raw", import: "default" }),
15
- ).map(([file, text]) => [file.replace("./fixtures/", ""), text]),
16
- );
17
-
18
- /** Handles `{PPLE_SERVICES_URL}/platform/payments/…` as the mocks would for `persona`. */
19
- export function offlinePayments(persona: string): (req: Request) => Response {
20
- return (req) => {
21
- const prefix = "/platform/payments";
22
- const url = new URL(req.url);
23
- return answerFromOverlay(files, {
24
- entry: "platform/payments",
25
- path: url.pathname.slice(prefix.length),
26
- method: req.method,
27
- persona,
28
- // As `callService` offline does: the Payer's token, when one is sent.
29
- ...(req.headers.get("x-pple-user-token") ? { userToken: req.headers.get("x-pple-user-token")! } : {}),
30
- overlayRoot: "overlay/",
31
- });
32
- };
33
- }