-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit test and validation message improvements
- Loading branch information
1 parent
82c131c
commit abcedbd
Showing
7 changed files
with
242 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { buildTS } from './build-ts'; | ||
|
||
const cleanCode = (code: string): string => { | ||
return code.replace(/\s+/g, ''); | ||
}; | ||
|
||
describe('buildTS function', () => { | ||
it('should transform TypeScript code correctly', () => { | ||
const inputCode = ` | ||
function add(a: number, b: number): number { | ||
return a + b; | ||
} | ||
console.log(add(1, 2)); | ||
`; | ||
|
||
const expectedCode = ` | ||
function add(a, b) { | ||
return a + b; | ||
} | ||
console.log(add(1, 2)); | ||
`; | ||
|
||
const transformedCode = buildTS(inputCode); | ||
|
||
expect(cleanCode(transformedCode)).toBe(cleanCode(expectedCode)); | ||
}); | ||
|
||
it('should throw an error for invalid TypeScript code', () => { | ||
const invalidCode = ` | ||
function add(a: number, b: number) { | ||
return new.Date(); // wrong code | ||
} | ||
console.log(add(1, 2)); | ||
`; | ||
|
||
expect(() => buildTS(invalidCode)).toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
import { resolvePayload } from './resolve-payload'; // Adjust the path as needed | ||
|
||
// Mock the module dependencies | ||
vi.mock('./read-file', () => ({ | ||
readFileFromPath: () => { | ||
return Buffer.from("Hello JS"); | ||
}, | ||
})); | ||
|
||
vi.mock('./build-ts', () => ({ | ||
buildTS: () => { | ||
return `console.log("Hello, world TS!");`; | ||
}, | ||
})); | ||
|
||
describe('resolvePayload function', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should correctly resolve payload for a TypeScript file', async () => { | ||
const path = '/some/path'; | ||
const filename = 'example.ts'; | ||
|
||
const resultBuff = resolvePayload(path, filename); | ||
|
||
const result = resultBuff.toString(); | ||
expect(result).toBe('console.log("Hello, world TS!");'); | ||
}); | ||
|
||
it('should correctly resolve payload for a non-TypeScript file', async () => { | ||
const path = '/some/path'; | ||
const filename = 'example.js'; | ||
|
||
const resultBuff = resolvePayload(path, filename); | ||
|
||
const result = resultBuff.toString(); | ||
expect(result).toBe('Hello JS'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { zConnector, TypeConnector } from './connector'; // Adjust the path as needed | ||
|
||
describe('zConnector schema', () => { | ||
it('should validate a correct connector object', () => { | ||
const validData = { | ||
id: '123456789012345678901234', | ||
name: 'Test Connector', | ||
version: '1.0.0', | ||
networks: ['network1', 'network2'], | ||
description: 'A test connector description', | ||
install_text: 'Installation instructions', | ||
install_end_text: 'End of installation instructions', | ||
device_annotation: 'Device annotation text', | ||
device_parameters: [{}], | ||
logo: Buffer.from('logo data'), | ||
payload_decoder: Buffer.from('decoder data'), | ||
}; | ||
|
||
const result = zConnector.safeParse(validData); | ||
|
||
expect(result.success).toBe(true); | ||
}); | ||
|
||
it('should fail validation for a connector object with invalid data', () => { | ||
const invalidData = { | ||
id: '123', // too short | ||
name: 'Test Connector', | ||
version: '1.0.0', | ||
networks: [], // empty array | ||
description: 'A very long description that exceeds the maximum allowed length of five hundred characters' + | ||
'A very long description that exceeds the maximum allowed length of five hundred characters' + | ||
'A very long description that exceeds the maximum allowed length of five hundred characters' + | ||
'A very long description that exceeds the maximum allowed length of five hundred characters', | ||
install_text: 'Installation instructions', | ||
install_end_text: 'End of installation instructions', | ||
device_annotation: 'Device annotation text', | ||
device_parameters: { /* add invalid data according to zIntegrationParameters */ }, | ||
logo: 123, // not a buffer or string | ||
payload_decoder: 'too long data that exceeds the 64-byte limit', // too long | ||
}; | ||
|
||
const result = zConnector.safeParse(invalidData); | ||
expect(result.success).toBe(false); | ||
if (!result.success) { | ||
expect(result.error.issues[0].path[0]).toBe('networks'); | ||
expect(result.error.issues[0].message).toBe('Array must contain at least 1 element(s)'); | ||
|
||
expect(result.error.issues[1].path[0]).toBe('device_parameters'); | ||
expect(result.error.issues[1].message).toBe('Expected array, received object'); | ||
|
||
expect(result.error.issues[2].path[0]).toBe('logo'); | ||
expect(result.error.issues[2].message).toBe('Invalid input'); | ||
|
||
expect(result.error.issues[3].path[0]).toBe('payload_decoder'); | ||
expect(result.error.issues[3].message).toBe('Invalid Payload Parser'); | ||
} | ||
}); | ||
|
||
it('should transform networks to JSON string', () => { | ||
const data = { | ||
id: '123456789012345678901234', | ||
name: 'Test Connector', | ||
version: '1.0.0', | ||
networks: ['network1', 'network2'], | ||
}; | ||
|
||
const result = zConnector.parse(data); | ||
expect(result.networks).toBe(JSON.stringify(['network1', 'network2'])); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { zNetwork, TypeNetwork } from './network'; // Adjust the path as needed | ||
|
||
describe('zNetwork schema', () => { | ||
it('should validate a correct network object', () => { | ||
const validData = { | ||
id: '123456789012345678901234', | ||
name: 'Test Network', | ||
version: '1.0.0', | ||
description: 'A test network description', | ||
documentation_url: 'http://example.com', | ||
middleware_endpoint: 'middleware.example.com', | ||
serial_number: { | ||
case: 'upper', | ||
image: Buffer.from('image data'), | ||
label: 'Test Label', | ||
mask: 'Test Mask', | ||
required: true, | ||
}, | ||
device_parameters: null, | ||
icon: Buffer.from('icon data'), | ||
logo: 'logo data', | ||
banner: Buffer.from('banner data'), | ||
payload_decoder: Buffer.from('decoder data'), | ||
}; | ||
|
||
const result = zNetwork.safeParse(validData); | ||
|
||
expect(result.success).toBe(true); | ||
}); | ||
|
||
it('should fail validation for a network object with invalid data', () => { | ||
const invalidData = { | ||
id: '123', // too short | ||
name: 'Test Network', | ||
version: '1.0.0', | ||
description: 'A test network description', | ||
documentation_url: 'invalid-url', // not a valid URL | ||
middleware_endpoint: 'invalid-url', // not a valid URL | ||
serial_number: { | ||
case: 'invalid-case', // not a valid enum value | ||
image: 'not a buffer or string', | ||
label: 'A very long label that exceeds the maximum allowed length of one hundred characters' + | ||
'A very long label that exceeds the maximum allowed length of one hundred characters', | ||
mask: 'A very long mask that exceeds the maximum allowed length of one hundred characters' + | ||
'A very long mask that exceeds the maximum allowed length of one hundred characters', | ||
required: 'not a boolean', // should be boolean | ||
}, | ||
device_parameters: { /* add invalid data according to zIntegrationParameters */ }, | ||
icon: 123, // not a buffer or string | ||
logo: 123, // not a buffer or string | ||
banner: 123, // not a buffer or string | ||
payload_decoder: 'too long data that exceeds the 64-byte limit', // too long | ||
}; | ||
|
||
const result = zNetwork.safeParse(invalidData); | ||
|
||
expect(result.success).toBe(false); | ||
expect(result.error?.issues[0].path[0]).toBe('middleware_endpoint'); | ||
expect(result.error?.issues[0].message).toBe('Invalid domain'); | ||
expect(result.error?.issues[1].path[0]).toBe('serial_number'); | ||
expect(result.error?.issues[1].message).toBe("Invalid enum value. Expected 'lower' | 'upper' | '', received 'invalid-case'"); | ||
expect(result.error?.issues[2].path[0]).toBe('serial_number'); | ||
expect(result.error?.issues[2].message).toBe('String must contain at most 100 character(s)'); | ||
|
||
}); | ||
|
||
it('should transform serial_number to JSON string', () => { | ||
const data = { | ||
id: '123456789012345678901234', | ||
name: 'Test Network', | ||
version: '1.0.0', | ||
serial_number: { | ||
case: 'lower', | ||
label: 'Test Label', | ||
required: true, | ||
}, | ||
}; | ||
|
||
const result = zNetwork.parse(data); | ||
expect(result.serial_number).toBe(JSON.stringify({ | ||
case: 'lower', | ||
label: 'Test Label', | ||
required: true, | ||
})); | ||
}); | ||
}); |