@better-webhook/cli 3.8.0 → 3.10.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/LICENSE +0 -1
- package/dist/_binary_entry.js +29 -0
- package/dist/commands/capture.d.ts +2 -0
- package/dist/commands/capture.js +33 -0
- package/dist/commands/captures.d.ts +2 -0
- package/dist/commands/captures.js +316 -0
- package/dist/commands/dashboard.d.ts +2 -0
- package/dist/commands/dashboard.js +70 -0
- package/dist/commands/index.d.ts +6 -0
- package/dist/commands/index.js +6 -0
- package/dist/commands/replay.d.ts +2 -0
- package/dist/commands/replay.js +140 -0
- package/dist/commands/run.d.ts +2 -0
- package/dist/commands/run.js +182 -0
- package/dist/commands/templates.d.ts +2 -0
- package/dist/commands/templates.js +285 -0
- package/dist/core/capture-server.d.ts +37 -0
- package/dist/core/capture-server.js +400 -0
- package/dist/core/capture-server.test.d.ts +1 -0
- package/dist/core/capture-server.test.js +86 -0
- package/dist/core/cli-version.d.ts +1 -0
- package/dist/core/cli-version.js +30 -0
- package/dist/core/cli-version.test.d.ts +1 -0
- package/dist/core/cli-version.test.js +42 -0
- package/dist/core/dashboard-api.d.ts +8 -0
- package/dist/core/dashboard-api.js +333 -0
- package/dist/core/dashboard-server.d.ts +24 -0
- package/dist/core/dashboard-server.js +224 -0
- package/dist/core/debug-output.d.ts +3 -0
- package/dist/core/debug-output.js +69 -0
- package/dist/core/debug-verify.d.ts +25 -0
- package/dist/core/debug-verify.js +253 -0
- package/dist/core/executor.d.ts +11 -0
- package/dist/core/executor.js +152 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.js +5 -0
- package/dist/core/replay-engine.d.ts +20 -0
- package/dist/core/replay-engine.js +293 -0
- package/dist/core/replay-engine.test.d.ts +1 -0
- package/dist/core/replay-engine.test.js +482 -0
- package/dist/core/runtime-paths.d.ts +2 -0
- package/dist/core/runtime-paths.js +65 -0
- package/dist/core/runtime-paths.test.d.ts +1 -0
- package/dist/core/runtime-paths.test.js +50 -0
- package/dist/core/signature.d.ts +25 -0
- package/dist/core/signature.js +224 -0
- package/dist/core/signature.test.d.ts +1 -0
- package/dist/core/signature.test.js +38 -0
- package/dist/core/template-manager.d.ts +33 -0
- package/dist/core/template-manager.js +313 -0
- package/dist/core/template-manager.test.d.ts +1 -0
- package/dist/core/template-manager.test.js +236 -0
- package/dist/index.cjs +3472 -262
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3509 -276
- package/dist/types/index.d.ts +312 -0
- package/dist/types/index.js +87 -0
- package/package.json +1 -1
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { existsSync, rmSync, mkdirSync, readFileSync } from "fs";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
import { tmpdir } from "os";
|
|
5
|
+
import { TemplateManager } from "./template-manager.js";
|
|
6
|
+
describe("TemplateManager", () => {
|
|
7
|
+
let tempDir;
|
|
8
|
+
let manager;
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
tempDir = join(tmpdir(), `better-webhook-test-${Date.now()}`);
|
|
11
|
+
mkdirSync(tempDir, { recursive: true });
|
|
12
|
+
manager = new TemplateManager(tempDir);
|
|
13
|
+
});
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
if (existsSync(tempDir)) {
|
|
16
|
+
rmSync(tempDir, { recursive: true, force: true });
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
describe("templateExists", () => {
|
|
20
|
+
it("should return false when template does not exist", () => {
|
|
21
|
+
expect(manager.templateExists("non-existent")).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
it("should return true when template exists", () => {
|
|
24
|
+
const template = {
|
|
25
|
+
url: "http://localhost:3000/webhooks",
|
|
26
|
+
method: "POST",
|
|
27
|
+
headers: [],
|
|
28
|
+
body: { test: true },
|
|
29
|
+
provider: "github",
|
|
30
|
+
event: "push",
|
|
31
|
+
};
|
|
32
|
+
manager.saveUserTemplate(template, { id: "test-template" });
|
|
33
|
+
expect(manager.templateExists("test-template")).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
describe("saveUserTemplate", () => {
|
|
37
|
+
it("should save a template with explicit ID", () => {
|
|
38
|
+
const template = {
|
|
39
|
+
url: "http://localhost:3000/webhooks",
|
|
40
|
+
method: "POST",
|
|
41
|
+
headers: [{ key: "content-type", value: "application/json" }],
|
|
42
|
+
body: { action: "created" },
|
|
43
|
+
provider: "github",
|
|
44
|
+
event: "push",
|
|
45
|
+
};
|
|
46
|
+
const result = manager.saveUserTemplate(template, {
|
|
47
|
+
id: "my-template",
|
|
48
|
+
name: "My Template",
|
|
49
|
+
description: "A test template",
|
|
50
|
+
});
|
|
51
|
+
expect(result.id).toBe("my-template");
|
|
52
|
+
expect(result.filePath).toContain("my-template.json");
|
|
53
|
+
expect(existsSync(result.filePath)).toBe(true);
|
|
54
|
+
const saved = JSON.parse(readFileSync(result.filePath, "utf-8"));
|
|
55
|
+
expect(saved._metadata.id).toBe("my-template");
|
|
56
|
+
expect(saved._metadata.name).toBe("My Template");
|
|
57
|
+
expect(saved._metadata.description).toBe("A test template");
|
|
58
|
+
expect(saved._metadata.source).toBe("capture");
|
|
59
|
+
expect(saved._metadata.provider).toBe("github");
|
|
60
|
+
expect(saved._metadata.event).toBe("push");
|
|
61
|
+
});
|
|
62
|
+
it("should auto-generate ID from provider and event", () => {
|
|
63
|
+
const template = {
|
|
64
|
+
url: "http://localhost:3000/webhooks",
|
|
65
|
+
method: "POST",
|
|
66
|
+
headers: [],
|
|
67
|
+
body: {},
|
|
68
|
+
provider: "stripe",
|
|
69
|
+
event: "payment.succeeded",
|
|
70
|
+
};
|
|
71
|
+
const result = manager.saveUserTemplate(template);
|
|
72
|
+
expect(result.id).toBe("stripe-payment.succeeded");
|
|
73
|
+
});
|
|
74
|
+
it("should generate unique ID when base ID already exists", () => {
|
|
75
|
+
const template = {
|
|
76
|
+
url: "http://localhost:3000/webhooks",
|
|
77
|
+
method: "POST",
|
|
78
|
+
headers: [],
|
|
79
|
+
body: {},
|
|
80
|
+
provider: "github",
|
|
81
|
+
event: "push",
|
|
82
|
+
};
|
|
83
|
+
const result1 = manager.saveUserTemplate(template);
|
|
84
|
+
expect(result1.id).toBe("github-push");
|
|
85
|
+
const result2 = manager.saveUserTemplate(template);
|
|
86
|
+
expect(result2.id).toBe("github-push-1");
|
|
87
|
+
const result3 = manager.saveUserTemplate(template);
|
|
88
|
+
expect(result3.id).toBe("github-push-2");
|
|
89
|
+
});
|
|
90
|
+
it("should throw error when template exists and overwrite is false", () => {
|
|
91
|
+
const template = {
|
|
92
|
+
url: "http://localhost:3000/webhooks",
|
|
93
|
+
method: "POST",
|
|
94
|
+
headers: [],
|
|
95
|
+
body: {},
|
|
96
|
+
};
|
|
97
|
+
manager.saveUserTemplate(template, { id: "existing-template" });
|
|
98
|
+
expect(() => {
|
|
99
|
+
manager.saveUserTemplate(template, { id: "existing-template" });
|
|
100
|
+
}).toThrow('Template with ID "existing-template" already exists');
|
|
101
|
+
});
|
|
102
|
+
it("should overwrite when overwrite option is true", () => {
|
|
103
|
+
const template1 = {
|
|
104
|
+
url: "http://localhost:3000/webhooks",
|
|
105
|
+
method: "POST",
|
|
106
|
+
headers: [],
|
|
107
|
+
body: { version: 1 },
|
|
108
|
+
};
|
|
109
|
+
const template2 = {
|
|
110
|
+
url: "http://localhost:3000/webhooks",
|
|
111
|
+
method: "POST",
|
|
112
|
+
headers: [],
|
|
113
|
+
body: { version: 2 },
|
|
114
|
+
};
|
|
115
|
+
manager.saveUserTemplate(template1, { id: "my-template" });
|
|
116
|
+
const result = manager.saveUserTemplate(template2, {
|
|
117
|
+
id: "my-template",
|
|
118
|
+
overwrite: true,
|
|
119
|
+
});
|
|
120
|
+
const saved = JSON.parse(readFileSync(result.filePath, "utf-8"));
|
|
121
|
+
expect(saved.body.version).toBe(2);
|
|
122
|
+
});
|
|
123
|
+
it("should use custom provider as default", () => {
|
|
124
|
+
const template = {
|
|
125
|
+
url: "http://localhost:3000/webhooks",
|
|
126
|
+
method: "POST",
|
|
127
|
+
headers: [],
|
|
128
|
+
body: {},
|
|
129
|
+
};
|
|
130
|
+
const result = manager.saveUserTemplate(template);
|
|
131
|
+
expect(result.id).toBe("custom-webhook");
|
|
132
|
+
const saved = JSON.parse(readFileSync(result.filePath, "utf-8"));
|
|
133
|
+
expect(saved._metadata.provider).toBe("custom");
|
|
134
|
+
});
|
|
135
|
+
it("should use event option over template event", () => {
|
|
136
|
+
const template = {
|
|
137
|
+
url: "http://localhost:3000/webhooks",
|
|
138
|
+
method: "POST",
|
|
139
|
+
headers: [],
|
|
140
|
+
body: {},
|
|
141
|
+
provider: "github",
|
|
142
|
+
event: "push",
|
|
143
|
+
};
|
|
144
|
+
const result = manager.saveUserTemplate(template, {
|
|
145
|
+
event: "pull_request",
|
|
146
|
+
});
|
|
147
|
+
expect(result.id).toBe("github-pull_request");
|
|
148
|
+
const saved = JSON.parse(readFileSync(result.filePath, "utf-8"));
|
|
149
|
+
expect(saved._metadata.event).toBe("pull_request");
|
|
150
|
+
});
|
|
151
|
+
it("should create provider subdirectory", () => {
|
|
152
|
+
const template = {
|
|
153
|
+
url: "http://localhost:3000/webhooks",
|
|
154
|
+
method: "POST",
|
|
155
|
+
headers: [],
|
|
156
|
+
body: {},
|
|
157
|
+
provider: "slack",
|
|
158
|
+
event: "message",
|
|
159
|
+
};
|
|
160
|
+
const result = manager.saveUserTemplate(template);
|
|
161
|
+
expect(result.filePath).toContain("slack");
|
|
162
|
+
expect(existsSync(join(tempDir, "templates", "slack"))).toBe(true);
|
|
163
|
+
});
|
|
164
|
+
it("should include createdAt in metadata", () => {
|
|
165
|
+
const template = {
|
|
166
|
+
url: "http://localhost:3000/webhooks",
|
|
167
|
+
method: "POST",
|
|
168
|
+
headers: [],
|
|
169
|
+
body: {},
|
|
170
|
+
};
|
|
171
|
+
const beforeSave = new Date();
|
|
172
|
+
const result = manager.saveUserTemplate(template, { id: "test" });
|
|
173
|
+
const afterSave = new Date();
|
|
174
|
+
const saved = JSON.parse(readFileSync(result.filePath, "utf-8"));
|
|
175
|
+
const createdAt = new Date(saved._metadata.createdAt);
|
|
176
|
+
expect(createdAt.getTime()).toBeGreaterThanOrEqual(beforeSave.getTime());
|
|
177
|
+
expect(createdAt.getTime()).toBeLessThanOrEqual(afterSave.getTime());
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
describe("integration with listLocalTemplates", () => {
|
|
181
|
+
it("should list saved user templates", () => {
|
|
182
|
+
const template = {
|
|
183
|
+
url: "http://localhost:3000/webhooks",
|
|
184
|
+
method: "POST",
|
|
185
|
+
headers: [],
|
|
186
|
+
body: { test: true },
|
|
187
|
+
provider: "github",
|
|
188
|
+
event: "push",
|
|
189
|
+
};
|
|
190
|
+
manager.saveUserTemplate(template, {
|
|
191
|
+
id: "user-template",
|
|
192
|
+
name: "User Template",
|
|
193
|
+
});
|
|
194
|
+
const templates = manager.listLocalTemplates();
|
|
195
|
+
expect(templates).toHaveLength(1);
|
|
196
|
+
expect(templates[0].id).toBe("user-template");
|
|
197
|
+
expect(templates[0].metadata.name).toBe("User Template");
|
|
198
|
+
expect(templates[0].metadata.source).toBe("capture");
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
describe("integration with getLocalTemplate", () => {
|
|
202
|
+
it("should retrieve saved user template by ID", () => {
|
|
203
|
+
const template = {
|
|
204
|
+
url: "http://localhost:3000/webhooks",
|
|
205
|
+
method: "POST",
|
|
206
|
+
headers: [{ key: "x-custom", value: "test" }],
|
|
207
|
+
body: { data: "value" },
|
|
208
|
+
provider: "stripe",
|
|
209
|
+
event: "invoice.paid",
|
|
210
|
+
};
|
|
211
|
+
manager.saveUserTemplate(template, { id: "stripe-test" });
|
|
212
|
+
const retrieved = manager.getLocalTemplate("stripe-test");
|
|
213
|
+
expect(retrieved).not.toBeNull();
|
|
214
|
+
expect(retrieved.id).toBe("stripe-test");
|
|
215
|
+
expect(retrieved.template.body).toEqual({ data: "value" });
|
|
216
|
+
expect(retrieved.template.headers).toEqual([
|
|
217
|
+
{ key: "x-custom", value: "test" },
|
|
218
|
+
]);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
describe("integration with deleteLocalTemplate", () => {
|
|
222
|
+
it("should delete saved user template", () => {
|
|
223
|
+
const template = {
|
|
224
|
+
url: "http://localhost:3000/webhooks",
|
|
225
|
+
method: "POST",
|
|
226
|
+
headers: [],
|
|
227
|
+
body: {},
|
|
228
|
+
};
|
|
229
|
+
manager.saveUserTemplate(template, { id: "to-delete" });
|
|
230
|
+
expect(manager.templateExists("to-delete")).toBe(true);
|
|
231
|
+
const deleted = manager.deleteLocalTemplate("to-delete");
|
|
232
|
+
expect(deleted).toBe(true);
|
|
233
|
+
expect(manager.templateExists("to-delete")).toBe(false);
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
});
|