@apistudio/apim-cli
Version:
CLI for API Management Products
92 lines (72 loc) • 2.63 kB
text/typescript
import {
validateYamlFiles,
createAssetReferenceMap,
convertNumberToString,
isValidAsset,
addErrorToResponse,
constructErrorResponse
,
} from '../src/index.js';
import {YamlContent} from '../src/model/interface.js';
import yaml from 'js-yaml';
import path from 'path';
import fs from 'fs';
jest.mock('@apic/studio-logger', ()=> ({
LoggerConfig: {
isLoggerEnabled: jest.fn(),
},
}));
jest.mock('@apic/studio-shared', () => ({
ErrorResponse: jest.fn(),
Metadata_Ref: jest.fn(),
SpecObject: jest.fn(),
YamlContent: jest.fn(),
UpperCaseKinds: jest.fn(),
}));
jest.mock('../src/service/log-wrapper.ts');
describe('validateYamlFiles', () => {
it('should validate YAML files successfully', async() => {
const zipFilePath = path.resolve(__dirname, './assets/gateway-asset.zip');
const zipBuffer = fs.readFileSync(zipFilePath);
const result = await validateYamlFiles(zipBuffer);
expect(result).toBe(true);
});
it('should log error for invalid YAML files', async() => {
const zipFilePath = path.resolve(__dirname, './assets/invalid-yaml-asset.zip');
const zipBuffer = fs.readFileSync(zipFilePath);
const result = await validateYamlFiles(zipBuffer);
expect(result).toBe(false);
});
it('should create asset reference map successfully',
async () => {
const zipFilePath = path.resolve(__dirname, './assets/gateway-asset.zip');
const Buffer = fs.readFileSync(zipFilePath);
const result = await createAssetReferenceMap(Buffer);
expect(result).not.toBe(undefined);
expect(result).not.toBe(false);
});
it('should log error for invalid YAML parsing', async() => {
const zipFilePath = path.resolve(__dirname, './assets/invalid-yaml-asset.zip');
const zipBuffer = fs.readFileSync(zipFilePath);
await createAssetReferenceMap(zipBuffer);
});
it('should convert number to string correctly', () => {
expect(convertNumberToString(1)).toBe('1.0');
expect(convertNumberToString(1.5)).toBe('1.5');
expect(convertNumberToString('1.5')).toBe('1.5');
});
it('should return true for valid YAML content', () => {
const filePath = path.resolve(__dirname, './assets/validate/api.yaml');
const buffer = fs.readFileSync(filePath);
const yamlContent= yaml.load(buffer.toString()) as YamlContent;
expect(isValidAsset(yamlContent)).toBe(true);
});
});
describe('checking error response', () => {
it('should construct an error response object', () => {
addErrorToResponse('ERR001', 'field1', 'Error description 1');
addErrorToResponse('ERR002', 'field2', 'Error description 2');
const response = constructErrorResponse();
expect(response).not.toEqual([]);
});
});