simpler ReacordTester with more parallelizing

This commit is contained in:
itsMapleLeaf
2022-08-07 12:57:14 -05:00
parent 74bada9351
commit fd8f85ea89
7 changed files with 79 additions and 105 deletions

View File

@@ -1,16 +1,11 @@
import { ComponentType } from "discord.js"
import React from "react"
import { beforeAll, expect, test } from "vitest"
import { expect, test } from "vitest"
import { ActionRow, Button, Option, Select } from "../src/main"
import { ReacordTester } from "./tester"
let tester: ReacordTester
beforeAll(async () => {
tester = await ReacordTester.create()
})
test("action row", async () => {
const { message } = await tester.render(
const { message } = await ReacordTester.render(
"action row",
<>
<Button label="outside button" onClick={() => {}} />

View File

@@ -1,5 +1,5 @@
import React from "react"
import { beforeAll, expect, test } from "vitest"
import { expect, test } from "vitest"
import {
Embed,
EmbedAuthor,
@@ -11,15 +11,10 @@ import {
} from "../src/main"
import { ReacordTester } from "./tester"
let tester: ReacordTester
beforeAll(async () => {
tester = await ReacordTester.create()
})
test("kitchen sink", async () => {
const now = new Date()
const { message } = await tester.render(
const { message } = await ReacordTester.render(
"kitchen sink",
<>
<Embed color={0xfe_ee_ef}>
@@ -78,7 +73,7 @@ test("kitchen sink", async () => {
})
test("author variants", async () => {
const { message } = await tester.render(
const { message } = await ReacordTester.render(
"author variants",
<>
<Embed>
@@ -112,7 +107,7 @@ test("author variants", async () => {
}, 20_000)
test("field variants", async () => {
const { message } = await tester.render(
const { message } = await ReacordTester.render(
"field variants",
<>
<Embed>
@@ -157,7 +152,7 @@ test("field variants", async () => {
test("footer variants", async () => {
const now = new Date()
const { message } = await tester.render(
const { message } = await ReacordTester.render(
"footer variants",
<>
<Embed>
@@ -205,7 +200,7 @@ test("footer variants", async () => {
test("embed props", async () => {
const now = new Date()
const { message } = await tester.render(
const { message } = await ReacordTester.render(
"embed props",
<Embed
title="title text"

View File

@@ -1,5 +1,6 @@
import { ReacordTester } from "./tester"
export async function setup() {
console.info("Running test setup...")
await ReacordTester.removeChannels()
}

View File

@@ -1,16 +1,11 @@
import { ButtonStyle, ComponentType } from "discord.js"
import React from "react"
import { beforeEach, expect, test } from "vitest"
import { expect, test } from "vitest"
import { Link } from "../src/main"
import { ReacordTester } from "./tester"
let tester: ReacordTester
beforeEach(async () => {
tester = await ReacordTester.create()
})
test("link", async () => {
const { message } = await tester.render(
const { message } = await ReacordTester.render(
"link",
<>
<Link url="https://example.com/">link text</Link>

View File

@@ -1,14 +1,9 @@
import { waitFor } from "@reacord/helpers/wait-for.js"
import * as React from "react"
import { beforeAll, expect, test } from "vitest"
import { expect, test } from "vitest"
import { Button, Embed, EmbedField, EmbedTitle } from "../src/main"
import { ReacordTester } from "./tester"
let tester: ReacordTester
beforeAll(async () => {
tester = await ReacordTester.create()
})
test.skip("rendering behavior", async () => {
// const tester = new ReacordTester()
// const reply = tester.reply()
@@ -239,7 +234,7 @@ test.skip("rendering behavior", async () => {
})
test("destroy()", async () => {
const { message, channel, instance } = await tester.render(
const { message, channel, instance } = await ReacordTester.render(
"destroy()",
<>
some text

View File

@@ -1,5 +1,4 @@
import { raise } from "@reacord/helpers/raise"
import type { Client } from "discord.js"
import { CategoryChannel, ChannelType, GatewayIntentBits } from "discord.js"
import { kebabCase } from "lodash-es"
import { randomBytes } from "node:crypto"
@@ -8,78 +7,70 @@ import { createDiscordClient } from "../src/create-discord-client"
import { ReacordClient } from "../src/main"
import { testEnv } from "./test-env"
export class ReacordTester {
private static client?: Client
const reacord = new ReacordClient({
token: testEnv.TEST_BOT_TOKEN,
})
static async removeChannels() {
const client = await ReacordTester.getClient()
const category = await ReacordTester.getCategory(client)
for (const [, channel] of category.children.cache) {
await channel.delete()
}
}
const clientPromise = createDiscordClient(testEnv.TEST_BOT_TOKEN, {
intents: GatewayIntentBits.Guilds | GatewayIntentBits.GuildMessages,
})
static async create() {
const client = await ReacordTester.getClient()
const category = await ReacordTester.getCategory(client)
return new ReacordTester(client, category)
}
const categoryPromise = getCategory()
private static async getClient() {
return (this.client ??= await createDiscordClient(testEnv.TEST_BOT_TOKEN, {
intents: GatewayIntentBits.Guilds | GatewayIntentBits.GuildMessages,
}))
}
private static async getCategory(client: Client<true>) {
const category =
client.channels.cache.get(testEnv.TEST_CATEGORY_ID) ??
(await client.channels.fetch(testEnv.TEST_CATEGORY_ID))
if (!(category instanceof CategoryChannel)) {
throw new TypeError("Category channel not found")
}
return category
}
private reacord?: ReacordClient
constructor(readonly client: Client, readonly category: CategoryChannel) {}
private async getTestChannel(testName: string) {
const hash = randomBytes(16).toString("hex").slice(0, 6)
const channelName = `${kebabCase(testName)}-${hash}`
let channel = this.category.children.cache.find(
(the) => the.name === channelName,
)
if (!channel || !channel.isTextBased()) {
channel = await this.category.children.create({
type: ChannelType.GuildText,
name: channelName,
})
}
for (const [, message] of await channel.messages.fetch()) {
await message.delete()
}
return channel
}
async render(testName: string, content?: ReactNode) {
this.reacord ??= new ReacordClient({
token: testEnv.TEST_BOT_TOKEN,
})
const channel = await this.getTestChannel(testName)
await channel.sendTyping()
const instance = this.reacord.send(channel.id, content)
const result = await channel.awaitMessages({ max: 1 })
const message = result.first() ?? raise("failed to send message")
return { channel, message, instance }
async function removeChannels() {
const category = await categoryPromise
for (const [, channel] of category.children.cache) {
await channel.delete()
}
}
async function getCategory() {
const client = await clientPromise
const category =
client.channels.cache.get(testEnv.TEST_CATEGORY_ID) ??
(await client.channels.fetch(testEnv.TEST_CATEGORY_ID))
if (!(category instanceof CategoryChannel)) {
throw new TypeError("Category channel not found")
}
return category
}
async function getTestChannel(testName: string) {
const hash = randomBytes(16).toString("hex").slice(0, 6)
const channelName = `${kebabCase(testName)}-${hash}`
const category = await categoryPromise
let channel = category.children.cache.find((the) => the.name === channelName)
if (!channel || !channel.isTextBased()) {
channel = await category.children.create({
type: ChannelType.GuildText,
name: channelName,
})
}
for (const [, message] of await channel.messages.fetch()) {
await message.delete()
}
return channel
}
async function render(testName: string, content?: ReactNode) {
const channel = await getTestChannel(testName)
await channel.sendTyping()
const instance = reacord.send(channel.id, content)
const result = await channel.awaitMessages({ max: 1 })
const message = result.first() ?? raise("failed to send message")
return { channel, message, instance }
}
export const ReacordTester = {
render,
removeChannels,
}

View File

@@ -9,8 +9,10 @@ export default defineConfig({
globalSetup: ["packages/reacord/test/global-setup.ts"],
threads: false,
isolate: false,
hookTimeout: 20_000,
testTimeout: 20_000,
reporters: ["verbose"],
// rate limiting means these timeouts need to be long af
hookTimeout: 60_000,
testTimeout: 60_000,
},
})