@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,379 +0,0 @@
1
- // Offline readDataset (issue #270, ADR-0030). Vitest never lets readDataset
2
- // read `.pple/`. These tests feed the overlay fixture glob and read the
3
- // status, headers, and body a Builder would see.
4
-
5
- import { beforeAll, describe, expect, it, vi } from "vitest";
6
- import { platformError } from "../src/call-service";
7
- import { answerOfflineDataset, type OfflineDatasetCall } from "../src/dev-read-dataset";
8
-
9
- const overlay: Record<string, unknown> = import.meta.glob("./fixtures/overlay/**/*", {
10
- eager: true,
11
- query: "?raw",
12
- import: "default",
13
- });
14
-
15
- const writtenAt: Record<string, unknown> = import.meta.glob("./fixtures/overlay/**/*", {
16
- eager: true,
17
- query: "?mtime",
18
- import: "default",
19
- });
20
-
21
- beforeAll(() => {
22
- const keys = Object.keys(overlay).join("\n");
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");
27
- const offices = timeFor("mocks/offices.json");
28
- expect(offices).toMatch(/^\d{4}-\d{2}-\d{2}T/);
29
- });
30
-
31
- describe("offline readDataset", () => {
32
- it("filters, sorts with id descending on a tie, and pages", async () => {
33
- const filtered = await read({
34
- objectType: "offices",
35
- search: [
36
- ["province", "เชียงใหม่"],
37
- ["seats[gte]", "4"],
38
- ],
39
- });
40
- expect(filtered.status).toBe(200);
41
- expect(filtered.headers.get("x-pple-mock")).toBe("branch-list");
42
- expect(filtered.headers.get("x-pple-error")).toBeNull();
43
- expect(platformError(filtered)).toBeNull();
44
- const filteredBody = await filtered.json<{ objects: { id: string }[]; next: null; sweptAt: string; snapshot: number }>();
45
- expect(filteredBody.objects.map((object) => object.id)).toEqual(["branch-a", "branch-c"]);
46
- expect(filteredBody.sweptAt).toBe(timeFor("mocks/offices.json"));
47
- expect(filtered.headers.get("etag")).toBe(`"${filteredBody.snapshot}-${etagHash(filtered)}"`);
48
-
49
- const descending = await read({ objectType: "offices", search: [["sort", "-seats"]] });
50
- const descendingBody = await descending.json<{ objects: { id: string }[]; snapshot: number; sweptAt: string }>();
51
- expect(descendingBody.objects.map((object) => object.id)).toEqual([
52
- "branch-b",
53
- "branch-c",
54
- "branch-a",
55
- "branch-d",
56
- ]);
57
- expect(descendingBody.sweptAt).toBe(filteredBody.sweptAt);
58
- expect(descendingBody.snapshot).toBe(filteredBody.snapshot);
59
-
60
- const first = await read({ objectType: "offices", search: [["sort", "seats"], ["limit", "1"]] });
61
- const firstBody = await first.json<{ objects: { id: string }[]; next: string | null; sweptAt: string }>();
62
- expect(firstBody.objects.map((object) => object.id)).toEqual(["branch-d"]);
63
- expect(firstBody.next).toEqual(expect.any(String));
64
- expect(firstBody.sweptAt).toBe(timeFor("mocks/offices.json"));
65
-
66
- const second = await read({
67
- objectType: "offices",
68
- search: [["sort", "seats"], ["limit", "1"], ["next", firstBody.next ?? ""]],
69
- });
70
- const secondBody = await second.json<{ objects: { id: string }[]; next: string | null; sweptAt: string }>();
71
- expect(secondBody.objects.map((object) => object.id)).toEqual(["branch-a"]);
72
- expect(secondBody.sweptAt).toBe(firstBody.sweptAt);
73
- });
74
-
75
- it("uses the mock file's write time, not entry.md, when both are known", async () => {
76
- const res = await read(
77
- { objectType: "offices" },
78
- {
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",
81
- },
82
- );
83
- const body = await res.json<{ sweptAt: string }>();
84
- expect(body.sweptAt).toBe("2024-05-01T00:00:00.000Z");
85
- });
86
-
87
- it("looks an object up by id, and refuses a query on that lookup or on a singleton", async () => {
88
- const found = await read({ objectType: "offices", id: "branch-b" });
89
- expect(found.status).toBe(200);
90
- expect(await found.json()).toMatchObject({
91
- object: { id: "branch-b", province: "กรุงเทพ", seats: 9 },
92
- sweptAt: timeFor("mocks/offices.json"),
93
- });
94
-
95
- const missing = await read({ objectType: "offices", id: "branch-z" });
96
- expect(missing.status).toBe(404);
97
- expect(missing.headers.get("x-pple-error")).toBe("not_found");
98
- expect(missing.headers.get("x-pple-mock")).toBeNull();
99
- expect(platformError(missing)).toBe("not_found");
100
-
101
- const queried = await read({ objectType: "offices", id: "branch-b", search: [["limit", "1"]] });
102
- expect(queried.status).toBe(400);
103
- expect(queried.headers.get("x-pple-error")).toBe("invalid_query");
104
- expect(platformError(queried)).toBe("invalid_query");
105
-
106
- const summary = await read({ objectType: "summary", persona: "member", userToken: undefined });
107
- expect(summary.status).toBe(200);
108
- expect(summary.headers.get("x-pple-mock")).toBe("branch-list");
109
- expect(await summary.json()).toMatchObject({ object: { title: "สรุปสาขา" } });
110
-
111
- const summaryQuery = await read({ objectType: "summary", search: [["limit", "1"]] });
112
- expect(summaryQuery.status).toBe(400);
113
- expect(summaryQuery.headers.get("x-pple-error")).toBe("invalid_query");
114
-
115
- const summaryId = await read({ objectType: "summary", id: "1" });
116
- expect(summaryId.status).toBe(404);
117
- expect(summaryId.headers.get("x-pple-error")).toBe("not_found");
118
- });
119
-
120
- it("refuses the member-only persona on an internal type and admits hq-staff", async () => {
121
- for (const userToken of [undefined, "", " "]) {
122
- const res = await read({ objectType: "offices", persona: "member", userToken });
123
- expect(res.status).toBe(401);
124
- expect(res.headers.get("x-pple-error")).toBe("user_token_invalid");
125
- expect(platformError(res)).toBe("user_token_invalid");
126
- expect(res.headers.get("x-pple-mock")).toBeNull();
127
- }
128
-
129
- const member = await read({ objectType: "roster", persona: "member", userToken: "t" });
130
- expect(member.status).toBe(403);
131
- expect(member.headers.get("x-pple-error")).toBe("level_not_met");
132
- expect(member.headers.get("x-pple-mock")).toBeNull();
133
- expect(platformError(member)).toBe("level_not_met");
134
- expect(await member.json()).toEqual({ error: "level_not_met" });
135
-
136
- const staff = await read({ objectType: "roster", persona: "hq-staff", userToken: "t" });
137
- expect(staff.status).toBe(200);
138
- expect(await staff.json()).toMatchObject({ objects: [{ id: "r1", province: "เชียงใหม่" }] });
139
-
140
- const memberOffices = await read({ objectType: "offices", persona: "member", userToken: "t" });
141
- expect(memberOffices.status).toBe(200);
142
- const offices = await memberOffices.json<{ objects: { id: string }[] }>();
143
- expect(offices.objects.map((object) => object.id)).toEqual(["branch-a", "branch-b", "branch-c", "branch-d"]);
144
- });
145
-
146
- it("fails closed when subs are missing or swept", async () => {
147
- const swept = await read({ objectType: "audience", userToken: "t" });
148
- expect(swept.status).toBe(403);
149
- expect(swept.headers.get("x-pple-error")).toBe("level_not_met");
150
-
151
- const stripped = await read({ objectType: "named", userToken: "t" });
152
- expect(stripped.status).toBe(403);
153
- expect(stripped.headers.get("x-pple-error")).toBe("level_not_met");
154
-
155
- const memberSwept = await read({ objectType: "audience", persona: "member", userToken: "t" });
156
- expect(memberSwept.status).toBe(403);
157
- expect(memberSwept.headers.get("x-pple-error")).toBe("level_not_met");
158
- });
159
-
160
- it("refuses an invalid query the way production does", async () => {
161
- const badValue = await read({ objectType: "offices", search: [["seats", "nope"]] });
162
- expect(badValue.status).toBe(400);
163
- expect(badValue.headers.get("x-pple-error")).toBe("invalid_query");
164
- expect(platformError(badValue)).toBe("invalid_query");
165
- expect(badValue.headers.get("x-pple-mock")).toBeNull();
166
-
167
- const badField = await read({ objectType: "offices", search: [["city", "เชียงใหม่"]] });
168
- expect(badField.headers.get("x-pple-error")).toBe("invalid_query");
169
-
170
- const badLimit = await read({ objectType: "offices", search: [["limit", "0"]] });
171
- expect(badLimit.headers.get("x-pple-error")).toBe("invalid_query");
172
- });
173
-
174
- it("says no_mock for a missing entry or mock, and not_found for an unknown type", async () => {
175
- const missing = await read({ entry: "missing", objectType: "offices" });
176
- expect(missing.status).toBe(404);
177
- expect(missing.headers.get("x-pple-error")).toBe("no_mock");
178
- expect(missing.headers.get("x-pple-mock")).toBeNull();
179
- expect(platformError(missing)).toBeNull();
180
- const missingBody = await missing.json<{ error: string; message: string }>();
181
- expect(missingBody.error).toBe("no_mock");
182
- expect(missingBody.message).toContain("pple registry");
183
- expect(missingBody.message).toContain(".pple/registry/");
184
- expect(missingBody.message).toMatch(/[\u0E00-\u0E7F]/);
185
-
186
- const gap = await read({ objectType: "gap" });
187
- expect(gap.status).toBe(404);
188
- expect(gap.headers.get("x-pple-error")).toBe("no_mock");
189
- const gapBody = await gap.json<{ error: string; message: string }>();
190
- expect(gapBody.message).toContain("pple registry");
191
- expect(gapBody.message).toContain("gap");
192
-
193
- const unknown = await read({ objectType: "nope" });
194
- expect(unknown.status).toBe(404);
195
- expect(unknown.headers.get("x-pple-error")).toBe("not_found");
196
- expect(platformError(unknown)).toBe("not_found");
197
- expect(unknown.headers.get("x-pple-mock")).toBeNull();
198
- });
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
-
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
-
239
- it("returns the platform refusal before the token and the level", async () => {
240
- const res = await read({
241
- objectType: "roster",
242
- persona: "member",
243
- devRegistryError: "not_granted",
244
- });
245
- expect(res.status).toBe(403);
246
- expect(res.headers.get("x-pple-error")).toBe("not_granted");
247
- expect(res.headers.get("x-pple-mock")).toBeNull();
248
- expect(platformError(res)).toBe("not_granted");
249
- expect(await res.json()).toEqual({ error: "not_granted" });
250
- });
251
-
252
- it("lets a per-entry code win, and accepts a dataset refusal code", async () => {
253
- const specific = await read({
254
- objectType: "offices",
255
- devRegistryError: "not_granted, branch-list=needs_beta",
256
- });
257
- expect(specific.status).toBe(403);
258
- expect(specific.headers.get("x-pple-error")).toBe("needs_beta");
259
-
260
- const other = await read({
261
- entry: "orders",
262
- objectType: "offices",
263
- devRegistryError: "not_granted, branch-list=needs_beta",
264
- });
265
- expect(other.headers.get("x-pple-error")).toBe("not_granted");
266
-
267
- const level = await read({ objectType: "offices", devRegistryError: "level_not_met" });
268
- expect(level.status).toBe(403);
269
- expect(level.headers.get("x-pple-error")).toBe("level_not_met");
270
-
271
- const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
272
- try {
273
- const ignored = await read({
274
- objectType: "summary",
275
- devRegistryError: "branch-list=not_a_code, not_listed",
276
- });
277
- expect(ignored.status).toBe(404);
278
- expect(ignored.headers.get("x-pple-error")).toBe("not_listed");
279
- } finally {
280
- warn.mockRestore();
281
- }
282
- });
283
- });
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
-
355
- function timeFor(suffix: string): string {
356
- const key = Object.keys(writtenAt).find((file) => file.includes(suffix));
357
- const value = key ? writtenAt[key] : undefined;
358
- if (typeof value !== "string") throw new Error(`no mtime for ${suffix}`);
359
- return value;
360
- }
361
-
362
- function etagHash(response: Response): string {
363
- const tag = response.headers.get("etag") ?? "";
364
- const hash = tag.replaceAll('"', "").split("-")[1];
365
- return hash ?? "";
366
- }
367
-
368
- function read(
369
- input: Partial<OfflineDatasetCall> & { objectType: string },
370
- times: Record<string, unknown> = writtenAt,
371
- ): Promise<Response> {
372
- return Promise.resolve(answerOfflineDataset(overlay, times, {
373
- overlayRoot: "fixtures/overlay/",
374
- entry: "branch-list",
375
- persona: "hq-staff",
376
- userToken: "t",
377
- ...input,
378
- }));
379
- }