@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,404 +0,0 @@
1
- // Offline Provider development (ADR-0026). What the handler observes:
2
- // serviceCall, the tail path, and the status. The dev wrapper is the Worker
3
- // entry under vitest, where import.meta.env.DEV is true.
4
-
5
- import { afterEach, describe, expect, it, vi } from "vitest";
6
- import { DEV_PERSONAS } from "../src/dev-personas";
7
- import { devWorkerFetch } from "../src/dev-registry";
8
- import worker from "../src/index";
9
- import { serviceCall } from "../src/registry";
10
- import { makeHarness, testExecutionContext } from "./harness";
11
-
12
- const ctx = testExecutionContext();
13
-
14
- afterEach(() => {
15
- const registry = worker.registry ?? {};
16
- for (const key of Object.keys(registry)) delete registry[key];
17
- });
18
-
19
- function install(name: string): { request?: Request } {
20
- const seen: { request?: Request } = {};
21
- if (!worker.registry) throw new Error("worker has no registry");
22
- worker.registry[name] = (request) => {
23
- seen.request = request;
24
- const url = new URL(request.url);
25
- return Response.json({
26
- call: serviceCall(request),
27
- path: url.pathname,
28
- search: url.search,
29
- method: request.method,
30
- authorization: request.headers.get("authorization"),
31
- secret: request.headers.get("x-pple-cron-secret"),
32
- });
33
- };
34
- return seen;
35
- }
36
-
37
- describe("the emulated registry path", () => {
38
- it("delivers the tail to the handler with the default Persona and dev-consumer", async () => {
39
- install("branch-list");
40
- const h = makeHarness();
41
-
42
- const res = await worker.fetch(
43
- new Request("https://localhost/.pple/registry/branch-list/offices?q=1"),
44
- h.testEnv,
45
- ctx,
46
- );
47
-
48
- expect(res.status).toBe(200);
49
- expect(await res.json()).toEqual({
50
- call: {
51
- consumer: "dev-consumer",
52
- entry: "branch-list",
53
- identity: DEV_PERSONAS["hq-staff"],
54
- },
55
- path: "/offices",
56
- search: "?q=1",
57
- method: "GET",
58
- authorization: null,
59
- secret: null,
60
- });
61
- });
62
-
63
- it("uses DEV_PERSONA and DEV_CONSUMER, and ignores forged platform headers", async () => {
64
- install("branch-list");
65
- const h = makeHarness({}, { DEV_PERSONA: "province-coordinator", DEV_CONSUMER: "brave-purple-lion" });
66
-
67
- const res = await worker.fetch(
68
- new Request("https://localhost/.pple/registry/branch-list", {
69
- headers: {
70
- "x-pple-consumer": "forged",
71
- "x-pple-entry": "forged/nope",
72
- "x-pple-identity": "forged",
73
- authorization: "Bearer raw-token",
74
- "x-pple-cron-secret": "s3cret",
75
- },
76
- }),
77
- h.testEnv,
78
- ctx,
79
- );
80
-
81
- expect(await res.json()).toMatchObject({
82
- call: {
83
- consumer: "brave-purple-lion",
84
- entry: "branch-list",
85
- identity: DEV_PERSONAS["province-coordinator"],
86
- },
87
- path: "/",
88
- authorization: null,
89
- secret: null,
90
- });
91
- });
92
-
93
- it("sends no Identity when DEV_PERSONA is none", async () => {
94
- install("branch-list");
95
- const h = makeHarness({}, { DEV_PERSONA: "none" });
96
-
97
- const res = await worker.fetch(
98
- new Request("https://localhost/.pple/registry/branch-list/offices"),
99
- h.testEnv,
100
- ctx,
101
- );
102
-
103
- expect(await res.json()).toMatchObject({
104
- call: { consumer: "dev-consumer", entry: "branch-list", identity: null },
105
- path: "/offices",
106
- });
107
- });
108
-
109
- it("sends no Identity when the Persona name does not exist", async () => {
110
- install("branch-list");
111
- const h = makeHarness({}, { DEV_PERSONA: "not-a-persona" });
112
- const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
113
- try {
114
- const res = await worker.fetch(
115
- new Request("https://localhost/.pple/registry/branch-list/"),
116
- h.testEnv,
117
- ctx,
118
- );
119
-
120
- expect(await res.json()).toMatchObject({
121
- call: { consumer: "dev-consumer", identity: null },
122
- path: "/",
123
- });
124
- expect(warn).toHaveBeenCalled();
125
- } finally {
126
- warn.mockRestore();
127
- }
128
- });
129
-
130
- it("passes the method and body through, and a non-fetch value throws", async () => {
131
- const seen: { body?: string; type?: string | null; connection?: string | null } = {};
132
- if (!worker.registry) throw new Error("worker has no registry");
133
- worker.registry["branch-list"] = async (request) => {
134
- seen.body = await request.text();
135
- seen.type = request.headers.get("content-type");
136
- seen.connection = request.headers.get("connection");
137
- return new Response("created", { status: 201, headers: { "x-pple-extra": "kept-here" } });
138
- };
139
- const h = makeHarness();
140
-
141
- const res = await worker.fetch(
142
- new Request("https://localhost/.pple/registry/branch-list/items", {
143
- method: "POST",
144
- headers: { "content-type": "text/plain", connection: "close" },
145
- body: "เนื้อหา",
146
- }),
147
- h.testEnv,
148
- ctx,
149
- );
150
-
151
- expect(res.status).toBe(201);
152
- expect(await res.text()).toBe("created");
153
- expect(res.headers.get("x-pple-extra")).toBe("kept-here");
154
- expect(seen).toEqual({ body: "เนื้อหา", type: "text/plain", connection: null });
155
-
156
- Object.assign(worker.registry, { "branch-list": { pages: () => ({}) } });
157
- await expect(
158
- worker.fetch(new Request("https://localhost/.pple/registry/branch-list"), h.testEnv, ctx),
159
- ).rejects.toThrow("registry handler missing");
160
- });
161
-
162
- it("calls a Dataset page function with the cursor and answers with the page", async () => {
163
- if (!worker.registry) throw new Error("worker has no registry");
164
- let cursor: string | null = "unset";
165
- worker.registry["branch-list"] = {
166
- offices: (input) => {
167
- cursor = input.cursor;
168
- return { objects: [{ id: "a", name: "สาขา" }], next: null };
169
- },
170
- };
171
- const h = makeHarness();
172
-
173
- const res = await worker.fetch(
174
- new Request("https://localhost/.pple/registry/branch-list/offices", {
175
- method: "POST",
176
- headers: { "content-type": "application/json" },
177
- body: JSON.stringify({ cursor: "a" }),
178
- }),
179
- h.testEnv,
180
- ctx,
181
- );
182
-
183
- expect(res.status).toBe(200);
184
- expect(await res.json()).toEqual({ objects: [{ id: "a", name: "สาขา" }], next: null });
185
- expect(cursor).toBe("a");
186
- });
187
-
188
- it("calls a fetch-shaped object with itself as this", async () => {
189
- const app = {
190
- marker: "hono",
191
- fetch(this: { marker: string }, request: Request) {
192
- return Response.json({ self: this.marker, path: new URL(request.url).pathname });
193
- },
194
- };
195
- if (!worker.registry) throw new Error("worker has no registry");
196
- worker.registry["branch-list"] = app;
197
- const h = makeHarness();
198
-
199
- const res = await worker.fetch(
200
- new Request("https://localhost/.pple/registry/branch-list/offices"),
201
- h.testEnv,
202
- ctx,
203
- );
204
-
205
- expect(await res.json()).toEqual({ self: "hono", path: "/offices" });
206
- });
207
-
208
- it("routes a nested local path to a namespaced entry's handler", async () => {
209
- install("2027/score");
210
- install("score");
211
- const h = makeHarness();
212
-
213
- const res = await worker.fetch(
214
- new Request("https://localhost/.pple/registry/2027/score/district/7?round=2"),
215
- h.testEnv,
216
- ctx,
217
- );
218
-
219
- expect(res.status).toBe(200);
220
- const body = (await res.json()) as { call: { entry: string }; path: string; search: string };
221
- expect(body.call.entry).toBe("2027/score");
222
- expect(body.path).toBe("/district/7");
223
- expect(body.search).toBe("?round=2");
224
-
225
- const bare = await worker.fetch(new Request("https://localhost/.pple/registry/score/x"), h.testEnv, ctx);
226
- expect(((await bare.json()) as { path: string }).path).toBe("/x");
227
- });
228
-
229
- it("does not take a key that only shares leading text with the path", async () => {
230
- install("2027/score");
231
- const h = makeHarness();
232
- await expect(
233
- worker.fetch(new Request("https://localhost/.pple/registry/2027/scores/x"), h.testEnv, ctx),
234
- ).rejects.toThrow("registry handler missing");
235
- });
236
-
237
- it("answers 404 when the registry path names no entry", async () => {
238
- const h = makeHarness();
239
- const res = await worker.fetch(new Request("https://localhost/.pple/registry/"), h.testEnv, ctx);
240
- expect(res.status).toBe(404);
241
- expect(res.headers.get("cache-control")).toBe("no-store");
242
- });
243
- });
244
-
245
- describe("public traffic under dev", () => {
246
- it("strips x-pple-* before app code and still serves the route", async () => {
247
- let ran = false;
248
- if (!worker.registry) throw new Error("worker has no registry");
249
- worker.registry["branch-list"] = () => {
250
- ran = true;
251
- return new Response("no");
252
- };
253
- const h = makeHarness();
254
-
255
- const res = await worker.fetch(
256
- new Request("https://localhost/api/health", {
257
- headers: {
258
- "x-pple-consumer": "forged",
259
- "x-pple-entry": "forged/branch-list",
260
- "x-pple-identity": "forged",
261
- },
262
- }),
263
- h.testEnv,
264
- ctx,
265
- );
266
-
267
- expect(res.status).toBe(200);
268
- expect(await res.json()).toEqual({ ok: true });
269
- expect(ran).toBe(false);
270
-
271
- let seen: Request | undefined;
272
- const stripped = await devWorkerFetch(
273
- {
274
- fetch(request) {
275
- seen = request;
276
- return new Response("app");
277
- },
278
- registry: {},
279
- },
280
- new Request("https://localhost/api/notes", {
281
- headers: {
282
- authorization: "Bearer keep",
283
- cookie: "session=keep",
284
- "x-request-id": "keep",
285
- "x-pple-consumer": "forged",
286
- "x-pple-identity": "forged",
287
- "X-Pple-User-Token": "raw-token",
288
- },
289
- }),
290
- h.testEnv,
291
- ctx,
292
- );
293
-
294
- expect(stripped.status).toBe(200);
295
- if (!seen) throw new Error("public request did not reach app code");
296
- expect(serviceCall(seen)).toBeNull();
297
- expect(seen.headers.get("authorization")).toBe("Bearer keep");
298
- expect(seen.headers.get("cookie")).toBe("session=keep");
299
- expect(seen.headers.get("x-request-id")).toBe("keep");
300
- expect(seen.headers.get("x-pple-consumer")).toBeNull();
301
- expect(seen.headers.get("x-pple-user-token")).toBeNull();
302
- });
303
- });
304
-
305
- describe("callRegistry", () => {
306
- it("calls the handler with the Persona and Consumer overrides", async () => {
307
- const seen: { request?: Request } = {};
308
- if (!worker.registry) throw new Error("worker has no registry");
309
- worker.registry["branch-list"] = (request) => {
310
- seen.request = request;
311
- return Response.json(serviceCall(request));
312
- };
313
- const h = makeHarness({}, { DEV_PERSONA: "delegate", DEV_CONSUMER: "from-env" });
314
-
315
- const res = await h.callRegistry("branch-list", "/offices?q=1", {
316
- persona: "province-coordinator",
317
- consumer: "brave-purple-lion",
318
- });
319
-
320
- expect(res.status).toBe(200);
321
- expect(await res.json()).toEqual({
322
- consumer: "brave-purple-lion",
323
- entry: "branch-list",
324
- identity: DEV_PERSONAS["province-coordinator"],
325
- });
326
- if (!seen.request) throw new Error("handler did not run");
327
- const url = new URL(seen.request.url);
328
- expect(url.pathname).toBe("/offices");
329
- expect(url.search).toBe("?q=1");
330
- });
331
-
332
- it("preserves the method and body, and drops a forged platform header", async () => {
333
- if (!worker.registry) throw new Error("worker has no registry");
334
- worker.registry["branch-list"] = async (request) =>
335
- Response.json({
336
- call: serviceCall(request),
337
- method: request.method,
338
- type: request.headers.get("content-type"),
339
- authorization: request.headers.get("authorization"),
340
- body: await request.text(),
341
- });
342
- const h = makeHarness();
343
-
344
- const res = await h.callRegistry("branch-list", "/items", {
345
- persona: "none",
346
- consumer: "brave-purple-lion",
347
- init: {
348
- method: "POST",
349
- headers: {
350
- "content-type": "text/plain",
351
- "x-pple-consumer": "forged",
352
- authorization: "Bearer raw-token",
353
- },
354
- body: "เนื้อหา",
355
- },
356
- });
357
-
358
- expect(await res.json()).toEqual({
359
- call: {
360
- consumer: "brave-purple-lion",
361
- entry: "branch-list",
362
- identity: null,
363
- },
364
- method: "POST",
365
- type: "text/plain",
366
- authorization: null,
367
- body: "เนื้อหา",
368
- });
369
- });
370
-
371
- it("uses the harness Persona and Consumer when no override is passed", async () => {
372
- if (!worker.registry) throw new Error("worker has no registry");
373
- let path = "";
374
- worker.registry["branch-list"] = (request) => {
375
- path = new URL(request.url).pathname;
376
- return Response.json(serviceCall(request));
377
- };
378
- const h = makeHarness({}, { DEV_PERSONA: "delegate", DEV_CONSUMER: "from-env" });
379
-
380
- const res = await h.callRegistry("branch-list", "items");
381
-
382
- expect(await res.json()).toEqual({
383
- consumer: "from-env",
384
- entry: "branch-list",
385
- identity: DEV_PERSONAS.delegate,
386
- });
387
- expect(path).toBe("/items");
388
- });
389
-
390
- it("sends no Identity for persona none, and throws when the entry has no handler", async () => {
391
- if (!worker.registry) throw new Error("worker has no registry");
392
- worker.registry["branch-list"] = (request) => Response.json(serviceCall(request));
393
- const h = makeHarness({}, { DEV_PERSONA: "hq-staff" });
394
-
395
- const res = await h.callRegistry("branch-list", "/", { persona: "none" });
396
- expect(await res.json()).toMatchObject({ consumer: "dev-consumer", identity: null });
397
-
398
- delete worker.registry["branch-list"];
399
- // The platform wrapper throws this. pple-services answers provider_unavailable
400
- // (502) to the Consumer. callRegistry throws the same error and does not
401
- // invent that response.
402
- await expect(h.callRegistry("branch-list", "/offices")).rejects.toThrow("registry handler missing");
403
- });
404
- });
@@ -1,101 +0,0 @@
1
- // effective.sub (ADR-0028): userinfo onto /api/me, then the same passesLevel
2
- // a Dataset read will use. The read API does not exist yet.
3
-
4
- import { describe, expect, it } from "vitest";
5
- import { passesLevel, type LevelIdentity } from "../src/levels";
6
- import { makeHarness } from "./harness";
7
-
8
- const SUPERVISOR = "347513243977122153";
9
- const LEGACY = "11111111-2222-3333-4444-555555555555";
10
-
11
- function signedIn(ad: Record<string, unknown>, sub = "u-actor") {
12
- return makeHarness(
13
- {
14
- sso: () => Response.json({ sub, name: "Nok", ad }),
15
- },
16
- { DEV_PERSONA: "none" },
17
- );
18
- }
19
-
20
- async function me(ad: Record<string, unknown>, sub = "u-actor"): Promise<LevelIdentity> {
21
- const res = await signedIn(ad, sub).request("/api/me", {}, "valid-token");
22
- expect(res.status).toBe(200);
23
- return res.json<LevelIdentity>();
24
- }
25
-
26
- describe("effective.sub", () => {
27
- it("is the actor sub when nobody is delegating, even if ad.user carries a Zitadel id", async () => {
28
- const identity = await me(
29
- {
30
- user: { id: 7, role: "hq", zitadel_user_id: SUPERVISOR, sso_user_id: LEGACY },
31
- actualPpleUser: { id: 7, role: "hq" },
32
- },
33
- "u-42",
34
- );
35
- expect(identity.effective.isDelegated).toBe(false);
36
- expect(identity.effective.sub).toBe("u-42");
37
- expect(JSON.stringify(identity)).not.toContain(SUPERVISOR);
38
- expect(JSON.stringify(identity)).not.toContain(LEGACY);
39
- expect(passesLevel(identity, { subs: ["u-42"] })).toBe(true);
40
- expect(passesLevel(identity, { subs: [SUPERVISOR] })).toBe(false);
41
- });
42
-
43
- it("is the supervisor's zitadel id under delegation, and that passes their subs level", async () => {
44
- const identity = await me({
45
- user: { id: 9, role: "province", zitadel_user_id: SUPERVISOR, sso_user_id: LEGACY },
46
- actualPpleUser: { id: 14, role: "delegate" },
47
- activeRole: "province",
48
- });
49
- expect(identity.effective.isDelegated).toBe(true);
50
- expect(identity.actor.sub).toBe("u-actor");
51
- expect(identity.effective.sub).toBe(SUPERVISOR);
52
- expect(JSON.stringify(identity)).not.toContain(LEGACY);
53
- expect(passesLevel(identity, { subs: [SUPERVISOR] })).toBe(true);
54
- expect(passesLevel(identity, { subs: ["u-actor"] })).toBe(true);
55
- expect(passesLevel(identity, { subs: [LEGACY] })).toBe(false);
56
- });
57
-
58
- it.each([
59
- ["null", { zitadel_user_id: null }],
60
- ["only sso_user_id", {}],
61
- ["a uuid", { zitadel_user_id: LEGACY }],
62
- ["a number", { zitadel_user_id: 347513243977122153 }],
63
- ] as const)(
64
- "is null when the supervisor zitadel id is %s, and that passes no named subs level",
65
- async (_label, extra) => {
66
- const identity = await me({
67
- user: { id: 9, role: "province", sso_user_id: LEGACY, ...extra },
68
- actualPpleUser: { id: 14, role: "delegate" },
69
- });
70
- expect(identity.effective.sub).toBeNull();
71
- expect(JSON.stringify(identity)).not.toContain(LEGACY);
72
- expect(passesLevel(identity, { subs: [SUPERVISOR] })).toBe(false);
73
- expect(passesLevel(identity, { subs: [LEGACY] })).toBe(false);
74
- expect(passesLevel(identity, { subs: ["u-actor"] })).toBe(true);
75
- },
76
- );
77
-
78
- it("does not treat ad.user as a supervisor unless the logged-in role is delegate and the rows differ", async () => {
79
- const samePerson = await me(
80
- {
81
- user: { id: 14, role: "delegate", zitadel_user_id: SUPERVISOR },
82
- actualPpleUser: { id: 14, role: "delegate" },
83
- },
84
- "u-actor",
85
- );
86
- expect(samePerson.effective.isDelegated).toBe(false);
87
- expect(samePerson.effective.sub).toBe("u-actor");
88
-
89
- const notADelegate = await me(
90
- {
91
- user: { id: 9, role: "province", zitadel_user_id: SUPERVISOR },
92
- actualPpleUser: { id: 14, role: "hq" },
93
- },
94
- "u-actor",
95
- );
96
- // Delegated rows, but not a recognised delegate: never the actor's own sub.
97
- expect(notADelegate.effective.isDelegated).toBe(true);
98
- expect(notADelegate.effective.sub).toBeNull();
99
- expect(passesLevel(notADelegate, { subs: [SUPERVISOR] })).toBe(false);
100
- });
101
- });
@@ -1,51 +0,0 @@
1
- // Offline email (#435). The mocks for `platform/email` (a copy of
2
- // registry/email/mocks/responses.json) are the answers `npm run dev` gives. This
3
- // drives those files through the same matcher a Builder's `callService` uses
4
- // offline, so a fresh app can send an email against them with nothing deployed.
5
-
6
- import { describe, expect, it } from "vitest";
7
- import { answerFromOverlay } from "../src/dev-call-service";
8
- import { callService } from "../src/call-service";
9
-
10
- // The mocks as a fresh app has them (test/fixtures/overlay/platform/email),
11
- // laid out as `pple registry` writes them into `.pple/registry/`. They are a
12
- // copy of the registry's own, which a Builder's app does not carry; the repo
13
- // keeps the copy equal to the original (recipes/registry-fixtures.test.ts).
14
- const files: Record<string, unknown> = Object.fromEntries(
15
- Object.entries(
16
- import.meta.glob("./fixtures/overlay/platform/email/**/*", { eager: true, query: "?raw", import: "default" }),
17
- ).map(([file, text]) => [file.replace("./fixtures/", ""), text]),
18
- );
19
-
20
- const offline: typeof fetch = async (input, init) => {
21
- const req = new Request(input as RequestInfo, init);
22
- const prefix = "/platform/email";
23
- return answerFromOverlay(files, {
24
- entry: "platform/email",
25
- path: new URL(req.url).pathname.slice(prefix.length),
26
- method: req.method,
27
- persona: "hq-staff",
28
- overlayRoot: "overlay/",
29
- });
30
- };
31
-
32
- describe("platform/email offline", () => {
33
- it("answers a send with one sent message and no real delivery", async () => {
34
- const res = await callService(
35
- { PPLE_SERVICES_URL: "https://services.test", PPLE_APP_KEY: "k", DEV_PERSONA: "none" },
36
- "platform/email",
37
- "",
38
- {
39
- method: "POST",
40
- headers: { "content-type": "application/json" },
41
- body: JSON.stringify({ to: ["somying@example.com"], subject: "ยืนยัน", body: "ลงทะเบียนแล้ว" }),
42
- },
43
- { fetchImpl: offline },
44
- );
45
- expect(res.status).toBe(200);
46
- expect(await res.json()).toEqual({
47
- sent: [{ to: "somying@example.com", messageId: "offline-mock" }],
48
- rejected: [],
49
- });
50
- });
51
- });
@@ -1,117 +0,0 @@
1
- // src/email.ts: the `html` tag escapes what people typed, the party layout
2
- // carries the footer and a plain-text part, and sendEmail posts to
3
- // platform/email.
4
-
5
- import { describe, expect, it } from "vitest";
6
- import { escapeHtml, html, partyEmail, raw, safeUrl, sendEmail } from "../src/email";
7
-
8
- describe("html", () => {
9
- it("escapes every inserted value, so a user's text cannot become markup", () => {
10
- const name = `<a href="https://evil.example">ยืนยันบัญชี</a> & 'x'`;
11
- expect(html`<p>สวัสดี ${name}</p>`.value).toBe(
12
- "<p>สวัสดี &#60;a href=&#34;https://evil.example&#34;&#62;ยืนยันบัญชี&#60;/a&#62; &#38; &#39;x&#39;</p>",
13
- );
14
- });
15
-
16
- it("nests html results and lists without escaping them twice, and drops empty values", () => {
17
- const items = ["ก < ข", "ค"];
18
- expect(html`<ul>${items.map((i) => html`<li>${i}</li>`)}</ul>${null}${undefined}${false}`.value).toBe(
19
- "<ul><li>ก &#60; ข</li><li>ค</li></ul>",
20
- );
21
- expect(html`${raw("<br>")}${42}`.value).toBe("<br>42");
22
- expect(escapeHtml(`"&"`)).toBe("&#34;&#38;&#34;");
23
- });
24
- });
25
-
26
- describe("safeUrl", () => {
27
- it("allows https and mailto links, and refuses the rest", () => {
28
- expect(safeUrl("https://example.com/a?b=1")).toBe("https://example.com/a?b=1");
29
- expect(safeUrl("mailto:office@example.com")).toBe("mailto:office@example.com");
30
- for (const bad of ["javascript:alert(1)", "http://example.com", "data:text/html,x", "/relative"]) {
31
- expect(() => safeUrl(bad), bad).toThrow();
32
- }
33
- });
34
- });
35
-
36
- describe("partyEmail", () => {
37
- const email = partyEmail({
38
- heading: "ยืนยันการลงทะเบียน <ทดสอบ>",
39
- content: html`<p>สวัสดีคุณ ${"สมหญิง <b>"}</p>`,
40
- text: "สวัสดีคุณ สมหญิง <b>",
41
- button: { label: "ดูรายละเอียด", url: "https://my-app.ppleth.ai/events/1?a=1&b=2" },
42
- preheader: "งานวันอาทิตย์",
43
- });
44
-
45
- it("wraps the message in the party layout: logo, contact footer and address, and nothing under it", () => {
46
- expect(email.html).toContain('src="https://services.ppleth.ai/email-assets/logo.png"');
47
- expect(email.html).toContain("ติดต่อเรา");
48
- expect(email.html).toContain("02-038-5100");
49
- expect(email.html).toContain("mailto:office@peoplespartythailand.org");
50
- expect(email.html).toContain("@peoplesth");
51
- expect(email.html).toContain("กรุงเทพมหานคร 10240");
52
- expect(email.html).not.toContain("คุณได้รับอีเมลนี้");
53
- expect(email.html).not.toContain("ไม่รับการตอบกลับ");
54
- expect(email.html).toContain("งานวันอาทิตย์");
55
- });
56
-
57
- it("survives dark mode: navy and orange painted as images too, a light footer, and a dark stylesheet", () => {
58
- // Gmail's apps invert colours but not background images.
59
- expect(email.html).toContain("background-image:linear-gradient(#0b3a66,#0b3a66)");
60
- expect(email.html).toContain("background-image:linear-gradient(#ff6413,#ff6413)");
61
- // No white text on a dark block: Gmail turns it dark. The footer is light.
62
- expect(email.html).toMatch(/class="pe-foot" style="background:#f3f5f8/);
63
- expect(email.html).not.toContain("mix-blend-mode");
64
- // Mail apps that honour prefers-color-scheme get a real dark version.
65
- expect(email.html).toContain('<meta name="color-scheme" content="light dark">');
66
- expect(email.html).toContain("@media (prefers-color-scheme: dark)");
67
- });
68
-
69
- it("describes the logo for readers who see no images, and links the address on purpose", () => {
70
- expect(email.html).toContain('alt="ภาพโลโก้พรรคประชาชน"');
71
- expect(email.html).toContain('<meta name="format-detection" content="telephone=no,address=no,email=no,date=no">');
72
- expect(email.html).toContain("https://www.google.com/maps/search/?api=1&#38;query=");
73
- });
74
-
75
- it("escapes the heading, the content's values and the button, and keeps the link's query intact", () => {
76
- expect(email.html).toContain("ยืนยันการลงทะเบียน &#60;ทดสอบ&#62;");
77
- expect(email.html).toContain("สมหญิง &#60;b&#62;");
78
- expect(email.html).not.toContain("<ทดสอบ>");
79
- expect(email.html).toContain('href="https://my-app.ppleth.ai/events/1?a=1&#38;b=2"');
80
- });
81
-
82
- it("refuses a button link a reader should not follow", () => {
83
- expect(() =>
84
- partyEmail({ heading: "x", content: html`x`, text: "x", button: { label: "x", url: "javascript:alert(1)" } }),
85
- ).toThrow();
86
- });
87
-
88
- it("carries a plain-text part with the message, the link and the contact details", () => {
89
- expect(email.body).toContain("ยืนยันการลงทะเบียน <ทดสอบ>");
90
- expect(email.body).toContain("สวัสดีคุณ สมหญิง <b>");
91
- expect(email.body).toContain("ดูรายละเอียด: https://my-app.ppleth.ai/events/1?a=1&b=2");
92
- expect(email.body).toContain("โทร 02-038-5100");
93
- expect(email.body).toContain("กรุงเทพมหานคร 10240");
94
- });
95
- });
96
-
97
- describe("sendEmail", () => {
98
- it("posts the message to platform/email with the App Key, and reports the answer", async () => {
99
- let seen: { url: string; auth: string | null; json: unknown } | undefined;
100
- const fetchImpl: typeof fetch = async (input, init) => {
101
- const req = new Request(input as RequestInfo, init);
102
- seen = { url: req.url, auth: req.headers.get("authorization"), json: await req.json() };
103
- return Response.json({ sent: [{ to: "somying@example.com", messageId: "m-1" }], rejected: [] });
104
- };
105
- const result = await sendEmail(
106
- { PPLE_SERVICES_URL: "https://services.test", PPLE_APP_KEY: "k", DEV_PERSONA: "none" },
107
- { to: "somying@example.com", subject: "ยืนยัน", html: "<p>x</p>", body: "x" },
108
- fetchImpl,
109
- );
110
- expect(seen).toEqual({
111
- url: "https://services.test/platform/email",
112
- auth: "Bearer k",
113
- json: { to: "somying@example.com", subject: "ยืนยัน", html: "<p>x</p>", body: "x" },
114
- });
115
- expect(result).toEqual({ status: 200, platformError: null, body: { sent: [{ to: "somying@example.com", messageId: "m-1" }], rejected: [] } });
116
- });
117
- });