Monitor your systems and send alerts when issues are detected:
const monitor =actor({ state:{ alertEmail:nullasstring|null, isHealthy:true,}, actions:{configure:async(c, email:string)=>{ c.state.alertEmail = email;await c.schedule.at(Date.now()+60000,"checkHealth");},checkHealth:async(c)=>{// Simple mock health checkconst wasHealthy = c.state.isHealthy; c.state.isHealthy =awaitmockHealthCheck();// Alert on status change to unhealthyif(wasHealthy &&!c.state.isHealthy && c.state.alertEmail){await resend.emails.send({ from:"[email protected]", to: c.state.alertEmail, subject:"⚠️ System Alert", html:"<p>The system is experiencing issues.</p>",});}// Reschedule next checkawait c.schedule.at(Date.now()+60000,"checkHealth");},},});// Mock functionasyncfunctionmockHealthCheck(){return Math.random()>0.1;// 90% chance of being healthy}
When testing actors that use Resend, you should mock the Resend API to avoid sending real emails during tests. ActorCore’s testing utilities combined with Vitest make this straightforward:
import{ test, expect, vi, beforeEach }from"vitest";import{ setupTest }from"actor-core/test";import{ app }from"../actors/app";// Create mock for send methodconst mockSendEmail = vi.fn().mockResolvedValue({ success:true});beforeEach(()=>{ process.env.RESEND_API_KEY="test_mock_api_key_12345"; vi.mock("resend",()=>{return{ Resend: vi.fn().mockImplementation(()=>{return{ emails:{ send: mockSendEmail}};})};}); mockSendEmail.mockClear();});test("email is sent when action is called",async(t)=>{const{ client }=awaitsetupTest(t, app);const actor =await client.user.get();// Call the action that should send an emailawait actor.someActionThatSendsEmail("[email protected]");// Verify the email was sent with the right parametersexpect(mockSendEmail).toHaveBeenCalledWith( expect.objectContaining({ to:"[email protected]", subject:"Expected Subject",}),);});
Using vi.advanceTimersByTimeAsync() is particularly useful for testing scheduled emails:
// Fast forward time to test scheduled emailsawait vi.advanceTimersByTimeAsync(24*60*60*1000);// Advance 24 hours// Test that the scheduled email was sentexpect(mockSendEmail).toHaveBeenCalledTimes(2);