cloudflare-to-claude-fix 1.0.121 → 1.0.123
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/package.json +1 -1
- package/test/queue.test.js +164 -0
package/package.json
CHANGED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import worker from "../src/index.js";
|
|
3
|
+
|
|
4
|
+
const env = {
|
|
5
|
+
ROUTINE_FIRE_URL: "https://claude.example/fire",
|
|
6
|
+
ROUTINE_FIRE_TOKEN: "tok-123",
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const failedEvt = {
|
|
10
|
+
status: "failed",
|
|
11
|
+
worker_name: "my-worker",
|
|
12
|
+
build_id: "b-1",
|
|
13
|
+
branch: "main",
|
|
14
|
+
commit_hash: "abcdef1234567",
|
|
15
|
+
author: "dev@example.com",
|
|
16
|
+
timestamp: "2026-01-02T03:04:05Z",
|
|
17
|
+
error_messages: ["line one", "line two"],
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const msg = (body) => ({ body, ack: vi.fn(), retry: vi.fn() });
|
|
21
|
+
const ok = (json) => ({ ok: true, json: async () => json, text: async () => "" });
|
|
22
|
+
|
|
23
|
+
let fetchMock;
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
fetchMock = vi.fn();
|
|
27
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
28
|
+
vi.spyOn(console, "log").mockImplementation(() => {});
|
|
29
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
vi.unstubAllGlobals();
|
|
34
|
+
vi.restoreAllMocks();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe("queue consumer", () => {
|
|
38
|
+
it("acks non-failed builds without firing the routine", async () => {
|
|
39
|
+
const m = msg({ ...failedEvt, status: "success" });
|
|
40
|
+
await worker.queue({ messages: [m] }, env);
|
|
41
|
+
expect(m.ack).toHaveBeenCalledTimes(1);
|
|
42
|
+
expect(m.retry).not.toHaveBeenCalled();
|
|
43
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("fires the routine with a formatted failure report and acks", async () => {
|
|
47
|
+
fetchMock.mockResolvedValueOnce(ok({ claude_code_session_url: "https://s/1" }));
|
|
48
|
+
const m = msg(failedEvt);
|
|
49
|
+
|
|
50
|
+
await worker.queue({ messages: [m] }, env);
|
|
51
|
+
|
|
52
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
53
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
54
|
+
expect(url).toBe(env.ROUTINE_FIRE_URL);
|
|
55
|
+
expect(init.method).toBe("POST");
|
|
56
|
+
expect(init.headers.Authorization).toBe("Bearer tok-123");
|
|
57
|
+
expect(init.headers["anthropic-version"]).toBe("2023-06-01");
|
|
58
|
+
const { text } = JSON.parse(init.body);
|
|
59
|
+
expect(text).toContain("Cloudflare Workers build FAILED");
|
|
60
|
+
expect(text).toContain("Worker : my-worker");
|
|
61
|
+
expect(text).toContain("Build : b-1");
|
|
62
|
+
expect(text).toContain("Branch : main");
|
|
63
|
+
expect(text).toContain("Commit : abcdef1234567");
|
|
64
|
+
expect(text).toContain("Author : dev@example.com");
|
|
65
|
+
expect(text).toContain("=== Error log ===\nline one\nline two");
|
|
66
|
+
expect(m.ack).toHaveBeenCalledTimes(1);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("falls back to placeholders when optional fields are missing", async () => {
|
|
70
|
+
fetchMock.mockResolvedValueOnce(ok({ claude_code_session_url: "u" }));
|
|
71
|
+
const m = msg({
|
|
72
|
+
status: "failed",
|
|
73
|
+
worker_name: "w",
|
|
74
|
+
build_id: "b",
|
|
75
|
+
timestamp: "t",
|
|
76
|
+
error_messages: [],
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
await worker.queue({ messages: [m] }, env);
|
|
80
|
+
|
|
81
|
+
const { text } = JSON.parse(fetchMock.mock.calls[0][1].body);
|
|
82
|
+
expect(text).toContain("Branch : (unknown)");
|
|
83
|
+
expect(text).toContain("Commit : (unknown)");
|
|
84
|
+
expect(text).toContain("Author : (unknown)");
|
|
85
|
+
expect(text).toContain("no structured error messages");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("truncates oversized logs to fit the 65,536 char limit", async () => {
|
|
89
|
+
fetchMock.mockResolvedValueOnce(ok({ claude_code_session_url: "u" }));
|
|
90
|
+
const m = msg({ ...failedEvt, error_messages: ["x".repeat(100_000)] });
|
|
91
|
+
|
|
92
|
+
await worker.queue({ messages: [m] }, env);
|
|
93
|
+
|
|
94
|
+
const { text } = JSON.parse(fetchMock.mock.calls[0][1].body);
|
|
95
|
+
expect(text.length).toBeLessThanOrEqual(65_536);
|
|
96
|
+
expect(text).toContain("log truncated to fit 65,536-char limit");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("notifies the webhook with a short commit when NOTIFY_WEBHOOK_URL is set", async () => {
|
|
100
|
+
fetchMock
|
|
101
|
+
.mockResolvedValueOnce(ok({ claude_code_session_url: "https://s/2" }))
|
|
102
|
+
.mockResolvedValueOnce({ ok: true });
|
|
103
|
+
const m = msg(failedEvt);
|
|
104
|
+
|
|
105
|
+
await worker.queue({ messages: [m] }, { ...env, NOTIFY_WEBHOOK_URL: "https://hook" });
|
|
106
|
+
|
|
107
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
108
|
+
const [hookUrl, hookInit] = fetchMock.mock.calls[1];
|
|
109
|
+
expect(hookUrl).toBe("https://hook");
|
|
110
|
+
const body = JSON.parse(hookInit.body);
|
|
111
|
+
expect(body.text).toBe(body.content);
|
|
112
|
+
expect(body.text).toContain("`my-worker`");
|
|
113
|
+
expect(body.text).toContain("`abcdef1`");
|
|
114
|
+
expect(body.text).toContain("https://s/2");
|
|
115
|
+
expect(m.ack).toHaveBeenCalled();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("uses '?' placeholders in the webhook text when branch/commit are absent", async () => {
|
|
119
|
+
fetchMock
|
|
120
|
+
.mockResolvedValueOnce(ok({ claude_code_session_url: "u" }))
|
|
121
|
+
.mockResolvedValueOnce({ ok: true });
|
|
122
|
+
const m = msg({ status: "failed", worker_name: "w", build_id: "b", timestamp: "t" });
|
|
123
|
+
|
|
124
|
+
await worker.queue({ messages: [m] }, { ...env, NOTIFY_WEBHOOK_URL: "https://hook" });
|
|
125
|
+
|
|
126
|
+
const body = JSON.parse(fetchMock.mock.calls[1][1].body);
|
|
127
|
+
expect(body.text).toContain("branch: `?`");
|
|
128
|
+
expect(body.text).toContain("commit: `?`");
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("retries when the routine endpoint responds with an error", async () => {
|
|
132
|
+
fetchMock.mockResolvedValueOnce({ ok: false, status: 500, text: async () => "oops" });
|
|
133
|
+
const m = msg(failedEvt);
|
|
134
|
+
|
|
135
|
+
await worker.queue({ messages: [m] }, env);
|
|
136
|
+
|
|
137
|
+
expect(m.retry).toHaveBeenCalledTimes(1);
|
|
138
|
+
expect(m.ack).not.toHaveBeenCalled();
|
|
139
|
+
expect(console.error).toHaveBeenCalled();
|
|
140
|
+
const err = console.error.mock.calls[0][1];
|
|
141
|
+
expect(err.message).toContain("Routine fire failed (500): oops");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("retries when fetch itself rejects", async () => {
|
|
145
|
+
fetchMock.mockRejectedValueOnce(new Error("network"));
|
|
146
|
+
const m = msg(failedEvt);
|
|
147
|
+
await worker.queue({ messages: [m] }, env);
|
|
148
|
+
expect(m.retry).toHaveBeenCalled();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("processes each message in a batch independently", async () => {
|
|
152
|
+
fetchMock.mockResolvedValueOnce({ ok: false, status: 502, text: async () => "bad" });
|
|
153
|
+
fetchMock.mockResolvedValueOnce(ok({ claude_code_session_url: "u" }));
|
|
154
|
+
const a = msg(failedEvt);
|
|
155
|
+
const b = msg({ ...failedEvt, build_id: "b-2" });
|
|
156
|
+
const c = msg({ ...failedEvt, status: "success" });
|
|
157
|
+
|
|
158
|
+
await worker.queue({ messages: [a, b, c] }, env);
|
|
159
|
+
|
|
160
|
+
expect(a.retry).toHaveBeenCalled();
|
|
161
|
+
expect(b.ack).toHaveBeenCalled();
|
|
162
|
+
expect(c.ack).toHaveBeenCalled();
|
|
163
|
+
});
|
|
164
|
+
});
|