feat: migrate from SQLite to PostgreSQL and update database handling

This commit is contained in:
MythEclipse
2026-05-21 15:11:32 +07:00
parent 4a47855ad4
commit bfa91063ff
24 changed files with 252 additions and 181 deletions
+38 -4
View File
@@ -1,4 +1,4 @@
import { MessageFlags } from "discord.js";
import { MessageFlags, PermissionFlagsBits } from "discord.js";
import { describe, expect, test } from "bun:test";
import { handleInteraction, type BoosterRoleCommandService, type ChatInputInteractionLike } from "./interactionHandler";
@@ -8,6 +8,7 @@ class FakeInteraction implements ChatInputInteractionLike {
commandName = "booster-role";
user = { id: "user" };
guildId: string | null = "guild";
memberPermissions: { has(permission: bigint): boolean } | null = null;
replies: Reply[] = [];
constructor(private readonly subcommand: string, private readonly values: Record<string, unknown> = {}) {}
@@ -23,6 +24,7 @@ class FakeInteraction implements ChatInputInteractionLike {
options = {
getSubcommand: () => this.subcommand,
getString: (name: string) => (this.values[name] as string | null) ?? null,
getUser: (name: string) => (this.values[name] as { id: string } | null) ?? null,
getAttachment: (name: string) => (this.values[name] as { contentType: string | null; size: number; url: string } | null) ?? null
};
}
@@ -47,8 +49,8 @@ class FakeService implements BoosterRoleCommandService {
this.calls.push("icon");
}
async deleteRole(): Promise<void> {
this.calls.push("delete");
async deleteRole(input?: { userId: string }): Promise<void> {
this.calls.push(input?.userId ? `delete:${input.userId}` : "delete");
}
}
@@ -64,7 +66,7 @@ describe("handleInteraction", () => {
});
test("routes update subcommands and replies", async () => {
for (const subcommand of ["rename", "recolor", "icon", "delete"]) {
for (const subcommand of ["rename", "recolor", "icon"]) {
const interaction = new FakeInteraction(subcommand, { name: "Test", color: "#AABBCC", image: { contentType: "image/png", size: 100, url: "https://cdn.discordapp.com/icon.png" } });
const service = new FakeService();
@@ -75,6 +77,38 @@ describe("handleInteraction", () => {
}
});
test("routes own delete command", async () => {
const interaction = new FakeInteraction("delete");
const service = new FakeService();
await handleInteraction(interaction, service, { isBoosting: async () => true });
expect(service.calls).toEqual(["delete:user"]);
expect(interaction.replies[0]).toEqual({ content: "Booster role deleted.", flags: MessageFlags.Ephemeral });
});
test("routes admin delete command for target user", async () => {
const interaction = new FakeInteraction("admin-delete", { user: { id: "target-user" } });
interaction.memberPermissions = { has: (permission) => permission === PermissionFlagsBits.Administrator };
const service = new FakeService();
await handleInteraction(interaction, service, { isBoosting: async () => true });
expect(service.calls).toEqual(["delete:target-user"]);
expect(interaction.replies[0]).toEqual({ content: "Booster role deleted by admin.", flags: MessageFlags.Ephemeral });
});
test("rejects admin delete without Administrator permission", async () => {
const interaction = new FakeInteraction("admin-delete", { user: { id: "target-user" } });
interaction.memberPermissions = { has: () => false };
const service = new FakeService();
await handleInteraction(interaction, service, { isBoosting: async () => true });
expect(service.calls).toEqual([]);
expect(interaction.replies[0]).toEqual({ content: "Administrator permission is required", flags: MessageFlags.Ephemeral });
});
test("turns service errors into private replies", async () => {
const interaction = new FakeInteraction("claim", { name: "VIP" });
const service = new FakeService();