Skip to content

Commit

Permalink
feat(cli): add tests for saving and loading options with custom YAML …
Browse files Browse the repository at this point in the history
…path
  • Loading branch information
Red-Asuka authored and ysfscream committed Nov 15, 2024
1 parent dd49696 commit 890a640
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions cli/src/__tests__/utils/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { join } from 'path'
import { expect, afterAll, describe, it, jest, beforeAll } from '@jest/globals'

const testFilePath = join(__dirname, 'test-options.json')
const testYamlFilePath = join(__dirname, 'test-options.yaml')
const defaultPath = join(process.cwd(), 'mqttx-cli-options.json')

describe('options', () => {
Expand All @@ -19,6 +20,9 @@ describe('options', () => {
if (existsSync(testFilePath)) {
unlinkSync(testFilePath)
}
if (existsSync(testYamlFilePath)) {
unlinkSync(testYamlFilePath)
}
if (existsSync(defaultPath)) {
unlinkSync(defaultPath)
}
Expand Down Expand Up @@ -81,6 +85,45 @@ describe('options', () => {
expect(overriddenOptions.message).toEqual('User message')
})

it('should save and load options for command type "pub" with custom yaml path', () => {
const options: PublishOptions = {
mqttVersion: 5,
hostname: 'localhost',
clientId: 'testClient',
clean: true,
keepalive: 60,
reconnectPeriod: 1000,
maximumReconnectTimes: 10,
topic: 'test/topic',
message: 'Hello, MQTT!',
qos: 1,
saveOptions: testYamlFilePath,
}

handleSaveOptions('pub', options)
const loadedOptions = handleLoadOptions('pub', testYamlFilePath, {} as PublishOptions)
const { saveOptions, loadOptions, ...expectedOptions } = options
expect(loadedOptions).toMatchObject(expectedOptions as unknown as Record<string, unknown>)

const userOptions: PublishOptions = {
mqttVersion: 5,
hostname: 'localhost',
clientId: 'testClient',
clean: true,
keepalive: 60,
reconnectPeriod: 1000,
maximumReconnectTimes: 10,
topic: 'user/topic',
message: 'User message',
qos: 1,
saveOptions: testYamlFilePath,
}

const overriddenOptions = handleLoadOptions('pub', testYamlFilePath, userOptions)
expect(overriddenOptions.topic).toEqual('user/topic')
expect(overriddenOptions.message).toEqual('User message')
})

it('should save and load options for command type "sub" with default path', () => {
const options: SubscribeOptions = {
mqttVersion: 5,
Expand Down Expand Up @@ -171,6 +214,29 @@ describe('options', () => {
expect(loadedOptions).toMatchObject(expectedOptions as unknown as Record<string, unknown>)
})

it('should save and load options for command type "benchSub" with custom yaml path', () => {
const options: BenchSubscribeOptions = {
mqttVersion: 5,
hostname: 'localhost',
clientId: 'testClient',
clean: true,
keepalive: 60,
reconnectPeriod: 1000,
maximumReconnectTimes: 10,
topic: ['test/topic'],
qos: [1],
count: 100,
interval: 1000,
saveOptions: testYamlFilePath,
verbose: true,
}

handleSaveOptions('benchSub', options)
const loadedOptions = handleLoadOptions('benchSub', testYamlFilePath, {} as BenchSubscribeOptions)
const { saveOptions, loadOptions, ...expectedOptions } = options
expect(loadedOptions).toMatchObject(expectedOptions as unknown as Record<string, unknown>)
})

it('should save and load options for command type "simulate" with default path', () => {
const options: SimulatePubOptions = {
mqttVersion: 5,
Expand Down

0 comments on commit 890a640

Please sign in to comment.