Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add tests for saving and loading options with custom YAML path #1812

Merged
merged 1 commit into from
Nov 15, 2024
Merged
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
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
Loading