Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 164 additions & 63 deletions packages/bot/src/functions/music/commands/autoplay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ const QueueRepeatMode = {
} as const

const requireGuildMock = jest.fn()
const requireQueueMock = jest.fn()
const interactionReplyMock = jest.fn()
const createEmbedMock = jest.fn((payload: unknown) => payload)
const replenishQueueMock = jest.fn()
const debugLogMock = jest.fn()
const errorLogMock = jest.fn()
const warnLogMock = jest.fn()
const resolveGuildQueueMock = jest.fn()
const getGuildSettingsMock = jest.fn()
const setGuildSettingsMock = jest.fn()

jest.mock('../../../utils/command/commandValidations', () => ({
requireGuild: (...args: unknown[]) => requireGuildMock(...args),
requireQueue: (...args: unknown[]) => requireQueueMock(...args),
}))

jest.mock('discord-player', () => ({
Expand Down Expand Up @@ -58,6 +58,13 @@ jest.mock('@lucky/shared/utils', () => ({
warnLog: (...args: unknown[]) => warnLogMock(...args),
}))

jest.mock('@lucky/shared/services', () => ({
guildSettingsService: {
getGuildSettings: (...args: unknown[]) => getGuildSettingsMock(...args),
setGuildSettings: (...args: unknown[]) => setGuildSettingsMock(...args),
},
}))

function createInteraction(guildId = 'guild-1') {
const interaction = {
guildId,
Expand Down Expand Up @@ -96,9 +103,8 @@ describe('autoplay command', () => {
beforeEach(() => {
jest.clearAllMocks()
requireGuildMock.mockResolvedValue(true)
requireQueueMock.mockImplementation(async (queue: unknown) =>
Boolean(queue),
)
getGuildSettingsMock.mockResolvedValue(null)
setGuildSettingsMock.mockResolvedValue(true)
resolveGuildQueueMock.mockReturnValue({
queue: null,
source: 'miss',
Expand All @@ -110,11 +116,9 @@ describe('autoplay command', () => {
})
})

it('uses resolved guild queue when fallback source recovers queue', async () => {
it('enables autoplay on active queue and persists preference', async () => {
const queue = createQueue(QueueRepeatMode.OFF)
const client = createClient({
directQueue: null,
})
const client = createClient({ directQueue: null })
const interaction = createInteraction()
resolveGuildQueueMock.mockReturnValue({
queue,
Expand All @@ -131,20 +135,19 @@ describe('autoplay command', () => {
interaction,
} as any)

expect(resolveGuildQueueMock).toHaveBeenCalledWith(client, 'guild-1')
expect(requireQueueMock).toHaveBeenCalledWith(queue, interaction)
expect(queue.setRepeatMode).toHaveBeenCalledWith(
QueueRepeatMode.AUTOPLAY,
)
expect(setGuildSettingsMock).toHaveBeenCalledWith('guild-1', {
autoPlayEnabled: true,
})
expect(replenishQueueMock).toHaveBeenCalledWith(queue)
expect(interactionReplyMock).toHaveBeenCalled()
})

it('disables autoplay when already enabled', async () => {
const queue = createQueue(QueueRepeatMode.AUTOPLAY)
const client = createClient({
directQueue: queue,
})
const client = createClient({ directQueue: queue })
const interaction = createInteraction()
resolveGuildQueueMock.mockReturnValue({
queue,
Expand All @@ -162,55 +165,110 @@ describe('autoplay command', () => {
} as any)

expect(queue.setRepeatMode).toHaveBeenCalledWith(QueueRepeatMode.OFF)
expect(setGuildSettingsMock).toHaveBeenCalledWith('guild-1', {
autoPlayEnabled: false,
})
expect(replenishQueueMock).not.toHaveBeenCalled()
expect(interactionReplyMock).toHaveBeenCalled()
})

it('returns early when interaction guild id is missing', async () => {
const queue = createQueue(QueueRepeatMode.OFF)
const client = createClient({
directQueue: queue,
})
const interaction = createInteraction(null as unknown as string)
it('enables autoplay when no queue exists and settings are missing', async () => {
const client = createClient({ directQueue: null })
const interaction = createInteraction()

await autoplayCommand.execute({
client,
interaction,
} as any)

expect(interaction.deferReply).not.toHaveBeenCalled()
expect(requireQueueMock).not.toHaveBeenCalled()
expect(interactionReplyMock).not.toHaveBeenCalled()
expect(resolveGuildQueueMock).not.toHaveBeenCalled()
expect(setGuildSettingsMock).toHaveBeenCalledWith('guild-1', {
autoPlayEnabled: true,
})
expect(interactionReplyMock).toHaveBeenCalled()
const embedPayload = createEmbedMock.mock.calls[0]?.[0] as {
description: string
}
expect(embedPayload.description).toContain(
'Next time you use /play',
)
})

it('passes null queue to validator when resolver misses', async () => {
it('shows an error when enabling autoplay without a queue fails to persist', async () => {
const client = createClient({ directQueue: null })
const interaction = createInteraction()
requireQueueMock.mockResolvedValue(false)
setGuildSettingsMock.mockResolvedValue(false)

await autoplayCommand.execute({
client,
interaction,
} as any)

expect(requireQueueMock).toHaveBeenCalledWith(null, interaction)
expect(interactionReplyMock).not.toHaveBeenCalled()
expect(warnLogMock).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Failed to persist autoplay enabled preference',
}),
)
expect(createEmbedMock).toHaveBeenCalledWith(
expect.objectContaining({
title: 'Autoplay preference not saved',
}),
)
expect(interactionReplyMock).toHaveBeenCalledWith(
expect.objectContaining({
content: expect.objectContaining({
ephemeral: true,
}),
}),
)
expect(replenishQueueMock).not.toHaveBeenCalled()
})

it('passes null queue to validator when resolver returns miss diagnostics', async () => {
const client = createClient({
directQueue: null,
it('shows a queue-only warning when disabling autoplay cannot persist', async () => {
const queue = createQueue(QueueRepeatMode.AUTOPLAY)
const client = createClient({ directQueue: queue })
const interaction = createInteraction()
setGuildSettingsMock.mockResolvedValue(false)
resolveGuildQueueMock.mockReturnValue({
queue,
source: 'nodes.get',
diagnostics: {
guildId: 'guild-1',
cacheSize: 1,
cacheSampleKeys: ['guild-1'],
},
})
const interaction = createInteraction('guild-1')
requireQueueMock.mockResolvedValue(false)

await autoplayCommand.execute({
client,
interaction,
} as any)

expect(queue.setRepeatMode).toHaveBeenCalledWith(QueueRepeatMode.OFF)
expect(warnLogMock).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Failed to persist autoplay disabled preference',
}),
)
expect(createEmbedMock).toHaveBeenCalledWith(
expect.objectContaining({
title: 'Autoplay disabled for current queue only',
}),
)
expect(replenishQueueMock).not.toHaveBeenCalled()
})

it('shows a queue-only warning when enabling autoplay cannot persist', async () => {
const queue = createQueue(QueueRepeatMode.OFF)
const client = createClient({ directQueue: queue })
const interaction = createInteraction()
setGuildSettingsMock.mockResolvedValue(false)
resolveGuildQueueMock.mockReturnValue({
queue: null,
source: 'miss',
queue,
source: 'nodes.get',
diagnostics: {
guildId: 'guild-1',
cacheSize: 2,
cacheSampleKeys: ['guild-2', 'guild-3'],
cacheSize: 1,
cacheSampleKeys: ['guild-1'],
},
})

Expand All @@ -219,15 +277,75 @@ describe('autoplay command', () => {
interaction,
} as any)

expect(requireQueueMock).toHaveBeenCalledWith(null, interaction)
expect(queue.setRepeatMode).toHaveBeenCalledWith(
QueueRepeatMode.AUTOPLAY,
)
expect(warnLogMock).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Failed to persist autoplay enabled preference',
}),
)
expect(createEmbedMock).toHaveBeenCalledWith(
expect.objectContaining({
title: 'Autoplay enabled for current queue only',
}),
)
expect(replenishQueueMock).toHaveBeenCalledWith(queue)
})

it('disables stored preference when no queue and was enabled', async () => {
const client = createClient({ directQueue: null })
const interaction = createInteraction()
getGuildSettingsMock.mockResolvedValue({ autoPlayEnabled: true })

await autoplayCommand.execute({
client,
interaction,
} as any)

expect(setGuildSettingsMock).toHaveBeenCalledWith('guild-1', {
autoPlayEnabled: false,
})
expect(createEmbedMock).toHaveBeenCalledWith(
expect.objectContaining({ title: 'Autoplay disabled' }),
)
})

it('enables stored preference when no queue and autoplay was disabled', async () => {
const client = createClient({ directQueue: null })
const interaction = createInteraction()
getGuildSettingsMock.mockResolvedValue({ autoPlayEnabled: false })

await autoplayCommand.execute({
client,
interaction,
} as any)

expect(setGuildSettingsMock).toHaveBeenCalledWith('guild-1', {
autoPlayEnabled: true,
})
expect(createEmbedMock).toHaveBeenCalledWith(
expect.objectContaining({ title: 'Autoplay enabled' }),
)
})

it('returns early when interaction guild id is missing', async () => {
const queue = createQueue(QueueRepeatMode.OFF)
const client = createClient({ directQueue: queue })
const interaction = createInteraction(null as unknown as string)

await autoplayCommand.execute({
client,
interaction,
} as any)

expect(interactionReplyMock).not.toHaveBeenCalled()
expect(resolveGuildQueueMock).not.toHaveBeenCalled()
})

it('replies before replenishment finishes', async () => {
const queue = createQueue(QueueRepeatMode.OFF)
const client = createClient({
directQueue: queue,
})
const client = createClient({ directQueue: queue })
const interaction = createInteraction()
let resolveReplenish: () => void = () => {}

Expand Down Expand Up @@ -266,11 +384,9 @@ describe('autoplay command', () => {
await executePromise
})

it('logs error and still replies when queue replenish fails', async () => {
it('logs replenish failures after enabling autoplay', async () => {
const queue = createQueue(QueueRepeatMode.OFF)
const client = createClient({
directQueue: queue,
})
const client = createClient({ directQueue: queue })
const interaction = createInteraction()
replenishQueueMock.mockRejectedValue(new Error('replenish failed'))
resolveGuildQueueMock.mockReturnValue({
Expand All @@ -289,25 +405,19 @@ describe('autoplay command', () => {
} as any)

await Promise.resolve()
expect(queue.setRepeatMode).toHaveBeenCalledWith(
QueueRepeatMode.AUTOPLAY,
)
expect(errorLogMock).toHaveBeenCalledWith(
expect.objectContaining({
message: 'Error replenishing queue after enabling autoplay:',
}),
)
expect(interactionReplyMock).toHaveBeenCalled()
})

it('uses autoplay error response when execution throws unexpectedly', async () => {
it('uses autoplay error response when execution throws', async () => {
const queue = createQueue(QueueRepeatMode.OFF)
queue.setRepeatMode.mockImplementation(() => {
throw new Error('unexpected')
})
const client = createClient({
directQueue: queue,
})
const client = createClient({ directQueue: queue })
const interaction = createInteraction()
resolveGuildQueueMock.mockReturnValue({
queue,
Expand All @@ -330,16 +440,7 @@ describe('autoplay command', () => {
}),
)
expect(createEmbedMock).toHaveBeenCalledWith(
expect.objectContaining({
title: 'Error',
}),
)
expect(interactionReplyMock).toHaveBeenCalledWith(
expect.objectContaining({
content: expect.objectContaining({
ephemeral: true,
}),
}),
expect.objectContaining({ title: 'Error' }),
)
})
})
Loading