Skip to content

Commit e446679

Browse files
authored
perf(test): reuse SMS test modules (#125151)
1 parent 3b8f9fb commit e446679

2 files changed

Lines changed: 51 additions & 63 deletions

File tree

‎extensions/sms/src/channel.test.ts‎

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
// Sms tests cover channel plugin behavior.
22
import { isChannelPartialDeliveryError } from "openclaw/plugin-sdk/channel-inbound";
3+
import { PlatformMessageNotDispatchedError } from "openclaw/plugin-sdk/error-runtime";
34
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5+
import { smsPlugin } from "./channel.js";
46
import type { SmsDeliveryRecord } from "./delivery-observations.js";
57
import type { probeSmsAccount as probeSmsAccountType } from "./status.js";
68
import type { sendSmsViaTwilio as sendSmsViaTwilioType } from "./twilio.js";
79

8-
type ChannelModule = typeof import("./channel.js");
9-
type PlatformMessageNotDispatchedErrorConstructor =
10-
(typeof import("openclaw/plugin-sdk/error-runtime"))["PlatformMessageNotDispatchedError"];
11-
12-
let smsPlugin: ChannelModule["smsPlugin"];
13-
let PlatformMessageNotDispatchedError: PlatformMessageNotDispatchedErrorConstructor;
14-
1510
const sendSmsViaTwilio = vi.hoisted(() =>
1611
vi.fn<typeof sendSmsViaTwilioType>(async ({ to, onPlatformSendDispatch }) => {
1712
await onPlatformSendDispatch?.();
@@ -45,8 +40,25 @@ const probeSmsAccount = vi.hoisted(() =>
4540
})),
4641
);
4742

48-
beforeEach(async () => {
49-
vi.resetModules();
43+
vi.mock("./twilio.js", async (importOriginal) => ({
44+
...(await importOriginal<typeof import("./twilio.js")>()),
45+
sendSmsViaTwilio,
46+
}));
47+
vi.mock("./media.js", async (importOriginal) => ({
48+
...(await importOriginal<typeof import("./media.js")>()),
49+
prepareHostedSmsMedia: hostedMediaMocks.prepare,
50+
}));
51+
vi.mock("./delivery-observations.js", async (importOriginal) => ({
52+
...(await importOriginal<typeof import("./delivery-observations.js")>()),
53+
listRecentSmsDeliveryRecords,
54+
recordInitialSmsDeliveryResult,
55+
}));
56+
vi.mock("./status.js", async (importOriginal) => ({
57+
...(await importOriginal<typeof import("./status.js")>()),
58+
probeSmsAccount,
59+
}));
60+
61+
beforeEach(() => {
5062
sendSmsViaTwilio.mockReset();
5163
sendSmsViaTwilio.mockImplementation(async ({ to, onPlatformSendDispatch }) => {
5264
await onPlatformSendDispatch?.();
@@ -68,31 +80,10 @@ beforeEach(async () => {
6880
recordInitialSmsDeliveryResult.mockReset();
6981
recordInitialSmsDeliveryResult.mockResolvedValue(null);
7082
probeSmsAccount.mockClear();
71-
vi.doMock("./twilio.js", () => ({
72-
sendSmsViaTwilio,
73-
TWILIO_MESSAGE_BODY_MAX_LENGTH: 1600,
74-
}));
75-
vi.doMock("./media.js", () => ({
76-
prepareHostedSmsMedia: hostedMediaMocks.prepare,
77-
}));
78-
vi.doMock("./delivery-observations.js", () => ({
79-
listRecentSmsDeliveryRecords,
80-
recordInitialSmsDeliveryResult,
81-
}));
82-
vi.doMock("./status.js", () => ({
83-
probeSmsAccount,
84-
formatSmsProbeLines: vi.fn(() => []),
85-
}));
86-
({ PlatformMessageNotDispatchedError } = await import("openclaw/plugin-sdk/error-runtime"));
87-
({ smsPlugin } = await import("./channel.js"));
8883
});
8984

9085
afterEach(() => {
9186
vi.useRealTimers();
92-
vi.doUnmock("./twilio.js");
93-
vi.doUnmock("./media.js");
94-
vi.doUnmock("./delivery-observations.js");
95-
vi.doUnmock("./status.js");
9687
});
9788

9889
describe("smsPlugin status", () => {

‎extensions/sms/src/send.test.ts‎

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
// Sms tests cover send plugin behavior.
22
import { isChannelPartialDeliveryError } from "openclaw/plugin-sdk/channel-inbound";
3+
import { PlatformMessageNotDispatchedError } from "openclaw/plugin-sdk/error-runtime";
34
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5+
import { resolveSmsAccount } from "./accounts.js";
6+
import {
7+
prepareSmsMediaAttempt,
8+
sendPreparedSmsMediaAttempt,
9+
sendSmsTextChunks,
10+
toSmsPlainText,
11+
} from "./send.js";
412
import type { sendSmsViaTwilio as sendSmsViaTwilioType } from "./twilio.js";
513
import type { ResolvedSmsAccount } from "./types.js";
614

715
type SendModule = typeof import("./send.js");
816
type SendSmsMediaParams = Parameters<SendModule["prepareSmsMediaAttempt"]>[0] &
917
Omit<Parameters<SendModule["sendPreparedSmsMediaAttempt"]>[0], "attempt">;
1018

11-
let sendSmsTextChunks: SendModule["sendSmsTextChunks"];
12-
let prepareSmsMediaAttempt: SendModule["prepareSmsMediaAttempt"];
13-
let sendPreparedSmsMediaAttempt: SendModule["sendPreparedSmsMediaAttempt"];
14-
let toSmsPlainText: SendModule["toSmsPlainText"];
15-
let resolveSmsAccount: (typeof import("./accounts.js"))["resolveSmsAccount"];
16-
1719
const sendSmsViaTwilio = vi.hoisted(() =>
1820
vi.fn<typeof sendSmsViaTwilioType>(async ({ to, onPlatformSendDispatch }) => {
1921
await onPlatformSendDispatch?.();
@@ -33,8 +35,28 @@ const hostedMediaMocks = vi.hoisted(() => {
3335
const recordInitialSmsDeliveryResult = vi.hoisted(() => vi.fn(async () => null));
3436
const deliveryWarn = vi.hoisted(() => vi.fn());
3537

36-
beforeEach(async () => {
37-
vi.resetModules();
38+
vi.mock("./twilio.js", async (importOriginal) => ({
39+
...(await importOriginal<typeof import("./twilio.js")>()),
40+
sendSmsViaTwilio,
41+
}));
42+
vi.mock("./media.js", async (importOriginal) => ({
43+
...(await importOriginal<typeof import("./media.js")>()),
44+
prepareHostedSmsMedia: hostedMediaMocks.prepare,
45+
}));
46+
vi.mock("./delivery-observations.js", async (importOriginal) => ({
47+
...(await importOriginal<typeof import("./delivery-observations.js")>()),
48+
recordInitialSmsDeliveryResult,
49+
}));
50+
vi.mock("./runtime.js", async (importOriginal) => ({
51+
...(await importOriginal<typeof import("./runtime.js")>()),
52+
getSmsRuntime: () => ({
53+
logging: {
54+
getChildLogger: () => ({ warn: deliveryWarn }),
55+
},
56+
}),
57+
}));
58+
59+
beforeEach(() => {
3860
sendSmsViaTwilio.mockReset();
3961
sendSmsViaTwilio.mockImplementation(async ({ to, onPlatformSendDispatch }) => {
4062
await onPlatformSendDispatch?.();
@@ -50,33 +72,9 @@ beforeEach(async () => {
5072
recordInitialSmsDeliveryResult.mockReset();
5173
recordInitialSmsDeliveryResult.mockResolvedValue(null);
5274
deliveryWarn.mockClear();
53-
vi.doMock("./twilio.js", () => ({
54-
sendSmsViaTwilio,
55-
TWILIO_MESSAGE_BODY_MAX_LENGTH: 1600,
56-
}));
57-
vi.doMock("./media.js", () => ({
58-
prepareHostedSmsMedia: hostedMediaMocks.prepare,
59-
}));
60-
vi.doMock("./delivery-observations.js", () => ({
61-
recordInitialSmsDeliveryResult,
62-
}));
63-
vi.doMock("./runtime.js", () => ({
64-
getSmsRuntime: () => ({
65-
logging: {
66-
getChildLogger: () => ({ warn: deliveryWarn }),
67-
},
68-
}),
69-
}));
70-
({ prepareSmsMediaAttempt, sendPreparedSmsMediaAttempt, sendSmsTextChunks, toSmsPlainText } =
71-
await import("./send.js"));
72-
({ resolveSmsAccount } = await import("./accounts.js"));
7375
});
7476

7577
afterEach(() => {
76-
vi.doUnmock("./twilio.js");
77-
vi.doUnmock("./media.js");
78-
vi.doUnmock("./delivery-observations.js");
79-
vi.doUnmock("./runtime.js");
8078
delete process.env.TWILIO_ACCOUNT_SID;
8179
delete process.env.TWILIO_AUTH_TOKEN;
8280
delete process.env.TWILIO_PHONE_NUMBER;
@@ -408,7 +406,6 @@ describe("sendSmsTextChunks", () => {
408406

409407
describe("sendSmsMedia", () => {
410408
it("preserves existing pre-dispatch proof from hosted-media staging", async () => {
411-
const { PlatformMessageNotDispatchedError } = await import("openclaw/plugin-sdk/error-runtime");
412409
const rejection = new PlatformMessageNotDispatchedError("unsupported hosted media", {
413410
cause: new Error("unsupported content type"),
414411
retryable: false,

0 commit comments

Comments
 (0)