Skip to content

Commit

Permalink
🛠️ fix cron schedule and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kawamataryo committed Mar 7, 2024
1 parent d53bf86 commit 0984f47
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
69 changes: 69 additions & 0 deletions src/scheduled.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Mock, beforeEach, describe, expect, it, vi } from "vitest";
import { DiscordClient } from "./clients/discord";
import { EventsRepository } from "./repositories/eventsRepository";
import scheduled from "./scheduled";

vi.mock("./repositories/eventsRepository");
vi.mock("./clients/discord");

const mockEnv = {
DB: {},
DISCORD_TOKEN: "test-token",
MOKUMOKU_CHANNEL_ID: "test-channel-id",
};

describe("scheduled", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it.each([
{
cron: "0 6 * * SAT,SUN",
isEventDay: true,
isSendMessageCalled: true,
},
{
cron: "50 8 * * SAT,SUN",
isEventDay: true,
isSendMessageCalled: true,
},
{
cron: "0 6 * * SAT,SUN",
isEventDay: false,
isSendMessageCalled: false,
},
{
cron: "50 8 * * SAT,SUN",
isEventDay: false,
isSendMessageCalled: false,
},
])(
"should send message to mokumoku channel on event day. %o",
async ({ cron, isEventDay, isSendMessageCalled }) => {
const mockSendMessage = vi.fn();
(EventsRepository as Mock).mockImplementation(() => ({
findTodayEvent: vi.fn().mockResolvedValue(isEventDay),
}));
(DiscordClient as Mock).mockImplementation(() => ({
sendMessage: mockSendMessage,
}));

// biome-ignore lint/style/noNonNullAssertion: <explanation>
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
await scheduled!({ cron } as any, mockEnv as any, {} as any);

if (isSendMessageCalled) {
expect(mockSendMessage).toBeCalled();
expect(mockSendMessage.mock.calls[0][0]).toEqual({
channelId: mockEnv.MOKUMOKU_CHANNEL_ID,
body: {
content: expect.any(String),
},
});
} else {
expect(mockSendMessage).not.toBeCalled();
}
},
);
});
4 changes: 2 additions & 2 deletions src/scheduled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const scheduled: ExportedHandler<Bindings>["scheduled"] = async (
const isEventDay = !!(await eventsRepository.findTodayEvent());

switch (event.cron) {
case "0 6 * * *":
case "0 6 * * SAT,SUN":
if (isEventDay) {
await client.sendMessage({
channelId: env.MOKUMOKU_CHANNEL_ID,
Expand All @@ -28,7 +28,7 @@ const scheduled: ExportedHandler<Bindings>["scheduled"] = async (
});
}
break;
case "50 8 * * *":
case "50 8 * * SAT,SUN":
if (isEventDay) {
await client.sendMessage({
channelId: env.MOKUMOKU_CHANNEL_ID,
Expand Down
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ migrations_dir = "drizzle"
[triggers]
# triggersはUTC指定なので、時差を考慮して設定
# 日本時間の15:00と、17:50に実行
crons = [ "0 6 * * *", "50 8 * * * "]
crons = [ "0 6 * * SAT,SUN", "50 8 * * SAT,SUN"]

0 comments on commit 0984f47

Please sign in to comment.