Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(cli): test support #1747

Merged
merged 3 commits into from
Sep 3, 2024
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
29 changes: 29 additions & 0 deletions .github/workflows/units_test_cli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: units test cli

on:
pull_request:
paths:
- 'cli/**'
release:
types:
- published

jobs:
cli_test:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: '18.17'

- name: Install dependencies and run tests
run: |
cd cli
yarn
yarn test:coverage

# - name: Upload coverage reports to Codecov
# uses: codecov/codecov-action@v3
# env:
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
3 changes: 2 additions & 1 deletion cli/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ yarn-error.log*
*.tgz
package
release
dist
dist
coverage
6 changes: 6 additions & 0 deletions cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
coverageReporters: ['text', 'lcov'],
collectCoverageFrom: ['src/**/*.ts', '!src/**/*.test.ts', '!src/__tests__/**/*.ts'],
}
7 changes: 6 additions & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
],
"scripts": {
"dev": "tsc -w",
"build": "tsc"
"build": "tsc",
"test": "jest",
"test:coverage": "jest --coverage"
},
"main": "./dist/src/index.js",
"bin": {
Expand Down Expand Up @@ -47,6 +49,7 @@
"@types/concat-stream": "^2.0.0",
"@types/debug": "^4.1.12",
"@types/ini": "^4.1.0",
"@types/jest": "^29.5.12",
"@types/js-yaml": "^4.0.5",
"@types/json-bigint": "^1.0.4",
"@types/lodash": "^4.17.1",
Expand All @@ -56,6 +59,8 @@
"@types/signale": "^1.4.4",
"@types/split2": "^3.2.1",
"@types/ws": "^8.5.3",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"typescript": "^4.7.3"
},
"pkg": {
Expand Down
28 changes: 28 additions & 0 deletions cli/src/__tests__/utils/binaryFormats.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import isSupportedBinaryFormatForMQTT, { supportedBinaryFormatsForMQTT } from '../../utils/binaryFormats'

describe('isSupportedBinaryFormatForMQTT', () => {
it('should return true for supported binary formats', () => {
const supportedFormats = ['.png', '.mp4', '.mp3', '.zip', '.exe', '.pdf']
supportedFormats.forEach((format) => {
expect(isSupportedBinaryFormatForMQTT(format)).toBe(true)
})
})

it('should return false for unsupported binary formats', () => {
const unsupportedFormats = ['.txt', '.doc', '.xls', '.ppt', '.html', '.css']
unsupportedFormats.forEach((format) => {
expect(isSupportedBinaryFormatForMQTT(format)).toBe(false)
})
})

it('should be case-sensitive', () => {
expect(isSupportedBinaryFormatForMQTT('.PNG')).toBe(false)
expect(isSupportedBinaryFormatForMQTT('.png')).toBe(true)
})

it('should include all supported formats', () => {
supportedBinaryFormatsForMQTT.forEach((format) => {
expect(isSupportedBinaryFormatForMQTT(format)).toBe(true)
})
})
})
16 changes: 16 additions & 0 deletions cli/src/__tests__/utils/delay.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import delay from '../../utils/delay'

describe('delay function', () => {
it('should delay execution for the specified time', async () => {
const startTime = Date.now()
const delayTime = 1000 // 1 second

await delay(delayTime)

const endTime = Date.now()
const elapsedTime = endTime - startTime

expect(elapsedTime).toBeGreaterThanOrEqual(delayTime)
expect(elapsedTime).toBeLessThan(delayTime + 50)
})
})
42 changes: 42 additions & 0 deletions cli/src/__tests__/utils/jsonUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { jsonParse, jsonStringify } from '../../utils/jsonUtils'

describe('jsonUtils', () => {
describe('jsonParse', () => {
it('should parse JSON with large integers correctly', () => {
const json = '{"bigInt": 9007199254740991}'
const result = jsonParse(json)
expect(result.bigInt.toString()).toBe('9007199254740991')
})

it('should parse JSON with floating-point numbers correctly', () => {
const json = '{"float": 123.456}'
const result = jsonParse(json)
expect(result.float.toString()).toBe('123.456')
})

it('should throw an error for invalid JSON', () => {
const invalidJson = '{invalid: json}'
expect(() => jsonParse(invalidJson)).toThrow()
})
})

describe('jsonStringify', () => {
it('should stringify objects with large integers correctly', () => {
const obj = { bigInt: Number.MAX_SAFE_INTEGER }
const result = jsonStringify(obj)
expect(result).toBe('{"bigInt":9007199254740991}')
})

it('should stringify objects with floating-point numbers correctly', () => {
const obj = { float: 123.456 }
const result = jsonStringify(obj)
expect(result).toBe('{"float":123.456}')
})

it('should stringify mixed objects correctly', () => {
const obj = { bigInt: Number.MAX_SAFE_INTEGER, float: 123.456 }
const result = jsonStringify(obj)
expect(result).toBe('{"bigInt":9007199254740991,"float":123.456}')
})
})
})
2 changes: 1 addition & 1 deletion cli/src/utils/binaryFormats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const supportedBinaryFormatsForMQTT = [
export const supportedBinaryFormatsForMQTT = [
'.png',
'.jpg',
'.jpeg',
Expand Down
5 changes: 3 additions & 2 deletions cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"types": ["@types/signale"]
"types": ["@types/signale", "jest"],
},
"include": ["src"]
"include": ["src"],
"exclude": ["src/**/*.test.ts", "src/__tests__"]
}
Loading
Loading