Skip to content

Commit

Permalink
fix: fix channel name lookup bug for edge drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
Sitlintac committed Sep 21, 2023
1 parent 0ebd3c7 commit 4a5d13c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-dodos-attack.md
@@ -0,0 +1,5 @@
---
"@smartthings/plugin-cli-edge": patch
---

fix channel name lookup bug for edge:drivers:installed command
15 changes: 15 additions & 0 deletions packages/edge/src/__tests__/lib/commands/channels-util.test.ts
Expand Up @@ -305,5 +305,20 @@ describe('channels-util', () => {
expect(apiListChannelsMock).toHaveBeenCalledWith(expect.objectContaining({ includeReadOnly: true }))
expect(apiGetChannelsMock).toHaveBeenCalledTimes(0)
})

it('looks up and adds channel name when missing from list', async () => {
apiListChannelsMock.mockResolvedValueOnce([channel1])
apiGetChannelsMock.mockResolvedValueOnce(channel2)

expect(await withChannelNames(client, [thingWithChannel1, thingWithChannel2])).toStrictEqual([
{ channelId: 'channel-id-1', channelName: 'Channel 1' },
{ channelId: 'channel-id-2', channelName: 'Channel 2' },
])

expect(apiListChannelsMock).toHaveBeenCalledTimes(1)
expect(apiListChannelsMock).toHaveBeenCalledWith(expect.objectContaining({ includeReadOnly: true }))
expect(apiGetChannelsMock).toHaveBeenCalledTimes(1)
expect(apiGetChannelsMock).toHaveBeenCalledWith('channel-id-2')
})
})
})
8 changes: 8 additions & 0 deletions packages/edge/src/lib/commands/channels-util.ts
Expand Up @@ -95,6 +95,14 @@ export async function withChannelNames<T extends WithChannel>(client: SmartThing
if (Array.isArray(input)) {
const channels = await listChannels(client, { includeReadOnly: true })
const channelNamesById = new Map(channels.map(channel => [channel.channelId, channel.name]))

for (const inputItem of input) {
if (!channelNamesById.get(inputItem.channelId)) {
const channelName = (await client.channels.get(inputItem.channelId)).name
channelNamesById.set(inputItem.channelId, channelName)
}
}

return input.map(input => ({ ...input, channelName: channelNamesById.get(input.channelId) }))
}

Expand Down

0 comments on commit 4a5d13c

Please sign in to comment.