Skip to content
Merged
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

This file was deleted.

35 changes: 0 additions & 35 deletions packages/edge/src/commands/edge/channels/enrollments.ts

This file was deleted.

97 changes: 97 additions & 0 deletions src/__tests__/commands/edge/channels/enrollments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { jest } from '@jest/globals'

import type { ArgumentsCamelCase, Argv } from 'yargs'

import type { EnrolledChannel, HubdevicesEndpoint } from '@smartthings/core-sdk'

import type { CommandArgs } from '../../../../commands/edge/channels/enrollments.js'
import type { APICommand, APICommandFlags } from '../../../../lib/command/api-command.js'
import type { outputList, outputListBuilder } from '../../../../lib/command/output-list.js'
import type { chooseHub } from '../../../../lib/command/util/hubs-choose.js'
import { apiCommandMocks } from '../../../test-lib/api-command-mock.js'
import { buildArgvMock, buildArgvMockStub } from '../../../test-lib/builder-mock.js'


const { apiCommandMock, apiCommandBuilderMock, apiDocsURLMock } = apiCommandMocks('../../../..')

const outputListMock = jest.fn<typeof outputList>()
const outputListBuilderMock = jest.fn<typeof outputListBuilder>()
jest.unstable_mockModule('../../../../lib/command/output-list.js', () => ({
outputList: outputListMock,
outputListBuilder: outputListBuilderMock,
}))

const chooseHubMock = jest.fn<typeof chooseHub>()
jest.unstable_mockModule('../../../../lib/command/util/hubs-choose.js', () => ({
chooseHub: chooseHubMock,
}))


const { default: cmd } = await import('../../../../commands/edge/channels/enrollments.js')


test('builder', async () => {
const yargsMock = buildArgvMockStub<object>()
const {
yargsMock: apiCommandBuilderArgvMock,
positionalMock,
optionMock,
exampleMock,
epilogMock,
argvMock,
} = buildArgvMock<APICommandFlags, CommandArgs>()

apiCommandBuilderMock.mockReturnValue(apiCommandBuilderArgvMock)
outputListBuilderMock.mockReturnValue(argvMock)

const builder = cmd.builder as (yargs: Argv<object>) => Argv<CommandArgs>

expect(builder(yargsMock)).toBe(argvMock)

expect(apiCommandBuilderMock).toHaveBeenCalledExactlyOnceWith(yargsMock)
expect(outputListBuilderMock).toHaveBeenCalledExactlyOnceWith(apiCommandBuilderArgvMock)
expect(positionalMock).toHaveBeenCalledTimes(1)
expect(optionMock).toHaveBeenCalledTimes(0)
expect(exampleMock).toHaveBeenCalledTimes(1)
expect(apiDocsURLMock).toHaveBeenCalledTimes(1)
expect(epilogMock).toHaveBeenCalledTimes(1)
})

test('handler', async () => {
const apiHubDevicesEnrolledChannelsMock = jest.fn<typeof HubdevicesEndpoint.prototype.enrolledChannels>()
const command = {
client:
{
hubdevices: {
enrolledChannels: apiHubDevicesEnrolledChannelsMock,
},
},
} as unknown as APICommand
apiCommandMock.mockResolvedValueOnce(command)
chooseHubMock.mockResolvedValueOnce('chosen-hub-id')

const inputArgv = {
profile: 'default',
idOrIndex: 'cmd-line-id',
} as ArgumentsCamelCase<CommandArgs>

await expect(cmd.handler(inputArgv)).resolves.not.toThrow()

expect(apiCommandMock).toHaveBeenCalledExactlyOnceWith(inputArgv)
expect(chooseHubMock).toHaveBeenCalledExactlyOnceWith(
command,
'cmd-line-id',
{ allowIndex: true, useConfigDefault: true },
)
expect(outputListMock).toHaveBeenCalledExactlyOnceWith(
command,
expect.objectContaining({ primaryKeyName: 'channelId' }),
expect.any(Function),
)

const listFunction = outputListMock.mock.calls[0][2]
const channels = [{ channelId: 'channel-id' } as EnrolledChannel]
apiHubDevicesEnrolledChannelsMock.mockResolvedValueOnce(channels)

expect(await listFunction()).toBe(channels)
})
68 changes: 68 additions & 0 deletions src/commands/edge/channels/enrollments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { type ArgumentsCamelCase, type Argv, type CommandModule } from 'yargs'

import type { EnrolledChannel } from '@smartthings/core-sdk'

import { apiCommand, apiCommandBuilder, apiDocsURL, type APICommandFlags } from '../../../lib/command/api-command.js'
import {
outputList,
outputListBuilder,
type OutputListConfig,
type OutputListFlags,
} from '../../../lib/command/output-list.js'
import { chooseHub } from '../../../lib/command/util/hubs-choose.js'


export type CommandArgs =
& APICommandFlags
& OutputListFlags
& {
idOrIndex?: string
}

const command = 'edge:channels:enrollments [id-or-index]'

const describe = 'list all channels a given hub is enrolled in'


const builder = (yargs: Argv): Argv<CommandArgs> =>
outputListBuilder(apiCommandBuilder(yargs))
.positional('id-or-index', { describe: 'hub id or number in list', type: 'string' })
.example([
[
'$0 edge:channels:enrollments',
'prompt for a hub (or use default) and display channels it is enrolled in',
],
[
'$0 edge:channels:enrollments 86042494-ae59-43a8-bc09-654103b5c5b3',
'display channels the specified hub is enrolled in',
],
])
.epilog(apiDocsURL('listDriverChannels'))

const handler = async (argv: ArgumentsCamelCase<CommandArgs>): Promise<void> => {
const command = await apiCommand(argv)

const config: OutputListConfig<EnrolledChannel> = {
primaryKeyName: 'channelId',
sortKeyName: 'name',
listTableFieldDefinitions: [
'channelId',
'name',
'description',
'createdDate',
'lastModifiedDate',
'subscriptionUrl',
],
}

const hubId = await chooseHub(
command,
argv.idOrIndex,
{ allowIndex: true, useConfigDefault: true },
)

await outputList(command, config, () => command.client.hubdevices.enrolledChannels(hubId))
}

const cmd: CommandModule<object, CommandArgs> = { command, describe, builder, handler }
export default cmd
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import edgeChannelsAssignCommand from './edge/channels/assign.js'
import edgeChannelsCreateCommand from './edge/channels/create.js'
import edgeChannelsDeleteCommand from './edge/channels/delete.js'
import edgeChannelsDriversCommand from './edge/channels/drivers.js'
import edgeChannelsEnrollmentsCommand from './edge/channels/enrollments.js'
import edgeChannelsInvitesCommand from './edge/channels/invites.js'
import edgeChannelsInvitesAcceptCommand from './edge/channels/invites/accept.js'
import edgeChannelsInvitesCreateCommand from './edge/channels/invites/create.js'
Expand Down Expand Up @@ -186,6 +187,7 @@ export const commands: CommandModule<object, any>[] = [
edgeChannelsCreateCommand,
edgeChannelsDeleteCommand,
edgeChannelsDriversCommand,
edgeChannelsEnrollmentsCommand,
edgeChannelsInvitesCommand,
edgeChannelsInvitesAcceptCommand,
edgeChannelsInvitesCreateCommand,
Expand Down