Skip to content

Commit cfece54

Browse files
committed
test: decoupled
1 parent 1c584d3 commit cfece54

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { vi, describe, it, expect, beforeEach, afterEach } from "vitest";
2+
import { promises as fs } from "fs";
3+
import CommandUseCaseResolver from "../../domain/service/commandUseCaseResolver";
4+
import SendMessageToChannelUseCase from "../../application/usecases/sendMessageToChannel/sendMessageToChannelUseCase";
5+
import SendCodewarsLeaderboardToChannelUseCase from "../../application/usecases/sendCodewarsLeaderboardToChannel/sendCodewarsLeaderboardToChannelUseCase";
6+
import { Context } from "../../types";
7+
8+
vi.mock("../../application/usecases/sendMessageToChannel/sendMessageToChannelUseCase");
9+
vi.mock("../../application/usecases/sendCodewarsLeaderboardToChannel/sendCodewarsLeaderboardToChannelUseCase");
10+
11+
vi.mock("fs", () => ({
12+
promises: {
13+
readFile: vi.fn(),
14+
},
15+
}));
16+
17+
describe("CommandUseCaseResolver", () => {
18+
let commandUseCaseResolver: CommandUseCaseResolver;
19+
const mockContext: Context = {
20+
channelId: "test-channel",
21+
};
22+
23+
beforeEach(async () => {
24+
(fs.readFile as vi.Mock).mockResolvedValueOnce(
25+
JSON.stringify({
26+
"!ja": "Olá! Test message",
27+
"!oc": ":warning: Only Code questions!",
28+
})
29+
);
30+
31+
commandUseCaseResolver = new CommandUseCaseResolver({
32+
messageRepository: {
33+
getRandomIntroMessage: vi.fn(),
34+
getRandomWelcomingMessage: vi.fn(),
35+
},
36+
chatService: {
37+
sendMessageToChannel: vi.fn(),
38+
},
39+
loggerService: { log: vi.fn() },
40+
channelResolver: {
41+
getBySlug: vi.fn(),
42+
},
43+
kataService: {
44+
getLeaderboard: vi.fn().mockResolvedValue([]),
45+
},
46+
});
47+
});
48+
49+
it("should send the correct message for !ja command", async () => {
50+
await commandUseCaseResolver.resolveByCommand("!ja", mockContext);
51+
52+
expect(SendMessageToChannelUseCase.prototype.execute).toHaveBeenCalledWith({
53+
channelId: "test-channel",
54+
message: "Olá! Test message",
55+
});
56+
});
57+
58+
it("should send the correct message for !oc command", async () => {
59+
await commandUseCaseResolver.resolveByCommand("!oc", mockContext);
60+
61+
expect(SendMessageToChannelUseCase.prototype.execute).toHaveBeenCalledWith({
62+
channelId: "test-channel",
63+
message: ":warning: Only Code questions!",
64+
});
65+
});
66+
67+
it("should handle the !cwl command by sending the leaderboard", async () => {
68+
await commandUseCaseResolver.resolveByCommand("!cwl", mockContext);
69+
70+
expect(SendCodewarsLeaderboardToChannelUseCase.prototype.execute).toHaveBeenCalledWith({
71+
channelId: "test-channel",
72+
});
73+
});
74+
75+
it("should throw UseCaseNotFound error for unknown command", async () => {
76+
await expect(commandUseCaseResolver.resolveByCommand("!unknown", mockContext)).rejects.toThrow(
77+
'Use case for command "!unknown" not found'
78+
);
79+
});
80+
81+
afterEach(() => {
82+
vi.resetAllMocks();
83+
});
84+
});

0 commit comments

Comments
 (0)