Skip to content

Commit 720a3cd

Browse files
committed
test(sms): type request mocks
1 parent 0b835d1 commit 720a3cd

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

extensions/sms/src/inbound.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { describe, expect, it, vi } from "vitest";
22
import { dispatchSmsInboundEvent, type SmsChannelRuntime } from "./inbound.js";
33
import type { ResolvedSmsAccount } from "./types.js";
44

5+
type SendSmsViaTwilio = typeof import("./twilio.js").sendSmsViaTwilio;
6+
57
const sendSmsViaTwilio = vi.hoisted(() =>
6-
vi.fn(async () => ({ sid: "SM-pair", to: "+15551234567" })),
8+
vi.fn<SendSmsViaTwilio>(async () => ({ sid: "SM-pair", to: "+15551234567" })),
79
);
810

911
vi.mock("./twilio.js", () => ({

extensions/sms/src/twilio.test.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ function createAccount(overrides: Partial<ResolvedSmsAccount> = {}): ResolvedSms
2929
};
3030
}
3131

32+
function requireUrlSearchParamsBody(init: RequestInit | undefined): URLSearchParams {
33+
expect(init?.body).toBeInstanceOf(URLSearchParams);
34+
return init?.body as URLSearchParams;
35+
}
36+
3237
describe("Twilio SMS helpers", () => {
3338
it("parses Twilio form bodies and inbound messages", () => {
3439
const form = parseTwilioFormBody(
@@ -105,7 +110,7 @@ describe("Twilio SMS helpers", () => {
105110
});
106111

107112
it("sends SMS through Twilio's Messages API", async () => {
108-
const fetchImpl = vi.fn(
113+
const fetchImpl = vi.fn<typeof fetch>(
109114
async () =>
110115
new Response(
111116
JSON.stringify({
@@ -156,14 +161,14 @@ describe("Twilio SMS helpers", () => {
156161
authorization: `Basic ${Buffer.from("AC123:secret").toString("base64")}`,
157162
"content-type": "application/x-www-form-urlencoded",
158163
});
159-
const body = new URLSearchParams(String(init?.body));
164+
const body = requireUrlSearchParamsBody(init);
160165
expect(body.get("From")).toBe("+15557654321");
161166
expect(body.get("To")).toBe("+15551234567");
162167
expect(body.get("Body")).toBe("hello");
163168
});
164169

165170
it("can send through a Twilio Messaging Service SID", async () => {
166-
const fetchImpl = vi.fn(
171+
const fetchImpl = vi.fn<typeof fetch>(
167172
async () =>
168173
new Response(JSON.stringify({ sid: "SM789" }), {
169174
status: 201,
@@ -193,14 +198,14 @@ describe("Twilio SMS helpers", () => {
193198
});
194199

195200
const [, init] = fetchImpl.mock.calls[0] ?? [];
196-
const body = new URLSearchParams(String(init?.body));
201+
const body = requireUrlSearchParamsBody(init);
197202
expect(body.get("MessagingServiceSid")).toBe("MG123");
198203
expect(body.get("To")).toBe("+15551234567");
199204
expect(body.get("Body")).toBe("hello");
200205
});
201206

202207
it("prefers an explicit from number when both sender options are resolved", async () => {
203-
const fetchImpl = vi.fn(
208+
const fetchImpl = vi.fn<typeof fetch>(
204209
async () =>
205210
new Response(JSON.stringify({ sid: "SM999" }), {
206211
status: 201,
@@ -230,13 +235,13 @@ describe("Twilio SMS helpers", () => {
230235
});
231236

232237
const [, init] = fetchImpl.mock.calls[0] ?? [];
233-
const body = new URLSearchParams(String(init?.body));
238+
const body = requireUrlSearchParamsBody(init);
234239
expect(body.get("From")).toBe("+15557654321");
235240
expect(body.get("MessagingServiceSid")).toBeNull();
236241
});
237242

238243
it("throws structured Twilio errors from JSON error bodies", async () => {
239-
const fetchImpl = vi.fn(
244+
const fetchImpl = vi.fn<typeof fetch>(
240245
async () =>
241246
new Response(
242247
JSON.stringify({
@@ -266,7 +271,9 @@ describe("Twilio SMS helpers", () => {
266271
});
267272

268273
it("includes non-JSON Twilio error text in send failures", async () => {
269-
const fetchImpl = vi.fn(async () => new Response("upstream unavailable", { status: 503 }));
274+
const fetchImpl = vi.fn<typeof fetch>(
275+
async () => new Response("upstream unavailable", { status: 503 }),
276+
);
270277

271278
await expect(
272279
sendSmsViaTwilio({
@@ -279,7 +286,7 @@ describe("Twilio SMS helpers", () => {
279286
});
280287

281288
it("rejects malformed JSON from successful Twilio sends", async () => {
282-
const fetchImpl = vi.fn(async () => new Response("not json", { status: 201 }));
289+
const fetchImpl = vi.fn<typeof fetch>(async () => new Response("not json", { status: 201 }));
283290

284291
await expect(
285292
sendSmsViaTwilio({
@@ -304,7 +311,7 @@ describe("Twilio SMS helpers", () => {
304311
});
305312

306313
it("requires successful Twilio sends to include a Message SID", async () => {
307-
const fetchImpl = vi.fn(
314+
const fetchImpl = vi.fn<typeof fetch>(
308315
async () => new Response(JSON.stringify({ status: "queued" }), { status: 201 }),
309316
);
310317

0 commit comments

Comments
 (0)