From 51b0bfd5b55b5811a3ce85d59dbc71f3ba95603d Mon Sep 17 00:00:00 2001 From: Red-Asuka Date: Fri, 15 Nov 2024 17:10:42 +0800 Subject: [PATCH] feat(cli): add tests for saving and loading options with custom YAML path --- cli/src/__tests__/utils/options.test.ts | 66 +++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/cli/src/__tests__/utils/options.test.ts b/cli/src/__tests__/utils/options.test.ts index f980c64c9..0bc24c899 100644 --- a/cli/src/__tests__/utils/options.test.ts +++ b/cli/src/__tests__/utils/options.test.ts @@ -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', () => { @@ -19,6 +20,9 @@ describe('options', () => { if (existsSync(testFilePath)) { unlinkSync(testFilePath) } + if (existsSync(testYamlFilePath)) { + unlinkSync(testYamlFilePath) + } if (existsSync(defaultPath)) { unlinkSync(defaultPath) } @@ -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) + + 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, @@ -171,6 +214,29 @@ describe('options', () => { expect(loadedOptions).toMatchObject(expectedOptions as unknown as Record) }) + 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) + }) + it('should save and load options for command type "simulate" with default path', () => { const options: SimulatePubOptions = { mqttVersion: 5,