Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2
3import { renderWebhookPrompt, type WebhookRequest } from "./webhookTemplate.ts";
4
5const githubPullRequest: WebhookRequest = {
6 method: "POST",
7 path: "/api/hooks/task-1",
8 query: "source=github",
9 headers: { "content-type": "application/json", "x-github-event": "pull_request" },
10 bodyText: JSON.stringify({
11 action: "opened",
12 pull_request: { url: "https://github.com/org/repo/pull/45", number: 45, labels: ["bug"] },
13 }),
14};
15
16describe("renderWebhookPrompt", () => {
17 it("sends only what the template names", () => {
18 const rendered = renderWebhookPrompt(
19 "Review this PR: {{body.pull_request.url}}",
20 githubPullRequest,
21 );
22 assert.equal(rendered.prompt, "Review this PR: https://github.com/org/repo/pull/45");
23 assert.deepEqual(rendered.missing, []);
24 });
25
26 it("reads headers case-insensitively, query parameters and array indexes", () => {
27 const rendered = renderWebhookPrompt(
28 "{{ headers.X-GitHub-Event }} #{{body.pull_request.number}} {{body.pull_request.labels.0}} {{query.source}}",
29 githubPullRequest,
30 );
31 assert.equal(rendered.prompt, "pull_request #45 bug github");
32 });
33
34 it("renders objects as JSON and the raw body and request on request", () => {
35 const rendered = renderWebhookPrompt(
36 "{{body.pull_request.labels}}|{{body}}",
37 githubPullRequest,
38 );
39 assert.equal(rendered.prompt, `[\n "bug"\n]|${githubPullRequest.bodyText}`);
40 const request = renderWebhookPrompt("{{request}}", githubPullRequest).prompt;
41 assert.isTrue(request.startsWith("POST /api/hooks/task-1?source=github\n"));
42 assert.include(request, "x-github-event: pull_request");
43 assert.isTrue(request.endsWith(githubPullRequest.bodyText));
44 });
45
46 it("renders missing fields empty and reports them once", () => {
47 const rendered = renderWebhookPrompt(
48 "a={{body.nope}} b={{body.nope}} c={{headers.x-missing}} d={{unknown}}",
49 githubPullRequest,
50 );
51 assert.equal(rendered.prompt, "a= b= c= d=");
52 assert.deepEqual(rendered.missing, ["body.nope", "headers.x-missing", "unknown"]);
53 });
54
55 it("addresses form-encoded bodies and leaves other bodies to {{body}}", () => {
56 const form = renderWebhookPrompt("{{body.text}} by {{body.user_name}}", {
57 ...githubPullRequest,
58 headers: { "content-type": "application/x-www-form-urlencoded" },
59 bodyText: "text=deploy+prod&user_name=alice",
60 });
61 assert.equal(form.prompt, "deploy prod by alice");
62
63 const plain = renderWebhookPrompt("{{body.field}}{{body}}", {
64 ...githubPullRequest,
65 headers: { "content-type": "text/plain" },
66 bodyText: "build failed",
67 });
68 assert.equal(plain.prompt, "build failed");
69 assert.deepEqual(plain.missing, ["body.field"]);
70 });
71
72 it("redacts credentials in whole-request placeholders but not named ones", () => {
73 const request: WebhookRequest = {
74 ...githubPullRequest,
75 query: "source=github&access_token=q-secret",
76 headers: {
77 ...githubPullRequest.headers,
78 authorization: "Bearer h-secret",
79 "x-hub-signature-256": "sha256=abc",
80 },
81 };
82 const whole = renderWebhookPrompt("{{request}}|{{headers}}|{{query}}", request).prompt;
83 for (const secret of ["q-secret", "h-secret", "sha256=abc"]) {
84 assert.notInclude(whole, secret);
85 }
86 assert.include(whole, "?source=github&access_token=[redacted]\n");
87 assert.include(whole, "authorization: [redacted]");
88 assert.include(whole, "x-github-event: pull_request");
89
90 const named = renderWebhookPrompt(
91 "{{headers.authorization}} {{query.access_token}}",
92 request,
93 ).prompt;
94 assert.equal(named, "Bearer h-secret q-secret");
95 });
96
97 it("does not resolve inherited object properties", () => {
98 const rendered = renderWebhookPrompt(
99 "{{body.constructor}}{{body.__proto__}}",
100 githubPullRequest,
101 );
102 assert.equal(rendered.prompt, "");
103 });
104});