rendering to channel + simplified adapter interface
This commit is contained in:
214
library/core/reacord-tester.ts
Normal file
214
library/core/reacord-tester.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
/* eslint-disable class-methods-use-this */
|
||||
/* eslint-disable require-await */
|
||||
import { nanoid } from "nanoid"
|
||||
import { nextTick } from "node:process"
|
||||
import { promisify } from "node:util"
|
||||
import type { ReactNode } from "react"
|
||||
import { logPretty } from "../../helpers/log-pretty"
|
||||
import { omit } from "../../helpers/omit"
|
||||
import { raise } from "../../helpers/raise"
|
||||
import type { Channel } from "../internal/channel"
|
||||
import { Container } from "../internal/container"
|
||||
import type {
|
||||
ButtonInteraction,
|
||||
CommandInteraction,
|
||||
SelectInteraction,
|
||||
} from "../internal/interaction"
|
||||
import type {
|
||||
Message,
|
||||
MessageButtonOptions,
|
||||
MessageOptions,
|
||||
MessageSelectOptions,
|
||||
} from "../internal/message"
|
||||
import type { ReacordInstance } from "./reacord"
|
||||
import { Reacord } from "./reacord"
|
||||
|
||||
const nextTickPromise = promisify(nextTick)
|
||||
|
||||
export class ReacordTester extends Reacord {
|
||||
private messageContainer = new Container<TestMessage>()
|
||||
|
||||
constructor() {
|
||||
super({ maxInstances: 2 })
|
||||
}
|
||||
|
||||
get messages(): readonly TestMessage[] {
|
||||
return [...this.messageContainer]
|
||||
}
|
||||
|
||||
send(): ReacordInstance {
|
||||
return this.createChannelRendererInstance(
|
||||
new TestChannel(this.messageContainer),
|
||||
)
|
||||
}
|
||||
|
||||
reply(): ReacordInstance {
|
||||
return this.createCommandReplyRendererInstance(
|
||||
new TestCommandInteraction(this.messageContainer),
|
||||
)
|
||||
}
|
||||
|
||||
async assertMessages(expected: ReturnType<this["sampleMessages"]>) {
|
||||
await nextTickPromise()
|
||||
expect(this.sampleMessages()).toEqual(expected)
|
||||
}
|
||||
|
||||
async assertRender(
|
||||
content: ReactNode,
|
||||
expected: ReturnType<this["sampleMessages"]>,
|
||||
) {
|
||||
const instance = this.reply()
|
||||
instance.render(content)
|
||||
await this.assertMessages(expected)
|
||||
instance.destroy()
|
||||
}
|
||||
|
||||
logMessages() {
|
||||
logPretty(this.sampleMessages())
|
||||
}
|
||||
|
||||
sampleMessages() {
|
||||
return this.messages.map((message) => ({
|
||||
...message.options,
|
||||
actionRows: message.options.actionRows.map((row) =>
|
||||
row.map((component) =>
|
||||
omit(component, ["customId", "onClick", "onSelect", "onSelectValue"]),
|
||||
),
|
||||
),
|
||||
}))
|
||||
}
|
||||
|
||||
findButtonByLabel(label: string) {
|
||||
for (const message of this.messageContainer) {
|
||||
for (const component of message.options.actionRows.flat()) {
|
||||
if (component.type === "button" && component.label === label) {
|
||||
return this.createButtonActions(component, message)
|
||||
}
|
||||
}
|
||||
}
|
||||
raise(`Couldn't find button with label "${label}"`)
|
||||
}
|
||||
|
||||
findSelectByPlaceholder(placeholder: string) {
|
||||
for (const message of this.messageContainer) {
|
||||
for (const component of message.options.actionRows.flat()) {
|
||||
if (
|
||||
component.type === "select" &&
|
||||
component.placeholder === placeholder
|
||||
) {
|
||||
return this.createSelectActions(component, message)
|
||||
}
|
||||
}
|
||||
}
|
||||
raise(`Couldn't find select with placeholder "${placeholder}"`)
|
||||
}
|
||||
|
||||
private createButtonActions(
|
||||
button: MessageButtonOptions,
|
||||
message: TestMessage,
|
||||
) {
|
||||
return {
|
||||
click: () => {
|
||||
this.handleComponentInteraction(
|
||||
new TestButtonInteraction(button.customId, message),
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
private createSelectActions(
|
||||
component: MessageSelectOptions,
|
||||
message: TestMessage,
|
||||
) {
|
||||
return {
|
||||
select: (...values: string[]) => {
|
||||
this.handleComponentInteraction(
|
||||
new TestSelectInteraction(component.customId, message, values),
|
||||
)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestMessage implements Message {
|
||||
constructor(
|
||||
public options: MessageOptions,
|
||||
private container: Container<TestMessage>,
|
||||
) {
|
||||
container.add(this)
|
||||
}
|
||||
|
||||
async edit(options: MessageOptions): Promise<void> {
|
||||
this.options = options
|
||||
}
|
||||
|
||||
async disableComponents(): Promise<void> {
|
||||
for (const row of this.options.actionRows) {
|
||||
for (const action of row) {
|
||||
if (action.type === "button") {
|
||||
action.disabled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async delete(): Promise<void> {
|
||||
this.container.remove(this)
|
||||
}
|
||||
}
|
||||
|
||||
class TestCommandInteraction implements CommandInteraction {
|
||||
readonly type = "command"
|
||||
readonly id = "test-command-interaction"
|
||||
readonly channelId = "test-channel-id"
|
||||
|
||||
constructor(private messageContainer: Container<TestMessage>) {}
|
||||
|
||||
reply(messageOptions: MessageOptions): Promise<Message> {
|
||||
return Promise.resolve(
|
||||
new TestMessage(messageOptions, this.messageContainer),
|
||||
)
|
||||
}
|
||||
|
||||
followUp(messageOptions: MessageOptions): Promise<Message> {
|
||||
return Promise.resolve(
|
||||
new TestMessage(messageOptions, this.messageContainer),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class TestButtonInteraction implements ButtonInteraction {
|
||||
readonly type = "button"
|
||||
readonly id = nanoid()
|
||||
readonly channelId = "test-channel-id"
|
||||
|
||||
constructor(readonly customId: string, readonly message: TestMessage) {}
|
||||
|
||||
async update(options: MessageOptions): Promise<void> {
|
||||
this.message.options = options
|
||||
}
|
||||
}
|
||||
|
||||
class TestSelectInteraction implements SelectInteraction {
|
||||
readonly type = "select"
|
||||
readonly id = nanoid()
|
||||
readonly channelId = "test-channel-id"
|
||||
|
||||
constructor(
|
||||
readonly customId: string,
|
||||
readonly message: TestMessage,
|
||||
readonly values: string[],
|
||||
) {}
|
||||
|
||||
async update(options: MessageOptions): Promise<void> {
|
||||
this.message.options = options
|
||||
}
|
||||
}
|
||||
|
||||
class TestChannel implements Channel {
|
||||
constructor(private messageContainer: Container<TestMessage>) {}
|
||||
|
||||
async send(messageOptions: MessageOptions): Promise<Message> {
|
||||
return new TestMessage(messageOptions, this.messageContainer)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user