Skip to content
Open
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
37 changes: 37 additions & 0 deletions apps/sim/blocks/blocks/spotify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import { SpotifyBlock } from '@/blocks/blocks/spotify'
import type { BlockToolParamsContext } from '@/blocks/types'

/** Merges the mapper's result over the inputs the way the generic handler and tool calls do. */
function toolParams(inputs: Record<string, unknown>, context?: BlockToolParamsContext) {
return { ...inputs, ...SpotifyBlock.tools.config?.params?.(inputs, context) }
}

describe('Spotify player params', () => {
it('transfers playback to the Target Device ID', () => {
expect(
toolParams({ operation: 'spotify_transfer_playback', target_device_id: 'kitchen' })
).toMatchObject({ device_id: 'kitchen' })
})

it.each([true, false])('sets shuffle to the switch value %s', (shuffle) => {
expect(
toolParams({ operation: 'spotify_set_shuffle', shuffle_state: shuffle, device_id: 'kitchen' })
).toMatchObject({ state: shuffle, device_id: 'kitchen' })
})

it("keeps the model's own args on a tool call that leaves the block fields blank", () => {
expect(
toolParams(
{ target_device_id: '', device_id: 'kitchen' },
{ operation: 'spotify_transfer_playback' }
)
).toMatchObject({ device_id: 'kitchen' })
expect(toolParams({ state: false }, { operation: 'spotify_set_shuffle' })).toMatchObject({
state: false,
})
})
})
11 changes: 9 additions & 2 deletions apps/sim/blocks/blocks/spotify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SpotifyIcon } from '@/components/icons'
import type { BlockConfig, BlockMeta } from '@/blocks/types'
import { AuthMode, IntegrationType } from '@/blocks/types'
import { normalizeFileInput } from '@/blocks/utils'
import { blockFieldOrToolArg, mapperOperation, normalizeFileInput } from '@/blocks/utils'

/**
* Canonical basic/advanced pair for the playlist cover art, shared by the card
Expand Down Expand Up @@ -1009,8 +1009,15 @@ export const SpotifyBlock: BlockConfig = {
if (params.playUris) params.uris = params.playUris
return params.operation || 'spotify_search'
},
params: (params) => {
params: (params, context) => {
const result: Record<string, unknown> = {}
const operation = mapperOperation(params, context)
if (operation === 'spotify_transfer_playback') {
result.device_id = blockFieldOrToolArg(context, params.target_device_id, params.device_id)
}
if (operation === 'spotify_set_shuffle') {
result.state = blockFieldOrToolArg(context, params.shuffle_state, params.state)
}
if (params.limit) result.limit = Number(params.limit)
if (params.volume_percent) result.volume_percent = Number(params.volume_percent)
if (params.range_start) result.range_start = Number(params.range_start)
Expand Down