Skip to content

Commit

Permalink
Increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 committed Nov 15, 2023
1 parent 329a813 commit 6bd699f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
9 changes: 0 additions & 9 deletions src/ConfigCatLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,6 @@ export class LoggerWrapper implements IConfigCatLogger {
);
}

circularDependencyDetected(condition: string, key: string, dependencyCycle: string): LogMessage {
return this.log(
LogLevel.Warn, 3005,
FormattableLogMessage.from(
"CONDITION", "KEY", "DEPENDENCY_CYCLE"
)`Cannot evaluate condition (${condition}) for setting '${key}' (circular dependency detected between the following depending flags: ${dependencyCycle}). Please check your feature flag definition and eliminate the circular dependency.`
);
}

configServiceCannotInitiateHttpCalls(): LogMessage {
return this.log(
LogLevel.Warn, 3200,
Expand Down
18 changes: 14 additions & 4 deletions test/ConfigCatClientTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { EvaluateContext, IEvaluateResult, IEvaluationDetails, IRolloutEvaluator
import { User } from "../src/User";
import { delay } from "../src/Utils";
import "./helpers/ConfigCatClientCacheExtensions";
import { FakeCache, FakeConfigCatKernel, FakeConfigFetcher, FakeConfigFetcherBase, FakeConfigFetcherWithAlwaysVariableEtag, FakeConfigFetcherWithNullNewConfig, FakeConfigFetcherWithPercentageRules, FakeConfigFetcherWithRules, FakeConfigFetcherWithTwoCaseSensitiveKeys, FakeConfigFetcherWithTwoKeys, FakeConfigFetcherWithTwoKeysAndRules, FakeExternalAsyncCache, FakeExternalCache, FakeExternalCacheWithInitialData, FakeLogger } from "./helpers/fakes";
import { FakeCache, FakeConfigCatKernel, FakeConfigFetcher, FakeConfigFetcherBase, FakeConfigFetcherWithAlwaysVariableEtag, FakeConfigFetcherWithNullNewConfig, FakeConfigFetcherWithPercentageOptions, FakeConfigFetcherWithRules, FakeConfigFetcherWithTwoCaseSensitiveKeys, FakeConfigFetcherWithTwoKeys, FakeConfigFetcherWithTwoKeysAndRules, FakeExternalAsyncCache, FakeExternalCache, FakeExternalCacheWithInitialData, FakeLogger } from "./helpers/fakes";
import { allowEventLoop } from "./helpers/utils";

describe("ConfigCatClient", () => {
Expand Down Expand Up @@ -311,7 +311,7 @@ describe("ConfigCatClient", () => {
const defaultValue = "N/A";
const timestamp = new Date().getTime();

const configFetcherClass = FakeConfigFetcherWithPercentageRules;
const configFetcherClass = FakeConfigFetcherWithPercentageOptions;
const cachedPc = new ProjectConfig(configFetcherClass.configJson, new Config(JSON.parse(configFetcherClass.configJson)), timestamp, "etag");
const configCache = new FakeCache(cachedPc);
const configCatKernel: FakeConfigCatKernel = { configFetcher: new configFetcherClass(), sdkType: "common", sdkVersion: "1.0.0" };
Expand Down Expand Up @@ -620,7 +620,12 @@ describe("ConfigCatClient", () => {
assert.isAtMost(ellapsedMilliseconds, (maxInitWaitTimeSeconds * 1000) + 50); // 50 ms for tolerance

assert.equal(state, ClientReadyState.NoFlagData);
assert.equal(client.snapshot().getValue("debug", false), false);

const snapshot = client.snapshot();
assert.equal(snapshot.getValue("debug", false), false);
const evaluationDetails = snapshot.getValueDetails("debug", false);
assert.isTrue(evaluationDetails.isDefaultValue);
assert.equal(evaluationDetails.value, false);
});

it("AutoPoll - should wait for maxInitWaitTimeSeconds and return cached", async () => {
Expand All @@ -640,7 +645,12 @@ describe("ConfigCatClient", () => {
assert.isAtMost(ellapsedMilliseconds, (maxInitWaitTimeSeconds * 1000) + 50); // 50 ms for tolerance

assert.equal(state, ClientReadyState.HasCachedFlagDataOnly);
assert.equal(client.snapshot().getValue("debug", false), true);

const snapshot = client.snapshot();
assert.equal(snapshot.getValue("debug", false), true);
const evaluationDetails = snapshot.getValueDetails("debug", false);
assert.isFalse(evaluationDetails.isDefaultValue);
assert.equal(evaluationDetails.value, true);
});

it("LazyLoad - return cached", async () => {
Expand Down
19 changes: 18 additions & 1 deletion test/UtilsTests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from "chai";
import "mocha";
import { parseFloatStrict, utf8Encode } from "../src/Utils";
import { formatStringList, parseFloatStrict, utf8Encode } from "../src/Utils";

describe("Utils", () => {

Expand Down Expand Up @@ -56,4 +56,21 @@ describe("Utils", () => {
});
}

for (const [items, maxLength, addOmittedItemsText, separator, expectedOutput] of <[string[], number, boolean, string | undefined, string][]>[
[[], 0, false, void 0, ""],
[[], 1, true, void 0, ""],
[["a"], 0, false, void 0, "'a'"],
[["a"], 1, true, void 0, "'a'"],
[["a"], 1, true, " -> ", "'a'"],
[["a", "b", "c"], 0, false, void 0, "'a', 'b', 'c'"],
[["a", "b", "c"], 3, false, void 0, "'a', 'b', 'c'"],
[["a", "b", "c"], 2, false, void 0, "'a', 'b'"],
[["a", "b", "c"], 2, true, void 0, "'a', 'b', ...1 item(s) omitted"],
[["a", "b", "c"], 0, true, " -> ", "'a' -> 'b' -> 'c'"],
]) {
it(`formatStringList - items: ${items} | maxLength: ${maxLength} | addOmittedItemsText: ${addOmittedItemsText}`, () => {
const actualOutput = formatStringList(items, maxLength, addOmittedItemsText ? count => `, ...${count} item(s) omitted` : void 0, separator);
assert.strictEqual(actualOutput, expectedOutput);
});
}
});
18 changes: 16 additions & 2 deletions test/VariationIdTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert } from "chai";
import "mocha";
import { ConfigCatClient, IConfigCatClient } from "../src/ConfigCatClient";
import { AutoPollOptions } from "../src/ConfigCatClientOptions";
import { FakeConfigCatKernel, FakeConfigFetcherWithNullNewConfig, FakeConfigFetcherWithTwoKeysAndRules } from "./helpers/fakes";
import { FakeConfigCatKernel, FakeConfigFetcherWithNullNewConfig, FakeConfigFetcherWithPercentageOptionsWithinTargetingRule, FakeConfigFetcherWithTwoKeysAndRules } from "./helpers/fakes";

describe("ConfigCatClient", () => {
it("getKeyAndValueAsync() works with default", async () => {
Expand Down Expand Up @@ -33,7 +33,7 @@ describe("ConfigCatClient", () => {
assert.equal(result?.settingValue, "value");
});

it("getKeyAndValueAsync() works with percentage rules", async () => {
it("getKeyAndValueAsync() works with percentage options", async () => {
const configCatKernel: FakeConfigCatKernel = {
configFetcher: new FakeConfigFetcherWithTwoKeysAndRules(),
sdkType: "common",
Expand All @@ -47,6 +47,20 @@ describe("ConfigCatClient", () => {
assert.equal(result?.settingValue, "value2");
});

it("getKeyAndValueAsync() works with percentage options within targeting rule", async () => {
const configCatKernel: FakeConfigCatKernel = {
configFetcher: new FakeConfigFetcherWithPercentageOptionsWithinTargetingRule(),
sdkType: "common",
sdkVersion: "1.0.0"
};
const options: AutoPollOptions = new AutoPollOptions("APIKEY", "common", "1.0.0", { logger: null }, null);
const client: IConfigCatClient = new ConfigCatClient(options, configCatKernel);

const result = await client.getKeyAndValueAsync("622f5d07");
assert.equal(result?.settingKey, "debug");
assert.equal(result?.settingValue, "value2");
});

it("getKeyAndValueAsync() with null config", async () => {
const configCatKernel: FakeConfigCatKernel = {
configFetcher: new FakeConfigFetcherWithNullNewConfig(),
Expand Down
8 changes: 7 additions & 1 deletion test/helpers/fakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ export class FakeConfigFetcherWithTwoKeysAndRules extends FakeConfigFetcher {
}
}

export class FakeConfigFetcherWithPercentageOptionsWithinTargetingRule extends FakeConfigFetcher {
static get configJson(): string {
return '{"f":{"debug":{"t":1,"r":[{"c":[{"u":{"a":"a","c":1,"l":["abcd"]}}],"s":{"v":{"s":"value"},"i":"6ada5ff2"}},{"c":[{"u":{"a":"a","c":0,"l":["abcd"]}}],"p":[{"p":50,"v":{"s":"value1"},"i":"d227b334"},{"p":50,"v":{"s":"value2"},"i":"622f5d07"}]}],"v":{"s":"def"},"i":"abcdefgh"}}}';
}
}

export class FakeConfigFetcherWithRules extends FakeConfigFetcher {
static get configJson(): string {
return '{"f":{"debug":{"t":1,"r":[{"c":[{"u":{"a":"eyeColor","c":0,"l":["red"]}}],"s":{"v":{"s":"redValue"},"i":"redVariationId"}},{"c":[{"u":{"a":"eyeColor","c":0,"l":["blue"]}}],"s":{"v":{"s":"blueValue"},"i":"blueVariationId"}}],"v":{"s":"defaultValue"},"i":"defaultVariationId"}}}';
Expand All @@ -184,7 +190,7 @@ export class FakeConfigFetcherWithAlwaysVariableEtag extends FakeConfigFetcher {
}
}

export class FakeConfigFetcherWithPercentageRules extends FakeConfigFetcher {
export class FakeConfigFetcherWithPercentageOptions extends FakeConfigFetcher {
static get configJson(): string {
return '{"f":{"string25Cat25Dog25Falcon25Horse":{"t":1,"p":[{"p":25,"v":{"s":"Cat"},"i":"CatVariationId"},{"p":25,"v":{"s":"Dog"},"i":"DogVariationId"},{"p":25,"v":{"s":"Falcon"},"i":"FalconVariationId"},{"p":25,"v":{"s":"Horse"},"i":"HorseVariationId"}],"v":{"s":"Chicken"},"i":"ChickenVariationId"}}}';
}
Expand Down

0 comments on commit 6bd699f

Please sign in to comment.