Skip to content

Commit

Permalink
カバレッジ向上
Browse files Browse the repository at this point in the history
  • Loading branch information
na2na-p committed Dec 4, 2023
1 parent b0125aa commit 28b6c42
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/features/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export { Client } from './internal/Client/index.js';
export {
Voice,
getJoinableStateStatus,
JOINABLE_STATE_STATUS,
getActorConnectionState,
type ConnectionState,
getVoiceInstance,
} from './internal/Voice/index.js';
export type { Voice } from './internal/Voice/index.js';
9 changes: 8 additions & 1 deletion src/features/core/internal/Voice/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
export { Voice, getVoiceInstance } from './internal/Voice.class.js';
import { singleton } from '@/features/others/singleton/index.js';

import { Voice } from './internal/Voice.class.js';

export type { Voice } from './internal/Voice.class.js';
export type { ConnectionState } from './internal/Voice.types.js';
export {
getJoinableStateStatus,
JOINABLE_STATE_STATUS,
} from './internal/funcs/getJoinableStateStatus/index.js';
export { getActorConnectionState } from './internal/funcs/getActorConnectionState/index.js';

const createVoiceInstance = () => new Voice();
export const getVoiceInstance = singleton(createVoiceInstance);
55 changes: 54 additions & 1 deletion src/features/core/internal/Voice/internal/Voice.class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,59 @@ describe('Voice', () => {
);
});

it('should throw an error if the channel is null', async () => {
const mockedInteraction = {
reply: vi.fn(),
} as unknown as Readonly<ChatInputCommandInteraction>;

(getActorId as MockedFunction<typeof getActorId>).mockResolvedValueOnce({
voice: { channel: null },
} as GuildMember);
(
getJoinableStateStatus as MockedFunction<typeof getJoinableStateStatus>
).mockReturnValueOnce(JOINABLE_STATE_STATUS.ALREADY_JOINED);

await expect(
voice.join({ interaction: mockedInteraction })
).rejects.toThrowError(
'Channel is null. Please check getJoinableStateStatus()'
);
});

// JOINABLE_STATE_STATUS.ALREADY_JOINED
it('should return false if the channel is already joined', async () => {
const mockedInteraction = {
reply: vi.fn(),
} as unknown as Readonly<ChatInputCommandInteraction>;

(getActorId as MockedFunction<typeof getActorId>).mockResolvedValueOnce({
voice: {
channel: {
id: 'channelId',
guild: {
id: 'guildId',
},
},
},
} as GuildMember);
(
getJoinableStateStatus as MockedFunction<typeof getJoinableStateStatus>
).mockReturnValueOnce(JOINABLE_STATE_STATUS.ALREADY_JOINED);

const result = await voice.join({ interaction: mockedInteraction });

expect(getJoinableStateStatus).toHaveBeenCalledWith({
channel: {
id: 'channelId',
guild: {
id: 'guildId',
},
},
});

expect(result).toBe(false);
});

it('should join the voice channel and return true', async () => {
const mockedGuildMember = {
voice: {
Expand Down Expand Up @@ -230,7 +283,7 @@ describe('Voice', () => {
.mockReturnValueOnce('unknown' as any);

await expect(voice.join({ interaction })).rejects.toThrowError(
'unknown is unexpected value. Should have been never.'
'Unknown joinable type.'
);
});
});
Expand Down
15 changes: 6 additions & 9 deletions src/features/core/internal/Voice/internal/Voice.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ import type {
} from '@/features/library/index.js';
import { isNil, joinVoiceChannel } from '@/features/library/index.js';
import { LogicException } from '@/features/others/Error/LogicException.js';
import { assertNever } from '@/features/others/assertNever.js';
import { assertNever } from '@/features/others/assertNever/index.js';
import { getActorId } from '@/features/others/discord/index.js';
import { singleton } from '@/features/others/singleton/index.js';

import { getActorConnectionState } from './funcs/getActorConnectionState/index.js';
import {
JOINABLE_STATE_STATUS,
getJoinableStateStatus,
} from './funcs/getJoinableStateStatus/index.js';

const createVoiceInstance = () => new Voice();
export const getVoiceInstance = singleton(createVoiceInstance);

export class Voice {
connection: Array<{
guildId: string;
Expand Down Expand Up @@ -80,7 +76,7 @@ export class Voice {
adapterCreator: channel.guild.voiceAdapterCreator,
});
this.connection.push({
guildId: actor.guild.id,
guildId: channel.guildId,
connection: connectedChannel,
player: undefined,
});
Expand Down Expand Up @@ -110,10 +106,11 @@ export class Voice {
return true;

default:
assertNever(joinable);
assertNever(joinable, false);
break;
// HACK: カバレッジを100%にするための処置
}
/* c8 ignore next */
return;
return Promise.reject(new LogicException('Unknown joinable type.'));
}

public async leave({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { VoiceBasedChannel } from '@/features/library/index.js';

import { getJoinableStateStatus } from './getJoinableStateStatus.func.js';
import { JOINABLE_STATE_STATUS } from './getJoinableStateStatus.constants.js';
import { getJoinableStateStatus } from './getJoinableStateStatus.func.js';

describe('getJoinableStateStatus', () => {
it('should return NOT_FOUND if the channel is null', () => {
Expand Down
3 changes: 0 additions & 3 deletions src/features/others/assertNever.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/features/others/assertNever/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { assertNever } from './internal/assertNever.func.js';
4 changes: 4 additions & 0 deletions src/features/others/assertNever/internal/assertNever.func.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const assertNever = (x: never, shouldThrow: boolean = true): never => {
if (shouldThrow) throw new Error(`Unexpected object: ${x}`);
return x;
};
15 changes: 15 additions & 0 deletions src/features/others/assertNever/internal/assertNever.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { assertNever } from './assertNever.func.js';

describe('assertNever', () => {
it('should throw an error with the unexpected object', () => {
const unexpectedObject = {} as never;
expect(() => assertNever(unexpectedObject)).toThrowError(
'Unexpected object: [object Object]'
);
});

it('should not throw an error when shouldThrow is false', () => {
const unexpectedObject = {} as never;
expect(() => assertNever(unexpectedObject, false)).not.toThrow();
});
});

0 comments on commit 28b6c42

Please sign in to comment.