@pplethai/cli 0.12.0 → 0.14.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.
@@ -0,0 +1,230 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import type { Interaction } from "../shared/api";
3
+ import { makeHarness } from "./harness";
4
+
5
+ const NOTIFY_ENV = {
6
+ NOTIFY_API_BASE: "https://today.test/external",
7
+ NOTIFY_API_KEY: "app-key",
8
+ };
9
+
10
+ const interaction: Interaction = {
11
+ ref: "leave:123",
12
+ expiresAt: "2026-11-17T00:00:00Z",
13
+ tray: false,
14
+ fields: [{ id: "note", type: "text", label: "เหตุผล", multiline: true, maxLength: 300 }],
15
+ actions: [
16
+ { id: "approve", label: "อนุมัติ", icon: "check", style: "primary" },
17
+ {
18
+ id: "reject",
19
+ label: "ไม่อนุมัติ",
20
+ style: "destructive",
21
+ requires: ["note"],
22
+ confirm: { title: "ยืนยันการไม่อนุมัติ?" },
23
+ },
24
+ ],
25
+ };
26
+
27
+ const direct = { kind: "direct", recipients: [{ sub: "u-1" }] };
28
+
29
+ const sendWith = (h: ReturnType<typeof makeHarness>, extra: Record<string, unknown> = {}) =>
30
+ h.request("/api/notify", {
31
+ method: "POST",
32
+ headers: { "content-type": "application/json" },
33
+ body: JSON.stringify({ audience: direct, title: "Leave", body: "Please answer", ...extra }),
34
+ });
35
+
36
+ const refusal = (status: number, code: string, message = "refused") => ({
37
+ notify: () => Response.json({ error: { code, message } }, { status }),
38
+ });
39
+
40
+ describe("interactive notifications: sending", () => {
41
+ it("passes the interaction to today-v2 exactly as written", async () => {
42
+ const h = makeHarness(
43
+ {
44
+ notify: () =>
45
+ Response.json(
46
+ { notificationId: "n_1", results: [{ status: "delivered" }] },
47
+ { status: 201 },
48
+ ),
49
+ },
50
+ NOTIFY_ENV,
51
+ );
52
+ const res = await sendWith(h, { interaction });
53
+ expect(res.status).toBe(200);
54
+ const payload = await h.calls.notify[0]!.json<Record<string, unknown>>();
55
+ expect(payload.interaction).toEqual(interaction);
56
+ });
57
+
58
+ it("does not add an interaction key to a send without one", async () => {
59
+ const h = makeHarness(
60
+ { notify: () => Response.json({ notificationId: "n_1", results: [] }) },
61
+ NOTIFY_ENV,
62
+ );
63
+ await sendWith(h);
64
+ const payload = await h.calls.notify[0]!.json<Record<string, unknown>>();
65
+ expect("interaction" in payload).toBe(false);
66
+ });
67
+
68
+ it("returns the notificationId for a Direct and a Broadcast send", async () => {
69
+ const h = makeHarness(
70
+ {
71
+ notify: () =>
72
+ Response.json({ notificationId: "n_7", results: [{ status: "delivered" }] }),
73
+ },
74
+ NOTIFY_ENV,
75
+ );
76
+ expect(await (await sendWith(h)).json()).toMatchObject({ ok: true, notificationId: "n_7" });
77
+
78
+ const b = makeHarness(
79
+ { notify: () => Response.json({ notificationId: "n_8" }, { status: 201 }) },
80
+ NOTIFY_ENV,
81
+ );
82
+ expect(await (await sendWith(b, { audience: { kind: "all" } })).json()).toEqual({
83
+ ok: true,
84
+ notificationId: "n_8",
85
+ });
86
+ });
87
+
88
+ it("answers without a notificationId when today-v2 gave none", async () => {
89
+ const h = makeHarness({ notify: () => new Response(null, { status: 202 }) }, NOTIFY_ENV);
90
+ expect(await (await sendWith(h, { audience: { kind: "all" } })).json()).toEqual({ ok: true });
91
+ });
92
+
93
+ it("names the failing path when today-v2 refuses the interaction", async () => {
94
+ const h = makeHarness(
95
+ refusal(
96
+ 400,
97
+ "NOTIFICATION_INVALID_INTERACTION",
98
+ "interaction.actions[1].requires[0]: unknown field",
99
+ ),
100
+ NOTIFY_ENV,
101
+ );
102
+ const res = await sendWith(h, { interaction });
103
+ expect(res.status).toBe(400);
104
+ expect(await res.json()).toEqual({
105
+ error: "invalid_interaction",
106
+ message: "interaction.actions[1].requires[0]: unknown field",
107
+ });
108
+ });
109
+
110
+ describe("asking for a Live Activity", () => {
111
+ const urgent: Interaction = {
112
+ ref: "door:7",
113
+ expiresAt: "2026-11-10T16:00:00Z",
114
+ liveActivity: true,
115
+ actions: [
116
+ { id: "open", label: "เปิดประตู", icon: "check", style: "primary" },
117
+ { id: "deny", label: "ไม่เปิด", icon: "x", style: "destructive" },
118
+ ],
119
+ };
120
+
121
+ it("passes liveActivity to today-v2 unchanged", async () => {
122
+ const h = makeHarness(
123
+ {
124
+ notify: () =>
125
+ Response.json(
126
+ { notificationId: "n_2", results: [{ status: "delivered" }] },
127
+ { status: 201 },
128
+ ),
129
+ },
130
+ NOTIFY_ENV,
131
+ );
132
+ const res = await sendWith(h, { interaction: urgent });
133
+ expect(res.status).toBe(200);
134
+ const payload = await h.calls.notify[0]!.json<{ interaction: Record<string, unknown> }>();
135
+ expect(payload.interaction).toEqual(urgent);
136
+ });
137
+
138
+ it("surfaces today-v2's refusal of a Live Activity as invalid_interaction", async () => {
139
+ const h = makeHarness(
140
+ refusal(400, "NOTIFICATION_INVALID_INTERACTION", "interaction.expiresAt: at most 8 h ahead"),
141
+ NOTIFY_ENV,
142
+ );
143
+ const res = await sendWith(h, {
144
+ interaction: { ...urgent, expiresAt: "2026-11-17T00:00:00Z" },
145
+ });
146
+ expect(res.status).toBe(400);
147
+ expect(await res.json()).toEqual({
148
+ error: "invalid_interaction",
149
+ message: "interaction.expiresAt: at most 8 h ahead",
150
+ });
151
+ });
152
+ });
153
+
154
+ it.each(["NOTIFICATION_INTERACTION_NO_CALLBACK", "INTERACTIVE_NOTIFICATIONS_DISABLED"])(
155
+ "reports %s as interaction_unavailable",
156
+ async (code) => {
157
+ const h = makeHarness(refusal(400, code), NOTIFY_ENV);
158
+ const res = await sendWith(h, { interaction });
159
+ expect(res.status).toBe(400);
160
+ expect(await res.json()).toMatchObject({ error: "interaction_unavailable" });
161
+ },
162
+ );
163
+
164
+ it("still reads a plain 400 as invalid_recipients", async () => {
165
+ const h = makeHarness(refusal(400, "NOTIFICATION_INVALID_RECIPIENTS"), NOTIFY_ENV);
166
+ expect(await (await sendWith(h)).json()).toMatchObject({ error: "invalid_recipients" });
167
+ });
168
+ });
169
+
170
+ describe("interactive notifications: closing early", () => {
171
+ const close = (h: ReturnType<typeof makeHarness>, id = "n_1") =>
172
+ h.request(`/api/notify/${id}/close`, { method: "POST" });
173
+
174
+ it("calls today-v2's close endpoint with the app's key", async () => {
175
+ const h = makeHarness(
176
+ { close: () => Response.json({ closedAt: "2026-11-10T00:00:00Z" }) },
177
+ NOTIFY_ENV,
178
+ );
179
+ const res = await close(h);
180
+ expect(res.status).toBe(200);
181
+ expect(await res.json()).toEqual({ ok: true, closedAt: "2026-11-10T00:00:00Z" });
182
+ const sent = h.calls.close[0]!;
183
+ expect(sent.method).toBe("POST");
184
+ expect(new URL(sent.url).pathname).toBe("/external/notifications/n_1/interaction/close");
185
+ expect(sent.headers.get("authorization")).toBe("Bearer app-key");
186
+ });
187
+
188
+ it("is idempotent: closing twice is two successes", async () => {
189
+ const h = makeHarness(
190
+ { close: () => Response.json({ closedAt: "2026-11-10T00:00:00Z" }) },
191
+ NOTIFY_ENV,
192
+ );
193
+ expect((await close(h)).status).toBe(200);
194
+ expect((await close(h)).status).toBe(200);
195
+ });
196
+
197
+ it("reports a 404 as notification_not_found", async () => {
198
+ const h = makeHarness(
199
+ {
200
+ close: () =>
201
+ Response.json({ error: { code: "NOTIFICATION_NOT_FOUND" } }, { status: 404 }),
202
+ },
203
+ NOTIFY_ENV,
204
+ );
205
+ const res = await close(h);
206
+ expect(res.status).toBe(404);
207
+ expect(await res.json()).toMatchObject({ error: "notification_not_found" });
208
+ });
209
+
210
+ it("reports the flag being off as interaction_unavailable", async () => {
211
+ const h = makeHarness(
212
+ {
213
+ close: () =>
214
+ Response.json(
215
+ { error: { code: "INTERACTIVE_NOTIFICATIONS_DISABLED" } },
216
+ { status: 400 },
217
+ ),
218
+ },
219
+ NOTIFY_ENV,
220
+ );
221
+ expect(await (await close(h)).json()).toMatchObject({ error: "interaction_unavailable" });
222
+ });
223
+
224
+ it("fails loud when no key is bound", async () => {
225
+ const h = makeHarness({}, { NOTIFY_API_BASE: "" });
226
+ const res = await close(h);
227
+ expect(res.status).toBe(503);
228
+ expect(await res.json()).toMatchObject({ error: "notifications_not_configured" });
229
+ });
230
+ });
@@ -0,0 +1,79 @@
1
+ // How to test a Notification Response handler (docs/adr/0035). The platform
2
+ // wrapper hands your handler the parsed event and your env, so a test calls
3
+ // `notificationResponse` on the default export directly with a sample event.
4
+ // There is no request to build and no secret to send. The wrapper's own work
5
+ // (the status mapping, 404 for no handler) is the platform's, not yours to test.
6
+
7
+ import { afterEach, describe, expect, it } from "vitest";
8
+ import worker from "../src/index";
9
+ import type { NotificationResponseEvent } from "../src/notification-response";
10
+ import { makeHarness, testExecutionContext } from "./harness";
11
+
12
+ const sample = (overrides: Partial<NotificationResponseEvent> = {}): NotificationResponseEvent => ({
13
+ type: "notification.response",
14
+ id: "clx1",
15
+ notificationId: "clw1",
16
+ ref: "leave:123",
17
+ app: { slug: "leave-request" },
18
+ responder: { sub: "sub-1" },
19
+ action: "reject",
20
+ values: { note: "ไม่ว่าง" },
21
+ respondedAt: "2026-11-10T07:03:00.000Z",
22
+ attempt: 1,
23
+ ...overrides,
24
+ });
25
+
26
+ afterEach(() => {
27
+ delete worker.notificationResponse;
28
+ });
29
+
30
+ describe("a Notification Response handler", () => {
31
+ it("is not defined until you add one, which the platform answers with 404 so answers keep coming", () => {
32
+ expect(worker.notificationResponse).toBeUndefined();
33
+ });
34
+
35
+ it("is called with the event, and counts an answer once however often it is delivered", async () => {
36
+ const decisions = new Map<string, string>();
37
+ worker.notificationResponse = async (event) => {
38
+ // At-least-once delivery: the same id may come again, so key on it.
39
+ if (decisions.has(event.id)) return;
40
+ decisions.set(event.id, `${event.ref}:${event.action}`);
41
+ };
42
+ const h = makeHarness();
43
+ const ctx = testExecutionContext();
44
+
45
+ // Returning nothing is a 204: delivered.
46
+ expect(await worker.notificationResponse(sample(), h.testEnv, ctx)).toBeUndefined();
47
+ await worker.notificationResponse(sample({ attempt: 2 }), h.testEnv, ctx);
48
+
49
+ expect([...decisions]).toEqual([["clx1", "leave:123:reject"]]);
50
+ });
51
+
52
+ it("can opt out of answers by returning a 410", async () => {
53
+ worker.notificationResponse = () => new Response(null, { status: 410 });
54
+ const h = makeHarness();
55
+
56
+ const result = await worker.notificationResponse(sample(), h.testEnv, testExecutionContext());
57
+
58
+ expect((result as Response).status).toBe(410);
59
+ });
60
+
61
+ it("can leave slow work to waitUntil, after the answer is acknowledged", async () => {
62
+ const background: Promise<unknown>[] = [];
63
+ const ctx = { ...testExecutionContext(), waitUntil: (p: Promise<unknown>) => void background.push(p) };
64
+ let notified = false;
65
+ worker.notificationResponse = (_event, _env, handlerCtx) => {
66
+ handlerCtx.waitUntil(
67
+ (async () => {
68
+ notified = true;
69
+ })(),
70
+ );
71
+ };
72
+ const h = makeHarness();
73
+
74
+ await worker.notificationResponse(sample(), h.testEnv, ctx as unknown as ExecutionContext);
75
+ await Promise.all(background);
76
+
77
+ expect(notified).toBe(true);
78
+ });
79
+ });