hardlounge/test/tests/mergeConfig.ts

287 lines
4.7 KiB
TypeScript
Raw Permalink Normal View History

import {expect} from "chai";
import sinon from "ts-sinon";
import log from "../../server/log";
import Config from "../../server/config";
import TestUtil from "../util";
describe("mergeConfig", function () {
it("should mutate object", function () {
const config = {
ip: "default",
} as any;
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(config, {
2019-07-17 09:33:59 +00:00
ip: "overridden",
} as any)
2019-07-17 09:33:59 +00:00
).to.deep.equal({
ip: "overridden",
});
expect(config).to.deep.equal({
ip: "overridden",
});
});
it("should merge new properties", function () {
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
ip: "default",
newProp: "this should appear too",
} as any,
2019-07-17 09:33:59 +00:00
{
ip: "overridden",
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
ip: "overridden",
newProp: "this should appear too",
});
});
it("should extend objects", function () {
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
tlsOptions: {},
} as any,
2019-07-17 09:33:59 +00:00
{
tlsOptions: {
user: "test",
thing: 123,
},
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
tlsOptions: {
user: "test",
thing: 123,
},
});
});
it("should warn for unknown top level keys", function () {
let warning = "";
const warnStub = sinon
.stub(log, "warn")
.callsFake(TestUtil.sanitizeLog((str) => (warning += str)));
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
optionOne: 123,
} as any,
2019-07-17 09:33:59 +00:00
{
optionOne: 456,
optionTwo: 789,
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
optionOne: 456,
optionTwo: 789,
});
warnStub.restore();
expect(warning).to.equal('Unknown key "optionTwo", please verify your config.\n');
});
it("should not warn for unknown second level keys", function () {
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
optionOne: {
subOne: 123,
},
} as any,
2019-07-17 09:33:59 +00:00
{
optionOne: {
subOne: 123,
subTwo: 123,
},
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
optionOne: {
subOne: 123,
subTwo: 123,
},
});
});
it("should allow changing nulls", function () {
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
oidentd: null,
} as any,
2019-07-17 09:33:59 +00:00
{
oidentd: "some path",
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
oidentd: "some path",
});
});
it("should allow changing nulls with objects", function () {
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
webirc: null,
} as any,
2019-07-17 09:33:59 +00:00
{
webirc: {
serverone: "password",
servertwo: "password2",
},
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
webirc: {
serverone: "password",
servertwo: "password2",
},
});
});
it("should allow changing nulls with objects that has function", function () {
const callbackFunction = () => ({});
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
webirc: null,
} as any,
2019-07-17 09:33:59 +00:00
{
webirc: {
servercb: callbackFunction,
},
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
webirc: {
servercb: callbackFunction,
},
});
});
it("should keep new properties inside of objects", function () {
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
nestedOnce: {
ip: "default",
},
nestedTwice: {
thing: "default",
nested: {
otherThing: "also default",
newThing: "but also this",
},
},
} as any,
2019-07-17 09:33:59 +00:00
{
nestedOnce: {},
nestedTwice: {
nested: {
otherThing: "overridden",
},
},
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
nestedOnce: {
ip: "default",
},
nestedTwice: {
thing: "default",
nested: {
otherThing: "overridden",
newThing: "but also this",
},
},
});
});
it("should not merge arrays", function () {
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
test: ["sqlite", "text"],
} as any,
2019-07-17 09:33:59 +00:00
{
test: ["sqlite"],
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
test: ["sqlite"],
});
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
test: ["sqlite", "text"],
} as any,
2019-07-17 09:33:59 +00:00
{
test: [],
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
test: [],
});
});
it("should change order in arrays", function () {
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
test: ["sqlite", "text"],
} as any,
2019-07-17 09:33:59 +00:00
{
test: ["text", "sqlite"],
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
test: ["text", "sqlite"],
});
});
it("should only merge same type", function () {
const logWarnStub = sinon.stub(log, "warn");
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
shouldBeObject: {
thing: "yes",
},
} as any,
2019-07-17 09:33:59 +00:00
{
shouldBeObject: "bad type",
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
shouldBeObject: {
thing: "yes",
},
});
2019-07-17 09:33:59 +00:00
expect(
Config._merge_config_objects(
2019-07-17 09:33:59 +00:00
{
shouldBeString: "string",
} as any,
2019-07-17 09:33:59 +00:00
{
shouldBeString: 1234567,
} as any
2019-07-17 09:33:59 +00:00
)
).to.deep.equal({
shouldBeString: "string",
});
logWarnStub.restore();
});
});