@pplethai/cli 0.12.0 → 0.13.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/dist/bin.js +109 -109
- package/package.json +1 -1
- package/templates/golden-path/CLAUDE.md +10 -0
- package/templates/golden-path/README.md +6 -0
- package/templates/golden-path/shared/api.ts +94 -0
- package/templates/golden-path/src/index.ts +19 -3
- package/templates/golden-path/src/notification-response.ts +55 -0
- package/templates/golden-path/src/notify.ts +127 -8
- package/templates/golden-path/src/registry.ts +3 -0
- package/templates/golden-path/test/harness.ts +7 -0
- package/templates/golden-path/test/interaction.test.ts +186 -0
- package/templates/golden-path/test/notification-response.test.ts +79 -0
|
@@ -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
|
+
});
|