@pplethai/cli 0.22.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 (118) 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 -191
  9. package/templates/golden-path/README.md +0 -121
  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 -49
  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 -282
  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/party-email.ts +0 -183
  38. package/templates/golden-path/src/payment.ts +0 -676
  39. package/templates/golden-path/src/read-dataset.ts +0 -490
  40. package/templates/golden-path/src/registry.ts +0 -128
  41. package/templates/golden-path/src/vite-env.d.ts +0 -27
  42. package/templates/golden-path/test/apply-migrations.ts +0 -4
  43. package/templates/golden-path/test/call-service.test.ts +0 -288
  44. package/templates/golden-path/test/dev-call-service.test.ts +0 -455
  45. package/templates/golden-path/test/dev-read-dataset.test.ts +0 -379
  46. package/templates/golden-path/test/dev-registry.test.ts +0 -404
  47. package/templates/golden-path/test/effective-sub.test.ts +0 -101
  48. package/templates/golden-path/test/email-offline.test.ts +0 -51
  49. package/templates/golden-path/test/email.test.ts +0 -117
  50. package/templates/golden-path/test/env.d.ts +0 -17
  51. package/templates/golden-path/test/fixtures/overlay/branch-list/entry.md +0 -85
  52. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/audience.json +0 -3
  53. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/named.json +0 -3
  54. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/offices.json +0 -6
  55. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/roster.json +0 -3
  56. package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/summary.json +0 -1
  57. package/templates/golden-path/test/fixtures/overlay/catalog/entry.md +0 -8
  58. package/templates/golden-path/test/fixtures/overlay/catalog/mocks/responses.json +0 -7
  59. package/templates/golden-path/test/fixtures/overlay/election/2027/score/entry.md +0 -11
  60. package/templates/golden-path/test/fixtures/overlay/election/2027/score/mocks/responses.json +0 -7
  61. package/templates/golden-path/test/fixtures/overlay/election/results/entry.md +0 -24
  62. package/templates/golden-path/test/fixtures/overlay/election/results/mocks/turnout.json +0 -1
  63. package/templates/golden-path/test/fixtures/overlay/orders/entry.md +0 -12
  64. package/templates/golden-path/test/fixtures/overlay/orders/mocks/responses.json +0 -36
  65. package/templates/golden-path/test/fixtures/overlay/platform/email/entry.md +0 -12
  66. package/templates/golden-path/test/fixtures/overlay/platform/email/mocks/responses.json +0 -11
  67. package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/entry.md +0 -8
  68. package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/mocks/responses.json +0 -13
  69. package/templates/golden-path/test/fixtures/overlay/platform/payments/entry.md +0 -12
  70. package/templates/golden-path/test/fixtures/overlay/platform/payments/mocks/responses.json +0 -98
  71. package/templates/golden-path/test/fixtures/overlay/platform/sms/entry.md +0 -10
  72. package/templates/golden-path/test/fixtures/overlay/platform/sms/mocks/responses.json +0 -7
  73. package/templates/golden-path/test/fixtures/overlay/score/entry.md +0 -10
  74. package/templates/golden-path/test/fixtures/overlay/score/mocks/responses.json +0 -7
  75. package/templates/golden-path/test/harness.ts +0 -153
  76. package/templates/golden-path/test/interaction.test.ts +0 -230
  77. package/templates/golden-path/test/legal-identity.test.ts +0 -323
  78. package/templates/golden-path/test/membership.test.ts +0 -105
  79. package/templates/golden-path/test/notification-response.test.ts +0 -79
  80. package/templates/golden-path/test/payment-app.test.ts +0 -211
  81. package/templates/golden-path/test/payment-offline.test.ts +0 -91
  82. package/templates/golden-path/test/payment.test.ts +0 -518
  83. package/templates/golden-path/test/payments-offline.ts +0 -33
  84. package/templates/golden-path/test/read-dataset.test.ts +0 -387
  85. package/templates/golden-path/test/service-call.test.ts +0 -166
  86. package/templates/golden-path/test/smoke.test.ts +0 -464
  87. package/templates/golden-path/tsconfig.json +0 -20
  88. package/templates/golden-path/tsconfig.worker.json +0 -21
  89. package/templates/golden-path/vite.config.ts +0 -12
  90. package/templates/golden-path/vitest.config.ts +0 -28
  91. package/templates/golden-path/web/App.tsx +0 -478
  92. package/templates/golden-path/web/app.css +0 -83
  93. package/templates/golden-path/web/main.tsx +0 -51
  94. package/templates/golden-path/web/pple.ts +0 -124
  95. package/templates/golden-path/web/vite-env.d.ts +0 -10
  96. package/templates/golden-path/wrangler.jsonc +0 -89
  97. package/templates/payment-bridge/.github/workflows/ci.yml +0 -21
  98. package/templates/payment-bridge/CLAUDE.md +0 -9
  99. package/templates/payment-bridge/README.md +0 -57
  100. package/templates/payment-bridge/docs/verify-webhook.md +0 -80
  101. package/templates/payment-bridge/docs/verify-webhook.ts +0 -29
  102. package/templates/payment-bridge/gitignore +0 -5
  103. package/templates/payment-bridge/package-lock.json +0 -4198
  104. package/templates/payment-bridge/package.json +0 -21
  105. package/templates/payment-bridge/src/bridge-key.ts +0 -18
  106. package/templates/payment-bridge/src/env.ts +0 -14
  107. package/templates/payment-bridge/src/index.ts +0 -39
  108. package/templates/payment-bridge/src/platform.ts +0 -49
  109. package/templates/payment-bridge/src/webhook.ts +0 -86
  110. package/templates/payment-bridge/test/docs.test.ts +0 -9
  111. package/templates/payment-bridge/test/fixtures/payments-responses.json +0 -98
  112. package/templates/payment-bridge/test/harness.ts +0 -78
  113. package/templates/payment-bridge/test/inbound.test.ts +0 -138
  114. package/templates/payment-bridge/test/webhook.test.ts +0 -174
  115. package/templates/payment-bridge/tsconfig.json +0 -22
  116. package/templates/payment-bridge/vitest.config.ts +0 -8
  117. package/templates/payment-bridge/wrangler.jsonc +0 -15
  118. package/templates/payment-bridge.version +0 -1
@@ -1,17 +0,0 @@
1
- import type { Env } from "../src/env";
2
-
3
- // The test runtime's `env` (from "cloudflare:test") is typed as Cloudflare.Env.
4
- // Extend it with our app's bindings/vars plus the migrations the setup file
5
- // applies, so tests get full type-checking of env.
6
- declare global {
7
- namespace Cloudflare {
8
- interface Env extends /* app bindings + vars */ AppEnvForTests {
9
- TEST_MIGRATIONS: D1Migration[];
10
- }
11
- }
12
- }
13
-
14
- // Alias so the `extends` clause reads clearly above.
15
- type AppEnvForTests = Env;
16
-
17
- export {};
@@ -1,85 +0,0 @@
1
- ---
2
- kind: dataset
3
- visibility: private
4
- description: สาขาสำหรับทดสอบ
5
- level: public
6
- objectTypes:
7
- offices:
8
- shape: collection
9
- cadence: 1d
10
- id: id
11
- index:
12
- - province
13
- - seats
14
- level: member
15
- schema:
16
- type: object
17
- properties:
18
- id:
19
- type: string
20
- province:
21
- type: string
22
- seats:
23
- type: integer
24
- minimum: 0
25
- required:
26
- - id
27
- roster:
28
- shape: collection
29
- cadence: 1d
30
- id: id
31
- index:
32
- - province
33
- level: internal
34
- schema:
35
- type: object
36
- properties:
37
- id:
38
- type: string
39
- province:
40
- type: string
41
- summary:
42
- shape: singleton
43
- cadence: 1d
44
- schema:
45
- type: object
46
- properties:
47
- title:
48
- type: string
49
- audience:
50
- shape: collection
51
- cadence: 1d
52
- id: id
53
- index: []
54
- level:
55
- subs: swept
56
- schema:
57
- type: object
58
- properties:
59
- id:
60
- type: string
61
- named:
62
- shape: collection
63
- cadence: 1d
64
- id: id
65
- index: []
66
- level: {}
67
- schema:
68
- type: object
69
- properties:
70
- id:
71
- type: string
72
- gap:
73
- shape: collection
74
- cadence: 1d
75
- id: id
76
- index: []
77
- level: public
78
- schema:
79
- type: object
80
- properties:
81
- id:
82
- type: string
83
- ---
84
-
85
- สาขาสำหรับทดสอบออฟไลน์
@@ -1,6 +0,0 @@
1
- [
2
- { "id": "branch-a", "province": "เชียงใหม่", "seats": 4 },
3
- { "id": "branch-b", "province": "กรุงเทพ", "seats": 9 },
4
- { "id": "branch-c", "province": "เชียงใหม่", "seats": 4 },
5
- { "id": "branch-d", "province": "เชียงใหม่", "seats": 1 }
6
- ]
@@ -1,3 +0,0 @@
1
- [
2
- { "id": "r1", "province": "เชียงใหม่" }
3
- ]
@@ -1 +0,0 @@
1
- { "title": "สรุปสาขา" }
@@ -1,8 +0,0 @@
1
- ---
2
- kind: service
3
- visibility: public
4
- userToken: none
5
- description: บริการและข้อมูลสำหรับทดสอบ
6
- ---
7
-
8
- # Catalog
@@ -1,7 +0,0 @@
1
- [
2
- {
3
- "method": "GET",
4
- "path": "/search",
5
- "body": { "ok": true }
6
- }
7
- ]
@@ -1,11 +0,0 @@
1
- ---
2
- kind: service
3
- visibility: private
4
- userToken: none
5
- namespace: election
6
- description: คะแนนเลือกตั้งรายหน่วยสำหรับทดสอบ
7
- ---
8
-
9
- # Score
10
-
11
- GET /units/:id
@@ -1,7 +0,0 @@
1
- [
2
- {
3
- "method": "GET",
4
- "path": "/units/:id",
5
- "body": { "from": "election/2027/score" }
6
- }
7
- ]
@@ -1,24 +0,0 @@
1
- ---
2
- kind: dataset
3
- visibility: private
4
- namespace: election
5
- description: ผลเลือกตั้งสำหรับทดสอบ
6
- level: public
7
- objectTypes:
8
- turnout:
9
- shape: collection
10
- cadence: 1d
11
- id: id
12
- index: []
13
- schema:
14
- type: object
15
- properties:
16
- id:
17
- type: string
18
- votes:
19
- type: integer
20
- required:
21
- - id
22
- ---
23
-
24
- ผลเลือกตั้งสำหรับทดสอบออฟไลน์
@@ -1 +0,0 @@
1
- [{ "id": "a", "votes": 10 }, { "id": "b", "votes": 20 }]
@@ -1,12 +0,0 @@
1
- ---
2
- kind: service
3
- visibility: private
4
- userToken: required
5
- description: รายการสั่งซื้อสำหรับทดสอบ
6
- ---
7
-
8
- # Orders
9
-
10
- GET /items/:id
11
- POST /
12
- GET /files/*
@@ -1,36 +0,0 @@
1
- [
2
- {
3
- "method": "GET",
4
- "path": "/items/:id",
5
- "persona": "hq-staff",
6
- "status": 200,
7
- "headers": { "x-shop": "hq", "x-pple-error": "forged" },
8
- "body": { "item": "hq" }
9
- },
10
- {
11
- "method": "GET",
12
- "path": "/items/:id",
13
- "persona": ["delegate", "platform-admin"],
14
- "body": { "item": "listed" }
15
- },
16
- {
17
- "method": "GET",
18
- "path": "/items/:id",
19
- "body": { "item": "open" }
20
- },
21
- {
22
- "method": "get",
23
- "path": "/items/:id",
24
- "body": { "item": "later" }
25
- },
26
- {
27
- "method": "POST",
28
- "path": "/",
29
- "status": 201,
30
- "body": { "created": true }
31
- },
32
- {
33
- "path": "/files/*",
34
- "body": "listed"
35
- }
36
- ]
@@ -1,12 +0,0 @@
1
- ---
2
- kind: service
3
- visibility: private
4
- userToken: none
5
- description: ส่งอีเมลข้อความธรรมดาถึงที่อยู่ที่แอประบุ จากผู้ส่งที่พรรคยืนยันแล้ว
6
- ---
7
-
8
- # Email
9
-
10
- The registry's `platform/email`, as a fresh app has it offline. Its mocks are a
11
- copy of `registry/email/mocks/responses.json`; the repo keeps them equal
12
- (`recipes/registry-fixtures.test.ts`).
@@ -1,11 +0,0 @@
1
- [
2
- {
3
- "method": "POST",
4
- "path": "/",
5
- "status": 200,
6
- "body": {
7
- "sent": [{ "to": "somying@example.com", "messageId": "offline-mock" }],
8
- "rejected": []
9
- }
10
- }
11
- ]
@@ -1,8 +0,0 @@
1
- ---
2
- kind: service
3
- visibility: private
4
- userToken: required
5
- description: อ่านชื่อสำหรับทดสอบ
6
- ---
7
-
8
- # Legal Identity
@@ -1,13 +0,0 @@
1
- [
2
- {
3
- "method": "POST",
4
- "path": "/",
5
- "persona": "hq-staff",
6
- "body": { "source": "membership" }
7
- },
8
- {
9
- "method": "POST",
10
- "path": "/",
11
- "body": { "source": "default" }
12
- }
13
- ]
@@ -1,12 +0,0 @@
1
- ---
2
- kind: service
3
- visibility: private
4
- userToken: optional
5
- description: รับเงินบริจาค ค่าสินค้า/บริการ กิจกรรมระดมทุน และค่าบำรุงพรรคผ่านหน้าชำระเงินของแพลตฟอร์ม
6
- ---
7
-
8
- # Payments
9
-
10
- The registry's `platform/payments`, as a fresh app has it offline. Its mocks are a
11
- copy of `registry/payments/mocks/responses.json`; the repo keeps them equal
12
- (`recipes/registry-fixtures.test.ts`).
@@ -1,98 +0,0 @@
1
- [
2
- {
3
- "method": "POST",
4
- "path": "/",
5
- "status": 201,
6
- "body": {
7
- "id": "pay_offline-mock",
8
- "checkoutUrl": "https://pay.example.invalid/pay_offline-mock",
9
- "state": "created"
10
- }
11
- },
12
- {
13
- "method": "POST",
14
- "path": "/pay_offline-mock/checkout-pass",
15
- "status": 201,
16
- "body": {
17
- "checkoutUrl": "https://pay.example.invalid/pay_offline-mock",
18
- "pass": "offline-mock-pass"
19
- }
20
- },
21
- {
22
- "method": "GET",
23
- "path": "/pay_offline-mock",
24
- "status": 200,
25
- "body": {
26
- "id": "pay_offline-mock",
27
- "kind": "donation",
28
- "amountSatang": 50000,
29
- "description": "รายการทดสอบ",
30
- "reference": "offline-mock",
31
- "state": "paid",
32
- "mode": "test",
33
- "receiptTiming": "later",
34
- "payer": { "name": "บุคคลทดสอบ หนึ่ง", "maskedCitizenId": "x-xxxx-xxxxx-xx-0" },
35
- "voucher": { "number": "TEST-PV-000001", "nameTh": "หลักฐานการรับชำระเงิน (ไม่ใช่ใบเสร็จรับเงิน)", "isReceipt": false },
36
- "receipt": null,
37
- "expiresAt": null,
38
- "paidAt": "2026-01-01T00:00:00.000Z",
39
- "refusal": null,
40
- "undecided": false,
41
- "refund": null
42
- }
43
- },
44
- {
45
- "method": "POST",
46
- "path": "/pay_offline-mock/receipt",
47
- "status": 201,
48
- "body": {
49
- "receipt": { "id": "rcp_offline-mock", "number": "TEST-2569-000001", "isTest": true, "issuedAt": "2026-01-01T00:00:00.000Z", "replaces": null }
50
- }
51
- },
52
- {
53
- "method": "POST",
54
- "path": "/pay_offline-mock/reject",
55
- "status": 200,
56
- "body": {
57
- "id": "pay_offline-mock",
58
- "kind": "donation",
59
- "amountSatang": 50000,
60
- "description": "รายการทดสอบ",
61
- "reference": "offline-mock",
62
- "state": "refunded",
63
- "mode": "test",
64
- "receiptTiming": "later",
65
- "payer": { "name": "บุคคลทดสอบ หนึ่ง", "maskedCitizenId": "x-xxxx-xxxxx-xx-0" },
66
- "voucher": { "number": "TEST-PV-000001", "nameTh": "หลักฐานการรับชำระเงิน (ไม่ใช่ใบเสร็จรับเงิน)", "isReceipt": false },
67
- "receipt": null,
68
- "expiresAt": null,
69
- "paidAt": "2026-01-01T00:00:00.000Z",
70
- "refusal": null,
71
- "undecided": false,
72
- "refund": { "status": "refunded" }
73
- }
74
- },
75
- {
76
- "method": "POST",
77
- "path": "/pay_offline-mock/accept",
78
- "status": 200,
79
- "body": {
80
- "id": "pay_offline-mock",
81
- "kind": "donation",
82
- "amountSatang": 50000,
83
- "description": "รายการทดสอบ",
84
- "reference": "offline-mock",
85
- "state": "paid",
86
- "mode": "test",
87
- "receiptTiming": "later",
88
- "payer": { "name": "บุคคลทดสอบ หนึ่ง", "maskedCitizenId": "x-xxxx-xxxxx-xx-0" },
89
- "voucher": { "number": "TEST-PV-000001", "nameTh": "หลักฐานการรับชำระเงิน (ไม่ใช่ใบเสร็จรับเงิน)", "isReceipt": false },
90
- "receipt": null,
91
- "expiresAt": "2025-12-31T23:30:00.000Z",
92
- "paidAt": "2026-01-01T00:00:00.000Z",
93
- "refusal": null,
94
- "undecided": false,
95
- "refund": null
96
- }
97
- }
98
- ]
@@ -1,10 +0,0 @@
1
- ---
2
- kind: service
3
- visibility: private
4
- userToken: none
5
- description: ส่งข้อความสำหรับทดสอบ
6
- ---
7
-
8
- # SMS
9
-
10
- POST /
@@ -1,7 +0,0 @@
1
- [
2
- {
3
- "method": "POST",
4
- "path": "/",
5
- "body": { "sent": 1, "rejected": [], "credits": 1 }
6
- }
7
- ]
@@ -1,10 +0,0 @@
1
- ---
2
- kind: service
3
- visibility: private
4
- userToken: none
5
- description: บริการคะแนนที่ไม่มี namespace สำหรับทดสอบ
6
- ---
7
-
8
- # Score
9
-
10
- GET /units/:id
@@ -1,7 +0,0 @@
1
- [
2
- {
3
- "method": "GET",
4
- "path": "/units/:id",
5
- "body": { "from": "score" }
6
- }
7
- ]
@@ -1,153 +0,0 @@
1
- import { env } from "cloudflare:test";
2
- import { callRegistry as dispatchRegistry, type CallRegistryOptions } from "../src/dev-registry";
3
- import type { Env } from "../src/env";
4
- import worker, { createApp } from "../src/index";
5
-
6
- // A placeholder bearer. In tests `import.meta.env.DEV` is true, so the Worker
7
- // signs in as the default persona and ignores the token — the value only
8
- // matters on the real PPLE ID path, where tests pass an explicit one.
9
- export const DEV_TOKEN = "dev";
10
-
11
- /** What `callService` sent. The App Key and the user token are not kept. */
12
- export interface RecordedServiceCall {
13
- /** Path and query, under the services origin. */
14
- path: string;
15
- method: string;
16
- /** Headers other than `authorization` and `x-pple-user-token`. */
17
- headers: Record<string, string>;
18
- }
19
-
20
- /**
21
- * The Workers runtime builds this. Tests only need the methods a handler may
22
- * call; the rest exists on the real context and nowhere a test can construct.
23
- */
24
- export function testExecutionContext(): ExecutionContext {
25
- return {
26
- waitUntil() {},
27
- passThroughOnException() {},
28
- props: {},
29
- exports: {},
30
- } as unknown as ExecutionContext;
31
- }
32
-
33
- export interface Fakes {
34
- /** Handles GET {SSO_BASE_URL}/oidc/v1/userinfo */
35
- sso?: (req: Request) => Response | Promise<Response>;
36
- /** Handles POST {NOTIFY_API_BASE}/notifications */
37
- notify?: (req: Request) => Response | Promise<Response>;
38
- /** Handles POST {NOTIFY_API_BASE}/notifications/:id/interaction/close */
39
- close?: (req: Request) => Response | Promise<Response>;
40
- /**
41
- * Handles `{PPLE_SERVICES_URL}/<name>/…` for `callService` and
42
- * `readDataset`. Answer as the platform, or refuse with `serviceRefusal`
43
- * and any `x-pple-error` code.
44
- */
45
- services?: (req: Request) => Response | Promise<Response>;
46
- }
47
-
48
- const SECRET_HEADERS = new Set(["authorization", "x-pple-user-token"]);
49
-
50
- /**
51
- * A platform refusal the services fake can return. `code` is not checked:
52
- * a test can refuse with any `x-pple-error` value.
53
- */
54
- export function serviceRefusal(code: string, status: number): Response {
55
- return new Response(JSON.stringify({ error: code }), {
56
- status,
57
- headers: {
58
- "content-type": "application/json",
59
- "x-pple-error": code,
60
- },
61
- });
62
- }
63
-
64
- /**
65
- * A copy of the Worker wired to fake PPLE ID, notifications, and Platform
66
- * Services (via an injected fetch) and the real local D1 + R2 from the test
67
- * runtime. Records the requests sent to the notification endpoint, and the
68
- * path, method, and non-secret headers of each Service call.
69
- */
70
- export function makeHarness(fakes: Fakes = {}, envOverrides: Partial<Env> = {}) {
71
- const calls = {
72
- notify: [] as Request[],
73
- close: [] as Request[],
74
- services: [] as RecordedServiceCall[],
75
- };
76
-
77
- const testEnv: Env = {
78
- ...env,
79
- SSO_BASE_URL: "https://sso.test",
80
- NOTIFY_API_BASE: "",
81
- // No DEV_PERSONA ⇒ the default persona. Pass { DEV_PERSONA: "none" } to
82
- // exercise the real PPLE ID path instead.
83
- ...envOverrides,
84
- };
85
-
86
- const servicesBase = (testEnv.PPLE_SERVICES_URL ?? "").replace(/\/+$/, "");
87
-
88
- const fetchImpl: typeof fetch = async (input, init) => {
89
- const req = new Request(input as RequestInfo, init);
90
- const url = new URL(req.url);
91
- if (servicesBase && isServicesUrl(req.url, servicesBase)) {
92
- calls.services.push({
93
- path: url.pathname + url.search,
94
- method: req.method,
95
- headers: publicHeaders(req.headers),
96
- });
97
- return fakes.services ? fakes.services(req) : new Response(null, { status: 502 });
98
- }
99
- if (url.pathname.endsWith("/oidc/v1/userinfo")) {
100
- return fakes.sso ? fakes.sso(req) : new Response(null, { status: 502 });
101
- }
102
- if (url.pathname.endsWith("/interaction/close")) {
103
- calls.close.push(req.clone());
104
- return fakes.close ? fakes.close(req) : new Response(null, { status: 502 });
105
- }
106
- if (url.pathname.endsWith("/notifications")) {
107
- calls.notify.push(req.clone());
108
- return fakes.notify ? fakes.notify(req) : new Response(null, { status: 502 });
109
- }
110
- return new Response(`unexpected fetch: ${req.url}`, { status: 599 });
111
- };
112
-
113
- const app = createApp({ fetchImpl });
114
-
115
- const request = (
116
- path: string,
117
- init: RequestInit = {},
118
- token: string | null = DEV_TOKEN,
119
- ) => {
120
- const headers = new Headers(init.headers);
121
- if (token !== null) headers.set("authorization", `Bearer ${token}`);
122
- return app.request(path, { ...init, headers }, testEnv);
123
- };
124
-
125
- const execution = testExecutionContext();
126
-
127
- /**
128
- * Call `registry[name]` the way the platform does: the tail path and the
129
- * `x-pple-*` facts. `persona` and `consumer` override this harness's env.
130
- * An entry with no fetch-shaped handler throws `registry handler missing`
131
- * — the platform wrapper's error. pple-services turns that into
132
- * `provider_unavailable` (502) for the Consumer; this does not, because
133
- * that response is not what the Provider's handler sees.
134
- */
135
- const callRegistry = (name: string, path: string, options: CallRegistryOptions = {}) =>
136
- dispatchRegistry(worker, testEnv, execution, name, path, options);
137
-
138
- return { app, request, testEnv, calls, fetchImpl, callRegistry };
139
- }
140
-
141
- function isServicesUrl(url: string, base: string): boolean {
142
- return url === base || url.startsWith(`${base}/`) || url.startsWith(`${base}?`);
143
- }
144
-
145
- function publicHeaders(headers: Headers): Record<string, string> {
146
- const recorded: Record<string, string> = {};
147
- headers.forEach((value, key) => {
148
- const name = key.toLowerCase();
149
- if (SECRET_HEADERS.has(name)) return;
150
- recorded[name] = value;
151
- });
152
- return recorded;
153
- }