@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
@@ -0,0 +1,7 @@
1
+ [
2
+ {
3
+ "method": "GET",
4
+ "path": "/units/:id",
5
+ "body": { "from": "election/2027/score" }
6
+ }
7
+ ]
@@ -0,0 +1,24 @@
1
+ ---
2
+ kind: dataset
3
+ visibility: private
4
+ namespace: election
5
+ description: ผลเลือกตั้งสำหรับทดสอบ
6
+ level: public
7
+ objectTypes:
8
+ turnout:
9
+ shape: collection
10
+ cadence: 1d
11
+ id: id
12
+ index: []
13
+ schema:
14
+ type: object
15
+ properties:
16
+ id:
17
+ type: string
18
+ votes:
19
+ type: integer
20
+ required:
21
+ - id
22
+ ---
23
+
24
+ ผลเลือกตั้งสำหรับทดสอบออฟไลน์
@@ -0,0 +1 @@
1
+ [{ "id": "a", "votes": 10 }, { "id": "b", "votes": 20 }]
@@ -0,0 +1,10 @@
1
+ ---
2
+ kind: service
3
+ visibility: private
4
+ userToken: none
5
+ description: ส่งข้อความสำหรับทดสอบ
6
+ ---
7
+
8
+ # SMS
9
+
10
+ POST /
@@ -0,0 +1,7 @@
1
+ [
2
+ {
3
+ "method": "POST",
4
+ "path": "/",
5
+ "body": { "sent": 1, "rejected": [], "credits": 1 }
6
+ }
7
+ ]
@@ -0,0 +1,10 @@
1
+ ---
2
+ kind: service
3
+ visibility: private
4
+ userToken: none
5
+ description: บริการคะแนนที่ไม่มี namespace สำหรับทดสอบ
6
+ ---
7
+
8
+ # Score
9
+
10
+ GET /units/:id
@@ -0,0 +1,7 @@
1
+ [
2
+ {
3
+ "method": "GET",
4
+ "path": "/units/:id",
5
+ "body": { "from": "score" }
6
+ }
7
+ ]
@@ -36,7 +36,7 @@ export interface Fakes {
36
36
  /** Handles POST {NOTIFY_API_BASE}/notifications */
37
37
  notify?: (req: Request) => Response | Promise<Response>;
38
38
  /**
39
- * Handles `{PPLE_SERVICES_URL}/<provider>/<name>/…` for `callService` and
39
+ * Handles `{PPLE_SERVICES_URL}/<name>/…` for `callService` and
40
40
  * `readDataset`. Answer as the platform, or refuse with `serviceRefusal`
41
41
  * and any `x-pple-error` code.
42
42
  */
@@ -1,9 +1,16 @@
1
1
  // readDataset (issue #270). The harness is the platform: tests see the URL,
2
2
  // the headers, the page, and the refusal a Builder would observe.
3
3
 
4
- import { describe, expect, it, vi } from "vitest";
4
+ import { describe, expect, expectTypeOf, it, vi } from "vitest";
5
5
  import { platformError } from "../src/call-service";
6
- import { readDataset } from "../src/read-dataset";
6
+ import {
7
+ readDataset,
8
+ type DatasetFailure,
9
+ type DatasetNotModified,
10
+ type DatasetObject,
11
+ type DatasetPage,
12
+ type DatasetSuccess,
13
+ } from "../src/read-dataset";
7
14
  import { makeHarness, serviceRefusal } from "./harness";
8
15
 
9
16
  const env = {
@@ -38,7 +45,7 @@ describe("readDataset", () => {
38
45
  env,
39
46
  );
40
47
 
41
- const result = await readDataset(h.testEnv, "directory/branches", "offices", {
48
+ const result = await readDataset(h.testEnv, "branch-list", "offices", {
42
49
  userToken: "user-token",
43
50
  filter: { province: "เชียงใหม่", seats: { gte: 4, lt: 9 }, status: ["open", "closed"] },
44
51
  sort: "-seats",
@@ -59,7 +66,7 @@ describe("readDataset", () => {
59
66
  });
60
67
  expect(seen).toEqual({ authorization: "Bearer app-key", userToken: "user-token" });
61
68
  const url = new URL(`https://services.test${h.calls.services[0]?.path ?? ""}`);
62
- expect(url.pathname).toBe("/directory/branches/offices");
69
+ expect(url.pathname).toBe("/branch-list/offices");
63
70
  expect(url.searchParams.get("province")).toBe("เชียงใหม่");
64
71
  expect(url.searchParams.get("seats[gte]")).toBe("4");
65
72
  expect(url.searchParams.get("seats[lt]")).toBe("9");
@@ -87,7 +94,7 @@ describe("readDataset", () => {
87
94
  env,
88
95
  );
89
96
 
90
- const result = await readDataset(h.testEnv, "directory/branches", "offices", {
97
+ const result = await readDataset(h.testEnv, "branch-list", "offices", {
91
98
  id: "branch-b",
92
99
  fetchImpl: h.fetchImpl,
93
100
  });
@@ -100,7 +107,7 @@ describe("readDataset", () => {
100
107
  sweptAt: SWEPT,
101
108
  snapshot: 3,
102
109
  });
103
- expect(h.calls.services[0]?.path).toBe("/directory/branches/offices/branch-b");
110
+ expect(h.calls.services[0]?.path).toBe("/branch-list/offices/branch-b");
104
111
  });
105
112
 
106
113
  it("reads a singleton", async () => {
@@ -110,7 +117,7 @@ describe("readDataset", () => {
110
117
  },
111
118
  env,
112
119
  );
113
- const result = await readDataset(h.testEnv, "directory/branches", "summary", {
120
+ const result = await readDataset(h.testEnv, "branch-list", "summary", {
114
121
  shape: "singleton",
115
122
  fetchImpl: h.fetchImpl,
116
123
  });
@@ -118,7 +125,7 @@ describe("readDataset", () => {
118
125
  ok: true,
119
126
  data: { object: { title: "สรุปสาขา" }, sweptAt: SWEPT, snapshot: 1 },
120
127
  });
121
- expect(h.calls.services[0]?.path).toBe("/directory/branches/summary");
128
+ expect(h.calls.services[0]?.path).toBe("/branch-list/summary");
122
129
  });
123
130
 
124
131
  it("reads platform refusals from x-pple-error", async () => {
@@ -128,6 +135,7 @@ describe("readDataset", () => {
128
135
  ["level_not_met", 403],
129
136
  ["not_granted", 403],
130
137
  ["user_token_invalid", 401],
138
+ ["renamed", 410],
131
139
  ["no_mock", 404],
132
140
  ] as const;
133
141
  let next = serviceRefusal("not_found", 404);
@@ -135,7 +143,7 @@ describe("readDataset", () => {
135
143
 
136
144
  for (const [code, status] of codes) {
137
145
  next = serviceRefusal(code, status);
138
- const result = await readDataset(h.testEnv, "directory/branches", "offices", {
146
+ const result = await readDataset(h.testEnv, "branch-list", "offices", {
139
147
  userToken: "t",
140
148
  fetchImpl: h.fetchImpl,
141
149
  });
@@ -169,7 +177,7 @@ describe("readDataset", () => {
169
177
  env,
170
178
  );
171
179
 
172
- const result = await readDataset(h.testEnv, "directory/branches", "offices", {
180
+ const result = await readDataset(h.testEnv, "branch-list", "offices", {
173
181
  limit: 1,
174
182
  follow: true,
175
183
  fetchImpl: h.fetchImpl,
@@ -192,23 +200,188 @@ describe("readDataset", () => {
192
200
  ]);
193
201
  });
194
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
+
195
235
  it("uses the injected fetch under vitest, including when a persona is set", async () => {
196
236
  const fetchImpl = vi.fn<typeof fetch>(async () =>
197
237
  Response.json({ objects: [], next: null, sweptAt: null, snapshot: null }),
198
238
  );
199
239
  const result = await readDataset(
200
240
  { ...env, DEV_PERSONA: "hq-staff", DEV_REGISTRY_ERROR: "not_granted" },
201
- "directory/branches",
241
+ "branch-list",
202
242
  "offices",
203
243
  { fetchImpl },
204
244
  );
205
245
  expect(result).toMatchObject({ ok: true, data: { objects: [], next: null, sweptAt: null } });
206
246
  expect(fetchImpl).toHaveBeenCalledOnce();
207
- expect(String(fetchImpl.mock.calls[0]?.[0])).toBe("https://services.test/directory/branches/offices");
247
+ expect(String(fetchImpl.mock.calls[0]?.[0])).toBe("https://services.test/branch-list/offices");
208
248
 
209
- await expect(readDataset(env, "directory/branches", "offices")).rejects.toThrow(/injected fetch/);
249
+ await expect(readDataset(env, "branch-list", "offices")).rejects.toThrow(/injected fetch/);
210
250
  await expect(
211
- readDataset({ DEV_PERSONA: "none" }, "directory/branches", "offices", { fetchImpl }),
251
+ readDataset({ DEV_PERSONA: "none" }, "branch-list", "offices", { fetchImpl }),
212
252
  ).rejects.toThrow(/PPLE_SERVICES_URL/);
213
253
  });
214
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,8 +0,0 @@
1
- ---
2
- kind: service
3
- visibility: public
4
- userToken: none
5
- description: แคตตาล็อกสำหรับทดสอบ
6
- ---
7
-
8
- # Catalog