You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.0 KiB
102 lines
3.0 KiB
import { describe, expect, test } from "bun:test";
|
|
import { notifyReplyCommentCreated } from "./index";
|
|
|
|
function createDeps() {
|
|
const state = {
|
|
sendMailCalled: false,
|
|
};
|
|
|
|
return {
|
|
state,
|
|
deps: {
|
|
getParentAuthorUserId: async () => 2,
|
|
getGlobalConfig: async () => ({
|
|
enabled: true,
|
|
fromEmail: "noreply@example.com",
|
|
smtpHost: "smtp.example.com",
|
|
smtpPort: 465,
|
|
smtpSecure: true,
|
|
smtpUser: "smtp-user",
|
|
smtpPass: "smtp-pass",
|
|
}),
|
|
getReceiverNotifyEnabled: async () => true,
|
|
getReceiverProfile: async () => ({
|
|
email: "receiver@example.com",
|
|
username: "receiver",
|
|
nickname: "Receiver",
|
|
}),
|
|
sendMail: async () => {
|
|
state.sendMailCalled = true;
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
describe("notifyReplyCommentCreated", () => {
|
|
test("全局开关关闭 -> 不发送", async () => {
|
|
const { deps, state } = createDeps();
|
|
deps.getGlobalConfig = async () => ({
|
|
enabled: false,
|
|
fromEmail: "noreply@example.com",
|
|
smtpHost: "smtp.example.com",
|
|
smtpPort: 465,
|
|
smtpSecure: true,
|
|
smtpUser: "smtp-user",
|
|
smtpPass: "smtp-pass",
|
|
});
|
|
|
|
await notifyReplyCommentCreated({ postId: 10, commentId: 200, parentId: 100, actorUserId: 1, replyBody: "hello" }, deps);
|
|
|
|
expect(state.sendMailCalled).toBe(false);
|
|
});
|
|
|
|
test("用户偏好关闭 -> 不发送", async () => {
|
|
const { deps, state } = createDeps();
|
|
deps.getReceiverNotifyEnabled = async () => false;
|
|
|
|
await notifyReplyCommentCreated({ postId: 10, commentId: 201, parentId: 100, actorUserId: 1, replyBody: "hello" }, deps);
|
|
|
|
expect(state.sendMailCalled).toBe(false);
|
|
});
|
|
|
|
test("无 parentId -> 不发送", async () => {
|
|
const { deps, state } = createDeps();
|
|
|
|
await notifyReplyCommentCreated({ postId: 10, commentId: 202, parentId: null, actorUserId: 1, replyBody: "hello" }, deps);
|
|
|
|
expect(state.sendMailCalled).toBe(false);
|
|
});
|
|
|
|
test("自通知 -> 不发送", async () => {
|
|
const { deps, state } = createDeps();
|
|
deps.getParentAuthorUserId = async () => 1;
|
|
|
|
await notifyReplyCommentCreated({ postId: 10, commentId: 203, parentId: 100, actorUserId: 1, replyBody: "hello" }, deps);
|
|
|
|
expect(state.sendMailCalled).toBe(false);
|
|
});
|
|
|
|
test("发送异常 -> 不抛出(best-effort)", async () => {
|
|
const { deps } = createDeps();
|
|
deps.sendMail = async () => {
|
|
throw new Error("smtp send failed");
|
|
};
|
|
|
|
await expect(
|
|
notifyReplyCommentCreated({ postId: 10, commentId: 204, parentId: 100, actorUserId: 1, replyBody: "hello" }, deps),
|
|
).resolves.toBeUndefined();
|
|
});
|
|
|
|
test("接收方邮箱非法 -> 不发送", async () => {
|
|
const { deps, state } = createDeps();
|
|
deps.getReceiverProfile = async () => ({
|
|
email: "not-an-email",
|
|
username: "receiver",
|
|
nickname: "Receiver",
|
|
});
|
|
|
|
await notifyReplyCommentCreated({ postId: 10, commentId: 205, parentId: 100, actorUserId: 1, replyBody: "hello" }, deps);
|
|
|
|
expect(state.sendMailCalled).toBe(false);
|
|
});
|
|
});
|
|
|