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({ parentId: 100, actorUserId: 1, replyBody: "hello" }, deps); expect(state.sendMailCalled).toBe(false); }); test("用户偏好关闭 -> 不发送", async () => { const { deps, state } = createDeps(); deps.getReceiverNotifyEnabled = async () => false; await notifyReplyCommentCreated({ parentId: 100, actorUserId: 1, replyBody: "hello" }, deps); expect(state.sendMailCalled).toBe(false); }); test("无 parentId -> 不发送", async () => { const { deps, state } = createDeps(); await notifyReplyCommentCreated({ parentId: null, actorUserId: 1, replyBody: "hello" }, deps); expect(state.sendMailCalled).toBe(false); }); test("自通知 -> 不发送", async () => { const { deps, state } = createDeps(); deps.getParentAuthorUserId = async () => 1; await notifyReplyCommentCreated({ parentId: 100, actorUserId: 1, replyBody: "hello" }, deps); expect(state.sendMailCalled).toBe(false); }); });