Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions answeroverflow.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@
"name": "db",
"path": "packages/db",
},
{
"name": "db-mock",
"path": "packages/db-mock"
},
{
"name": "prisma-types",
"path": "packages/prisma-types"
},
{
"name": "elastic-types",
"path": "packages/elastic-types"
},
{
"name": "utils",
"path": "packages/utils"
},
{
"name": "config",
"path": "packages/config"
Expand Down
4 changes: 2 additions & 2 deletions apps/discord-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
"format": "prettier --write \"src/**/*.ts\"",
"dev": "yarn run with-env rm -rf dist && yarn run build && run-p watch start",
"start": "yarn run with-env node dist/index.js",
"test:discord-bot": "yarn run with-env jest --runInBand",
"test:watch": "yarn run with-env jest --watch --runInBand",
"test:discord-bot": "yarn run with-env jest",
"test:watch": "yarn run with-env jest --watch",
"install-local-reacord": "cp -r ~/repos/reacord/packages/reacord/dist/ /workspace/node_modules/@answeroverflow/reacord/"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import React from "react";
import { getDefaultChannelSettingsWithFlags } from "@answeroverflow/db";
import type { ReacordTester } from "@answeroverflow/reacord";
import type { ForumChannel, Guild, PublicThreadChannel, TextChannel } from "discord.js";
import { clearDatabase } from "@answeroverflow/db";
import { mockReacord, setupAnswerOverflowBot } from "~discord-bot/test/sapphire-mock";
import {
createGuildMemberVariants,
Expand All @@ -22,7 +21,6 @@ let forum_channel: ForumChannel;
let guild: Guild;
let members: GuildMemberVariants;
beforeEach(async () => {
await clearDatabase();
const client = await setupAnswerOverflowBot();
reacord = mockReacord();
guild = mockGuild(client);
Expand Down
3 changes: 1 addition & 2 deletions apps/discord-bot/src/domains/indexing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TextChannel,
} from "discord.js";

import { clearDatabase, Message as AOMessage } from "@answeroverflow/db";
import type { Message as AOMessage } from "@answeroverflow/db";

import { testOnlyAPICall } from "~discord-bot/test/helpers";
import {
Expand Down Expand Up @@ -47,7 +47,6 @@ let text_channel: TextChannel;
let forum_channel: ForumChannel;
let news_channel: NewsChannel;
beforeEach(async () => {
await clearDatabase();
client = await setupAnswerOverflowBot();
text_channel = mockTextChannel(client);
forum_channel = mockForumChannel(client);
Expand Down
2 changes: 0 additions & 2 deletions apps/discord-bot/src/domains/mark-solution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
ButtonStyle,
EmbedData,
} from "discord.js";
import { clearDatabase } from "@answeroverflow/db";

import {
checkIfCanMarkSolution,
Expand Down Expand Up @@ -48,7 +47,6 @@ let forum_channel: ForumChannel;
let text_channel_thread: AnyThreadChannel;
let forum_channel_thread: AnyThreadChannel;
beforeEach(async () => {
await clearDatabase();
client = await setupAnswerOverflowBot();
guild = mockGuild(client);
text_channel = mockTextChannel(client, guild);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { clearDatabase } from "@answeroverflow/db";
import { Client, Events } from "discord.js";
import {
emitEvent,
Expand All @@ -15,7 +14,6 @@ import { toAOChannelWithServer } from "~discord-bot/utils/conversions";

let client: Client;
beforeEach(async () => {
await clearDatabase();
client = await setupAnswerOverflowBot();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Client, Events } from "discord.js";
import { testOnlyAPICall } from "~discord-bot/test/helpers";
import { toAOServer } from "~discord-bot/utils/conversions";
import { clearDatabase } from "@answeroverflow/db";
import {
type GuildMemberVariants,
createGuildMemberVariants,
Expand All @@ -15,7 +14,6 @@ import { setupAnswerOverflowBot } from "~discord-bot/test/sapphire-mock";
let client: Client;
let members: GuildMemberVariants;
beforeEach(async () => {
await clearDatabase();
client = await setupAnswerOverflowBot();
members = await createGuildMemberVariants(client);
});
Expand Down
2 changes: 0 additions & 2 deletions apps/discord-bot/src/listeners/parity/channel-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { SapphireClient } from "@sapphire/framework";
import { Events, PublicThreadChannel, TextChannel } from "discord.js";
import { testOnlyAPICall } from "~discord-bot/test/helpers";
import { toAOChannelWithServer, toAOThread } from "~discord-bot/utils/conversions";
import { clearDatabase } from "@answeroverflow/db";
import {
mockTextChannel,
mockPublicThread,
Expand All @@ -16,7 +15,6 @@ let client: SapphireClient;
let text_channel: TextChannel;
let thread: PublicThreadChannel;
beforeEach(async () => {
await clearDatabase();
client = await setupAnswerOverflowBot();
text_channel = mockTextChannel(client);
thread = mockPublicThread({ client });
Expand Down
2 changes: 0 additions & 2 deletions apps/discord-bot/src/listeners/parity/guild-parity.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { SapphireClient } from "@sapphire/framework";
import { Events, Guild } from "discord.js";
import { prisma } from "@answeroverflow/db";
import { clearDatabase } from "@answeroverflow/db";
import {
mockGuild,
mockTextChannel,
Expand All @@ -15,7 +14,6 @@ let client: SapphireClient;
let guild: Guild;

beforeEach(async () => {
await clearDatabase();
client = await setupAnswerOverflowBot();
guild = mockGuild(client);
});
Expand Down
2 changes: 0 additions & 2 deletions apps/discord-bot/src/listeners/parity/message-parity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Collection, Events, Message, TextChannel } from "discord.js";
import { elastic } from "@answeroverflow/db";
import type { SapphireClient } from "@sapphire/framework";
import { toAOMessage } from "~discord-bot/utils/conversions";
import { clearDatabase } from "@answeroverflow/db";
import { mockTextChannel, mockMessage, emitEvent, copyClass } from "@answeroverflow/discordjs-mock";
import { setupAnswerOverflowBot } from "~discord-bot/test/sapphire-mock";

Expand All @@ -11,7 +10,6 @@ let message: Message;
let text_channel: TextChannel;

beforeEach(async () => {
await clearDatabase();
client = await setupAnswerOverflowBot();
text_channel = mockTextChannel(client);
message = mockMessage({ client, channel: text_channel });
Expand Down
2 changes: 0 additions & 2 deletions apps/discord-bot/src/utils/conversions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
extractUsersSetFromMessages,
messagesToAOMessagesSet,
} from "./conversions";
import { clearDatabase } from "@answeroverflow/db";
import {
mockTextChannel,
mockMessages,
Expand All @@ -17,7 +16,6 @@ import { setupAnswerOverflowBot } from "~discord-bot/test/sapphire-mock";
let text_channel: TextChannel;
let client: Client;
beforeEach(async () => {
await clearDatabase();
client = await setupAnswerOverflowBot();
text_channel = mockTextChannel(client);
});
Expand Down
6 changes: 4 additions & 2 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"clean": "rm -rf .turbo node_modules coverage",
"lint": "eslint src && tsc --noEmit",
"lint:fix": "eslint src --fix",
"test:api": "yarn run with-env vitest run --no-threads ",
"test:watch": "yarn run with-env vitest --no-threads --coverage",
"test": "yarn run with-env vitest run --coverage",
"test:watch": "yarn run with-env vitest",
"type-check": "tsc --noEmit"
},
"dependencies": {
"@answeroverflow/auth": "*",
"@answeroverflow/db": "*",
"@answeroverflow/utils": "*",
"@sapphire/bitfield": "^1.0.0",
"@trpc/client": "^10.4.3",
"@trpc/server": "^10.4.3",
Expand All @@ -25,6 +26,7 @@
},
"devDependencies": {
"@answeroverflow/tsconfig": "*",
"@answeroverflow/db-mock": "*",
"@vitest/coverage-c8": "^0.25.1",
"dotenv-cli": "^6.0.0",
"eslint": "^8.28.0",
Expand Down
20 changes: 12 additions & 8 deletions packages/api/src/router/channel/channel.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Channel, clearDatabase, getDefaultChannelSettings, Server } from "@answeroverflow/db";
import { Channel, getDefaultChannelSettings, Server } from "@answeroverflow/db";
import {
mockServer,
mockChannel,
createAnswerOverflowBotCtx,
testAllVariantsThatThrowErrors,
mockAccountWithServersCallerCtx,
Expand All @@ -10,7 +8,8 @@ import {
import { channelRouter, CHANNEL_NOT_FOUND_MESSAGES } from "./channel";
import { serverRouter } from "../server/server";
import { MISSING_PERMISSIONS_TO_EDIT_SERVER_MESSAGE } from "~api/utils/permissions";
import { pick } from "~api/utils/utils";
import { mockChannel, mockServer } from "@answeroverflow/db-mock";
import { pick } from "@answeroverflow/utils";

let ao_bot_server_router: ReturnType<(typeof serverRouter)["createCaller"]>;
let ao_bot_channel_router: ReturnType<(typeof channelRouter)["createCaller"]>;
Expand All @@ -19,7 +18,6 @@ let channel: Channel;
let channel2: Channel;

beforeEach(async () => {
await clearDatabase();
server = mockServer();
channel = mockChannel(server);
channel2 = mockChannel(server);
Expand Down Expand Up @@ -50,7 +48,9 @@ describe("Channel Operations", () => {
const fetched = await ao_bot_channel_router.byId(channel.id);
expect(fetched).toEqual({
...channel,
settings: getDefaultChannelSettings(channel.id),
settings: getDefaultChannelSettings({
channel_id: channel.id,
}),
});
});
it("tests all variants for fetching a single channel", async () => {
Expand Down Expand Up @@ -79,11 +79,15 @@ describe("Channel Operations", () => {
const fetched = await ao_bot_channel_router.byIdMany([channel.id, channel2.id]);
expect(fetched).toContainEqual({
...channel,
settings: getDefaultChannelSettings(channel.id),
settings: getDefaultChannelSettings({
channel_id: channel.id,
}),
});
expect(fetched).toContainEqual({
...channel2,
settings: getDefaultChannelSettings(channel2.id),
settings: getDefaultChannelSettings({
channel_id: channel2.id,
}),
});
});
it("tests all variants for fetching many channels", async () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/api/src/router/channel/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "~api/utils/protected-procedures";
import { assertCanEditServer, assertCanEditServerBotOnly } from "~api/utils/permissions";
import { serverRouter, z_server_upsert } from "../server/server";
import { omit } from "~api/utils/utils";
import { omit } from "@answeroverflow/utils";

export const CHANNEL_NOT_FOUND_MESSAGES = "Channel does not exist";

Expand Down Expand Up @@ -83,7 +83,8 @@ const fetch_router = router({
);
const channel_with_settings = {
...omit(channel, "channel_settings"),
settings: channel.channel_settings ?? getDefaultChannelSettings(channel.id),
settings:
channel.channel_settings ?? getDefaultChannelSettings({ channel_id: channel.id }),
};

return channel_with_settings;
Expand All @@ -104,7 +105,8 @@ const fetch_router = router({
});
return channels.map((channel) => ({
...omit(channel, "channel_settings"),
settings: channel.channel_settings ?? getDefaultChannelSettings(channel.id),
settings:
channel.channel_settings ?? getDefaultChannelSettings({ channel_id: channel.id }),
}));
},
permissions: (data) => assertCanEditServer(ctx, data.server_id),
Expand Down
21 changes: 11 additions & 10 deletions packages/api/src/router/channel/channel_settings.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {
createAnswerOverflowBotCtx,
mockAccountWithServersCallerCtx,
mockChannel,
mockServer,
testAllVariantsThatThrowErrors,
} from "~api/test/utils";
import { serverRouter } from "../server/server";
import { channelRouter } from "./channel";
import { channelSettingsRouter } from "./channel_settings";
import { Server, Channel, clearDatabase } from "@answeroverflow/db";
import type { Server, Channel } from "@answeroverflow/db";
import { MISSING_PERMISSIONS_TO_EDIT_SERVER_MESSAGE } from "~api/utils/permissions";
import { mockChannel, mockServer } from "@answeroverflow/db-mock";

let ao_bot_server_router: ReturnType<(typeof serverRouter)["createCaller"]>;
let ao_bot_channel_router: ReturnType<(typeof channelRouter)["createCaller"]>;
Expand All @@ -18,7 +17,6 @@ let server: Server;
let channel: Channel;

beforeEach(async () => {
await clearDatabase();
server = mockServer();
channel = mockChannel(server);
const ao_bot = await createAnswerOverflowBotCtx();
Expand Down Expand Up @@ -54,23 +52,26 @@ describe("Channel Settings Operations", () => {
});
});
describe("Channel Settings By Invite Code", () => {
let invite_code: string;
beforeEach(async () => {
// set to a random string to avoid collisions
invite_code = Math.random().toString(36).substring(7);
await ao_bot_channel_settings_router.create({
channel_id: channel.id,
invite_code: "potato",
invite_code,
});
});
it("should fetch channel settings by id as the Answer Overflow Bot", async () => {
const channel_settings = await ao_bot_channel_settings_router.byInviteCode("potato");
it("should fetch channel settings by invite code as the Answer Overflow Bot", async () => {
const channel_settings = await ao_bot_channel_settings_router.byInviteCode(invite_code);
expect(channel_settings.channel_id).toBe(channel.id);
expect(channel_settings.invite_code).toBe("potato");
expect(channel_settings.invite_code).toBe(invite_code);
});
it("should test fetching channel settings by id with all varaints", async () => {
it("should test fetching channel settings by invite code with all varaints", async () => {
await testAllVariantsThatThrowErrors({
async operation({ source, permission }) {
const caller = await mockAccountWithServersCallerCtx(server, source, permission);
const router = channelSettingsRouter.createCaller(caller.ctx);
await router.byInviteCode("potato");
await router.byInviteCode(invite_code);
},
permission_failure_message: MISSING_PERMISSIONS_TO_EDIT_SERVER_MESSAGE,
permissionsThatShouldWork: ["ManageGuild", "Administrator"],
Expand Down
7 changes: 6 additions & 1 deletion packages/api/src/router/channel/channel_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ const channelSettingsCreateUpdate = router({
protectedMutationFetchFirst({
fetch: () => channelRouter.createCaller(ctx).byId(input.channel_id),
operation: async (channel) => {
const new_settings = mergeChannelSettings(getDefaultChannelSettings(channel.id), input);
const new_settings = mergeChannelSettings(
getDefaultChannelSettings({
channel_id: channel.id,
}),
input
);
const data = await ctx.prisma.channelSettings.create({
data: { ...new_settings, channel_id: channel.id },
});
Expand Down
9 changes: 3 additions & 6 deletions packages/api/src/router/message/message.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { Channel, clearDatabase, DiscordAccount, Message, Server } from "@answeroverflow/db";
import type { Channel, DiscordAccount, Message, Server } from "@answeroverflow/db";
import {
createAnswerOverflowBotCtx,
mockAccount,
mockAccountCallerCtx,
mockAccountWithServersCallerCtx,
mockChannel,
mockMessage,
mockServer,
testAllDataVariants,
testAllVariants,
} from "~api/test/utils";
Expand All @@ -24,6 +20,8 @@ import {
toMessageWithDiscordAccount,
toPrivateMessageWithStrippedData,
} from "~api/test/public_data";
import { mockAccount, mockChannel, mockMessage, mockServer } from "@answeroverflow/db-mock";

let server: Server;
let channel: Channel;
let author: DiscordAccount;
Expand All @@ -40,7 +38,6 @@ let ao_bot_user_server_settings_router: ReturnType<
>;
let ao_bot_message_router: ReturnType<(typeof messageRouter)["createCaller"]>;
beforeEach(async () => {
await clearDatabase();
server = mockServer();
channel = mockChannel(server);
author = mockAccount();
Expand Down
Loading