@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,387 +0,0 @@
1
- // readDataset (issue #270). The harness is the platform: tests see the URL,
2
- // the headers, the page, and the refusal a Builder would observe.
3
-
4
- import { describe, expect, expectTypeOf, it, vi } from "vitest";
5
- import { platformError } from "../src/call-service";
6
- import {
7
- readDataset,
8
- type DatasetFailure,
9
- type DatasetNotModified,
10
- type DatasetObject,
11
- type DatasetPage,
12
- type DatasetSuccess,
13
- } from "../src/read-dataset";
14
- import { makeHarness, serviceRefusal } from "./harness";
15
-
16
- const env = {
17
- DEV_PERSONA: "none",
18
- PPLE_SERVICES_URL: "https://services.test",
19
- PPLE_APP_KEY: "app-key",
20
- };
21
-
22
- const SWEPT = "2026-11-01T00:00:00.000Z";
23
-
24
- describe("readDataset", () => {
25
- it("sends the App Key, the user token and the query, and returns one page", async () => {
26
- let seen: { authorization: string | null; userToken: string | null } | undefined;
27
- const h = makeHarness(
28
- {
29
- services: (req) => {
30
- seen = {
31
- authorization: req.headers.get("authorization"),
32
- userToken: req.headers.get("x-pple-user-token"),
33
- };
34
- return Response.json(
35
- {
36
- objects: [{ id: "branch-a", province: "เชียงใหม่" }],
37
- next: "cursor-1",
38
- sweptAt: SWEPT,
39
- snapshot: 3,
40
- },
41
- { headers: { etag: '"3-abc"' } },
42
- );
43
- },
44
- },
45
- env,
46
- );
47
-
48
- const result = await readDataset(h.testEnv, "branch-list", "offices", {
49
- userToken: "user-token",
50
- filter: { province: "เชียงใหม่", seats: { gte: 4, lt: 9 }, status: ["open", "closed"] },
51
- sort: "-seats",
52
- limit: 2,
53
- fetchImpl: h.fetchImpl,
54
- });
55
-
56
- expect(result).toEqual({
57
- ok: true,
58
- status: 200,
59
- etag: '"3-abc"',
60
- data: {
61
- objects: [{ id: "branch-a", province: "เชียงใหม่" }],
62
- next: "cursor-1",
63
- sweptAt: SWEPT,
64
- snapshot: 3,
65
- },
66
- });
67
- expect(seen).toEqual({ authorization: "Bearer app-key", userToken: "user-token" });
68
- const url = new URL(`https://services.test${h.calls.services[0]?.path ?? ""}`);
69
- expect(url.pathname).toBe("/branch-list/offices");
70
- expect(url.searchParams.get("province")).toBe("เชียงใหม่");
71
- expect(url.searchParams.get("seats[gte]")).toBe("4");
72
- expect(url.searchParams.get("seats[lt]")).toBe("9");
73
- expect(url.searchParams.getAll("status")).toEqual(["open", "closed"]);
74
- expect(url.searchParams.get("sort")).toBe("-seats");
75
- expect(url.searchParams.get("limit")).toBe("2");
76
- expect(h.calls.services[0]?.method).toBe("GET");
77
- expect(h.calls.services[0]?.headers).not.toHaveProperty("authorization");
78
- expect(h.calls.services[0]?.headers).not.toHaveProperty("x-pple-user-token");
79
- });
80
-
81
- it("omits the user token unless one was given, and looks an object up by id", async () => {
82
- let userToken: string | null = "unset";
83
- const h = makeHarness(
84
- {
85
- services: (req) => {
86
- userToken = req.headers.get("x-pple-user-token");
87
- return Response.json({
88
- object: { id: "branch-b", province: "กรุงเทพ" },
89
- sweptAt: SWEPT,
90
- snapshot: 3,
91
- });
92
- },
93
- },
94
- env,
95
- );
96
-
97
- const result = await readDataset(h.testEnv, "branch-list", "offices", {
98
- id: "branch-b",
99
- fetchImpl: h.fetchImpl,
100
- });
101
-
102
- expect(userToken).toBeNull();
103
- expect(result.ok).toBe(true);
104
- if (!result.ok) return;
105
- expect(result.data).toEqual({
106
- object: { id: "branch-b", province: "กรุงเทพ" },
107
- sweptAt: SWEPT,
108
- snapshot: 3,
109
- });
110
- expect(h.calls.services[0]?.path).toBe("/branch-list/offices/branch-b");
111
- });
112
-
113
- it("reads a singleton", async () => {
114
- const h = makeHarness(
115
- {
116
- services: () => Response.json({ object: { title: "สรุปสาขา" }, sweptAt: SWEPT, snapshot: 1 }),
117
- },
118
- env,
119
- );
120
- const result = await readDataset(h.testEnv, "branch-list", "summary", {
121
- shape: "singleton",
122
- fetchImpl: h.fetchImpl,
123
- });
124
- expect(result).toMatchObject({
125
- ok: true,
126
- data: { object: { title: "สรุปสาขา" }, sweptAt: SWEPT, snapshot: 1 },
127
- });
128
- expect(h.calls.services[0]?.path).toBe("/branch-list/summary");
129
- });
130
-
131
- it("reads platform refusals from x-pple-error", async () => {
132
- const codes = [
133
- ["not_found", 404],
134
- ["invalid_query", 400],
135
- ["level_not_met", 403],
136
- ["not_granted", 403],
137
- ["user_token_invalid", 401],
138
- ["renamed", 410],
139
- ["no_mock", 404],
140
- ] as const;
141
- let next = serviceRefusal("not_found", 404);
142
- const h = makeHarness({ services: () => next }, env);
143
-
144
- for (const [code, status] of codes) {
145
- next = serviceRefusal(code, status);
146
- const result = await readDataset(h.testEnv, "branch-list", "offices", {
147
- userToken: "t",
148
- fetchImpl: h.fetchImpl,
149
- });
150
- expect(result).toEqual({ ok: false, status, error: code });
151
- const header = serviceRefusal(code, status);
152
- if (code === "no_mock") expect(platformError(header)).toBeNull();
153
- else expect(platformError(header)).toBe(code);
154
- }
155
- });
156
-
157
- it("follows next and restarts when the snapshot changes", async () => {
158
- const pages: { path: string }[] = [];
159
- const h = makeHarness(
160
- {
161
- services: (req) => {
162
- const url = new URL(req.url);
163
- pages.push({ path: url.pathname + url.search });
164
- const cursor = url.searchParams.get("next");
165
- if (cursor === null && pages.length === 1) {
166
- return Response.json({ objects: [{ id: "a" }], next: "old", sweptAt: SWEPT, snapshot: 1 });
167
- }
168
- if (cursor === "old") {
169
- return Response.json({ objects: [{ id: "b" }], next: null, sweptAt: SWEPT, snapshot: 2 });
170
- }
171
- if (cursor === null) {
172
- return Response.json({ objects: [{ id: "a2" }], next: "new", sweptAt: SWEPT, snapshot: 2 });
173
- }
174
- return Response.json({ objects: [{ id: "b2" }], next: null, sweptAt: SWEPT, snapshot: 2 });
175
- },
176
- },
177
- env,
178
- );
179
-
180
- const result = await readDataset(h.testEnv, "branch-list", "offices", {
181
- limit: 1,
182
- follow: true,
183
- fetchImpl: h.fetchImpl,
184
- });
185
-
186
- expect(result).toMatchObject({
187
- ok: true,
188
- data: {
189
- objects: [{ id: "a2" }, { id: "b2" }],
190
- next: null,
191
- snapshot: 2,
192
- sweptAt: SWEPT,
193
- },
194
- });
195
- expect(pages.map((page) => new URL(`https://services.test${page.path}`).searchParams.get("next"))).toEqual([
196
- null,
197
- "old",
198
- null,
199
- "new",
200
- ]);
201
- });
202
-
203
- it("reads a bare name and a full name, building services.<domain>/<full name>/…", async () => {
204
- const fetchImpl = vi.fn<typeof fetch>(async () =>
205
- Response.json({ objects: [], next: null, sweptAt: null, snapshot: null }),
206
- );
207
- await readDataset(env, "branch-list", "offices", { id: "b-1", fetchImpl });
208
- await readDataset(env, "election/score", "turnout", { fetchImpl });
209
- await readDataset(env, "election/2027/score", "turnout", { id: "u-1", fetchImpl });
210
- expect(fetchImpl.mock.calls.map((call) => String(call[0]))).toEqual([
211
- "https://services.test/branch-list/offices/b-1",
212
- "https://services.test/election/score/turnout",
213
- "https://services.test/election/2027/score/turnout/u-1",
214
- ]);
215
- });
216
-
217
- it("takes platform/<x> as a full name, and rejects any name the validator refuses", async () => {
218
- const fetchImpl = vi.fn<typeof fetch>(async () =>
219
- Response.json({ objects: [], next: null, sweptAt: null, snapshot: null }),
220
- );
221
- await readDataset(env, "platform/turnout", "districts", { fetchImpl });
222
- expect(String(fetchImpl.mock.calls[0]?.[0])).toBe("https://services.test/platform/turnout/districts");
223
-
224
- fetchImpl.mockClear();
225
- await expect(readDataset(env, "Election/score", "offices", { fetchImpl })).rejects.toThrow(
226
- /is not an entry name/,
227
- );
228
- await expect(readDataset(env, "health", "offices", { fetchImpl })).rejects.toThrow(/reserved/);
229
- await expect(readDataset(env, "platform", "offices", { fetchImpl })).rejects.toThrow(
230
- /"platform" belongs to the platform/,
231
- );
232
- expect(fetchImpl).not.toHaveBeenCalled();
233
- });
234
-
235
- it("uses the injected fetch under vitest, including when a persona is set", async () => {
236
- const fetchImpl = vi.fn<typeof fetch>(async () =>
237
- Response.json({ objects: [], next: null, sweptAt: null, snapshot: null }),
238
- );
239
- const result = await readDataset(
240
- { ...env, DEV_PERSONA: "hq-staff", DEV_REGISTRY_ERROR: "not_granted" },
241
- "branch-list",
242
- "offices",
243
- { fetchImpl },
244
- );
245
- expect(result).toMatchObject({ ok: true, data: { objects: [], next: null, sweptAt: null } });
246
- expect(fetchImpl).toHaveBeenCalledOnce();
247
- expect(String(fetchImpl.mock.calls[0]?.[0])).toBe("https://services.test/branch-list/offices");
248
-
249
- await expect(readDataset(env, "branch-list", "offices")).rejects.toThrow(/injected fetch/);
250
- await expect(
251
- readDataset({ DEV_PERSONA: "none" }, "branch-list", "offices", { fetchImpl }),
252
- ).rejects.toThrow(/PPLE_SERVICES_URL/);
253
- });
254
- });
255
-
256
- describe("readDataset with ifNoneMatch", () => {
257
- // The platform answers 304 when the header names the current ETag, and a
258
- // full 200 when it names a stale one (workers/src/services/dataset-read.ts).
259
- const platform = (req: Request) =>
260
- req.headers.get("if-none-match") === '"3-abc"'
261
- ? new Response(null, { status: 304, headers: { etag: '"3-abc"' } })
262
- : Response.json(
263
- { objects: [{ id: "branch-a" }], next: null, sweptAt: SWEPT, snapshot: 4 },
264
- { headers: { etag: '"4-abc"' } },
265
- );
266
-
267
- it("sends If-None-Match and returns the not-modified result on a live 304", async () => {
268
- const h = makeHarness({ services: platform }, env);
269
-
270
- const result = await readDataset(h.testEnv, "branch-list", "offices", {
271
- ifNoneMatch: '"3-abc"',
272
- fetchImpl: h.fetchImpl,
273
- });
274
-
275
- expect(h.calls.services[0]?.headers["if-none-match"]).toBe('"3-abc"');
276
- expect(result).toEqual({ ok: true, status: 304, etag: '"3-abc"', notModified: true });
277
- expect(result).not.toHaveProperty("data");
278
- });
279
-
280
- it("returns the new page when the ETag is stale, with the new etag to send next time", async () => {
281
- const h = makeHarness({ services: platform }, env);
282
-
283
- const result = await readDataset(h.testEnv, "branch-list", "offices", {
284
- ifNoneMatch: '"2-abc"',
285
- fetchImpl: h.fetchImpl,
286
- });
287
-
288
- expect(h.calls.services[0]?.headers["if-none-match"]).toBe('"2-abc"');
289
- expect(result).toEqual({
290
- ok: true,
291
- status: 200,
292
- etag: '"4-abc"',
293
- data: { objects: [{ id: "branch-a" }], next: null, sweptAt: SWEPT, snapshot: 4 },
294
- });
295
- });
296
-
297
- it("works on a singleton and an id lookup, and sends no header when none is given", async () => {
298
- const object = (req: Request) =>
299
- req.headers.get("if-none-match") === '"3-abc"'
300
- ? new Response(null, { status: 304, headers: { etag: '"3-abc"' } })
301
- : Response.json({ object: { id: "x" }, sweptAt: SWEPT, snapshot: 3 }, { headers: { etag: '"3-abc"' } });
302
- const h = makeHarness({ services: object }, env);
303
-
304
- const singleton = await readDataset(h.testEnv, "branch-list", "summary", {
305
- shape: "singleton",
306
- ifNoneMatch: '"3-abc"',
307
- fetchImpl: h.fetchImpl,
308
- });
309
- const lookup = await readDataset(h.testEnv, "branch-list", "offices", {
310
- id: "x",
311
- ifNoneMatch: '"3-abc"',
312
- fetchImpl: h.fetchImpl,
313
- });
314
- const plain = await readDataset(h.testEnv, "branch-list", "offices", { id: "x", fetchImpl: h.fetchImpl });
315
- const unset = await readDataset(h.testEnv, "branch-list", "offices", {
316
- id: "x",
317
- ifNoneMatch: undefined,
318
- fetchImpl: h.fetchImpl,
319
- });
320
-
321
- expect(singleton).toMatchObject({ ok: true, status: 304, notModified: true });
322
- expect(lookup).toMatchObject({ ok: true, status: 304, notModified: true });
323
- expect(plain).toMatchObject({ ok: true, status: 200, data: { object: { id: "x" } } });
324
- expect(unset).toMatchObject({ ok: true, status: 200 });
325
- expect(h.calls.services.map((call) => call.headers["if-none-match"])).toEqual([
326
- '"3-abc"',
327
- '"3-abc"',
328
- undefined,
329
- undefined,
330
- ]);
331
- });
332
-
333
- it("still reports a platform refusal as a refusal", async () => {
334
- const h = makeHarness({ services: () => serviceRefusal("level_not_met", 403) }, env);
335
- const result = await readDataset(h.testEnv, "branch-list", "offices", {
336
- ifNoneMatch: '"3-abc"',
337
- fetchImpl: h.fetchImpl,
338
- });
339
- expect(result).toEqual({ ok: false, status: 403, error: "level_not_met" });
340
- });
341
-
342
- it("does not take a 304 for success when no ifNoneMatch was sent", async () => {
343
- const h = makeHarness({ services: () => new Response(null, { status: 304 }) }, env);
344
- const result = await readDataset(h.testEnv, "branch-list", "offices", { fetchImpl: h.fetchImpl });
345
- expect(result).toEqual({ ok: false, status: 304, error: null });
346
- });
347
-
348
- it("refuses ifNoneMatch together with follow, in the types and at runtime", async () => {
349
- const fetchImpl = vi.fn<typeof fetch>(async () => new Response(null, { status: 304 }));
350
- await expect(
351
- readDataset(env, "branch-list", "offices", {
352
- follow: true,
353
- // @ts-expect-error a walk has one ETag per page
354
- ifNoneMatch: '"3-abc"',
355
- fetchImpl,
356
- }),
357
- ).rejects.toThrow(/ifNoneMatch does not apply with follow/);
358
- expect(fetchImpl).not.toHaveBeenCalled();
359
- });
360
-
361
- it("narrows the not-modified result apart from data and from a refusal", async () => {
362
- // Types only: nothing here is awaited, so no request is made.
363
- const probe = async () => {
364
- const page = await readDataset(env, "branch-list", "offices", { ifNoneMatch: "e", limit: 1 });
365
- const one = await readDataset(env, "branch-list", "offices", { id: "x", ifNoneMatch: "e" });
366
- const single = await readDataset(env, "branch-list", "summary", { shape: "singleton", ifNoneMatch: "e" });
367
-
368
- if (!page.ok) {
369
- expectTypeOf(page).toEqualTypeOf<DatasetFailure>();
370
- } else if (page.notModified) {
371
- expectTypeOf(page).toEqualTypeOf<DatasetNotModified>();
372
- // @ts-expect-error a not-modified result has no data
373
- page.data;
374
- } else {
375
- expectTypeOf(page).toEqualTypeOf<DatasetSuccess<DatasetPage>>();
376
- expectTypeOf(page.data.objects).toEqualTypeOf<Record<string, unknown>[]>();
377
- }
378
- if (one.ok && !one.notModified) expectTypeOf(one.data).toEqualTypeOf<DatasetObject>();
379
- if (single.ok && single.notModified) expectTypeOf(single.status).toEqualTypeOf<304>();
380
-
381
- // Without ifNoneMatch there is nothing to narrow: `data` is always there on success.
382
- const plain = await readDataset(env, "branch-list", "offices", { limit: 1 });
383
- if (plain.ok) expectTypeOf(plain.data).toEqualTypeOf<DatasetPage>();
384
- };
385
- expectTypeOf(probe).toBeFunction();
386
- });
387
- });
@@ -1,166 +0,0 @@
1
- // serviceCall (ADR-0024). The Identity header is base64url of the UTF-8 JSON,
2
- // so a Thai name round-trips. encodeIdentityHeader is the one encoder the
3
- // dev wrapper and callRegistry share; serviceCall undoes it.
4
-
5
- import { describe, expect, it } from "vitest";
6
- import type { Identity } from "../shared/api";
7
- import { encodeIdentityHeader } from "../src/identity-header";
8
- import worker from "../src/index";
9
- import { serviceCall } from "../src/registry";
10
-
11
- const IDENTITY: Identity = {
12
- actor: {
13
- sub: "u-delegate",
14
- name: "ชื่อนกทดสอบ",
15
- mobile: "0810000014",
16
- kyc: {
17
- status: "verified",
18
- verified: true,
19
- verifiedAt: "2024-04-01T00:00:00.000Z",
20
- source: "membership",
21
- },
22
- membership: { memberSince: "2020-05-01" },
23
- },
24
- effective: {
25
- isDelegated: true,
26
- sub: "347513243977122153",
27
- activeRole: "province",
28
- eligibleRoles: ["province"],
29
- provinces: ["เชียงใหม่"],
30
- extraRoles: ["exco", "province_co"],
31
- },
32
- };
33
-
34
- describe("serviceCall", () => {
35
- it("round-trips the shared encoder, including a Thai name", () => {
36
- const encoded = encodeIdentityHeader(IDENTITY);
37
- expect(encoded).toMatch(/^[A-Za-z0-9_-]+$/);
38
- expect(encoded).not.toContain("ชื่อ");
39
-
40
- const call = serviceCall(
41
- new Request("https://provider.invalid/offices", {
42
- headers: {
43
- "x-pple-consumer": "brave-purple-lion",
44
- "x-pple-entry": "calm-amber-fox/branch-list",
45
- "x-pple-identity": encoded,
46
- "x-pple-user-token": "raw-token-must-not-surface",
47
- authorization: "Bearer raw-token-must-not-surface",
48
- },
49
- }),
50
- );
51
-
52
- expect(call).toEqual({
53
- consumer: "brave-purple-lion",
54
- entry: "calm-amber-fox/branch-list",
55
- identity: IDENTITY,
56
- });
57
- expect(JSON.stringify(call)).not.toContain("raw-token-must-not-surface");
58
- });
59
-
60
- it("round-trips a null effective sub", () => {
61
- const identity: Identity = {
62
- ...IDENTITY,
63
- effective: { ...IDENTITY.effective, sub: null },
64
- };
65
- const call = serviceCall(
66
- new Request("https://provider.invalid/", {
67
- headers: {
68
- "x-pple-consumer": "brave-purple-lion",
69
- "x-pple-entry": "calm-amber-fox/branch-list",
70
- "x-pple-identity": encodeIdentityHeader(identity),
71
- },
72
- }),
73
- );
74
- expect(call?.identity).toEqual(identity);
75
- });
76
-
77
- it("treats an Identity header with no effective sub as no Identity", () => {
78
- const effective = {
79
- isDelegated: IDENTITY.effective.isDelegated,
80
- activeRole: IDENTITY.effective.activeRole,
81
- eligibleRoles: IDENTITY.effective.eligibleRoles,
82
- provinces: IDENTITY.effective.provinces,
83
- extraRoles: IDENTITY.effective.extraRoles,
84
- };
85
- const call = serviceCall(
86
- new Request("https://provider.invalid/", {
87
- headers: {
88
- "x-pple-consumer": "brave-purple-lion",
89
- "x-pple-entry": "calm-amber-fox/branch-list",
90
- "x-pple-identity": encodeIdentityHeader({ actor: IDENTITY.actor, effective } as Identity),
91
- },
92
- }),
93
- );
94
- expect(call?.identity).toBeNull();
95
- });
96
-
97
- it("treats an Identity header with no membership as no Identity", () => {
98
- const stale = {
99
- actor: {
100
- sub: IDENTITY.actor.sub,
101
- name: IDENTITY.actor.name,
102
- mobile: IDENTITY.actor.mobile,
103
- kyc: IDENTITY.actor.kyc,
104
- },
105
- effective: IDENTITY.effective,
106
- };
107
- const call = serviceCall(
108
- new Request("https://provider.invalid/", {
109
- headers: {
110
- "x-pple-consumer": "brave-purple-lion",
111
- "x-pple-entry": "calm-amber-fox/branch-list",
112
- "x-pple-identity": encodeIdentityHeader(stale as Identity),
113
- },
114
- }),
115
- );
116
- expect(call?.identity).toBeNull();
117
- });
118
-
119
- it("returns the call with no Identity when the entry declares userToken none", () => {
120
- expect(
121
- serviceCall(
122
- new Request("https://provider.invalid/", {
123
- headers: {
124
- "x-pple-consumer": "brave-purple-lion",
125
- "x-pple-entry": "calm-amber-fox/branch-list",
126
- "x-pple-user-token": "still-not-a-fact",
127
- },
128
- }),
129
- ),
130
- ).toEqual({
131
- consumer: "brave-purple-lion",
132
- entry: "calm-amber-fox/branch-list",
133
- identity: null,
134
- });
135
- });
136
-
137
- it("returns null when the request is not a platform registry call", () => {
138
- expect(serviceCall(new Request("https://app.example/api/notes"))).toBeNull();
139
- expect(
140
- serviceCall(new Request("https://app.example/api/notes", { headers: { "x-pple-consumer": "only-one" } })),
141
- ).toBeNull();
142
- });
143
-
144
- it("treats an identity header that is not the encoding as no Identity", () => {
145
- expect(
146
- serviceCall(
147
- new Request("https://provider.invalid/", {
148
- headers: {
149
- "x-pple-consumer": "brave-purple-lion",
150
- "x-pple-entry": "calm-amber-fox/branch-list",
151
- "x-pple-identity": "not-json",
152
- },
153
- }),
154
- ),
155
- ).toEqual({
156
- consumer: "brave-purple-lion",
157
- entry: "calm-amber-fox/branch-list",
158
- identity: null,
159
- });
160
- });
161
-
162
- it("puts a registry slot on the Worker default export", () => {
163
- expect(worker.registry).toEqual({});
164
- expect(typeof worker.fetch).toBe("function");
165
- });
166
- });