@pplethai/cli 0.22.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.
- package/README.md +12 -14
- package/dist/bin.js +102 -102
- package/package.json +3 -6
- package/templates/golden-path/.dev.vars.example +0 -33
- package/templates/golden-path/.env.example +0 -10
- package/templates/golden-path/.github/workflows/ci.yml +0 -22
- package/templates/golden-path/AGENTS.md +0 -1
- package/templates/golden-path/CLAUDE.md +0 -191
- package/templates/golden-path/README.md +0 -121
- package/templates/golden-path/gitignore +0 -17
- package/templates/golden-path/index.html +0 -16
- package/templates/golden-path/migrations/0001_init.sql +0 -10
- package/templates/golden-path/overlay-mtime.mjs +0 -30
- package/templates/golden-path/package-lock.json +0 -4318
- package/templates/golden-path/package.json +0 -41
- package/templates/golden-path/scripts/assert-dev-compiled-out.mjs +0 -45
- package/templates/golden-path/shared/api.ts +0 -325
- package/templates/golden-path/src/auth.ts +0 -230
- package/templates/golden-path/src/call-service.ts +0 -147
- package/templates/golden-path/src/dataset-query.ts +0 -470
- package/templates/golden-path/src/dataset-schema.ts +0 -398
- package/templates/golden-path/src/db.ts +0 -32
- package/templates/golden-path/src/dev-call-service.ts +0 -392
- package/templates/golden-path/src/dev-personas.ts +0 -186
- package/templates/golden-path/src/dev-read-dataset.ts +0 -444
- package/templates/golden-path/src/dev-registry.ts +0 -294
- package/templates/golden-path/src/email.ts +0 -49
- package/templates/golden-path/src/entry-name.ts +0 -114
- package/templates/golden-path/src/env.ts +0 -38
- package/templates/golden-path/src/files.ts +0 -38
- package/templates/golden-path/src/identity-header.ts +0 -17
- package/templates/golden-path/src/index.ts +0 -282
- package/templates/golden-path/src/legal-identity.ts +0 -180
- package/templates/golden-path/src/levels.ts +0 -132
- package/templates/golden-path/src/notification-response.ts +0 -55
- package/templates/golden-path/src/notify.ts +0 -311
- package/templates/golden-path/src/party-email.ts +0 -183
- package/templates/golden-path/src/payment.ts +0 -676
- package/templates/golden-path/src/read-dataset.ts +0 -490
- package/templates/golden-path/src/registry.ts +0 -128
- package/templates/golden-path/src/vite-env.d.ts +0 -27
- package/templates/golden-path/test/apply-migrations.ts +0 -4
- package/templates/golden-path/test/call-service.test.ts +0 -288
- package/templates/golden-path/test/dev-call-service.test.ts +0 -455
- package/templates/golden-path/test/dev-read-dataset.test.ts +0 -379
- package/templates/golden-path/test/dev-registry.test.ts +0 -404
- package/templates/golden-path/test/effective-sub.test.ts +0 -101
- package/templates/golden-path/test/email-offline.test.ts +0 -51
- package/templates/golden-path/test/email.test.ts +0 -117
- package/templates/golden-path/test/env.d.ts +0 -17
- package/templates/golden-path/test/fixtures/overlay/branch-list/entry.md +0 -85
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/audience.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/named.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/offices.json +0 -6
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/roster.json +0 -3
- package/templates/golden-path/test/fixtures/overlay/branch-list/mocks/summary.json +0 -1
- package/templates/golden-path/test/fixtures/overlay/catalog/entry.md +0 -8
- package/templates/golden-path/test/fixtures/overlay/catalog/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/entry.md +0 -11
- package/templates/golden-path/test/fixtures/overlay/election/2027/score/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/election/results/entry.md +0 -24
- package/templates/golden-path/test/fixtures/overlay/election/results/mocks/turnout.json +0 -1
- package/templates/golden-path/test/fixtures/overlay/orders/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/orders/mocks/responses.json +0 -36
- package/templates/golden-path/test/fixtures/overlay/platform/email/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/platform/email/mocks/responses.json +0 -11
- package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/entry.md +0 -8
- package/templates/golden-path/test/fixtures/overlay/platform/legal-identity/mocks/responses.json +0 -13
- package/templates/golden-path/test/fixtures/overlay/platform/payments/entry.md +0 -12
- package/templates/golden-path/test/fixtures/overlay/platform/payments/mocks/responses.json +0 -98
- package/templates/golden-path/test/fixtures/overlay/platform/sms/entry.md +0 -10
- package/templates/golden-path/test/fixtures/overlay/platform/sms/mocks/responses.json +0 -7
- package/templates/golden-path/test/fixtures/overlay/score/entry.md +0 -10
- package/templates/golden-path/test/fixtures/overlay/score/mocks/responses.json +0 -7
- package/templates/golden-path/test/harness.ts +0 -153
- package/templates/golden-path/test/interaction.test.ts +0 -230
- package/templates/golden-path/test/legal-identity.test.ts +0 -323
- package/templates/golden-path/test/membership.test.ts +0 -105
- package/templates/golden-path/test/notification-response.test.ts +0 -79
- package/templates/golden-path/test/payment-app.test.ts +0 -211
- package/templates/golden-path/test/payment-offline.test.ts +0 -91
- package/templates/golden-path/test/payment.test.ts +0 -518
- package/templates/golden-path/test/payments-offline.ts +0 -33
- package/templates/golden-path/test/read-dataset.test.ts +0 -387
- package/templates/golden-path/test/service-call.test.ts +0 -166
- package/templates/golden-path/test/smoke.test.ts +0 -464
- package/templates/golden-path/tsconfig.json +0 -20
- package/templates/golden-path/tsconfig.worker.json +0 -21
- package/templates/golden-path/vite.config.ts +0 -12
- package/templates/golden-path/vitest.config.ts +0 -28
- package/templates/golden-path/web/App.tsx +0 -478
- package/templates/golden-path/web/app.css +0 -83
- package/templates/golden-path/web/main.tsx +0 -51
- package/templates/golden-path/web/pple.ts +0 -124
- package/templates/golden-path/web/vite-env.d.ts +0 -10
- package/templates/golden-path/wrangler.jsonc +0 -89
- package/templates/payment-bridge/.github/workflows/ci.yml +0 -21
- package/templates/payment-bridge/CLAUDE.md +0 -9
- package/templates/payment-bridge/README.md +0 -57
- package/templates/payment-bridge/docs/verify-webhook.md +0 -80
- package/templates/payment-bridge/docs/verify-webhook.ts +0 -29
- package/templates/payment-bridge/gitignore +0 -5
- package/templates/payment-bridge/package-lock.json +0 -4198
- package/templates/payment-bridge/package.json +0 -21
- package/templates/payment-bridge/src/bridge-key.ts +0 -18
- package/templates/payment-bridge/src/env.ts +0 -14
- package/templates/payment-bridge/src/index.ts +0 -39
- package/templates/payment-bridge/src/platform.ts +0 -49
- package/templates/payment-bridge/src/webhook.ts +0 -86
- package/templates/payment-bridge/test/docs.test.ts +0 -9
- package/templates/payment-bridge/test/fixtures/payments-responses.json +0 -98
- package/templates/payment-bridge/test/harness.ts +0 -78
- package/templates/payment-bridge/test/inbound.test.ts +0 -138
- package/templates/payment-bridge/test/webhook.test.ts +0 -174
- package/templates/payment-bridge/tsconfig.json +0 -22
- package/templates/payment-bridge/vitest.config.ts +0 -8
- package/templates/payment-bridge/wrangler.jsonc +0 -15
- package/templates/payment-bridge.version +0 -1
|
@@ -1,464 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import type { Note, Notification, UploadedFile } from "../shared/api";
|
|
3
|
-
import { makeHarness } from "./harness";
|
|
4
|
-
|
|
5
|
-
describe("health", () => {
|
|
6
|
-
it("is public and reports ok", async () => {
|
|
7
|
-
const h = makeHarness();
|
|
8
|
-
const res = await h.request("/api/health", {}, null);
|
|
9
|
-
expect(res.status).toBe(200);
|
|
10
|
-
expect(await res.json()).toEqual({ ok: true });
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
describe("local dev personas", () => {
|
|
15
|
-
it("signs in as the default persona (hq-staff) with no config", async () => {
|
|
16
|
-
const h = makeHarness();
|
|
17
|
-
const res = await h.request("/api/me");
|
|
18
|
-
expect(res.status).toBe(200);
|
|
19
|
-
expect(await res.json()).toMatchObject({
|
|
20
|
-
actor: { sub: "dev-hq-staff" },
|
|
21
|
-
effective: { activeRole: "hq", provinces: ["all"], isDelegated: false },
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it("switches persona via DEV_PERSONA, even without a token", async () => {
|
|
26
|
-
const h = makeHarness({}, { DEV_PERSONA: "province-coordinator" });
|
|
27
|
-
const res = await h.request("/api/me", {}, null);
|
|
28
|
-
expect(res.status).toBe(200);
|
|
29
|
-
expect(await res.json()).toMatchObject({
|
|
30
|
-
effective: { activeRole: "province", provinces: ["เชียงใหม่"], extraRoles: ["province_co"] },
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("exposes a delegate persona as effective≠actor", async () => {
|
|
35
|
-
const h = makeHarness({}, { DEV_PERSONA: "delegate" });
|
|
36
|
-
const { actor, effective } = await (await h.request("/api/me")).json<{
|
|
37
|
-
actor: { sub: string };
|
|
38
|
-
effective: { isDelegated: boolean };
|
|
39
|
-
}>();
|
|
40
|
-
expect(actor.sub).toBe("dev-delegate");
|
|
41
|
-
expect(effective.isDelegated).toBe(true);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it("falls back to the default persona on an unknown DEV_PERSONA", async () => {
|
|
45
|
-
const h = makeHarness({}, { DEV_PERSONA: "does-not-exist" });
|
|
46
|
-
expect(await (await h.request("/api/me")).json()).toMatchObject({
|
|
47
|
-
actor: { sub: "dev-hq-staff" },
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
describe("real PPLE ID authentication (DEV_PERSONA=none)", () => {
|
|
53
|
-
it("rejects requests without a token", async () => {
|
|
54
|
-
const h = makeHarness({}, { DEV_PERSONA: "none" });
|
|
55
|
-
const res = await h.request("/api/me", {}, null);
|
|
56
|
-
expect(res.status).toBe(401);
|
|
57
|
-
expect(await res.json()).toMatchObject({ error: "not_authenticated" });
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("validates real tokens against PPLE ID userinfo", async () => {
|
|
61
|
-
const h = makeHarness(
|
|
62
|
-
{ sso: () => Response.json({ error: "invalid_token" }, { status: 401 }) },
|
|
63
|
-
{ DEV_PERSONA: "none" },
|
|
64
|
-
);
|
|
65
|
-
const res = await h.request("/api/me", {}, "some-real-looking-token");
|
|
66
|
-
expect(res.status).toBe(401);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it("resolves an effective/actor Identity from a valid token", async () => {
|
|
70
|
-
const h = makeHarness(
|
|
71
|
-
{
|
|
72
|
-
sso: () =>
|
|
73
|
-
Response.json({
|
|
74
|
-
sub: "u-42",
|
|
75
|
-
name: "Somchai",
|
|
76
|
-
ad: {
|
|
77
|
-
user: {
|
|
78
|
-
id: 7,
|
|
79
|
-
role: "hq",
|
|
80
|
-
mobile: "0810000007",
|
|
81
|
-
metadata: { ad: { extra_roles: ["exco"] } },
|
|
82
|
-
},
|
|
83
|
-
actualPpleUser: { id: 7, role: "hq", mobile: "0810000007" },
|
|
84
|
-
activeRole: "hq",
|
|
85
|
-
eligibleRoles: ["hq"],
|
|
86
|
-
eligibleProvinces: ["all"],
|
|
87
|
-
},
|
|
88
|
-
}),
|
|
89
|
-
},
|
|
90
|
-
{ DEV_PERSONA: "none" },
|
|
91
|
-
);
|
|
92
|
-
const res = await h.request("/api/me", {}, "valid-token");
|
|
93
|
-
expect(await res.json()).toMatchObject({
|
|
94
|
-
actor: { sub: "u-42", name: "Somchai", mobile: "0810000007" },
|
|
95
|
-
effective: {
|
|
96
|
-
isDelegated: false,
|
|
97
|
-
activeRole: "hq",
|
|
98
|
-
provinces: ["all"],
|
|
99
|
-
extraRoles: ["exco"],
|
|
100
|
-
},
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it("keeps actor (the delegate) distinct from effective (the supervisor)", async () => {
|
|
105
|
-
const h = makeHarness(
|
|
106
|
-
{
|
|
107
|
-
sso: () =>
|
|
108
|
-
Response.json({
|
|
109
|
-
sub: "u-delegate",
|
|
110
|
-
name: "Nok",
|
|
111
|
-
ad: {
|
|
112
|
-
// The supervisor whose authority is in effect…
|
|
113
|
-
user: { id: 9, role: "province", mobile: "0810000009" },
|
|
114
|
-
// …while the real human acting is the delegate.
|
|
115
|
-
actualPpleUser: { id: 14, role: "delegate", mobile: "0810000014" },
|
|
116
|
-
activeRole: "province",
|
|
117
|
-
eligibleRoles: ["province"],
|
|
118
|
-
eligibleProvinces: ["เชียงใหม่"],
|
|
119
|
-
},
|
|
120
|
-
}),
|
|
121
|
-
},
|
|
122
|
-
{ DEV_PERSONA: "none" },
|
|
123
|
-
);
|
|
124
|
-
const res = await h.request("/api/me", {}, "valid-token");
|
|
125
|
-
expect(await res.json()).toMatchObject({
|
|
126
|
-
actor: { sub: "u-delegate", mobile: "0810000014" },
|
|
127
|
-
effective: { isDelegated: true, activeRole: "province", provinces: ["เชียงใหม่"] },
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
describe("D1 read/write (notes)", () => {
|
|
133
|
-
it("stores a note and reads it back", async () => {
|
|
134
|
-
const h = makeHarness();
|
|
135
|
-
|
|
136
|
-
const created = await h.request("/api/notes", {
|
|
137
|
-
method: "POST",
|
|
138
|
-
headers: { "content-type": "application/json" },
|
|
139
|
-
body: JSON.stringify({ body: "hello d1" }),
|
|
140
|
-
});
|
|
141
|
-
expect(created.status).toBe(201);
|
|
142
|
-
expect(await created.json<Note>()).toMatchObject({ body: "hello d1" });
|
|
143
|
-
|
|
144
|
-
const listed = await h.request("/api/notes");
|
|
145
|
-
const { notes } = await listed.json<{ notes: Note[] }>();
|
|
146
|
-
expect(notes.some((n) => n.body === "hello d1")).toBe(true);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it("rejects an empty note", async () => {
|
|
150
|
-
const h = makeHarness();
|
|
151
|
-
const res = await h.request("/api/notes", {
|
|
152
|
-
method: "POST",
|
|
153
|
-
headers: { "content-type": "application/json" },
|
|
154
|
-
body: JSON.stringify({ body: " " }),
|
|
155
|
-
});
|
|
156
|
-
expect(res.status).toBe(400);
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
describe("R2 upload (files)", () => {
|
|
161
|
-
it("uploads a file and lists it", async () => {
|
|
162
|
-
const h = makeHarness();
|
|
163
|
-
|
|
164
|
-
const form = new FormData();
|
|
165
|
-
form.append("file", new File(["file contents"], "photo.txt", { type: "text/plain" }));
|
|
166
|
-
const uploaded = await h.request("/api/files", { method: "POST", body: form });
|
|
167
|
-
expect(uploaded.status).toBe(201);
|
|
168
|
-
const meta = await uploaded.json<UploadedFile>();
|
|
169
|
-
expect(meta).toMatchObject({ name: "photo.txt", size: 13 });
|
|
170
|
-
|
|
171
|
-
const listed = await h.request("/api/files");
|
|
172
|
-
const { files } = await listed.json<{ files: UploadedFile[] }>();
|
|
173
|
-
expect(files.some((f) => f.name === "photo.txt")).toBe(true);
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
// Shaped like what the platform actually binds, so these tests exercise the
|
|
178
|
-
// real `${base}/notifications` the Worker will call in production.
|
|
179
|
-
const NOTIFY_ENV = {
|
|
180
|
-
NOTIFY_API_BASE: "https://today.test/external",
|
|
181
|
-
NOTIFY_API_KEY: "app-key",
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
/** A platform that accepts everything and reports every named entry delivered. */
|
|
185
|
-
const accepts = (body: unknown = null) => ({
|
|
186
|
-
notify: () => (body === null ? new Response(null, { status: 202 }) : Response.json(body)),
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
describe("notifications: the audience is required", () => {
|
|
190
|
-
it("is a compile error to omit it, not a runtime one", () => {
|
|
191
|
-
// @ts-expect-error — `audience` is required. The dangerous bug in this
|
|
192
|
-
// feature is omission: a dropped field turning a message meant for one
|
|
193
|
-
// person into a message to every App User. The discriminated union kills
|
|
194
|
-
// it at the compiler, which is where it is cheapest.
|
|
195
|
-
const missingAudience: Notification = { title: "Hi", body: "there" };
|
|
196
|
-
expect(missingAudience).toBeTruthy();
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it("refuses a send with no audience before it reaches the platform", async () => {
|
|
200
|
-
const h = makeHarness(accepts(), NOTIFY_ENV);
|
|
201
|
-
const res = await h.request("/api/notify", {
|
|
202
|
-
method: "POST",
|
|
203
|
-
headers: { "content-type": "application/json" },
|
|
204
|
-
body: JSON.stringify({ title: "Hi", body: "there" }),
|
|
205
|
-
});
|
|
206
|
-
expect(res.status).toBe(400);
|
|
207
|
-
expect(await res.json()).toMatchObject({ error: "invalid_body" });
|
|
208
|
-
// Never a broadcast, never a silent no-op.
|
|
209
|
-
expect(h.calls.notify).toHaveLength(0);
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
it("refuses an empty recipient list before it reaches the platform", async () => {
|
|
213
|
-
const h = makeHarness(accepts(), NOTIFY_ENV);
|
|
214
|
-
const res = await h.request("/api/notify", {
|
|
215
|
-
method: "POST",
|
|
216
|
-
headers: { "content-type": "application/json" },
|
|
217
|
-
body: JSON.stringify({
|
|
218
|
-
audience: { kind: "direct", recipients: [] },
|
|
219
|
-
title: "Hi",
|
|
220
|
-
body: "there",
|
|
221
|
-
}),
|
|
222
|
-
});
|
|
223
|
-
expect(res.status).toBe(400);
|
|
224
|
-
expect(h.calls.notify).toHaveLength(0);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
it("refuses more than 200 recipients rather than truncating them", async () => {
|
|
228
|
-
const h = makeHarness(accepts(), NOTIFY_ENV);
|
|
229
|
-
const res = await h.request("/api/notify", {
|
|
230
|
-
method: "POST",
|
|
231
|
-
headers: { "content-type": "application/json" },
|
|
232
|
-
body: JSON.stringify({
|
|
233
|
-
audience: {
|
|
234
|
-
kind: "direct",
|
|
235
|
-
recipients: Array.from({ length: 201 }, (_, i) => ({ sub: `u-${i}` })),
|
|
236
|
-
},
|
|
237
|
-
title: "Hi",
|
|
238
|
-
body: "there",
|
|
239
|
-
}),
|
|
240
|
-
});
|
|
241
|
-
expect(res.status).toBe(400);
|
|
242
|
-
expect(h.calls.notify).toHaveLength(0);
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
it("refuses an entry naming both a sub and a phone", async () => {
|
|
246
|
-
const h = makeHarness(accepts(), NOTIFY_ENV);
|
|
247
|
-
const res = await h.request("/api/notify", {
|
|
248
|
-
method: "POST",
|
|
249
|
-
headers: { "content-type": "application/json" },
|
|
250
|
-
body: JSON.stringify({
|
|
251
|
-
audience: {
|
|
252
|
-
kind: "direct",
|
|
253
|
-
recipients: [{ sub: "u-1", phone: "0812345678" }],
|
|
254
|
-
},
|
|
255
|
-
title: "Hi",
|
|
256
|
-
body: "there",
|
|
257
|
-
}),
|
|
258
|
-
});
|
|
259
|
-
expect(res.status).toBe(400);
|
|
260
|
-
expect(h.calls.notify).toHaveLength(0);
|
|
261
|
-
});
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
describe("Broadcast Notification (การแจ้งเตือนทุกคน)", () => {
|
|
265
|
-
it("reports not-configured when no key is set", async () => {
|
|
266
|
-
const h = makeHarness();
|
|
267
|
-
const res = await h.request("/api/notify", {
|
|
268
|
-
method: "POST",
|
|
269
|
-
headers: { "content-type": "application/json" },
|
|
270
|
-
body: JSON.stringify({
|
|
271
|
-
audience: { kind: "all" },
|
|
272
|
-
title: "Hi",
|
|
273
|
-
body: "there",
|
|
274
|
-
}),
|
|
275
|
-
});
|
|
276
|
-
expect(res.status).toBe(503);
|
|
277
|
-
expect(await res.json()).toMatchObject({ error: "notifications_not_configured" });
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
it("states its audience explicitly and names no one", async () => {
|
|
281
|
-
const h = makeHarness(accepts(), NOTIFY_ENV);
|
|
282
|
-
|
|
283
|
-
const res = await h.request("/api/notify", {
|
|
284
|
-
method: "POST",
|
|
285
|
-
headers: { "content-type": "application/json" },
|
|
286
|
-
body: JSON.stringify({
|
|
287
|
-
audience: { kind: "all" },
|
|
288
|
-
title: "Event tonight",
|
|
289
|
-
body: "Doors at 7",
|
|
290
|
-
}),
|
|
291
|
-
});
|
|
292
|
-
expect(res.status).toBe(200);
|
|
293
|
-
expect(await res.json()).toEqual({ ok: true });
|
|
294
|
-
|
|
295
|
-
expect(h.calls.notify).toHaveLength(1);
|
|
296
|
-
const sent = h.calls.notify[0]!;
|
|
297
|
-
expect(sent.headers.get("authorization")).toBe("Bearer app-key");
|
|
298
|
-
// PPLE Today's wire shape, which src/notify.ts translates into.
|
|
299
|
-
const payload = await sent.json<Record<string, unknown>>();
|
|
300
|
-
expect(payload).toEqual({
|
|
301
|
-
audience: { kind: "all" },
|
|
302
|
-
content: { header: "Event tonight", message: "Doors at 7" },
|
|
303
|
-
});
|
|
304
|
-
expect(JSON.stringify(payload)).not.toMatch(/phone|recipient/i);
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
it("forwards optional linkPath on the platform wire shape", async () => {
|
|
308
|
-
const h = makeHarness(accepts(), NOTIFY_ENV);
|
|
309
|
-
|
|
310
|
-
const res = await h.request("/api/notify", {
|
|
311
|
-
method: "POST",
|
|
312
|
-
headers: { "content-type": "application/json" },
|
|
313
|
-
body: JSON.stringify({
|
|
314
|
-
audience: { kind: "all" },
|
|
315
|
-
title: "Check-in open",
|
|
316
|
-
body: "Tap to open the form",
|
|
317
|
-
linkPath: "/check-in",
|
|
318
|
-
}),
|
|
319
|
-
});
|
|
320
|
-
expect(res.status).toBe(200);
|
|
321
|
-
|
|
322
|
-
const payload = await h.calls.notify[0]!.json<Record<string, unknown>>();
|
|
323
|
-
expect(payload).toEqual({
|
|
324
|
-
audience: { kind: "all" },
|
|
325
|
-
content: { header: "Check-in open", message: "Tap to open the form" },
|
|
326
|
-
linkPath: "/check-in",
|
|
327
|
-
});
|
|
328
|
-
});
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
describe("Direct Notification (การแจ้งเตือนรายบุคคล)", () => {
|
|
332
|
-
const recipients = [{ sub: "u-42" }, { phone: "+66812345678" }];
|
|
333
|
-
|
|
334
|
-
const sendDirect = (h: ReturnType<typeof makeHarness>, extra: object = {}) =>
|
|
335
|
-
h.request("/api/notify", {
|
|
336
|
-
method: "POST",
|
|
337
|
-
headers: { "content-type": "application/json" },
|
|
338
|
-
body: JSON.stringify({
|
|
339
|
-
audience: { kind: "direct", recipients },
|
|
340
|
-
title: "Reimbursement approved",
|
|
341
|
-
body: "Your claim was approved",
|
|
342
|
-
...extra,
|
|
343
|
-
}),
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
it("names recipients by sub or phone and reports each one's outcome", async () => {
|
|
347
|
-
const h = makeHarness(
|
|
348
|
-
accepts({
|
|
349
|
-
results: [
|
|
350
|
-
{ recipient: { sub: "u-42" }, status: "delivered" },
|
|
351
|
-
{ recipient: { phone: "+66812345678" }, status: "not_reachable" },
|
|
352
|
-
],
|
|
353
|
-
}),
|
|
354
|
-
NOTIFY_ENV,
|
|
355
|
-
);
|
|
356
|
-
|
|
357
|
-
const res = await sendDirect(h);
|
|
358
|
-
expect(res.status).toBe(200);
|
|
359
|
-
// Every named entry comes back, echoed as the caller named it, so a
|
|
360
|
-
// Builder can match a result straight onto the list it holds.
|
|
361
|
-
expect(await res.json()).toEqual({
|
|
362
|
-
ok: true,
|
|
363
|
-
results: [
|
|
364
|
-
{ recipient: { sub: "u-42" }, status: "delivered" },
|
|
365
|
-
{ recipient: { phone: "+66812345678" }, status: "not_reachable" },
|
|
366
|
-
],
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
const payload = await h.calls.notify[0]!.json<Record<string, unknown>>();
|
|
370
|
-
expect(payload).toEqual({
|
|
371
|
-
audience: { kind: "direct", recipients },
|
|
372
|
-
content: {
|
|
373
|
-
header: "Reimbursement approved",
|
|
374
|
-
message: "Your claim was approved",
|
|
375
|
-
},
|
|
376
|
-
});
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
it("forwards an idempotencyKey so a retry after a timeout is safe", async () => {
|
|
380
|
-
const h = makeHarness(accepts({ results: [] }), NOTIFY_ENV);
|
|
381
|
-
await sendDirect(h, { idempotencyKey: "claim-123" });
|
|
382
|
-
const payload = await h.calls.notify[0]!.json<Record<string, unknown>>();
|
|
383
|
-
expect(payload).toMatchObject({ idempotencyKey: "claim-123" });
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
const unconfirmed = {
|
|
387
|
-
ok: true,
|
|
388
|
-
results: [
|
|
389
|
-
{ recipient: { sub: "u-42" }, status: "not_reachable" },
|
|
390
|
-
{ recipient: { phone: "+66812345678" }, status: "not_reachable" },
|
|
391
|
-
],
|
|
392
|
-
};
|
|
393
|
-
|
|
394
|
-
it("never claims a delivery the platform did not confirm", async () => {
|
|
395
|
-
// A body this helper cannot read is not evidence of delivery.
|
|
396
|
-
const h = makeHarness(accepts({ unexpected: true }), NOTIFY_ENV);
|
|
397
|
-
expect(await (await sendDirect(h)).json()).toEqual(unconfirmed);
|
|
398
|
-
});
|
|
399
|
-
|
|
400
|
-
it("trusts none of a result list that is not one-per-named-entry", async () => {
|
|
401
|
-
// Results are paired by position, so a list of a different length is not
|
|
402
|
-
// the answer to this send — reading it would attribute someone's delivery
|
|
403
|
-
// to the wrong person.
|
|
404
|
-
const h = makeHarness(
|
|
405
|
-
accepts({ results: [{ recipient: { sub: "u-42" }, status: "delivered" }] }),
|
|
406
|
-
NOTIFY_ENV,
|
|
407
|
-
);
|
|
408
|
-
expect(await (await sendDirect(h)).json()).toEqual(unconfirmed);
|
|
409
|
-
});
|
|
410
|
-
});
|
|
411
|
-
|
|
412
|
-
describe("notification refusals map to legible errors", () => {
|
|
413
|
-
const refuses = (status: number, body: unknown = null) => ({
|
|
414
|
-
notify: () =>
|
|
415
|
-
body === null
|
|
416
|
-
? new Response(null, { status })
|
|
417
|
-
: Response.json(body, { status }),
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
const send = (h: ReturnType<typeof makeHarness>) =>
|
|
421
|
-
h.request("/api/notify", {
|
|
422
|
-
method: "POST",
|
|
423
|
-
headers: { "content-type": "application/json" },
|
|
424
|
-
body: JSON.stringify({
|
|
425
|
-
audience: { kind: "direct", recipients: [{ sub: "u-42" }] },
|
|
426
|
-
title: "Hi",
|
|
427
|
-
body: "there",
|
|
428
|
-
}),
|
|
429
|
-
});
|
|
430
|
-
|
|
431
|
-
// The platform splits schema-level refusals (422) from recipient-level ones
|
|
432
|
-
// (400); a client that branches on 400 alone is wrong. See the status table
|
|
433
|
-
// in docs/integration/direct-notifications.md.
|
|
434
|
-
it.each([
|
|
435
|
-
[422, "invalid_request"],
|
|
436
|
-
[400, "invalid_recipients"],
|
|
437
|
-
[403, "not_permitted"],
|
|
438
|
-
[409, "idempotency_key_conflict"],
|
|
439
|
-
[429, "quota_exceeded"],
|
|
440
|
-
[500, "send_failed"],
|
|
441
|
-
])("maps %i to %s", async (status, error) => {
|
|
442
|
-
const h = makeHarness(refuses(status), NOTIFY_ENV);
|
|
443
|
-
const res = await send(h);
|
|
444
|
-
expect(res.status).toBe(status);
|
|
445
|
-
expect(await res.json()).toMatchObject({ error });
|
|
446
|
-
});
|
|
447
|
-
|
|
448
|
-
it("passes the quota figures through on a 429 so an app can back off", async () => {
|
|
449
|
-
const h = makeHarness(
|
|
450
|
-
refuses(429, {
|
|
451
|
-
// Deliveries, not calls — the budget's denomination changed with
|
|
452
|
-
// Direct Notifications, and nothing was delivered by this call.
|
|
453
|
-
dailyQuota: 1000,
|
|
454
|
-
remaining: 3,
|
|
455
|
-
resetAt: "2026-08-15T00:00:00+07:00",
|
|
456
|
-
}),
|
|
457
|
-
NOTIFY_ENV,
|
|
458
|
-
);
|
|
459
|
-
expect(await (await send(h)).json()).toMatchObject({
|
|
460
|
-
error: "quota_exceeded",
|
|
461
|
-
quota: { dailyQuota: 1000, remaining: 3, resetAt: "2026-08-15T00:00:00+07:00" },
|
|
462
|
-
});
|
|
463
|
-
});
|
|
464
|
-
});
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// TypeScript config for the React SPA (web/) and shared code. The Worker has
|
|
2
|
-
// its own config (tsconfig.worker.json) because it runs in the Workers
|
|
3
|
-
// runtime, not the browser. `npm run typecheck` checks both.
|
|
4
|
-
{
|
|
5
|
-
"compilerOptions": {
|
|
6
|
-
"target": "ES2022",
|
|
7
|
-
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
8
|
-
"module": "ESNext",
|
|
9
|
-
"moduleResolution": "bundler",
|
|
10
|
-
"types": [],
|
|
11
|
-
"jsx": "react-jsx",
|
|
12
|
-
"strict": true,
|
|
13
|
-
"noUncheckedIndexedAccess": true,
|
|
14
|
-
"noEmit": true,
|
|
15
|
-
"skipLibCheck": true,
|
|
16
|
-
"isolatedModules": true,
|
|
17
|
-
"forceConsistentCasingInFileNames": true
|
|
18
|
-
},
|
|
19
|
-
"include": ["web", "shared"]
|
|
20
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// TypeScript config for the Worker (src/) and its tests (test/). Uses the
|
|
2
|
-
// Cloudflare Workers runtime types, not the browser DOM.
|
|
3
|
-
{
|
|
4
|
-
"compilerOptions": {
|
|
5
|
-
"target": "es2022",
|
|
6
|
-
"lib": ["es2022"],
|
|
7
|
-
"module": "es2022",
|
|
8
|
-
"moduleResolution": "bundler",
|
|
9
|
-
"types": [
|
|
10
|
-
"@cloudflare/workers-types",
|
|
11
|
-
"@cloudflare/vitest-pool-workers/types"
|
|
12
|
-
],
|
|
13
|
-
"strict": true,
|
|
14
|
-
"noUncheckedIndexedAccess": true,
|
|
15
|
-
"noEmit": true,
|
|
16
|
-
"skipLibCheck": true,
|
|
17
|
-
"isolatedModules": true,
|
|
18
|
-
"forceConsistentCasingInFileNames": true
|
|
19
|
-
},
|
|
20
|
-
"include": ["src", "test", "shared", "recipes"]
|
|
21
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { cloudflare } from "@cloudflare/vite-plugin";
|
|
2
|
-
import react from "@vitejs/plugin-react";
|
|
3
|
-
import { defineConfig } from "vite";
|
|
4
|
-
import { overlayMtimePlugin } from "./overlay-mtime.mjs";
|
|
5
|
-
|
|
6
|
-
// The Cloudflare plugin reads wrangler.jsonc, runs the Worker in the real
|
|
7
|
-
// Workers runtime during `vite dev`, and builds a deployable bundle with
|
|
8
|
-
// `vite build`. `vite dev` serves the React app and the /api/* Worker from a
|
|
9
|
-
// single origin, so no CORS or proxy config is needed.
|
|
10
|
-
export default defineConfig({
|
|
11
|
-
plugins: [overlayMtimePlugin(), react(), cloudflare()],
|
|
12
|
-
});
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import {
|
|
3
|
-
cloudflareTest,
|
|
4
|
-
readD1Migrations,
|
|
5
|
-
} from "@cloudflare/vitest-pool-workers";
|
|
6
|
-
import { defineConfig } from "vitest/config";
|
|
7
|
-
import { overlayMtimePlugin } from "./overlay-mtime.mjs";
|
|
8
|
-
|
|
9
|
-
// Runs the smoke tests inside the real Workers runtime (workerd) with local
|
|
10
|
-
// D1 and R2, so the tests exercise the same code paths as production. The D1
|
|
11
|
-
// migrations are applied to the test database in test/apply-migrations.ts.
|
|
12
|
-
export default defineConfig(async () => {
|
|
13
|
-
const migrations = await readD1Migrations(path.join(__dirname, "migrations"));
|
|
14
|
-
return {
|
|
15
|
-
plugins: [
|
|
16
|
-
overlayMtimePlugin(),
|
|
17
|
-
cloudflareTest({
|
|
18
|
-
wrangler: { configPath: "./wrangler.jsonc" },
|
|
19
|
-
miniflare: {
|
|
20
|
-
bindings: { TEST_MIGRATIONS: migrations },
|
|
21
|
-
},
|
|
22
|
-
}),
|
|
23
|
-
],
|
|
24
|
-
test: {
|
|
25
|
-
setupFiles: ["./test/apply-migrations.ts"],
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
});
|