Files
configucci/src/test/ConfigReader.test.ts
sokol e4b44c7b5e
Some checks failed
CI / build-and-test (push) Failing after 24m31s
refactor: improve type safety and code style
2026-02-20 11:22:56 +03:00

76 lines
2.1 KiB
TypeScript

import{test} from 'vitest'
import { ConfigReader } from '../models/ConfigReader';
const cfgTemplate = `
<engine>
<environment name="DEFAULT">
<parameter name = "host" value = "http://host1.xxx/api"/>
<parameter name="MessageBrokerHosts" value="[ &quot;smsk02ap432u:9096&quot; ]" />
</environment>
<environment name="env1">
<parameter name="port" value="60001"/>
</environment>
<environment name="env2">
<parameter name="port" value="60002"/>
<parameter name="MessageBrokerHosts" value="[&quot;smsk02ap430u:9096&quot; , &quot;smsk02ap433u:9096&quot; ]" />
</environment>
<template>
{
"Host": "@host@",
"Port": @port@,
"ApiPath":"@host@:@port@/v1/data",
"MessageBroker":{
"hosts":@MessageBrokerHosts@
},
"basePath":"./@env_name@/in",
"NoParam:"@no_param@"
}
</template>
</engine>
`
test("read from a file", async ({expect})=>{
const sut = new ConfigReader();
const file = new File([cfgTemplate],'cfg.json.xml',{type:'application/xml'});
// define a missing jsdom text() function,
// that presents in the real DOM
Object.defineProperty(file, 'text', {
value: () => Promise.resolve(cfgTemplate),
writable: true
});
const cfg = await sut.parseFromFile(file);
expect(cfg).not.toBeUndefined();
});
test("load environments and params", ({expect})=>{
const sut = new ConfigReader();
const cfg = sut.parseFromString(cfgTemplate);
expect(cfg?.envs).toHaveLength(3);
expect(cfg?.envs.map(x=>x.name))
.toEqual(expect.arrayContaining(["DEFAULT", "env1", "env2"]));
expect(cfg?.envs.flatMap(x=>x.params))
.toHaveLength(5);
});
test("load template", ({expect})=>{
const sut = new ConfigReader();
const cfg = sut.parseFromString(cfgTemplate);
expect(cfg?.template).toBeDefined();
expect(cfg?.template.content.length).toBeGreaterThan(20);
expect(cfg?.template.Params).toHaveLength(5)
expect(cfg?.getTemplateAsJson()).not.toBeUndefined();
expect(cfg?.validateParams()).does.has.length(1).and.contain("no_param");
});