generated from tiramisulabs/monorepo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,147 additions
and
525 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 103 additions & 105 deletions
208
packages/cooldown/test/manager.test.js → packages/cooldown/test/manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.