Skip to content

Commit

Permalink
refactor: truly decoupled commandUseCaseResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
diomonogatari committed Nov 7, 2024
1 parent ab15b4b commit c522a18
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
23 changes: 3 additions & 20 deletions domain/service/commandUseCaseResolver.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
import CodewarsLeaderboardCommand from "../../application/command/codewarsLeaderboardCommand";
import DontAskToAskCommand from "../../application/command/dontAskToAskCommand";
import OnlyCodeQuestionsCommand from "../../application/command/onlyCodeQuestionsCommand";
import { Command, Context } from "../../types";
import UseCaseNotFound from "../exception/useCaseNotFound";
import ChatService from "./chatService";
import KataService from "./kataService/kataService";
import LoggerService from "./loggerService";

export default class CommandUseCaseResolver {
private commands: Command[] = [];
private commands: Command[];

private loggerService: LoggerService;

constructor({
chatService,
kataService,
loggerService,
}: {
chatService: ChatService;
kataService: KataService;
loggerService: LoggerService;
}) {
constructor({ commands, loggerService }: { commands: Command[]; loggerService: LoggerService }) {
this.loggerService = loggerService;

this.commands.push(
new CodewarsLeaderboardCommand(chatService, kataService),
new DontAskToAskCommand(chatService),
new OnlyCodeQuestionsCommand(chatService)
);
this.commands = commands;
}

async resolveByCommand(command: string, context: Context): Promise<void> {
Expand Down
14 changes: 10 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import KataService from "./domain/service/kataService/kataService";
import CodewarsKataService from "./infrastructure/service/codewarsKataService";
import ContentAggregatorService from "./domain/service/contentAggregatorService/contentAggregatorService";
import LemmyContentAggregatorService from "./infrastructure/service/lemmyContentAggregatorService";
import CodewarsLeaderboardCommand from "./application/command/codewarsLeaderboardCommand";
import DontAskToAskCommand from "./application/command/dontAskToAskCommand";
import OnlyCodeQuestionsCommand from "./application/command/onlyCodeQuestionsCommand";
import { Command } from "./types";

dotenv.config();

Expand All @@ -29,12 +33,14 @@ const loggerService: LoggerService = new ConsoleLoggerService();
const channelResolver: ChannelResolver = new ChannelResolver();
const kataService: KataService = new CodewarsKataService();
const lemmyContentAggregatorService: ContentAggregatorService = new LemmyContentAggregatorService();
const commands: Command[] = [
new CodewarsLeaderboardCommand(chatService, kataService),
new DontAskToAskCommand(chatService),
new OnlyCodeQuestionsCommand(chatService),
];
const useCaseResolver = new CommandUseCaseResolver({
messageRepository,
chatService,
commands,
loggerService,
channelResolver,
kataService,
});

const checkForNewPosts = async () => {
Expand Down
24 changes: 17 additions & 7 deletions vitest/service/commandUseCaseResolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import { vi, describe, it, expect, beforeEach, afterEach } from "vitest";
import CommandUseCaseResolver from "../../domain/service/commandUseCaseResolver";
import { Context } from "../../types";
import { Command, Context } from "../../types";

describe("CommandUseCaseResolver", () => {
let commandUseCaseResolver: CommandUseCaseResolver;
const mockContext: Context = {
channelId: "test-channel",
};

const mockCommandJa: Command = {
name: "!ja",
execute: vi.fn(),
};

const mockCommandOc: Command = {
name: "!oc",
execute: vi.fn(),
};

const mockCommandCwl: Command = {
name: "!cwl",
execute: vi.fn(),
};

beforeEach(async () => {
commandUseCaseResolver = new CommandUseCaseResolver({
chatService: {
sendMessageToChannel: vi.fn(),
},
commands: [mockCommandJa, mockCommandCwl, mockCommandOc],
loggerService: { log: vi.fn() },
kataService: {
getLeaderboard: vi.fn().mockResolvedValue([]),
},
});
});

Expand Down

0 comments on commit c522a18

Please sign in to comment.