@pplethai/cli 0.9.0 → 0.11.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 (38) hide show
  1. package/dist/bin.js +128 -125
  2. package/package.json +1 -1
  3. package/templates/golden-path/.dev.vars.example +4 -3
  4. package/templates/golden-path/CLAUDE.md +13 -6
  5. package/templates/golden-path/README.md +5 -4
  6. package/templates/golden-path/src/call-service.ts +17 -10
  7. package/templates/golden-path/src/dev-call-service.ts +21 -20
  8. package/templates/golden-path/src/dev-read-dataset.ts +55 -40
  9. package/templates/golden-path/src/dev-registry.ts +25 -7
  10. package/templates/golden-path/src/entry-name.ts +114 -0
  11. package/templates/golden-path/src/env.ts +2 -2
  12. package/templates/golden-path/src/read-dataset.ts +130 -16
  13. package/templates/golden-path/src/registry.ts +1 -1
  14. package/templates/golden-path/test/call-service.test.ts +118 -8
  15. package/templates/golden-path/test/dev-call-service.test.ts +201 -25
  16. package/templates/golden-path/test/dev-read-dataset.test.ts +122 -13
  17. package/templates/golden-path/test/dev-registry.test.ts +35 -6
  18. package/templates/golden-path/test/fixtures/overlay/catalog/entry.md +8 -0
  19. package/templates/golden-path/test/fixtures/overlay/election/2027/score/entry.md +11 -0
  20. package/templates/golden-path/test/fixtures/overlay/election/2027/score/mocks/responses.json +7 -0
  21. package/templates/golden-path/test/fixtures/overlay/election/results/entry.md +24 -0
  22. package/templates/golden-path/test/fixtures/overlay/election/results/mocks/turnout.json +1 -0
  23. package/templates/golden-path/test/fixtures/overlay/platform/sms/entry.md +10 -0
  24. package/templates/golden-path/test/fixtures/overlay/platform/sms/mocks/responses.json +7 -0
  25. package/templates/golden-path/test/fixtures/overlay/score/entry.md +10 -0
  26. package/templates/golden-path/test/fixtures/overlay/score/mocks/responses.json +7 -0
  27. package/templates/golden-path/test/harness.ts +1 -1
  28. package/templates/golden-path/test/read-dataset.test.ts +187 -14
  29. package/templates/golden-path/test/fixtures/overlay/shop/catalog/entry.md +0 -8
  30. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/entry.md +0 -0
  31. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/audience.json +0 -0
  32. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/named.json +0 -0
  33. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/offices.json +0 -0
  34. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/roster.json +0 -0
  35. /package/templates/golden-path/test/fixtures/overlay/{directory/branches → branch-list}/mocks/summary.json +0 -0
  36. /package/templates/golden-path/test/fixtures/overlay/{shop/catalog → catalog}/mocks/responses.json +0 -0
  37. /package/templates/golden-path/test/fixtures/overlay/{shop/orders → orders}/entry.md +0 -0
  38. /package/templates/golden-path/test/fixtures/overlay/{shop/orders → orders}/mocks/responses.json +0 -0
@@ -14,9 +14,10 @@ const overlay: Record<string, unknown> = import.meta.glob("./fixtures/overlay/**
14
14
 
15
15
  beforeAll(() => {
16
16
  const keys = Object.keys(overlay).join("\n");
17
- expect(keys).toContain("shop/orders/mocks/responses.json");
18
- expect(keys).toContain("shop/orders/entry.md");
17
+ expect(keys).toContain("orders/mocks/responses.json");
18
+ expect(keys).toContain("orders/entry.md");
19
19
  expect(keys).toContain("platform/legal-identity/mocks/responses.json");
20
+ expect(keys).toContain("election/2027/score/entry.md");
20
21
  });
21
22
 
22
23
  const cases = [
@@ -71,9 +72,9 @@ describe("matchMockCase", () => {
71
72
 
72
73
  describe("overlay fixture", () => {
73
74
  it("matches method, path and persona, and marks the mock", async () => {
74
- const res = await call({ entry: "shop/orders", path: "/items/7?limit=2", method: "GET", persona: "hq-staff", userToken: "t" });
75
+ const res = await call({ entry: "orders", path: "/items/7?limit=2", method: "GET", persona: "hq-staff", userToken: "t" });
75
76
  expect(res.status).toBe(200);
76
- expect(res.headers.get("x-pple-mock")).toBe("shop/orders");
77
+ expect(res.headers.get("x-pple-mock")).toBe("orders");
77
78
  expect(res.headers.get("x-pple-error")).toBeNull();
78
79
  expect(res.headers.get("x-shop")).toBe("hq");
79
80
  expect(platformError(res)).toBeNull();
@@ -81,39 +82,39 @@ describe("overlay fixture", () => {
81
82
  });
82
83
 
83
84
  it("uses the first match, then an unscoped case for a persona the Builder added", async () => {
84
- const listed = await call({ entry: "shop/orders", path: "/items/7", method: "GET", persona: "delegate", userToken: "t" });
85
+ const listed = await call({ entry: "orders", path: "/items/7", method: "GET", persona: "delegate", userToken: "t" });
85
86
  expect(await listed.json()).toEqual({ item: "listed" });
86
87
 
87
- const added = await call({ entry: "shop/orders", path: "/items/7", method: "GET", persona: "intern", userToken: "t" });
88
- expect(added.headers.get("x-pple-mock")).toBe("shop/orders");
88
+ const added = await call({ entry: "orders", path: "/items/7", method: "GET", persona: "intern", userToken: "t" });
89
+ expect(added.headers.get("x-pple-mock")).toBe("orders");
89
90
  expect(await added.json()).toEqual({ item: "open" });
90
91
  });
91
92
 
92
93
  it("treats / as the entry root and sends a string body as text", async () => {
93
- const root = await call({ entry: "shop/orders", path: "", method: "POST", persona: "hq-staff", userToken: "t" });
94
+ const root = await call({ entry: "orders", path: "", method: "POST", persona: "hq-staff", userToken: "t" });
94
95
  expect(root.status).toBe(201);
95
- expect(root.headers.get("x-pple-mock")).toBe("shop/orders");
96
+ expect(root.headers.get("x-pple-mock")).toBe("orders");
96
97
  expect(await root.json()).toEqual({ created: true });
97
98
 
98
- const longer = await call({ entry: "shop/orders", path: "/created", method: "POST", persona: "hq-staff", userToken: "t" });
99
+ const longer = await call({ entry: "orders", path: "/created", method: "POST", persona: "hq-staff", userToken: "t" });
99
100
  expect(longer.status).toBe(404);
100
101
  expect(longer.headers.get("x-pple-error")).toBe("no_mock");
101
102
 
102
- const files = await call({ entry: "shop/orders", path: "/files/a/b", method: "GET", persona: "intern", userToken: "t" });
103
+ const files = await call({ entry: "orders", path: "/files/a/b", method: "GET", persona: "intern", userToken: "t" });
103
104
  expect(files.headers.get("content-type")).toContain("text/plain");
104
105
  expect(await files.text()).toBe("listed");
105
106
  });
106
107
 
107
108
  it("answers a userToken none entry with no token", async () => {
108
- const res = await call({ entry: "shop/catalog", path: "/search", method: "GET", persona: "hq-staff" });
109
+ const res = await call({ entry: "catalog", path: "/search", method: "GET", persona: "hq-staff" });
109
110
  expect(res.status).toBe(200);
110
- expect(res.headers.get("x-pple-mock")).toBe("shop/catalog");
111
+ expect(res.headers.get("x-pple-mock")).toBe("catalog");
111
112
  expect(await res.json()).toEqual({ ok: true });
112
113
  });
113
114
 
114
115
  it("refuses a required entry that was called with no token", async () => {
115
116
  for (const userToken of [undefined, "", " "]) {
116
- const res = await call({ entry: "shop/orders", path: "/items/7", method: "GET", persona: "hq-staff", userToken });
117
+ const res = await call({ entry: "orders", path: "/items/7", method: "GET", persona: "hq-staff", userToken });
117
118
  expect(res.status).toBe(401);
118
119
  expect(res.headers.get("x-pple-error")).toBe("user_token_invalid");
119
120
  expect(res.headers.get("x-pple-mock")).toBeNull();
@@ -123,7 +124,7 @@ describe("overlay fixture", () => {
123
124
  });
124
125
 
125
126
  it("says when the entry is missing, and when the route is", async () => {
126
- const missing = await call({ entry: "shop/missing", path: "/items/1", method: "GET", persona: "hq-staff", userToken: "t" });
127
+ const missing = await call({ entry: "missing", path: "/items/1", method: "GET", persona: "hq-staff", userToken: "t" });
127
128
  expect(missing.status).toBe(404);
128
129
  expect(missing.headers.get("x-pple-error")).toBe("no_mock");
129
130
  expect(missing.headers.get("x-pple-mock")).toBeNull();
@@ -134,7 +135,7 @@ describe("overlay fixture", () => {
134
135
  expect(missingBody.message).toContain(".pple/registry/");
135
136
  expect(missingBody.message).toMatch(/[\u0E00-\u0E7F]/);
136
137
 
137
- const route = await call({ entry: "shop/orders", path: "/nope", method: "GET", persona: "hq-staff", userToken: "t" });
138
+ const route = await call({ entry: "orders", path: "/nope", method: "GET", persona: "hq-staff", userToken: "t" });
138
139
  const routeBody = await route.json<{ error: string; message: string }>();
139
140
  expect(route.status).toBe(404);
140
141
  expect(route.headers.get("x-pple-error")).toBe("no_mock");
@@ -176,6 +177,68 @@ describe("overlay fixture", () => {
176
177
  });
177
178
  });
178
179
 
180
+ describe("offline answers from nested overlay folders", () => {
181
+ it("answers election/2027/score from .pple/registry/election/2027/score/", async () => {
182
+ const res = await call({
183
+ entry: "election/2027/score",
184
+ path: "/units/7",
185
+ method: "GET",
186
+ persona: "hq-staff",
187
+ });
188
+ expect(res.status).toBe(200);
189
+ expect(res.headers.get("x-pple-mock")).toBe("election/2027/score");
190
+ expect(await res.json()).toEqual({ from: "election/2027/score" });
191
+ });
192
+
193
+ it("answers platform/legal-identity and platform/sms from .pple/registry/platform/<name>/", async () => {
194
+ const legal = await call({
195
+ entry: "platform/legal-identity",
196
+ path: "",
197
+ method: "POST",
198
+ persona: "hq-staff",
199
+ userToken: "t",
200
+ });
201
+ expect(legal.headers.get("x-pple-mock")).toBe("platform/legal-identity");
202
+ expect(await legal.json()).toEqual({ source: "membership" });
203
+
204
+ const sms = await call({ entry: "platform/sms", path: "/", method: "POST", persona: "hq-staff" });
205
+ expect(sms.headers.get("x-pple-mock")).toBe("platform/sms");
206
+ expect(await sms.json()).toEqual({ sent: 1, rejected: [], credits: 1 });
207
+
208
+ // The platform has no bare names: the folder's own name is no entry.
209
+ for (const entry of ["legal-identity", "sms"]) {
210
+ const bare = await call({ entry, path: "/", method: "POST", persona: "hq-staff", userToken: "t" });
211
+ expect(bare.status).toBe(404);
212
+ expect(bare.headers.get("x-pple-error")).toBe("no_mock");
213
+ }
214
+ });
215
+
216
+ it("keeps a bare entry apart from a nested one that ends the same way", async () => {
217
+ const res = await call({ entry: "score", path: "/units/7", persona: "hq-staff" });
218
+ expect(await res.json()).toEqual({ from: "score" });
219
+ });
220
+
221
+ it("does not answer a name that is only the tail of a nested path", async () => {
222
+ for (const entry of ["2027/score", "election/score"]) {
223
+ const res = await call({ entry, path: "/units/7", persona: "hq-staff" });
224
+ expect(res.status).toBe(404);
225
+ expect(res.headers.get("x-pple-error")).toBe("no_mock");
226
+ expect(((await res.json()) as { message: string }).message).toContain(`ไม่มี ${entry} ใน .pple/registry/`);
227
+ }
228
+ });
229
+
230
+ it("reads the real overlay path, which has no fixture root", () => {
231
+ const res = answerFromOverlay(
232
+ {
233
+ "../.pple/registry/election/2027/score/entry.md": "---\nkind: service\nuserToken: none\n---\n",
234
+ "../.pple/registry/election/2027/score/mocks/responses.json": '[{ "body": "real" }]',
235
+ },
236
+ { entry: "election/2027/score", path: "/", persona: "hq-staff" },
237
+ );
238
+ return expect(res.text()).resolves.toBe("real");
239
+ });
240
+ });
241
+
179
242
  describe("DEV_REGISTRY_ERROR", () => {
180
243
  const statuses: [string, number][] = [
181
244
  ["invalid_app_key", 401],
@@ -190,11 +253,12 @@ describe("DEV_REGISTRY_ERROR", () => {
190
253
  ["not_found", 404],
191
254
  ["invalid_query", 400],
192
255
  ["level_not_met", 403],
256
+ ["renamed", 410],
193
257
  ];
194
258
 
195
259
  it.each(statuses)("global %s is %i", async (code, status) => {
196
260
  const res = await call({
197
- entry: "shop/orders",
261
+ entry: "orders",
198
262
  path: "/items/1",
199
263
  method: "GET",
200
264
  persona: "hq-staff",
@@ -209,9 +273,9 @@ describe("DEV_REGISTRY_ERROR", () => {
209
273
  });
210
274
 
211
275
  it("lets a per-entry code win over a global one, in either order", async () => {
212
- for (const spec of ["not_granted, shop/orders=provider_timeout", "shop/orders = provider_timeout, not_granted"]) {
276
+ for (const spec of ["not_granted, orders=provider_timeout", "orders = provider_timeout, not_granted"]) {
213
277
  const orders = await call({
214
- entry: "shop/orders",
278
+ entry: "orders",
215
279
  path: "/items/1",
216
280
  method: "GET",
217
281
  persona: "hq-staff",
@@ -222,7 +286,7 @@ describe("DEV_REGISTRY_ERROR", () => {
222
286
  expect(orders.headers.get("x-pple-error")).toBe("provider_timeout");
223
287
 
224
288
  const catalog = await call({
225
- entry: "shop/catalog",
289
+ entry: "catalog",
226
290
  path: "/search",
227
291
  method: "GET",
228
292
  persona: "hq-staff",
@@ -233,11 +297,123 @@ describe("DEV_REGISTRY_ERROR", () => {
233
297
  }
234
298
  });
235
299
 
300
+ it("takes renamed for one full name, as election/score=renamed", async () => {
301
+ const spec = "election/score=renamed";
302
+ const hit = await call({
303
+ entry: "election/score",
304
+ path: "/results",
305
+ method: "GET",
306
+ persona: "hq-staff",
307
+ userToken: "t",
308
+ devRegistryError: spec,
309
+ });
310
+ expect(hit.status).toBe(410);
311
+ expect(platformError(hit)).toBe("renamed");
312
+
313
+ const other = await call({
314
+ entry: "election/turnout",
315
+ path: "/results",
316
+ method: "GET",
317
+ persona: "hq-staff",
318
+ userToken: "t",
319
+ devRegistryError: spec,
320
+ });
321
+ expect(platformError(other)).toBeNull();
322
+ });
323
+
324
+ it("takes a full name: election/2027/score=503 fails only that entry", async () => {
325
+ const spec = "election/2027/score=provider_unavailable";
326
+ const nested = await call({
327
+ entry: "election/2027/score",
328
+ path: "/units/1",
329
+ method: "GET",
330
+ persona: "hq-staff",
331
+ devRegistryError: spec,
332
+ });
333
+ expect(nested.status).toBe(502);
334
+ expect(nested.headers.get("x-pple-error")).toBe("provider_unavailable");
335
+
336
+ // The same last segment, bare, is a different entry.
337
+ const bare = await call({
338
+ entry: "score",
339
+ path: "/units/1",
340
+ method: "GET",
341
+ persona: "hq-staff",
342
+ devRegistryError: spec,
343
+ });
344
+ expect(bare.status).toBe(200);
345
+ const orders = await call({
346
+ entry: "orders",
347
+ path: "/items/1",
348
+ method: "GET",
349
+ persona: "hq-staff",
350
+ userToken: "t",
351
+ devRegistryError: spec,
352
+ });
353
+ expect(orders.status).toBe(200);
354
+ });
355
+
356
+ it("answers 503 for election/2027/score=503-style specs written with the platform code", async () => {
357
+ const res = await call({
358
+ entry: "election/2027/score",
359
+ path: "/units/1",
360
+ persona: "hq-staff",
361
+ devRegistryError: "election/2027/score=identity_unavailable, orders=not_granted",
362
+ });
363
+ expect(res.status).toBe(503);
364
+ });
365
+
366
+ it("applies a platform/<name> rule to that platform entry only", async () => {
367
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
368
+ try {
369
+ const forced = await call({
370
+ entry: "platform/sms",
371
+ path: "/",
372
+ method: "POST",
373
+ persona: "hq-staff",
374
+ devRegistryError: "platform/sms=provider_timeout",
375
+ });
376
+ expect(forced.status).toBe(504);
377
+ expect(forced.headers.get("x-pple-error")).toBe("provider_timeout");
378
+
379
+ const other = await call({
380
+ entry: "orders",
381
+ path: "/items/1",
382
+ method: "GET",
383
+ persona: "hq-staff",
384
+ userToken: "t",
385
+ devRegistryError: "platform/sms=provider_timeout",
386
+ });
387
+ expect(other.status).toBe(200);
388
+ expect(warn).not.toHaveBeenCalled();
389
+ } finally {
390
+ warn.mockRestore();
391
+ }
392
+ });
393
+
394
+ it("ignores a rule whose name is not an entry name", async () => {
395
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
396
+ try {
397
+ const res = await call({
398
+ entry: "orders",
399
+ path: "/items/1",
400
+ method: "GET",
401
+ persona: "hq-staff",
402
+ userToken: "t",
403
+ devRegistryError: "Orders/x=not_granted",
404
+ });
405
+ expect(res.status).toBe(200);
406
+ expect(warn).toHaveBeenCalled();
407
+ } finally {
408
+ warn.mockRestore();
409
+ }
410
+ });
411
+
236
412
  it("ignores an unknown code and still applies a global one", async () => {
237
413
  const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
238
414
  try {
239
415
  const mocked = await call({
240
- entry: "shop/catalog",
416
+ entry: "catalog",
241
417
  path: "/search",
242
418
  method: "GET",
243
419
  persona: "hq-staff",
@@ -247,11 +423,11 @@ describe("DEV_REGISTRY_ERROR", () => {
247
423
  expect(await mocked.json()).toEqual({ ok: true });
248
424
 
249
425
  const forced = await call({
250
- entry: "shop/orders",
426
+ entry: "orders",
251
427
  path: "/items/1",
252
428
  method: "GET",
253
429
  persona: "hq-staff",
254
- devRegistryError: "shop/orders=not_a_code, needs_beta",
430
+ devRegistryError: "orders=not_a_code, needs_beta",
255
431
  });
256
432
  expect(forced.status).toBe(403);
257
433
  expect(forced.headers.get("x-pple-error")).toBe("needs_beta");
@@ -263,7 +439,7 @@ describe("DEV_REGISTRY_ERROR", () => {
263
439
 
264
440
  it("applies before the missing-token refusal", async () => {
265
441
  const res = await call({
266
- entry: "shop/orders",
442
+ entry: "orders",
267
443
  path: "/items/1",
268
444
  method: "GET",
269
445
  persona: "hq-staff",
@@ -275,5 +451,5 @@ describe("DEV_REGISTRY_ERROR", () => {
275
451
  });
276
452
 
277
453
  function call(input: OverlayCall): Promise<Response> {
278
- return Promise.resolve(answerFromOverlay(overlay, input));
454
+ return Promise.resolve(answerFromOverlay(overlay, { overlayRoot: "fixtures/overlay/", ...input }));
279
455
  }
@@ -20,9 +20,10 @@ const writtenAt: Record<string, unknown> = import.meta.glob("./fixtures/overlay/
20
20
 
21
21
  beforeAll(() => {
22
22
  const keys = Object.keys(overlay).join("\n");
23
- expect(keys).toContain("directory/branches/entry.md");
24
- expect(keys).toContain("directory/branches/mocks/offices.json");
25
- expect(keys).toContain("directory/branches/mocks/summary.json");
23
+ expect(keys).toContain("branch-list/entry.md");
24
+ expect(keys).toContain("branch-list/mocks/offices.json");
25
+ expect(keys).toContain("branch-list/mocks/summary.json");
26
+ expect(keys).toContain("election/results/mocks/turnout.json");
26
27
  const offices = timeFor("mocks/offices.json");
27
28
  expect(offices).toMatch(/^\d{4}-\d{2}-\d{2}T/);
28
29
  });
@@ -37,7 +38,7 @@ describe("offline readDataset", () => {
37
38
  ],
38
39
  });
39
40
  expect(filtered.status).toBe(200);
40
- expect(filtered.headers.get("x-pple-mock")).toBe("directory/branches");
41
+ expect(filtered.headers.get("x-pple-mock")).toBe("branch-list");
41
42
  expect(filtered.headers.get("x-pple-error")).toBeNull();
42
43
  expect(platformError(filtered)).toBeNull();
43
44
  const filteredBody = await filtered.json<{ objects: { id: string }[]; next: null; sweptAt: string; snapshot: number }>();
@@ -75,8 +76,8 @@ describe("offline readDataset", () => {
75
76
  const res = await read(
76
77
  { objectType: "offices" },
77
78
  {
78
- "/virtual/directory/branches/mocks/offices.json": "2024-05-01T00:00:00.000Z",
79
- "/virtual/directory/branches/entry.md": "2020-01-01T00:00:00.000Z",
79
+ "/fixtures/overlay/branch-list/mocks/offices.json": "2024-05-01T00:00:00.000Z",
80
+ "/fixtures/overlay/branch-list/entry.md": "2020-01-01T00:00:00.000Z",
80
81
  },
81
82
  );
82
83
  const body = await res.json<{ sweptAt: string }>();
@@ -104,7 +105,7 @@ describe("offline readDataset", () => {
104
105
 
105
106
  const summary = await read({ objectType: "summary", persona: "member", userToken: undefined });
106
107
  expect(summary.status).toBe(200);
107
- expect(summary.headers.get("x-pple-mock")).toBe("directory/branches");
108
+ expect(summary.headers.get("x-pple-mock")).toBe("branch-list");
108
109
  expect(await summary.json()).toMatchObject({ object: { title: "สรุปสาขา" } });
109
110
 
110
111
  const summaryQuery = await read({ objectType: "summary", search: [["limit", "1"]] });
@@ -171,7 +172,7 @@ describe("offline readDataset", () => {
171
172
  });
172
173
 
173
174
  it("says no_mock for a missing entry or mock, and not_found for an unknown type", async () => {
174
- const missing = await read({ entry: "directory/missing", objectType: "offices" });
175
+ const missing = await read({ entry: "missing", objectType: "offices" });
175
176
  expect(missing.status).toBe(404);
176
177
  expect(missing.headers.get("x-pple-error")).toBe("no_mock");
177
178
  expect(missing.headers.get("x-pple-mock")).toBeNull();
@@ -197,7 +198,44 @@ describe("offline readDataset", () => {
197
198
  });
198
199
  });
199
200
 
201
+ describe("offline readDataset from nested overlay folders", () => {
202
+ it("reads election/results from .pple/registry/election/results/", async () => {
203
+ const res = await read({ entry: "election/results", objectType: "turnout" });
204
+ expect(res.status).toBe(200);
205
+ expect(res.headers.get("x-pple-mock")).toBe("election/results");
206
+ const body = (await res.json()) as { objects: { id: string }[]; sweptAt: string };
207
+ expect(body.objects.map((o) => o.id)).toEqual(["a", "b"]);
208
+ expect(body.sweptAt).toBe(timeFor("election/results/mocks/turnout.json"));
209
+ });
210
+
211
+ it("does not answer a name that is only the tail of a nested path", async () => {
212
+ for (const entry of ["results", "election/branch-list"]) {
213
+ const res = await read({ entry, objectType: "turnout" });
214
+ expect(res.status).toBe(404);
215
+ expect(res.headers.get("x-pple-error")).toBe("no_mock");
216
+ }
217
+ });
218
+
219
+ it("fails only the entry a full-name DEV_REGISTRY_ERROR names", async () => {
220
+ const spec = "election/results=provider_unavailable";
221
+ const hit = await read({ entry: "election/results", objectType: "turnout", devRegistryError: spec });
222
+ expect(hit.status).toBe(502);
223
+ const other = await read({ objectType: "summary", devRegistryError: spec });
224
+ expect(other.status).toBe(200);
225
+ });
226
+ });
227
+
200
228
  describe("DEV_REGISTRY_ERROR", () => {
229
+ it("answers `renamed` as 410 for the full name it names, and only that one", async () => {
230
+ const spec = "election/results=renamed";
231
+ const hit = await read({ entry: "election/results", objectType: "turnout", devRegistryError: spec });
232
+ expect(hit.status).toBe(410);
233
+ expect(hit.headers.get("x-pple-error")).toBe("renamed");
234
+ expect(platformError(hit)).toBe("renamed");
235
+ const other = await read({ objectType: "summary", devRegistryError: spec });
236
+ expect(other.status).toBe(200);
237
+ });
238
+
201
239
  it("returns the platform refusal before the token and the level", async () => {
202
240
  const res = await read({
203
241
  objectType: "roster",
@@ -214,15 +252,15 @@ describe("DEV_REGISTRY_ERROR", () => {
214
252
  it("lets a per-entry code win, and accepts a dataset refusal code", async () => {
215
253
  const specific = await read({
216
254
  objectType: "offices",
217
- devRegistryError: "not_granted, directory/branches=needs_beta",
255
+ devRegistryError: "not_granted, branch-list=needs_beta",
218
256
  });
219
257
  expect(specific.status).toBe(403);
220
258
  expect(specific.headers.get("x-pple-error")).toBe("needs_beta");
221
259
 
222
260
  const other = await read({
223
- entry: "shop/orders",
261
+ entry: "orders",
224
262
  objectType: "offices",
225
- devRegistryError: "not_granted, directory/branches=needs_beta",
263
+ devRegistryError: "not_granted, branch-list=needs_beta",
226
264
  });
227
265
  expect(other.headers.get("x-pple-error")).toBe("not_granted");
228
266
 
@@ -234,7 +272,7 @@ describe("DEV_REGISTRY_ERROR", () => {
234
272
  try {
235
273
  const ignored = await read({
236
274
  objectType: "summary",
237
- devRegistryError: "directory/branches=not_a_code, not_listed",
275
+ devRegistryError: "branch-list=not_a_code, not_listed",
238
276
  });
239
277
  expect(ignored.status).toBe(404);
240
278
  expect(ignored.headers.get("x-pple-error")).toBe("not_listed");
@@ -244,6 +282,76 @@ describe("DEV_REGISTRY_ERROR", () => {
244
282
  });
245
283
  });
246
284
 
285
+ describe("offline readDataset with ifNoneMatch", () => {
286
+ const queries: { name: string; call: Partial<OfflineDatasetCall> & { objectType: string } }[] = [
287
+ { name: "a page", call: { objectType: "offices", search: [["sort", "-seats"], ["limit", "2"]] } },
288
+ { name: "an id lookup", call: { objectType: "offices", id: "branch-b" } },
289
+ { name: "a singleton", call: { objectType: "summary" } },
290
+ ];
291
+
292
+ it.each(queries)("answers 304 for $name when the ETag matches, and 200 when it does not", async ({ call }) => {
293
+ const fresh = await read(call);
294
+ const tag = fresh.headers.get("etag") ?? "";
295
+ expect(tag).toMatch(/^"\d+-\w+"$/);
296
+
297
+ // Stable: the same read gives the same ETag, even with a different write time.
298
+ const again = await read(call, { "/fixtures/overlay/branch-list/mocks/offices.json": "2030-01-01T00:00:00.000Z" });
299
+ expect(again.headers.get("etag")).toBe(tag);
300
+
301
+ const hit = await read({ ...call, ifNoneMatch: tag });
302
+ expect(hit.status).toBe(304);
303
+ expect(hit.headers.get("etag")).toBe(tag);
304
+ expect(hit.headers.get("x-pple-mock")).toBe("branch-list");
305
+ expect(await hit.text()).toBe("");
306
+
307
+ for (const stale of ['"1-stale"', '"0-0"']) {
308
+ const miss = await read({ ...call, ifNoneMatch: stale });
309
+ expect(miss.status).toBe(200);
310
+ expect(miss.headers.get("etag")).toBe(tag);
311
+ }
312
+ // The platform's match rule: a comma list, `W/` and quote marks ignored.
313
+ expect((await read({ ...call, ifNoneMatch: `"1-stale", W/${tag}` })).status).toBe(304);
314
+ });
315
+
316
+ it("gives a page a different ETag for a different query, so one ETag does not cover both", async () => {
317
+ const a = await read({ objectType: "offices", search: [["limit", "1"]] });
318
+ const b = await read({ objectType: "offices", search: [["limit", "2"]] });
319
+ expect(a.headers.get("etag")).not.toBe(b.headers.get("etag"));
320
+ const crossed = await read({ objectType: "offices", search: [["limit", "2"]], ifNoneMatch: a.headers.get("etag") ?? "" });
321
+ expect(crossed.status).toBe(200);
322
+ });
323
+
324
+ it("still refuses before it compares: a current ETag does not hide a refusal", async () => {
325
+ const tag = (await read({ objectType: "offices", id: "branch-b" })).headers.get("etag") ?? "";
326
+ const missing = await read({ objectType: "offices", id: "branch-z", ifNoneMatch: tag });
327
+ expect(missing.status).toBe(404);
328
+ expect(platformError(missing)).toBe("not_found");
329
+
330
+ const invalid = await read({ objectType: "offices", search: [["nope", "1"]], ifNoneMatch: tag });
331
+ expect(invalid.status).toBe(400);
332
+ expect(platformError(invalid)).toBe("invalid_query");
333
+
334
+ const refused = await read({ objectType: "roster", persona: "member", ifNoneMatch: tag });
335
+ expect(refused.status).toBe(403);
336
+ expect(platformError(refused)).toBe("level_not_met");
337
+ });
338
+
339
+ it("changes the ETag when the mock text changes", async () => {
340
+ const before = (await read({ objectType: "summary" })).headers.get("etag");
341
+ const key = Object.keys(overlay).find((file) => file.endsWith("branch-list/mocks/summary.json")) ?? "";
342
+ const edited = { ...overlay, [key]: String(overlay[key]).replace("สรุปสาขา", "สรุปสาขา (แก้ไข)") };
343
+ const after = answerOfflineDataset(edited, writtenAt, {
344
+ overlayRoot: "fixtures/overlay/",
345
+ entry: "branch-list",
346
+ persona: "member",
347
+ objectType: "summary",
348
+ ifNoneMatch: before ?? "",
349
+ });
350
+ expect(after.status).toBe(200);
351
+ expect(after.headers.get("etag")).not.toBe(before);
352
+ });
353
+ });
354
+
247
355
  function timeFor(suffix: string): string {
248
356
  const key = Object.keys(writtenAt).find((file) => file.includes(suffix));
249
357
  const value = key ? writtenAt[key] : undefined;
@@ -262,7 +370,8 @@ function read(
262
370
  times: Record<string, unknown> = writtenAt,
263
371
  ): Promise<Response> {
264
372
  return Promise.resolve(answerOfflineDataset(overlay, times, {
265
- entry: "directory/branches",
373
+ overlayRoot: "fixtures/overlay/",
374
+ entry: "branch-list",
266
375
  persona: "hq-staff",
267
376
  userToken: "t",
268
377
  ...input,
@@ -49,7 +49,7 @@ describe("the emulated registry path", () => {
49
49
  expect(await res.json()).toEqual({
50
50
  call: {
51
51
  consumer: "dev-consumer",
52
- entry: "dev-provider/branch-list",
52
+ entry: "branch-list",
53
53
  identity: DEV_PERSONAS["hq-staff"],
54
54
  },
55
55
  path: "/offices",
@@ -81,7 +81,7 @@ describe("the emulated registry path", () => {
81
81
  expect(await res.json()).toMatchObject({
82
82
  call: {
83
83
  consumer: "brave-purple-lion",
84
- entry: "dev-provider/branch-list",
84
+ entry: "branch-list",
85
85
  identity: DEV_PERSONAS["province-coordinator"],
86
86
  },
87
87
  path: "/",
@@ -101,7 +101,7 @@ describe("the emulated registry path", () => {
101
101
  );
102
102
 
103
103
  expect(await res.json()).toMatchObject({
104
- call: { consumer: "dev-consumer", entry: "dev-provider/branch-list", identity: null },
104
+ call: { consumer: "dev-consumer", entry: "branch-list", identity: null },
105
105
  path: "/offices",
106
106
  });
107
107
  });
@@ -205,6 +205,35 @@ describe("the emulated registry path", () => {
205
205
  expect(await res.json()).toEqual({ self: "hono", path: "/offices" });
206
206
  });
207
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
+
208
237
  it("answers 404 when the registry path names no entry", async () => {
209
238
  const h = makeHarness();
210
239
  const res = await worker.fetch(new Request("https://localhost/.pple/registry/"), h.testEnv, ctx);
@@ -291,7 +320,7 @@ describe("callRegistry", () => {
291
320
  expect(res.status).toBe(200);
292
321
  expect(await res.json()).toEqual({
293
322
  consumer: "brave-purple-lion",
294
- entry: "dev-provider/branch-list",
323
+ entry: "branch-list",
295
324
  identity: DEV_PERSONAS["province-coordinator"],
296
325
  });
297
326
  if (!seen.request) throw new Error("handler did not run");
@@ -329,7 +358,7 @@ describe("callRegistry", () => {
329
358
  expect(await res.json()).toEqual({
330
359
  call: {
331
360
  consumer: "brave-purple-lion",
332
- entry: "dev-provider/branch-list",
361
+ entry: "branch-list",
333
362
  identity: null,
334
363
  },
335
364
  method: "POST",
@@ -352,7 +381,7 @@ describe("callRegistry", () => {
352
381
 
353
382
  expect(await res.json()).toEqual({
354
383
  consumer: "from-env",
355
- entry: "dev-provider/branch-list",
384
+ entry: "branch-list",
356
385
  identity: DEV_PERSONAS.delegate,
357
386
  });
358
387
  expect(path).toBe("/items");
@@ -0,0 +1,8 @@
1
+ ---
2
+ kind: service
3
+ visibility: public
4
+ userToken: none
5
+ description: บริการและข้อมูลสำหรับทดสอบ
6
+ ---
7
+
8
+ # Catalog
@@ -0,0 +1,11 @@
1
+ ---
2
+ kind: service
3
+ visibility: private
4
+ userToken: none
5
+ namespace: election
6
+ description: คะแนนเลือกตั้งรายหน่วยสำหรับทดสอบ
7
+ ---
8
+
9
+ # Score
10
+
11
+ GET /units/:id