Skip to content

Commit

Permalink
テスト多少マシにする
Browse files Browse the repository at this point in the history
  • Loading branch information
na2na-p committed Nov 22, 2023
1 parent cd5eacf commit 707b627
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 101 deletions.
117 changes: 55 additions & 62 deletions src/features/core/internal/Voice/internal/Voice.class.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Mock } from 'vitest';
import type { MockedFunction } from 'vitest';

import {
joinVoiceChannel,
Expand All @@ -10,9 +10,10 @@ import type {
VoiceConnection,
} from '@/features/library/index.js';
import { LogicException } from '@/features/others/Error/LogicException.js';
import { getInteractionMemberId } from '@/features/others/discord/index.js';
import { getActorId } from '@/features/others/discord/index.js';

import { Voice } from './Voice.class.js';
import { getActorConnection } from './funcs/getActorConnection/index.js';
import {
JOINABLE_STATE_STATUS,
getJoinableStateStatus,
Expand All @@ -24,7 +25,7 @@ vi.mock('@/features/others/discord/index.js', async () => {
)) as object;
return {
...actual,
getInteractionMemberId: vi.fn(),
getActorId: vi.fn(),
};
});

Expand All @@ -49,6 +50,12 @@ vi.mock('./funcs/getJoinableStateStatus/index.js', async () => {
};
});

vi.mock('./funcs/getActorConnection/index.js', () => {
return {
getActorConnection: vi.fn(),
};
});

describe('Voice', () => {
let voice: Voice;

Expand All @@ -66,12 +73,12 @@ describe('Voice', () => {
reply: vi.fn(),
} as unknown as Readonly<ChatInputCommandInteraction>;

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

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

Expand All @@ -87,12 +94,12 @@ describe('Voice', () => {
reply: vi.fn(),
} as unknown as Readonly<ChatInputCommandInteraction>;

(getInteractionMemberId as Mock).mockResolvedValueOnce({
(getActorId as MockedFunction<typeof getActorId>).mockResolvedValueOnce({
voice: { channel: { joinable: true } },
});
(getJoinableStateStatus as Mock).mockReturnValueOnce(
JOINABLE_STATE_STATUS.NOT_JOINABLE
);
} as GuildMember);
(
getJoinableStateStatus as MockedFunction<typeof getJoinableStateStatus>
).mockReturnValueOnce(JOINABLE_STATE_STATUS.NOT_JOINABLE);

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

Expand All @@ -110,12 +117,12 @@ describe('Voice', () => {
reply: vi.fn(),
} as unknown as Readonly<ChatInputCommandInteraction>;

(getInteractionMemberId as Mock).mockResolvedValueOnce({
(getActorId as MockedFunction<typeof getActorId>).mockResolvedValueOnce({
voice: { channel: { joinable: true, viewable: false } },
});
(getJoinableStateStatus as Mock).mockReturnValueOnce(
JOINABLE_STATE_STATUS.NOT_VIEWABLE
);
} as GuildMember);
(
getJoinableStateStatus as MockedFunction<typeof getJoinableStateStatus>
).mockReturnValueOnce(JOINABLE_STATE_STATUS.NOT_VIEWABLE);

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

Expand All @@ -133,7 +140,7 @@ describe('Voice', () => {
reply: vi.fn(),
} as unknown as Readonly<ChatInputCommandInteraction>;

(getInteractionMemberId as Mock).mockRejectedValueOnce(
(getActorId as MockedFunction<typeof getActorId>).mockRejectedValueOnce(
new LogicException(
'Channel is null. Please check getJoinableStateStatus()'
)
Expand All @@ -149,12 +156,12 @@ describe('Voice', () => {
reply: vi.fn(),
} as unknown as Readonly<ChatInputCommandInteraction>;

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

await expect(
voice.join({ interaction: mockedInteraction })
Expand Down Expand Up @@ -182,11 +189,15 @@ describe('Voice', () => {
reply: vi.fn(),
} as const as unknown as Readonly<ChatInputCommandInteraction>;

(getInteractionMemberId as Mock).mockResolvedValueOnce(mockedGuildMember);
(joinVoiceChannel as Mock).mockReturnValueOnce('connection');
(getJoinableStateStatus as Mock).mockReturnValueOnce(
JOINABLE_STATE_STATUS.JOINABLE
(getActorId as MockedFunction<typeof getActorId>).mockResolvedValueOnce(
mockedGuildMember as GuildMember
);
(
joinVoiceChannel as MockedFunction<typeof joinVoiceChannel>
).mockReturnValueOnce({} as VoiceConnection);
(
getJoinableStateStatus as MockedFunction<typeof getJoinableStateStatus>
).mockReturnValueOnce(JOINABLE_STATE_STATUS.JOINABLE);

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

Expand All @@ -199,7 +210,7 @@ describe('Voice', () => {
adapterCreator: expect.any(Function),
});
expect(voice['connection']).toEqual([
{ guildId: 'guildId', connection: 'connection' },
{ guildId: 'guildId', connection: {} },
]);
expect(result).toBe(true);
});
Expand All @@ -210,11 +221,12 @@ describe('Voice', () => {
reply: vi.fn(),
} as unknown as Readonly<ChatInputCommandInteraction>;

(getInteractionMemberId as Mock).mockResolvedValueOnce({
(getActorId as MockedFunction<typeof getActorId>).mockResolvedValueOnce({
voice: { channel: null },
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(getJoinableStateStatus as Mock).mockReturnValueOnce('unknown' as any);
} as GuildMember);
(getJoinableStateStatus as MockedFunction<typeof getJoinableStateStatus>)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.mockReturnValueOnce('unknown' as any);

await expect(voice.join({ interaction })).rejects.toThrowError(
'Unknown joinable type.'
Expand All @@ -238,36 +250,15 @@ describe('Voice', () => {
guildId: 'guildId',
} as unknown as Readonly<ChatInputCommandInteraction>;

voice['connection'] = [
{
guildId: 'guildId',
connection: {
destroy: vi.fn(),
} as unknown as VoiceConnection,
},
];

const result = await voice.leave({ interaction });

expect(voice['connection']).toEqual([]);
expect(result).toBe(true);
});

it('should destroy the connection and return true if the target connection does not exist but the member has a connection', async () => {
const interaction = {
guildId: 'guildId',
} as unknown as Readonly<ChatInputCommandInteraction>;

(getInteractionMemberId as Mock).mockResolvedValueOnce({
guild: { id: 'guildId' },
});
(getVoiceConnection as Mock).mockReturnValueOnce({
(
getActorConnection as MockedFunction<typeof getActorConnection>
).mockResolvedValueOnce({
destroy: vi.fn(),
});
} as unknown as VoiceConnection);

const result = await voice.leave({ interaction });

expect(getVoiceConnection).toHaveBeenCalledWith('guildId');
expect(voice['connection']).toEqual([]);
expect(result).toBe(true);
});

Expand All @@ -276,10 +267,12 @@ describe('Voice', () => {
guildId: 'guildId',
} as unknown as Readonly<ChatInputCommandInteraction>;

(getInteractionMemberId as Mock).mockResolvedValueOnce({
(getActorId as MockedFunction<typeof getActorId>).mockResolvedValueOnce({
guild: { id: 'guildId' },
});
(getVoiceConnection as Mock).mockReturnValueOnce(null);
} as GuildMember);
(
getVoiceConnection as MockedFunction<typeof getVoiceConnection>
).mockReturnValueOnce(undefined);

const result = await voice.leave({ interaction });

Expand Down
38 changes: 11 additions & 27 deletions src/features/core/internal/Voice/internal/Voice.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ import type {
VoiceConnection,
ChatInputCommandInteraction,
} from '@/features/library/index.js';
import {
getVoiceConnection,
isNil,
joinVoiceChannel,
} from '@/features/library/index.js';
import { isNil, joinVoiceChannel } from '@/features/library/index.js';
import { LogicException } from '@/features/others/Error/LogicException.js';
import { getInteractionMemberId } from '@/features/others/discord/index.js';
import { getActorId } from '@/features/others/discord/index.js';

import { getActorConnection } from './funcs/getActorConnection/index.js';
import {
JOINABLE_STATE_STATUS,
getJoinableStateStatus,
Expand All @@ -28,7 +25,7 @@ export class Voice {
}) {
const {
voice: { channel },
} = await getInteractionMemberId(interaction);
} = await getActorId(interaction);
const joinable = getJoinableStateStatus({
channel,
});
Expand Down Expand Up @@ -85,27 +82,14 @@ export class Voice {
}: {
interaction: Readonly<ChatInputCommandInteraction>;
}): Promise<boolean> {
const guildId = interaction.guildId;
if (isNil(guildId)) return false;

const targetConnection = this.connection.find(
connection => connection.guildId === guildId
);
const actorConnection = await getActorConnection({
interaction,
connections: this.connection,
});

if (isNil(targetConnection)) {
const member = await getInteractionMemberId(interaction);
const connection = getVoiceConnection(member.guild.id);
if (isNil(connection)) {
return false;
} else {
connection.destroy();
return true;
}
} else {
targetConnection.connection.destroy();
this.connection = this.connection.filter(
connection => connection.guildId !== guildId
);
if (isNil(actorConnection)) return false;
else {
actorConnection.destroy();
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getActorConnection } from './internal/getActorConnection.func.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
isNil,
type ChatInputCommandInteraction,
type VoiceConnection,
getVoiceConnection,
} from '@/features/library/index.js';
import { getActorId } from '@/features/others/discord/index.js';

export const getActorConnection = async ({
interaction,
connections,
}: {
interaction: Readonly<ChatInputCommandInteraction>;
connections: Array<{
guildId: string;
connection: VoiceConnection;
}>;
}): Promise<VoiceConnection | null> => {
const guildId = interaction.guildId;
if (isNil(guildId)) return null;

const connection = connections.find(
connection => connection.guildId === guildId
);

if (isNil(connection)) {
const actor = await getActorId(interaction);
const connection = getVoiceConnection(actor.guild.id);

if (isNil(connection)) return null;
else return connection;
} else {
return connection.connection;
}
};
2 changes: 1 addition & 1 deletion src/features/others/discord/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { getGuildFromInteraction } from './internal/getGuildFromInteraction/index.js';
export { getInteractionMemberId } from './internal/getInteractionMemberId/index.js';
export { getActorId } from './internal/getActorId/index.js';
1 change: 1 addition & 0 deletions src/features/others/discord/internal/getActorId/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getActorId } from './internal/getActorId.func.js';
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@/features/library/index.js';
import { getGuildFromInteraction } from '@/features/others/discord/index.js';

export const getInteractionMemberId = async (
export const getActorId = async (
interaction: Readonly<ChatInputCommandInteraction>
): Promise<GuildMember> => {
const guild = getGuildFromInteraction({ interaction });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { Mock } from 'vitest';
import type { MockedFunction } from 'vitest';

import type { ChatInputCommandInteraction } from '@/features/library/index.js';
import type {
ChatInputCommandInteraction,
Guild,
} from '@/features/library/index.js';
import { isNil } from '@/features/library/index.js';
import { getGuildFromInteraction } from '@/features/others/discord/index.js';

import { getInteractionMemberId } from './getInteractionMemberId.func.js';
import { getActorId } from './getActorId.func.js';

vi.mock('@/features/library/index.js', () => {
return {
Expand All @@ -22,17 +25,19 @@ const mockedGuild = {
members: {
fetch: vi.fn().mockResolvedValue('mocked member'),
},
} as const;
} as unknown as Guild;

const fakeInteraction = {
member: { user: { id: '123' } },
} as ChatInputCommandInteraction;

describe('getInteractionMemberId', () => {
describe('getActorId', () => {
it('should fetch the member ID from the interaction', async () => {
(getGuildFromInteraction as unknown as Mock).mockReturnValue(mockedGuild);
(
getGuildFromInteraction as MockedFunction<typeof getGuildFromInteraction>
).mockReturnValue(mockedGuild);

const result = await getInteractionMemberId(fakeInteraction);
const result = await getActorId(fakeInteraction);

expect(getGuildFromInteraction).toHaveBeenCalledWith({
interaction: fakeInteraction,
Expand All @@ -46,9 +51,9 @@ describe('getInteractionMemberId', () => {
member: null,
} as ChatInputCommandInteraction;

(isNil as unknown as Mock).mockReturnValue(true);
(isNil as unknown as MockedFunction<typeof isNil>).mockReturnValue(true);

await expect(getInteractionMemberId(fakeInteraction)).rejects.toThrow(
await expect(getActorId(fakeInteraction)).rejects.toThrow(
'Member is null.'
);
});
Expand Down

This file was deleted.

0 comments on commit 707b627

Please sign in to comment.