mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
test: add tests
This commit is contained in:
@@ -21,7 +21,8 @@
|
||||
"build:dev": "tsup --metafile",
|
||||
"build:prod": "tsup --minify",
|
||||
"publish": "npm run build:prod",
|
||||
"pretty": "prettier --write ."
|
||||
"pretty": "prettier --write .",
|
||||
"test": "vitest"
|
||||
},
|
||||
"keywords": [
|
||||
"sern-handler",
|
||||
@@ -40,6 +41,7 @@
|
||||
"ts-results-es": "^3.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@faker-js/faker": "^8.0.1",
|
||||
"@types/node": "^18.15.11",
|
||||
"@typescript-eslint/eslint-plugin": "5.58.0",
|
||||
"@typescript-eslint/parser": "5.58.0",
|
||||
@@ -49,7 +51,8 @@
|
||||
"eslint": "8.38.0",
|
||||
"prettier": "2.8.7",
|
||||
"tsup": "^6.7.0",
|
||||
"typescript": "5.0.2"
|
||||
"typescript": "5.0.2",
|
||||
"vitest": "latest"
|
||||
},
|
||||
"prettier": {
|
||||
"semi": true,
|
||||
|
||||
@@ -12,6 +12,7 @@ import { Processed } from '../types';
|
||||
import { BothCommand, CommandModule, Module } from '../../core/types/modules';
|
||||
import { Args } from '../../shared';
|
||||
|
||||
|
||||
export function dispatchInteraction<T extends CommandModule, V extends BaseInteraction | Message>(
|
||||
payload: { module: Processed<T>; event: V },
|
||||
createArgs: (m: typeof payload.event) => unknown[],
|
||||
|
||||
@@ -4,9 +4,8 @@ import { startReadyEvent } from './events/ready';
|
||||
import { makeMessageHandler } from './events/messages';
|
||||
import { err, ok } from '../core/functions';
|
||||
import { getFullPathTree } from '../core/module-loading';
|
||||
import { catchError, finalize, merge } from 'rxjs';
|
||||
import { handleError } from '../core/operators';
|
||||
import { Services, useContainerRaw } from '../core/ioc';
|
||||
import { merge } from 'rxjs';
|
||||
import { Services } from '../core/ioc';
|
||||
import { Wrapper } from '../shared';
|
||||
import { handleCrash } from './events/generic';
|
||||
|
||||
|
||||
35
test/core/create-plugin.test.ts
Normal file
35
test/core/create-plugin.test.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { CommandControlPlugin, CommandInitPlugin, EventControlPlugin, EventInitPlugin } from '../../src/core/create-plugins'
|
||||
import { PluginType, controller } from '../../src/index'
|
||||
|
||||
describe('create-plugins', () => {
|
||||
it('should make proper control plugins', () => {
|
||||
const pl = EventControlPlugin(() => controller.next())
|
||||
expect(pl)
|
||||
.to.have.all.keys(['type', 'execute'])
|
||||
expect(pl.type).toBe(PluginType.Control)
|
||||
expect(pl.execute).an('function')
|
||||
const pl2 = CommandControlPlugin(() => controller.next())
|
||||
expect(pl2)
|
||||
.to.have.all.keys(['type', 'execute'])
|
||||
expect(pl2.type).toBe(PluginType.Control)
|
||||
expect(pl2.execute).an('function')
|
||||
|
||||
})
|
||||
it('should make proper init plugins', () => {
|
||||
const pl = EventInitPlugin(() => controller.next())
|
||||
expect(pl)
|
||||
.to.have.all.keys(['type', 'execute'])
|
||||
expect(pl.type).toBe(PluginType.Init)
|
||||
expect(pl.execute).an('function')
|
||||
|
||||
const pl2 = CommandInitPlugin(() => controller.next())
|
||||
expect(pl2)
|
||||
.to.have.all.keys(['type', 'execute'])
|
||||
expect(pl2.type).toBe(PluginType.Init)
|
||||
expect(pl2.execute).an('function')
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
|
||||
46
test/core/functions.test.ts
Normal file
46
test/core/functions.test.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { describe, it } from "vitest";
|
||||
import { PluginType, SernOptionsData, controller } from '../../src/index'
|
||||
import { partitionPlugins, treeSearch } from "../../src/core/functions";
|
||||
import { expect } from "chai";
|
||||
import { faker } from '@faker-js/faker';
|
||||
import { ApplicationCommandOptionType } from "discord.js";
|
||||
|
||||
describe('functions', () => {
|
||||
function createRandomPlugins(len: number) {
|
||||
const random = () => Math.floor(Math.random()*2)+1; // 1 or 2, plugin enum
|
||||
return Array.from({ length: len }, () => ({ type: random(), execute: () => random() === 1 ? controller.next():controller.stop() }))
|
||||
}
|
||||
function createRandomChoice() {
|
||||
return {
|
||||
type: faker.number.int({ min: 1, max: 11}),
|
||||
name: faker.word.noun(),
|
||||
description: faker.word.adjective(),
|
||||
}
|
||||
}
|
||||
it('should partition plugins correctly', () => {
|
||||
const plugins = createRandomPlugins(100);
|
||||
const [ onEvent, init ] = partitionPlugins(plugins)
|
||||
for(const el of onEvent)
|
||||
expect(el.type).to.equal(PluginType.Control)
|
||||
|
||||
for(const el of init)
|
||||
expect(el.type).to.equal(PluginType.Init)
|
||||
})
|
||||
|
||||
it('should tree search options tree depth 1', () => {
|
||||
const options : SernOptionsData[] = [
|
||||
createRandomChoice(),
|
||||
createRandomChoice(),
|
||||
createRandomChoice(),
|
||||
{ type: ApplicationCommandOptionType.String,
|
||||
name: 'autocomplete',
|
||||
description: 'here',
|
||||
autocomplete: true,
|
||||
command : { onEvent: [], execute:(a) => {} }
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
Reference in New Issue
Block a user