Skip to content

Commit fc5ba0e

Browse files
committed
test(gateway): remove cron redaction casts
1 parent a68f720 commit fc5ba0e

2 files changed

Lines changed: 27 additions & 22 deletions

File tree

src/gateway/server-cron-notifications.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ vi.mock("../cron/delivery.js", async (importOriginal) => {
2323

2424
import { dispatchGatewayCronFinishedNotifications } from "./server-cron-notifications.js";
2525

26+
function requireRecord(value: unknown, label: string): Record<string, unknown> {
27+
if (!value || typeof value !== "object") {
28+
throw new Error(`expected ${label}`);
29+
}
30+
return value as Record<string, unknown>;
31+
}
32+
33+
function webhookRequestBody() {
34+
const request = requireRecord(mocks.fetchWithSsrFGuard.mock.calls[0]?.[0], "webhook request");
35+
const init = requireRecord(request.init, "webhook request init");
36+
return JSON.parse(String(init.body));
37+
}
38+
2639
describe("dispatchGatewayCronFinishedNotifications", () => {
2740
beforeEach(() => {
2841
vi.clearAllMocks();
@@ -182,10 +195,7 @@ describe("dispatchGatewayCronFinishedNotifications", () => {
182195
});
183196

184197
await vi.waitFor(() => expect(mocks.fetchWithSsrFGuard).toHaveBeenCalledTimes(1));
185-
const [request] = mocks.fetchWithSsrFGuard.mock.calls[0] as unknown as [
186-
{ init?: { body?: string } },
187-
];
188-
const body = JSON.parse(String(request.init?.body));
198+
const body = webhookRequestBody();
189199
expect(body.summary).toContain("[redacted-url]");
190200
expect(body.summary).toContain("[redacted-code]");
191201
expect(body.summary).toContain("token=***");
@@ -267,10 +277,7 @@ describe("dispatchGatewayCronFinishedNotifications", () => {
267277
});
268278

269279
await vi.waitFor(() => expect(mocks.fetchWithSsrFGuard).toHaveBeenCalledTimes(1));
270-
const [request] = mocks.fetchWithSsrFGuard.mock.calls[0] as unknown as [
271-
{ init?: { body?: string } },
272-
];
273-
const body = JSON.parse(String(request.init?.body));
280+
const body = webhookRequestBody();
274281
expect(body).toMatchObject({
275282
action: "finished",
276283
jobId: job.id,

src/gateway/server-cron.test.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -620,12 +620,10 @@ describe("buildGatewayCronService", () => {
620620
runCronChangedMock.mockClear();
621621
await state.cron.run(job.id, "force");
622622

623-
const hookEvents = runCronChangedMock.mock.calls as unknown as Array<
624-
[{ action?: string; summary?: string }]
625-
>;
626-
const event = hookEvents.find(([hookEvent]) => hookEvent.action === "finished")?.[0];
627-
expect(event).toBeDefined();
628-
const summary = event?.summary ?? "";
623+
const event = runCronChangedMock.mock.calls
624+
.map((call) => requireRecord(call[0], "cron_changed event"))
625+
.find((hookEvent) => hookEvent.action === "finished");
626+
const summary = String(event?.summary ?? "");
629627
expect(summary).toContain("[redacted-url]");
630628
expect(summary).toContain("[redacted-code]");
631629
expect(summary).toContain("token=***");
@@ -671,10 +669,11 @@ describe("buildGatewayCronService", () => {
671669

672670
await state.cron.run(job.id, "force");
673671

674-
const announceCalls = sendCronAnnouncePayloadStrictMock.mock.calls as unknown as Array<
675-
[{ message?: string }]
676-
>;
677-
const message = announceCalls[0]?.[0]?.message ?? "";
672+
const announcePayload = requireRecord(
673+
callArg(sendCronAnnouncePayloadStrictMock, 0, 0, "cron announce payload"),
674+
"cron announce payload",
675+
);
676+
const message = String(announcePayload.message ?? "");
678677
expect(message).toContain("token=***");
679678
expect(message).not.toContain("opaque-secret-value");
680679
} finally {
@@ -714,10 +713,9 @@ describe("buildGatewayCronService", () => {
714713

715714
expect(sendCronAnnouncePayloadStrictMock).not.toHaveBeenCalled();
716715

717-
const hookEvents = runCronChangedMock.mock.calls as unknown as Array<
718-
[{ action?: string; summary?: string }]
719-
>;
720-
const event = hookEvents.find(([hookEvent]) => hookEvent.action === "finished")?.[0];
716+
const event = runCronChangedMock.mock.calls
717+
.map((call) => requireRecord(call[0], "cron_changed event"))
718+
.find((hookEvent) => hookEvent.action === "finished");
721719
expect(event?.summary).toBe(summary);
722720
} finally {
723721
state.cron.stop();

0 commit comments

Comments
 (0)