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 4b5c909 commit 239fe41
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ describe('Voice', () => {
.mockReturnValueOnce('unknown' as any);

await expect(voice.join({ interaction })).rejects.toThrowError(
'Unknown joinable type.'
'unknown is unexpected value. Should have been never.'
);
});
});
Expand Down
6 changes: 2 additions & 4 deletions src/features/core/internal/Voice/internal/Voice.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,9 @@ export class Voice {

default:
assertNever(joinable);
// HACK: ここでrejectするとSwitch外のカバレッジがuncoveredになる
break;
}

return Promise.reject(new LogicException('Unknown joinable type.'));
/* c8 ignore next */
return;
}

public async leave({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,28 @@ export const getJoinableStateStatus = ({
}: {
channel: VoiceBasedChannel | null;
}): (typeof JOINABLE_STATE_STATUS)[keyof typeof JOINABLE_STATE_STATUS] => {
// getVoiceConnectionやgetVoiceConnectionsを利用せずに、自身がjoinしているかどうかを判定する
const client = channel?.client;
const guildId = channel?.guildId;
if (!client || !guildId) {
return JOINABLE_STATE_STATUS.NOT_FOUND;
}

const voiceState = (() => {
const guild = client.guilds.cache.get(guildId);
if (!guild) return undefined;
const voiceState = guild.voiceStates.cache.get(client.user.id);
return voiceState;
})();

if (isNil(channel)) {
return JOINABLE_STATE_STATUS.NOT_FOUND;
} else if (!channel.joinable) {
return JOINABLE_STATE_STATUS.NOT_JOINABLE;
} else if (!channel.viewable) {
return JOINABLE_STATE_STATUS.NOT_VIEWABLE;
} else if (!isNil(voiceState)) {
return JOINABLE_STATE_STATUS.ALREADY_JOINED;
} else {
return JOINABLE_STATE_STATUS.JOINABLE;
// getVoiceConnectionやgetVoiceConnectionsを利用せずに、自身がjoinしているかどうかを判定する
const client = channel.client;
const guildId = channel.guildId;

const voiceState = (() => {
const guild = client.guilds.cache.get(guildId);
if (!guild) return undefined;
const voiceState = guild.voiceStates.cache.get(client.user.id);
return voiceState;
})();

if (!channel.joinable) {
return JOINABLE_STATE_STATUS.NOT_JOINABLE;
} else if (!channel.viewable) {
return JOINABLE_STATE_STATUS.NOT_VIEWABLE;
} else if (!!voiceState) {
return JOINABLE_STATE_STATUS.ALREADY_JOINED;
} else {
return JOINABLE_STATE_STATUS.JOINABLE;
}
}
};
Original file line number Diff line number Diff line change
@@ -1,35 +1,79 @@
import type { VoiceBasedChannel } from '@/features/library/index.js';

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

describe('getJoinableStateStatus', () => {
it('should return NOT_FOUND if the channel is null', () => {
const result = getJoinableStateStatus({ channel: null });

expect(result).toBe('NOT_FOUND');
expect(result).toBe(JOINABLE_STATE_STATUS.NOT_FOUND);
});

it('should return NOT_JOINABLE if the channel is not joinable', () => {
const result = getJoinableStateStatus({
channel: { joinable: false } as VoiceBasedChannel,
channel: {
joinable: false,
client: { guilds: { cache: new Map() } },
guildId: 'guildId',
} as VoiceBasedChannel,
});

expect(result).toBe('NOT_JOINABLE');
});

it('should return NOT_VIEWABLE if the channel is not viewable', () => {
const result = getJoinableStateStatus({
channel: { joinable: true, viewable: false } as VoiceBasedChannel,
channel: {
joinable: true,
viewable: false,
client: { guilds: { cache: new Map() } },
guildId: 'guildId',
} as VoiceBasedChannel,
});

expect(result).toBe('NOT_VIEWABLE');
});

it('should return JOINABLE if the channel is joinable and viewable', () => {
const result = getJoinableStateStatus({
channel: { joinable: true, viewable: true } as VoiceBasedChannel,
channel: {
joinable: true,
viewable: true,
client: { guilds: { cache: new Map() } },
guildId: 'guildId',
} as VoiceBasedChannel,
});

expect(result).toBe('JOINABLE');
expect(result).toBe(JOINABLE_STATE_STATUS.JOINABLE);
});

it('should return undefined if the voice state is not found', () => {
const result = getJoinableStateStatus({
channel: {
joinable: true,
viewable: true,
client: {
user: { id: 'userId' },
guilds: {
cache: new Map([
[
'guildId',
{
voiceStates: {
cache: new Map([
['userId', { voiceStates: { cache: new Map() } }],
]),
},
},
],
]),
},
},
guildId: 'guildId',
} as unknown as VoiceBasedChannel,
});

expect(result).toBe(JOINABLE_STATE_STATUS.ALREADY_JOINED);
});
});

0 comments on commit 239fe41

Please sign in to comment.