@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,230 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import type { Interaction } from "../shared/api";
3
- import { makeHarness } from "./harness";
4
-
5
- const NOTIFY_ENV = {
6
- NOTIFY_API_BASE: "https://today.test/external",
7
- NOTIFY_API_KEY: "app-key",
8
- };
9
-
10
- const interaction: Interaction = {
11
- ref: "leave:123",
12
- expiresAt: "2026-11-17T00:00:00Z",
13
- tray: false,
14
- fields: [{ id: "note", type: "text", label: "เหตุผล", multiline: true, maxLength: 300 }],
15
- actions: [
16
- { id: "approve", label: "อนุมัติ", icon: "check", style: "primary" },
17
- {
18
- id: "reject",
19
- label: "ไม่อนุมัติ",
20
- style: "destructive",
21
- requires: ["note"],
22
- confirm: { title: "ยืนยันการไม่อนุมัติ?" },
23
- },
24
- ],
25
- };
26
-
27
- const direct = { kind: "direct", recipients: [{ sub: "u-1" }] };
28
-
29
- const sendWith = (h: ReturnType<typeof makeHarness>, extra: Record<string, unknown> = {}) =>
30
- h.request("/api/notify", {
31
- method: "POST",
32
- headers: { "content-type": "application/json" },
33
- body: JSON.stringify({ audience: direct, title: "Leave", body: "Please answer", ...extra }),
34
- });
35
-
36
- const refusal = (status: number, code: string, message = "refused") => ({
37
- notify: () => Response.json({ error: { code, message } }, { status }),
38
- });
39
-
40
- describe("interactive notifications: sending", () => {
41
- it("passes the interaction to today-v2 exactly as written", async () => {
42
- const h = makeHarness(
43
- {
44
- notify: () =>
45
- Response.json(
46
- { notificationId: "n_1", results: [{ status: "delivered" }] },
47
- { status: 201 },
48
- ),
49
- },
50
- NOTIFY_ENV,
51
- );
52
- const res = await sendWith(h, { interaction });
53
- expect(res.status).toBe(200);
54
- const payload = await h.calls.notify[0]!.json<Record<string, unknown>>();
55
- expect(payload.interaction).toEqual(interaction);
56
- });
57
-
58
- it("does not add an interaction key to a send without one", async () => {
59
- const h = makeHarness(
60
- { notify: () => Response.json({ notificationId: "n_1", results: [] }) },
61
- NOTIFY_ENV,
62
- );
63
- await sendWith(h);
64
- const payload = await h.calls.notify[0]!.json<Record<string, unknown>>();
65
- expect("interaction" in payload).toBe(false);
66
- });
67
-
68
- it("returns the notificationId for a Direct and a Broadcast send", async () => {
69
- const h = makeHarness(
70
- {
71
- notify: () =>
72
- Response.json({ notificationId: "n_7", results: [{ status: "delivered" }] }),
73
- },
74
- NOTIFY_ENV,
75
- );
76
- expect(await (await sendWith(h)).json()).toMatchObject({ ok: true, notificationId: "n_7" });
77
-
78
- const b = makeHarness(
79
- { notify: () => Response.json({ notificationId: "n_8" }, { status: 201 }) },
80
- NOTIFY_ENV,
81
- );
82
- expect(await (await sendWith(b, { audience: { kind: "all" } })).json()).toEqual({
83
- ok: true,
84
- notificationId: "n_8",
85
- });
86
- });
87
-
88
- it("answers without a notificationId when today-v2 gave none", async () => {
89
- const h = makeHarness({ notify: () => new Response(null, { status: 202 }) }, NOTIFY_ENV);
90
- expect(await (await sendWith(h, { audience: { kind: "all" } })).json()).toEqual({ ok: true });
91
- });
92
-
93
- it("names the failing path when today-v2 refuses the interaction", async () => {
94
- const h = makeHarness(
95
- refusal(
96
- 400,
97
- "NOTIFICATION_INVALID_INTERACTION",
98
- "interaction.actions[1].requires[0]: unknown field",
99
- ),
100
- NOTIFY_ENV,
101
- );
102
- const res = await sendWith(h, { interaction });
103
- expect(res.status).toBe(400);
104
- expect(await res.json()).toEqual({
105
- error: "invalid_interaction",
106
- message: "interaction.actions[1].requires[0]: unknown field",
107
- });
108
- });
109
-
110
- describe("asking for a Live Activity", () => {
111
- const urgent: Interaction = {
112
- ref: "door:7",
113
- expiresAt: "2026-11-10T16:00:00Z",
114
- liveActivity: true,
115
- actions: [
116
- { id: "open", label: "เปิดประตู", icon: "check", style: "primary" },
117
- { id: "deny", label: "ไม่เปิด", icon: "x", style: "destructive" },
118
- ],
119
- };
120
-
121
- it("passes liveActivity to today-v2 unchanged", async () => {
122
- const h = makeHarness(
123
- {
124
- notify: () =>
125
- Response.json(
126
- { notificationId: "n_2", results: [{ status: "delivered" }] },
127
- { status: 201 },
128
- ),
129
- },
130
- NOTIFY_ENV,
131
- );
132
- const res = await sendWith(h, { interaction: urgent });
133
- expect(res.status).toBe(200);
134
- const payload = await h.calls.notify[0]!.json<{ interaction: Record<string, unknown> }>();
135
- expect(payload.interaction).toEqual(urgent);
136
- });
137
-
138
- it("surfaces today-v2's refusal of a Live Activity as invalid_interaction", async () => {
139
- const h = makeHarness(
140
- refusal(400, "NOTIFICATION_INVALID_INTERACTION", "interaction.expiresAt: at most 8 h ahead"),
141
- NOTIFY_ENV,
142
- );
143
- const res = await sendWith(h, {
144
- interaction: { ...urgent, expiresAt: "2026-11-17T00:00:00Z" },
145
- });
146
- expect(res.status).toBe(400);
147
- expect(await res.json()).toEqual({
148
- error: "invalid_interaction",
149
- message: "interaction.expiresAt: at most 8 h ahead",
150
- });
151
- });
152
- });
153
-
154
- it.each(["NOTIFICATION_INTERACTION_NO_CALLBACK", "INTERACTIVE_NOTIFICATIONS_DISABLED"])(
155
- "reports %s as interaction_unavailable",
156
- async (code) => {
157
- const h = makeHarness(refusal(400, code), NOTIFY_ENV);
158
- const res = await sendWith(h, { interaction });
159
- expect(res.status).toBe(400);
160
- expect(await res.json()).toMatchObject({ error: "interaction_unavailable" });
161
- },
162
- );
163
-
164
- it("still reads a plain 400 as invalid_recipients", async () => {
165
- const h = makeHarness(refusal(400, "NOTIFICATION_INVALID_RECIPIENTS"), NOTIFY_ENV);
166
- expect(await (await sendWith(h)).json()).toMatchObject({ error: "invalid_recipients" });
167
- });
168
- });
169
-
170
- describe("interactive notifications: closing early", () => {
171
- const close = (h: ReturnType<typeof makeHarness>, id = "n_1") =>
172
- h.request(`/api/notify/${id}/close`, { method: "POST" });
173
-
174
- it("calls today-v2's close endpoint with the app's key", async () => {
175
- const h = makeHarness(
176
- { close: () => Response.json({ closedAt: "2026-11-10T00:00:00Z" }) },
177
- NOTIFY_ENV,
178
- );
179
- const res = await close(h);
180
- expect(res.status).toBe(200);
181
- expect(await res.json()).toEqual({ ok: true, closedAt: "2026-11-10T00:00:00Z" });
182
- const sent = h.calls.close[0]!;
183
- expect(sent.method).toBe("POST");
184
- expect(new URL(sent.url).pathname).toBe("/external/notifications/n_1/interaction/close");
185
- expect(sent.headers.get("authorization")).toBe("Bearer app-key");
186
- });
187
-
188
- it("is idempotent: closing twice is two successes", async () => {
189
- const h = makeHarness(
190
- { close: () => Response.json({ closedAt: "2026-11-10T00:00:00Z" }) },
191
- NOTIFY_ENV,
192
- );
193
- expect((await close(h)).status).toBe(200);
194
- expect((await close(h)).status).toBe(200);
195
- });
196
-
197
- it("reports a 404 as notification_not_found", async () => {
198
- const h = makeHarness(
199
- {
200
- close: () =>
201
- Response.json({ error: { code: "NOTIFICATION_NOT_FOUND" } }, { status: 404 }),
202
- },
203
- NOTIFY_ENV,
204
- );
205
- const res = await close(h);
206
- expect(res.status).toBe(404);
207
- expect(await res.json()).toMatchObject({ error: "notification_not_found" });
208
- });
209
-
210
- it("reports the flag being off as interaction_unavailable", async () => {
211
- const h = makeHarness(
212
- {
213
- close: () =>
214
- Response.json(
215
- { error: { code: "INTERACTIVE_NOTIFICATIONS_DISABLED" } },
216
- { status: 400 },
217
- ),
218
- },
219
- NOTIFY_ENV,
220
- );
221
- expect(await (await close(h)).json()).toMatchObject({ error: "interaction_unavailable" });
222
- });
223
-
224
- it("fails loud when no key is bound", async () => {
225
- const h = makeHarness({}, { NOTIFY_API_BASE: "" });
226
- const res = await close(h);
227
- expect(res.status).toBe(503);
228
- expect(await res.json()).toMatchObject({ error: "notifications_not_configured" });
229
- });
230
- });
@@ -1,323 +0,0 @@
1
- // KYC Status on the actor, and getLegalIdentity (issue #216). Every refusal
2
- // the helper can return is named here. The offline path never yields a
3
- // citizen ID that would pass the Thai checksum.
4
-
5
- import { describe, expect, it, vi } from "vitest";
6
- import { DEV_PERSONAS } from "../src/dev-personas";
7
- import {
8
- getLegalIdentity,
9
- kycStartUrl,
10
- OFFLINE_INVALID_ID,
11
- OFFLINE_LEGAL_NAME,
12
- thaiIdChecksumValid,
13
- } from "../src/legal-identity";
14
- import { makeHarness, serviceRefusal } from "./harness";
15
-
16
- describe("actor.kyc", () => {
17
- it("gives every Persona a KYC Status, and members are verified via membership", () => {
18
- for (const persona of Object.values(DEV_PERSONAS)) {
19
- expect(persona.actor.kyc).toEqual(
20
- expect.objectContaining({
21
- status: expect.any(String),
22
- verified: expect.any(Boolean),
23
- }),
24
- );
25
- expect("kyc" in persona.effective).toBe(false);
26
- }
27
- expect(DEV_PERSONAS["hq-staff"]!.actor.kyc).toMatchObject({
28
- status: "verified",
29
- verified: true,
30
- source: "membership",
31
- });
32
- expect(DEV_PERSONAS.unverified!.actor.kyc.status).toBe("none");
33
- expect(DEV_PERSONAS["kyc-pending"]!.actor.kyc.status).toBe("pending_review");
34
- expect(DEV_PERSONAS["kyc-verified-nonmember"]!.actor.kyc).toMatchObject({
35
- status: "verified",
36
- source: "kyc",
37
- });
38
- });
39
-
40
- it("keeps the delegate's KYC Status, not the supervisor's", async () => {
41
- const delegate = makeHarness({}, { DEV_PERSONA: "delegate" });
42
- const supervisor = makeHarness({}, { DEV_PERSONA: "province-coordinator" });
43
- const delegateMe = await (await delegate.request("/api/me")).json<{
44
- actor: { kyc: { verifiedAt: string; source: string } };
45
- effective: Record<string, unknown>;
46
- }>();
47
- const supervisorMe = await (await supervisor.request("/api/me")).json<{
48
- actor: { kyc: { verifiedAt: string } };
49
- }>();
50
- expect(delegateMe.actor.kyc.verifiedAt).toBe("2024-04-01T00:00:00.000Z");
51
- expect(delegateMe.actor.kyc.verifiedAt).not.toBe(supervisorMe.actor.kyc.verifiedAt);
52
- expect(delegateMe.actor.kyc.source).toBe("membership");
53
- expect(delegateMe.effective.kyc).toBeUndefined();
54
- });
55
-
56
- it("parses userinfo kyc onto the actor only, including a delegated session", async () => {
57
- const h = makeHarness(
58
- {
59
- sso: () =>
60
- Response.json({
61
- sub: "u-delegate",
62
- name: "Nok",
63
- kyc: {
64
- status: "verified",
65
- verified: true,
66
- verifiedAt: "2024-06-01T00:00:00.000Z",
67
- source: "kyc",
68
- },
69
- ad: {
70
- user: { id: 9, role: "province", mobile: "0810000009" },
71
- actualPpleUser: { id: 14, role: "delegate", mobile: "0810000014" },
72
- activeRole: "province",
73
- eligibleRoles: ["province"],
74
- eligibleProvinces: ["เชียงใหม่"],
75
- },
76
- }),
77
- },
78
- { DEV_PERSONA: "none" },
79
- );
80
- const me = await (await h.request("/api/me", {}, "valid-token")).json<{
81
- actor: { kyc: { source: string; verifiedAt: string } };
82
- effective: Record<string, unknown>;
83
- }>();
84
- expect(me.actor.kyc).toEqual({
85
- status: "verified",
86
- verified: true,
87
- verifiedAt: "2024-06-01T00:00:00.000Z",
88
- source: "kyc",
89
- });
90
- expect(me.effective.kyc).toBeUndefined();
91
- });
92
-
93
- it("defaults a userinfo payload with no kyc to none", async () => {
94
- const h = makeHarness(
95
- {
96
- sso: () =>
97
- Response.json({
98
- sub: "u-42",
99
- name: "Somchai",
100
- ad: {
101
- user: { id: 7, role: "hq", mobile: "0810000007" },
102
- actualPpleUser: { id: 7, role: "hq", mobile: "0810000007" },
103
- activeRole: "hq",
104
- eligibleRoles: ["hq"],
105
- eligibleProvinces: ["all"],
106
- },
107
- }),
108
- },
109
- { DEV_PERSONA: "none" },
110
- );
111
- const me = await (await h.request("/api/me", {}, "valid-token")).json<{
112
- actor: { kyc: { status: string; verified: boolean; source: null } };
113
- }>();
114
- expect(me.actor.kyc).toEqual({
115
- status: "none",
116
- verified: false,
117
- verifiedAt: null,
118
- source: null,
119
- });
120
- });
121
- });
122
-
123
- describe("getLegalIdentity offline", () => {
124
- it("never returns a checksum-valid citizen ID", async () => {
125
- expect(thaiIdChecksumValid(OFFLINE_INVALID_ID)).toBe(false);
126
- // A known-valid check digit for the same first 12 digits is 8.
127
- expect(thaiIdChecksumValid("1100000000008")).toBe(true);
128
-
129
- for (const name of Object.keys(DEV_PERSONAS)) {
130
- const result = await getLegalIdentity({ DEV_PERSONA: name }, "ignored");
131
- if (!result.ok) {
132
- expect(result.reason).toBe("not_verified");
133
- continue;
134
- }
135
- expect(result.legalIdentity.name).toBe(OFFLINE_LEGAL_NAME);
136
- if (result.legalIdentity.idCardNumber !== null) {
137
- expect(thaiIdChecksumValid(result.legalIdentity.idCardNumber)).toBe(false);
138
- }
139
- }
140
-
141
- const member = await getLegalIdentity({ DEV_PERSONA: "hq-staff" }, "t");
142
- expect(member).toEqual({
143
- ok: true,
144
- legalIdentity: { source: "membership", name: OFFLINE_LEGAL_NAME, idCardNumber: null },
145
- });
146
- const card = await getLegalIdentity({ DEV_PERSONA: "kyc-verified-nonmember" }, "t");
147
- expect(card).toEqual({
148
- ok: true,
149
- legalIdentity: { source: "kyc", name: OFFLINE_LEGAL_NAME, idCardNumber: OFFLINE_INVALID_ID },
150
- });
151
- expect(await getLegalIdentity({ DEV_PERSONA: "unverified" }, "t")).toEqual({
152
- ok: false,
153
- reason: "not_verified",
154
- });
155
- expect(await getLegalIdentity({ DEV_PERSONA: "kyc-pending" }, "t")).toEqual({
156
- ok: false,
157
- reason: "not_verified",
158
- });
159
- });
160
- });
161
-
162
- describe("getLegalIdentity against Platform Services", () => {
163
- const env = {
164
- DEV_PERSONA: "none",
165
- PPLE_SERVICES_URL: "https://services.test",
166
- PPLE_APP_KEY: "app-key",
167
- };
168
-
169
- it("is not_configured until both bindings exist, and does not call out", async () => {
170
- const fetchImpl = vi.fn<typeof fetch>();
171
- expect(await getLegalIdentity({ DEV_PERSONA: "none" }, "tok", fetchImpl)).toEqual({
172
- ok: false,
173
- reason: "not_configured",
174
- });
175
- expect(
176
- await getLegalIdentity({ DEV_PERSONA: "none", PPLE_SERVICES_URL: "https://services.test" }, "tok", fetchImpl),
177
- ).toEqual({ ok: false, reason: "not_configured" });
178
- expect(fetchImpl).not.toHaveBeenCalled();
179
- });
180
-
181
- it("posts to platform/legal-identity with the App Key and the user token, and no body", async () => {
182
- let seen: { authorization: string | null; userToken: string | null; body: string } | undefined;
183
- const h = makeHarness(
184
- {
185
- services: async (req) => {
186
- seen = {
187
- authorization: req.headers.get("authorization"),
188
- userToken: req.headers.get("x-pple-user-token"),
189
- body: await req.text(),
190
- };
191
- return Response.json({ source: "kyc", name: "นาย ก", idCardNumber: "1100000000008" });
192
- },
193
- },
194
- env,
195
- );
196
-
197
- expect(await getLegalIdentity(h.testEnv, "user-token", h.fetchImpl)).toEqual({
198
- ok: true,
199
- legalIdentity: { source: "kyc", name: "นาย ก", idCardNumber: "1100000000008" },
200
- });
201
- expect(seen).toEqual({
202
- authorization: "Bearer app-key",
203
- userToken: "user-token",
204
- body: "",
205
- });
206
- expect(h.calls.services).toEqual([
207
- { path: "/platform/legal-identity", method: "POST", headers: {} },
208
- ]);
209
- });
210
-
211
- it("maps each platform code, and keeps needs_beta as its own reason", async () => {
212
- type Mapped =
213
- | "invalid_app_key"
214
- | "not_granted"
215
- | "needs_beta"
216
- | "user_token_invalid"
217
- | "upstream_unavailable";
218
- const mapped: [string, number, Mapped][] = [
219
- ["invalid_app_key", 401, "invalid_app_key"],
220
- ["not_granted", 403, "not_granted"],
221
- ["needs_beta", 403, "needs_beta"],
222
- ["user_token_invalid", 401, "user_token_invalid"],
223
- ["not_listed", 404, "upstream_unavailable"],
224
- ["identity_unavailable", 503, "upstream_unavailable"],
225
- ["provider_unavailable", 502, "upstream_unavailable"],
226
- ["provider_timeout", 504, "upstream_unavailable"],
227
- ];
228
- let next = serviceRefusal("invalid_app_key", 401);
229
- const h = makeHarness({ services: () => next }, env);
230
- for (const [code, status, reason] of mapped) {
231
- next = serviceRefusal(code, status);
232
- expect(await getLegalIdentity(h.testEnv, "user-token", h.fetchImpl)).toEqual({ ok: false, reason });
233
- }
234
-
235
- // A code this helper does not name is still a platform refusal.
236
- next = serviceRefusal("identity_not_enabled", 403);
237
- expect(await getLegalIdentity(h.testEnv, "user-token", h.fetchImpl)).toEqual({
238
- ok: false,
239
- reason: "upstream_unavailable",
240
- });
241
- });
242
-
243
- it("maps this Service's own answers, which carry no x-pple-error", async () => {
244
- type Answer = "not_verified" | "user_token_invalid" | "upstream_unavailable";
245
- const answers: [string, number, Answer][] = [
246
- ["not_verified", 404, "not_verified"],
247
- ["user_token_invalid", 401, "user_token_invalid"],
248
- ["upstream_unavailable", 502, "upstream_unavailable"],
249
- ];
250
- let next = Response.json({ error: "not_verified" }, { status: 404 });
251
- const h = makeHarness(
252
- {
253
- services: (req) => {
254
- expect(req.headers.get("x-pple-error")).toBeNull();
255
- return next;
256
- },
257
- },
258
- env,
259
- );
260
- for (const [error, status, reason] of answers) {
261
- next = Response.json({ error }, { status });
262
- expect(await getLegalIdentity(h.testEnv, "user-token", h.fetchImpl)).toEqual({ ok: false, reason });
263
- }
264
-
265
- // The old reason is not read off a Provider body.
266
- next = Response.json({ error: "identity_not_enabled" }, { status: 403 });
267
- expect(await getLegalIdentity(h.testEnv, "user-token", h.fetchImpl)).toEqual({
268
- ok: false,
269
- reason: "upstream_unavailable",
270
- });
271
- // needs_beta is a platform code. Without the header it is not that reason.
272
- next = Response.json({ error: "needs_beta" }, { status: 403 });
273
- expect(await getLegalIdentity(h.testEnv, "user-token", h.fetchImpl)).toEqual({
274
- ok: false,
275
- reason: "upstream_unavailable",
276
- });
277
- });
278
-
279
- it("treats a transport failure and a body-less error as upstream_unavailable", async () => {
280
- const down = vi.fn<typeof fetch>(async () => {
281
- throw new Error("network");
282
- });
283
- expect(await getLegalIdentity(env, "user-token", down)).toEqual({
284
- ok: false,
285
- reason: "upstream_unavailable",
286
- });
287
-
288
- const odd = vi.fn<typeof fetch>(async () => new Response("nope", { status: 502 }));
289
- expect(await getLegalIdentity(env, "user-token", odd)).toEqual({
290
- ok: false,
291
- reason: "upstream_unavailable",
292
- });
293
- });
294
- });
295
-
296
- describe("local KYC redirect", () => {
297
- it("sends a local click straight back with ?kyc=cancelled", () => {
298
- const back = kycStartUrl({
299
- ssoBaseUrl: "https://id.example",
300
- clientId: "zitadel-app",
301
- returnTo: "https://app.example/checkout?step=1#pay",
302
- dev: true,
303
- });
304
- const url = new URL(back);
305
- expect(url.origin + url.pathname).toBe("https://app.example/checkout");
306
- expect(url.searchParams.get("step")).toBe("1");
307
- expect(url.searchParams.get("kyc")).toBe("cancelled");
308
- expect(url.hash).toBe("#pay");
309
- });
310
-
311
- it("points production at PPLE ID /kyc", () => {
312
- expect(
313
- kycStartUrl({
314
- ssoBaseUrl: "https://id.example",
315
- clientId: "zitadel-app",
316
- returnTo: "https://app.example/checkout",
317
- dev: false,
318
- }),
319
- ).toBe(
320
- "https://id.example/kyc?client_id=zitadel-app&return_to=https%3A%2F%2Fapp.example%2Fcheckout",
321
- );
322
- });
323
- });
@@ -1,105 +0,0 @@
1
- // Actor membership (ADR-0028): userinfo onto the Persona path, and the
2
- // member-only Persona that makes member and internal differ offline.
3
-
4
- import { describe, expect, it } from "vitest";
5
- import { DEV_PERSONAS } from "../src/dev-personas";
6
- import { passesLevel } from "../src/levels";
7
- import { makeHarness } from "./harness";
8
-
9
- const AD = {
10
- user: { id: 7, role: "hq", mobile: "0810000007" },
11
- actualPpleUser: { id: 7, role: "hq", mobile: "0810000007" },
12
- activeRole: "hq",
13
- eligibleRoles: ["hq"],
14
- eligibleProvinces: ["all"],
15
- };
16
-
17
- function signedIn(membership: unknown) {
18
- return makeHarness(
19
- {
20
- sso: () =>
21
- Response.json({
22
- sub: "u-42",
23
- name: "Somchai",
24
- ad: AD,
25
- ...(membership === undefined ? {} : { membership }),
26
- }),
27
- },
28
- { DEV_PERSONA: "none" },
29
- );
30
- }
31
-
32
- describe("actor.membership", () => {
33
- it("keeps memberSince from userinfo and drops the address", async () => {
34
- const h = signedIn({
35
- type: "yearly",
36
- memberSince: "2020-05-01",
37
- registeredAddress: { province: "เชียงใหม่", district: "เมืองเชียงใหม่" },
38
- });
39
- const me = await (await h.request("/api/me", {}, "valid-token")).json<{
40
- actor: { membership: { memberSince: string } | null };
41
- effective: Record<string, unknown>;
42
- }>();
43
- expect(me.actor.membership).toEqual({ memberSince: "2020-05-01" });
44
- expect(JSON.stringify(me)).not.toContain("registeredAddress");
45
- expect(me.effective.membership).toBeUndefined();
46
- });
47
-
48
- it.each([
49
- ["missing", undefined],
50
- ["null", null],
51
- ["an empty object", {}],
52
- ["a datetime", { memberSince: "2020-05-01T00:00:00.000Z" }],
53
- ] as const)("is null when userinfo membership is %s", async (_label, membership) => {
54
- const h = signedIn(membership);
55
- const me = await (await h.request("/api/me", {}, "valid-token")).json<{
56
- actor: { membership: unknown };
57
- }>();
58
- expect(me.actor.membership).toBeNull();
59
- });
60
-
61
- it("gives every Persona a membership, and member has no Role", () => {
62
- for (const persona of Object.values(DEV_PERSONAS)) {
63
- expect(persona.actor.membership === null || typeof persona.actor.membership?.memberSince === "string").toBe(
64
- true,
65
- );
66
- expect("membership" in persona.effective).toBe(false);
67
- expect(typeof persona.effective.sub).toBe("string");
68
- if (persona.effective.isDelegated) {
69
- expect(persona.effective.sub).not.toBe(persona.actor.sub);
70
- } else {
71
- expect(persona.effective.sub).toBe(persona.actor.sub);
72
- }
73
- }
74
- expect(DEV_PERSONAS.member).toMatchObject({
75
- actor: { sub: "dev-member", membership: { memberSince: "2024-06-01" } },
76
- effective: { sub: "dev-member", activeRole: "", isDelegated: false, extraRoles: [] },
77
- });
78
- expect(DEV_PERSONAS["hq-staff"]?.actor.membership).toEqual({ memberSince: "2024-01-01" });
79
- expect(DEV_PERSONAS.unverified?.actor.membership).toBeNull();
80
- expect(DEV_PERSONAS["kyc-pending"]?.actor.membership).toBeNull();
81
- expect(DEV_PERSONAS["kyc-verified-nonmember"]?.actor.membership).toBeNull();
82
- });
83
-
84
- it("lets a member pass member and not internal, and staff pass both", () => {
85
- const persona = DEV_PERSONAS.member;
86
- if (!persona) throw new Error("member persona missing");
87
- expect(passesLevel(null, "public")).toBe(true);
88
- expect(passesLevel(null, "member")).toBe(false);
89
- expect(passesLevel(persona, "public")).toBe(true);
90
- expect(passesLevel(persona, "member")).toBe(true);
91
- expect(passesLevel(persona, "internal")).toBe(false);
92
- expect(passesLevel(persona, { roles: ["pple-ad:hq"] })).toBe(false);
93
- expect(passesLevel(DEV_PERSONAS["hq-staff"]!, "internal")).toBe(true);
94
- expect(passesLevel(DEV_PERSONAS["hq-staff"]!, "member")).toBe(true);
95
- expect(passesLevel(DEV_PERSONAS.unverified!, "member")).toBe(false);
96
- expect(passesLevel(DEV_PERSONAS["kyc-verified-nonmember"]!, "member")).toBe(false);
97
- expect(passesLevel(DEV_PERSONAS.delegate!, "member")).toBe(true);
98
- expect(passesLevel(DEV_PERSONAS.delegate!, { subs: ["dev-delegate"] })).toBe(true);
99
- expect(passesLevel(DEV_PERSONAS.delegate!, { subs: ["dev-province-coordinator"] })).toBe(true);
100
- expect(DEV_PERSONAS.delegate?.effective.sub).toBe("dev-province-coordinator");
101
- expect(passesLevel(DEV_PERSONAS["hq-staff"]!, { subs: ["dev-province-coordinator"] })).toBe(false);
102
- expect(passesLevel(DEV_PERSONAS["platform-admin"]!, { roles: ["pple-ad:mp"] })).toBe(false);
103
- expect(passesLevel(DEV_PERSONAS["platform-admin"]!, { roles: ["pple-ad:platform:admin"] })).toBe(true);
104
- });
105
- });