Skip to content

Commit

Permalink
Merge pull request #305 from na2na-p/topic/test
Browse files Browse the repository at this point in the history
テスト追加
  • Loading branch information
na2na-p authored Nov 13, 2023
2 parents 17182d7 + 630cff7 commit 24d26b7
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
coverage
dist/
.env
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
"version": "3.0.0-beta1",
"description": "",
"type": "module",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"start": "node --import 'data:text/javascript,import { register } from \"node:module\"; import { pathToFileURL } from \"node:url\"; register(\"./loader.mjs\", pathToFileURL(\"./\"));' dist/index.js",
"build": "swc ./src -s -d ./dist",
"dev": "tsx src/index.ts --watch",
"lint": "eslint --ext .js,.ts \"./src/**/*.{js,ts}\"",
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js"
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --coverage --silent",
"test:watch": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --watch"
},
"keywords": [],
"author": "",
Expand All @@ -35,7 +36,7 @@
"dependencies": {
"@t3-oss/env-core": "^0.7.1",
"chalk": "^5.3.0",
"discord.js": "^14.13.0",
"discord.js": "^14.14.1",
"dotenv": "^16.3.1",
"eslint": "^8.53.0",
"resolve": "^1.22.8",
Expand Down
112 changes: 57 additions & 55 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type {
CacheType,
ChatInputCommandInteraction,
} from '@/features/library/index.js';

import { CommandBase } from './CommandBase.class.js';

describe('CommandBase', () => {
class TestCommand extends CommandBase {
public readonly name = 'test';
public readonly description = 'test command';

public async interact(): Promise<void> {
// do nothing
}
}

let command: TestCommand;

beforeEach(() => {
command = new TestCommand();
});

describe('register', () => {
it('should return the command instance', () => {
expect(command.register()).toBe(command);
});
});

describe('isInteract', () => {
it('should return true if the interaction command name matches the command name', () => {
const interaction = { commandName: 'test' } as Readonly<
ChatInputCommandInteraction<CacheType>
>;
expect(command.isInteract({ interaction })).toBe(true);
});

it('should return false if the interaction command name does not match the command name', () => {
const interaction = { commandName: 'other' } as Readonly<
ChatInputCommandInteraction<CacheType>
>;
expect(command.isInteract({ interaction })).toBe(false);
});
});
});
32 changes: 32 additions & 0 deletions src/features/commands/internal/Ping/internal/Ping.class.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type {
CacheType,
ChatInputCommandInteraction,
} from '@/features/library/index.js';

import { Ping } from './Ping.class.js';

describe('Ping', () => {
let ping: Ping;

beforeEach(() => {
ping = new Ping();
});

describe('interact', () => {
it('should reply with the RTT in milliseconds', async () => {
const interaction = {
createdAt: new Date('2022-01-01T00:00:00.000Z'),
reply: jest.fn(),
} as unknown as Readonly<ChatInputCommandInteraction<CacheType>>;
const expectedRTT = 100;

jest
.spyOn(Date, 'now')
.mockReturnValueOnce(new Date('2022-01-01T00:00:00.100Z').getTime());

await ping.interact({ interaction });

expect(interaction.reply).toHaveBeenCalledWith(`RTT: ${expectedRTT}ms`);
});
});
});
6 changes: 5 additions & 1 deletion src/features/commands/internal/Ping/internal/Ping.class.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getRTT } from './funcs/getRTT/index.js';
import type { InteractArgs } from '../../CommandBase/index.js';
import { CommandBase } from '../../CommandBase/index.js';

Expand All @@ -7,7 +8,10 @@ export class Ping extends CommandBase {

public override async interact({ interaction }: InteractArgs): Promise<void> {
interaction.reply(
`RTT: ${new Date().getTime() - interaction.createdAt.getTime()}ms`
`RTT: ${getRTT({
serverTime: interaction.createdAt.getTime(),
localTime: Date.now(),
})}ms`
);

return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getRTT } from './internal/getRTT.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getRTT } from './getRTT.js';

describe('getRTT', () => {
it('should return the correct RTT', () => {
const localTime = 1000;
const serverTime = 500;
const expectedRTT = localTime - serverTime;

const result = getRTT({ localTime, serverTime });

expect(result).toBe(expectedRTT);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// ローカルタイム-サーバタイムの引き算をする
export const getRTT = ({
localTime,
serverTime,
}: {
localTime: number;
serverTime: number;
}): number => {
return localTime - serverTime;
};
Loading

0 comments on commit 24d26b7

Please sign in to comment.