Skip to content

Commit 0b7934b

Browse files
committed
fix: fix failure to output JSON or YAML history
1 parent f18d75c commit 0b7934b

File tree

5 files changed

+60
-42
lines changed

5 files changed

+60
-42
lines changed

.changeset/clever-trees-hang.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smartthings/cli": patch
3+
---
4+
5+
fixed bug when outputting JSON or YAML device or location history

packages/cli/src/__tests__/commands/devices/history.test.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
calculateOutputFormat,
44
chooseDevice,
55
IOFormat,
6-
jsonFormatter,
76
writeOutput,
87
} from '@smartthings/cli-lib'
98
import { Device, DeviceActivity, DevicesEndpoint, HistoryEndpoint, PaginatedList, SmartThingsClient } from '@smartthings/core-sdk'
@@ -14,29 +13,14 @@ import { calculateRequestLimit, getHistory, writeDeviceEventsTable } from '../..
1413
jest.mock('../../../lib/commands/history-util')
1514

1615
describe('DeviceHistoryCommand', () => {
16+
const chooseDeviceMock = jest.mocked(chooseDevice).mockResolvedValue('deviceId')
1717
const getDeviceSpy = jest.spyOn(DevicesEndpoint.prototype, 'get').mockImplementation()
1818
const historySpy = jest.spyOn(HistoryEndpoint.prototype, 'devices').mockImplementation()
19-
const deviceSelectionMock = jest.mocked(chooseDevice).mockResolvedValue('deviceId')
2019
const calculateOutputFormatMock = jest.mocked(calculateOutputFormat).mockReturnValue(IOFormat.COMMON)
2120
const writeDeviceEventsTableMock = jest.mocked(writeDeviceEventsTable)
22-
const calculateHistoryRequestLimitMock = jest.mocked(calculateRequestLimit)
21+
const calculateRequestLimitMock = jest.mocked(calculateRequestLimit)
2322
const getHistoryMock = jest.mocked(getHistory)
2423

25-
it('prompts user to select device', async () => {
26-
getDeviceSpy.mockResolvedValueOnce({ locationId: 'locationId' } as Device)
27-
historySpy.mockResolvedValueOnce({
28-
items: [],
29-
hasNext: (): boolean => false,
30-
} as unknown as PaginatedList<DeviceActivity>)
31-
await expect(DeviceHistoryCommand.run(['deviceId'])).resolves.not.toThrow()
32-
33-
expect(deviceSelectionMock).toHaveBeenCalledWith(
34-
expect.any(DeviceHistoryCommand),
35-
'deviceId',
36-
{ allowIndex: true },
37-
)
38-
})
39-
4024
it('queries history and writes event table interactively', async () => {
4125
getDeviceSpy.mockResolvedValueOnce({ locationId: 'locationId' } as Device)
4226
historySpy.mockResolvedValueOnce({
@@ -46,8 +30,13 @@ describe('DeviceHistoryCommand', () => {
4630

4731
await expect(DeviceHistoryCommand.run(['deviceId'])).resolves.not.toThrow()
4832

33+
expect(calculateRequestLimitMock).toHaveBeenCalledTimes(1)
34+
expect(calculateRequestLimitMock).toHaveBeenCalledWith(20)
35+
expect(chooseDeviceMock).toHaveBeenCalledTimes(1)
36+
expect(chooseDeviceMock).toHaveBeenCalledWith(expect.any(DeviceHistoryCommand), 'deviceId', { allowIndex: true })
4937
expect(getDeviceSpy).toHaveBeenCalledTimes(1)
5038
expect(getDeviceSpy).toHaveBeenCalledWith('deviceId')
39+
expect(calculateOutputFormatMock).toHaveBeenCalledTimes(1)
5140
expect(historySpy).toHaveBeenCalledTimes(1)
5241
expect(historySpy).toHaveBeenCalledWith({
5342
deviceId: 'deviceId',
@@ -56,23 +45,28 @@ describe('DeviceHistoryCommand', () => {
5645
expect(writeDeviceEventsTableMock).toHaveBeenCalledTimes(1)
5746
})
5847

59-
it('queries history and write event table directly', async () => {
60-
const buildOutputFormatterMock = jest.mocked(buildOutputFormatter)
48+
it('writes non-table output when specified', async () => {
49+
const outputFormatterMock = jest.fn().mockReturnValueOnce('formatted output')
50+
const buildOutputFormatterMock = jest.mocked(buildOutputFormatter<DeviceActivity[]>)
6151
const writeOutputMock = jest.mocked(writeOutput)
6252

63-
calculateHistoryRequestLimitMock.mockReturnValueOnce(20)
53+
const items = [{ deviceId: 'device-1' }] as DeviceActivity[]
54+
55+
calculateRequestLimitMock.mockReturnValueOnce(20)
6456
getDeviceSpy.mockResolvedValueOnce({ locationId: 'locationId' } as Device)
65-
historySpy.mockResolvedValueOnce({
66-
items: [],
67-
hasNext: (): boolean => false,
68-
} as unknown as PaginatedList<DeviceActivity>)
6957
calculateOutputFormatMock.mockReturnValueOnce(IOFormat.JSON)
70-
buildOutputFormatterMock.mockReturnValueOnce(jsonFormatter(4))
58+
buildOutputFormatterMock.mockReturnValueOnce(outputFormatterMock)
59+
getHistoryMock.mockResolvedValueOnce(items)
7160

7261
await expect(DeviceHistoryCommand.run(['deviceId'])).resolves.not.toThrow()
7362

63+
expect(calculateRequestLimitMock).toHaveBeenCalledTimes(1)
64+
expect(calculateRequestLimitMock).toHaveBeenCalledWith(20)
65+
expect(chooseDeviceMock).toHaveBeenCalledTimes(1)
66+
expect(chooseDeviceMock).toHaveBeenCalledWith(expect.any(DeviceHistoryCommand), 'deviceId', { allowIndex: true })
7467
expect(getDeviceSpy).toHaveBeenCalledTimes(1)
7568
expect(getDeviceSpy).toHaveBeenCalledWith('deviceId')
69+
expect(calculateOutputFormatMock).toHaveBeenCalledTimes(1)
7670
expect(getHistoryMock).toHaveBeenCalledTimes(1)
7771
expect(getHistoryMock).toHaveBeenCalledWith(
7872
expect.any(SmartThingsClient),
@@ -84,7 +78,10 @@ describe('DeviceHistoryCommand', () => {
8478
}),
8579
)
8680
expect(buildOutputFormatterMock).toHaveBeenCalledTimes(1)
81+
expect(outputFormatterMock).toHaveBeenCalledTimes(1)
82+
expect(outputFormatterMock).toHaveBeenCalledWith(items)
8783
expect(writeOutputMock).toHaveBeenCalledTimes(1)
84+
expect(writeOutputMock).toHaveBeenCalledWith('formatted output', undefined)
8885

8986
expect(historySpy).toHaveBeenCalledTimes(0)
9087
expect(writeDeviceEventsTableMock).toHaveBeenCalledTimes(0)

packages/cli/src/__tests__/commands/locations/history.test.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1+
import { DeviceActivity, HistoryEndpoint, PaginatedList, SmartThingsClient } from '@smartthings/core-sdk'
2+
13
import {
24
buildOutputFormatter,
35
calculateOutputFormat,
46
IOFormat,
5-
jsonFormatter,
67
writeOutput,
78
} from '@smartthings/cli-lib'
8-
import { DeviceActivity, HistoryEndpoint, PaginatedList, SmartThingsClient } from '@smartthings/core-sdk'
9+
910
import LocationHistoryCommand from '../../../commands/locations/history'
1011
import { calculateRequestLimit, getHistory, writeDeviceEventsTable } from '../../../lib/commands/history-util'
1112
import { chooseLocation } from '../../../commands/locations'
13+
import LocationDeviceHistoryCommand from '../../../commands/locations/history'
1214

1315

1416
jest.mock('../../../lib/commands/history-util')
1517
jest.mock('../../../commands/locations')
1618

1719
describe('LocationHistoryCommand', () => {
18-
const mockChooseLocation = jest.mocked(chooseLocation).mockResolvedValue('locationId')
20+
const chooseLocationMock = jest.mocked(chooseLocation).mockResolvedValue('locationId')
1921
const historySpy = jest.spyOn(HistoryEndpoint.prototype, 'devices').mockImplementation()
2022
const calculateOutputFormatMock = jest.mocked(calculateOutputFormat).mockReturnValue(IOFormat.COMMON)
2123
const writeDeviceEventsTableMock = jest.mocked(writeDeviceEventsTable)
22-
const calculateHistoryRequestLimitMock = jest.mocked(calculateRequestLimit)
24+
const calculateRequestLimitMock = jest.mocked(calculateRequestLimit)
2325
const getHistoryMock = jest.mocked(getHistory)
2426

2527
it('queries history and writes event table interactively', async () => {
@@ -30,7 +32,10 @@ describe('LocationHistoryCommand', () => {
3032

3133
await expect(LocationHistoryCommand.run(['locationId'])).resolves.not.toThrow()
3234

33-
expect(mockChooseLocation).toHaveBeenCalledTimes(1)
35+
expect(calculateRequestLimitMock).toHaveBeenCalledTimes(1)
36+
expect(calculateRequestLimitMock).toHaveBeenCalledWith(20)
37+
expect(chooseLocationMock).toHaveBeenCalledTimes(1)
38+
expect(chooseLocationMock).toHaveBeenCalledWith(expect.any(LocationDeviceHistoryCommand), 'locationId', true, true)
3439
expect(calculateOutputFormatMock).toHaveBeenCalledTimes(1)
3540
expect(historySpy).toHaveBeenCalledTimes(1)
3641
expect(historySpy).toHaveBeenCalledWith({
@@ -41,18 +46,23 @@ describe('LocationHistoryCommand', () => {
4146
})
4247

4348
it('queries history and write event table directly', async () => {
44-
const buildOutputFormatterMock = jest.mocked(buildOutputFormatter)
49+
const outputFormatterMock = jest.fn().mockReturnValueOnce('formatted output')
50+
const buildOutputFormatterMock = jest.mocked(buildOutputFormatter<DeviceActivity[]>)
4551
const writeOutputMock = jest.mocked(writeOutput)
4652

47-
calculateHistoryRequestLimitMock.mockReturnValueOnce(20)
53+
const items = [{ deviceId: 'device-1' }] as DeviceActivity[]
54+
55+
calculateRequestLimitMock.mockReturnValueOnce(20)
4856
calculateOutputFormatMock.mockReturnValueOnce(IOFormat.JSON)
49-
buildOutputFormatterMock.mockReturnValueOnce(jsonFormatter(4))
57+
buildOutputFormatterMock.mockReturnValueOnce(outputFormatterMock)
58+
getHistoryMock.mockResolvedValueOnce(items)
5059

5160
await expect(LocationHistoryCommand.run(['locationId'])).resolves.not.toThrow()
5261

53-
expect(calculateHistoryRequestLimitMock).toHaveBeenCalledTimes(1)
54-
expect(calculateHistoryRequestLimitMock).toHaveBeenCalledWith(20)
55-
expect(mockChooseLocation).toHaveBeenCalledTimes(1)
62+
expect(calculateRequestLimitMock).toHaveBeenCalledTimes(1)
63+
expect(calculateRequestLimitMock).toHaveBeenCalledWith(20)
64+
expect(chooseLocationMock).toHaveBeenCalledTimes(1)
65+
expect(chooseLocationMock).toHaveBeenCalledWith(expect.any(LocationDeviceHistoryCommand), 'locationId', true, true)
5666
expect(calculateOutputFormatMock).toHaveBeenCalledTimes(1)
5767
expect(getHistoryMock).toHaveBeenCalledTimes(1)
5868
expect(getHistoryMock).toHaveBeenCalledWith(
@@ -63,9 +73,11 @@ describe('LocationHistoryCommand', () => {
6373
locationId: 'locationId',
6474
}),
6575
)
66-
expect(writeDeviceEventsTableMock).toHaveBeenCalledTimes(0)
6776
expect(buildOutputFormatterMock).toHaveBeenCalledTimes(1)
77+
expect(outputFormatterMock).toHaveBeenCalledTimes(1)
78+
expect(outputFormatterMock).toHaveBeenCalledWith(items)
6879
expect(writeOutputMock).toHaveBeenCalledTimes(1)
80+
expect(writeOutputMock).toHaveBeenCalledWith('formatted output', undefined)
6981

7082
expect(historySpy).toHaveBeenCalledTimes(0)
7183
expect(writeDeviceEventsTableMock).toHaveBeenCalledTimes(0)

packages/cli/src/commands/devices/history.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { DeviceActivity } from '@smartthings/core-sdk'
2+
13
import {
24
APICommand,
35
buildOutputFormatter,
@@ -53,8 +55,8 @@ export default class DeviceHistoryCommand extends APICommand<typeof DeviceHistor
5355
const history = await this.client.history.devices(params)
5456
await writeDeviceEventsTable(this, history, { utcTimeFormat: this.flags.utc })
5557
} else {
56-
const items = getHistory(this.client, limit, perRequestLimit, params)
57-
const outputFormatter = buildOutputFormatter(this)
58+
const items = await getHistory(this.client, limit, perRequestLimit, params)
59+
const outputFormatter = buildOutputFormatter<DeviceActivity[]>(this)
5860
await writeOutput(outputFormatter(items), this.flags.output)
5961
}
6062
}

packages/cli/src/commands/locations/history.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { DeviceActivity } from '@smartthings/core-sdk'
2+
13
import {
24
APICommand,
35
buildOutputFormatter,
@@ -51,8 +53,8 @@ export default class LocationDeviceHistoryCommand extends APICommand<typeof Loca
5153
const history = await this.client.history.devices(params)
5254
await writeDeviceEventsTable(this, history, { includeName: true, utcTimeFormat: this.flags.utc })
5355
} else {
54-
const items = getHistory(this.client, limit, perRequestLimit, params)
55-
const outputFormatter = buildOutputFormatter(this)
56+
const items = await getHistory(this.client, limit, perRequestLimit, params)
57+
const outputFormatter = buildOutputFormatter<DeviceActivity[]>(this)
5658
await writeOutput(outputFormatter(items), this.flags.output)
5759
}
5860
}

0 commit comments

Comments
 (0)