Skip to content

Commit

Permalink
ci(test): change to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
socram03 committed Oct 8, 2024
1 parent 226f62d commit 532261d
Show file tree
Hide file tree
Showing 18 changed files with 1,147 additions and 525 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"devDependencies": {
"@biomejs/biome": "1.9.1",
"turbo": "^2.1.2"
"turbo": "^2.1.2",
"vitest": "^2.1.2"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
37 changes: 2 additions & 35 deletions packages/chartjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,5 @@
{
"compilerOptions": {
"module": "CommonJS",
"target": "ESNext",
"lib": [
"ESNext",
"WebWorker"
],
"moduleResolution": "node",
"declaration": true,
"sourceMap": false,
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"preserveConstEnums": true,
/* Type Checking */
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"noErrorTruncation": true,
"outDir": "./lib",
"stripInternal": true,
},
"exclude": [
"**/lib",
"**/__test__"
],
"include": [
"src"
"extends": [
"../../tsconfig.json"
]
}
2 changes: 1 addition & 1 deletion packages/cooldown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"lint": "biome lint --write ./src",
"format": "biome format --write ./src",
"checkb": "biome check --write --no-errors-on-unmatched ./src",
"test": "node --test ./test/*"
"test": "vitest run --config ./test/vitest.config.ts ./test/"
},
"devDependencies": {
"@types/node": "^22.5.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,105 +1,103 @@
const { strict: assert } = require('node:assert/strict');
const { test, describe, beforeEach } = require('node:test');
const { Client, Cache, MemoryAdapter, Logger } = require('seyfert');
const { CommandHandler } = require('seyfert/lib/commands/handler.js');
const { CooldownManager } = require('../lib/manager.js');
const { CooldownType } = require('../lib/resource.js');

describe('CooldownManager', async () => {
let client;
let cooldownManager;
let cooldownData;

beforeEach(() => {
client = new Client({
getRC: () => ({
debug: true,
intents: 0,
token: '',
locations: {
base: '',
output: '',
},
}),
});

const handler = new CommandHandler(new Logger({ active: true }), client);
cooldownData = {
type: CooldownType.User,
interval: 1000,
uses: 3,
};
handler.values = [
// @ts-expect-error
{
name: 'testCommand',
cooldown: cooldownData,
},
];

client.commands = handler;

client.cache = new Cache(0, new MemoryAdapter(), {}, client);
cooldownManager = new CooldownManager(client);
});

await test('Data should return cooldown data for a command', () => {
const data = cooldownManager.getCommandData('testCommand');
assert.deepEqual(data, {
type: CooldownType.User,
interval: 1000,
uses: 3,
});
});

await test('Data should return undefined for non-existent command', () => {
const data = cooldownManager.getCommandData('nonExistentCommand');
assert.equal(data, undefined);
});

await test('has should return false for a new cooldown', () => {
const result = cooldownManager.has('testCommand', 'user1');
assert.equal(result, false);
});

await test('has should return true when cooldown is active', () => {
for (let i = 0; i < cooldownData.uses; i++) {
cooldownManager.use('testCommand', 'user1');
}
const result = cooldownManager.has('testCommand', 'user1');
assert.equal(result, true);
});

await test('use should set cooldown when used for the first time', () => {
const result = cooldownManager.use('testCommand', 'user2');
assert.equal(result, true);
});

await test('use should return time left when cooldown is active', () => {
for (let i = 0; i < cooldownData.uses; i++) {
cooldownManager.use('testCommand', 'user3');
}
const result = cooldownManager.use('testCommand', 'user3');
assert.ok(typeof result === 'number');
});

await test('refill should refill the cooldown', () => {
cooldownManager.use('testCommand', 'user1');
const result = cooldownManager.refill('testCommand', 'user1');
assert.equal(result, true);
assert.equal(cooldownManager.has('testCommand', 'user1'), false);
});

await test('drip should drip the cooldown over time', async () => {
cooldownManager.use('testCommand', 'user1');

// Simulate time passing
await new Promise(resolve => setTimeout(resolve, 1000));

const data = cooldownManager.resource.get('testCommand:user:user1');
const props = cooldownManager.getCommandData('testCommand');
await cooldownManager.drip('testCommand', 'user1', props, data);
const getter = cooldownManager.resource.get('testCommand:user:user1');
assert.ok(getter.remaining === 2);
});
});
import { Cache, Client, Logger, MemoryAdapter } from 'seyfert';
import { CommandHandler } from 'seyfert/lib/commands/handler.js';
import { assert, beforeEach, describe, test } from 'vitest';
import { CooldownManager, type CooldownProps, CooldownType } from '../src';

describe('CooldownManager', async () => {
let client: Client;
let cooldownManager: CooldownManager;
let cooldownData: CooldownProps;

beforeEach(() => {
client = new Client({
getRC: () => ({
debug: true,
intents: 0,
token: '',
locations: {
base: '',
output: '',
},
}),
});

const handler = new CommandHandler(new Logger({ active: true }), client);
cooldownData = {
type: CooldownType.User,
interval: 1000,
uses: 3,
};
handler.values = [
// @ts-expect-error
{
name: 'testCommand',
cooldown: cooldownData,
},
];

client.commands = handler;

client.cache = new Cache(0, new MemoryAdapter(), {}, client);
cooldownManager = new CooldownManager(client);
});

await test('Data should return cooldown data for a command', () => {
const data = cooldownManager.getCommandData('testCommand');
assert.deepEqual(data, {
type: CooldownType.User,
interval: 1000,
uses: 3,
});
});

await test('Data should return undefined for non-existent command', () => {
const data = cooldownManager.getCommandData('nonExistentCommand');
assert.equal(data, undefined);
});

await test('has should return false for a new cooldown', () => {
const result = cooldownManager.has('testCommand', 'user1');
assert.equal(result, false);
});

await test('has should return true when cooldown is active', () => {
for (let i = 0; i < cooldownData.uses; i++) {
cooldownManager.use('testCommand', 'user1');
}
const result = cooldownManager.has('testCommand', 'user1');
assert.equal(result, true);
});

await test('use should set cooldown when used for the first time', () => {
const result = cooldownManager.use('testCommand', 'user2');
assert.equal(result, true);
});

await test('use should return time left when cooldown is active', () => {
for (let i = 0; i < cooldownData.uses; i++) {
cooldownManager.use('testCommand', 'user3');
}
const result = cooldownManager.use('testCommand', 'user3');
assert.ok(typeof result === 'number');
});

await test('refill should refill the cooldown', () => {
cooldownManager.use('testCommand', 'user1');
const result = cooldownManager.refill('testCommand', 'user1');
assert.equal(result, true);
assert.equal(cooldownManager.has('testCommand', 'user1'), false);
});

await test('drip should drip the cooldown over time', async () => {
cooldownManager.use('testCommand', 'user1');

// Simulate time passing
await new Promise(resolve => setTimeout(resolve, 1000));

const data = cooldownManager.resource.get('testCommand:user:user1');
const props = cooldownManager.getCommandData('testCommand');
await cooldownManager.drip('testCommand', 'user1', props!, data!);
const getter = cooldownManager.resource.get('testCommand:user:user1');
assert.ok(getter?.remaining === 2);
});
});
8 changes: 8 additions & 0 deletions packages/cooldown/test/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
fileParallelism: false,
isolate: false,
},
});
37 changes: 2 additions & 35 deletions packages/cooldown/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,5 @@
{
"compilerOptions": {
"module": "CommonJS",
"target": "ESNext",
"lib": [
"ESNext",
"WebWorker"
],
"moduleResolution": "node",
"declaration": true,
"sourceMap": false,
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"preserveConstEnums": true,
/* Type Checking */
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"noErrorTruncation": true,
"outDir": "./lib",
"stripInternal": true,
},
"exclude": [
"**/lib",
"**/__test__"
],
"include": [
"src"
"extends": [
"../../tsconfig.json"
]
}
37 changes: 2 additions & 35 deletions packages/generic-adapter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,5 @@
{
"compilerOptions": {
"module": "CommonJS",
"target": "ESNext",
"lib": [
"ESNext",
"WebWorker"
],
"moduleResolution": "node",
"declaration": true,
"sourceMap": false,
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"preserveConstEnums": true,
/* Type Checking */
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"noErrorTruncation": true,
"outDir": "./lib",
"stripInternal": true,
},
"exclude": [
"**/lib",
"**/__test__"
],
"include": [
"src"
"extends": [
"../../tsconfig.json"
]
}
2 changes: 1 addition & 1 deletion packages/redis-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"build": "tsc",
"lint": "biome lint --write ./src",
"format": "biome format --write ./src",
"test": "node --test ./test/*",
"test": "vitest run --config ./test/vitest.config.ts ./test/",
"checkb": "biome check --write --no-errors-on-unmatched ./src",
"test:adapter": "node --test ./test/adapter.test.js",
"test:cache": "node --test ./test/cache.test.js",
Expand Down
Loading

0 comments on commit 532261d

Please sign in to comment.