-
Notifications
You must be signed in to change notification settings - Fork 10
/
EvaluationLogTests.ts
135 lines (110 loc) · 4.79 KB
/
EvaluationLogTests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { assert } from "chai";
import * as fs from "fs";
import * as path from "path";
import * as util from "util";
import "mocha";
import { User } from "../src";
import { LogLevel, LoggerWrapper } from "../src/ConfigCatLogger";
import { SettingValue } from "../src/ProjectConfig";
import { RolloutEvaluator, evaluate } from "../src/RolloutEvaluator";
import { WellKnownUserObjectAttribute } from "../src/User";
import { errorToString } from "../src/Utils";
import { CdnConfigLocation, ConfigLocation, LocalFileConfigLocation } from "./helpers/ConfigLocation";
import { FakeLogger } from "./helpers/fakes";
import { normalizeLineEndings } from "./helpers/utils";
const testDataBasePath = path.join("test", "data", "evaluationlog");
type TestSet = {
sdkKey: string;
baseUrl?: string;
jsonOverride?: string;
tests?: ReadonlyArray<TestCase>;
}
type TestCase = {
key: string;
defaultValue: SettingValue;
returnValue: NonNullable<SettingValue>;
expectedLog: string;
user?: Readonly<{ [key: string]: string }>;
}
describe("Evaluation log", () => {
describeTestSet("simple_value");
describeTestSet("1_targeting_rule");
describeTestSet("2_targeting_rules");
describeTestSet("options_based_on_user_id");
describeTestSet("options_based_on_custom_attr");
describeTestSet("options_after_targeting_rule");
describeTestSet("options_within_targeting_rule");
describeTestSet("and_rules");
describeTestSet("segment");
describeTestSet("prerequisite_flag");
describeTestSet("comparators");
describeTestSet("epoch_date_validation");
describeTestSet("number_validation");
describeTestSet("semver_validation");
describeTestSet("list_truncation");
});
function describeTestSet(testSetName: string) {
for (const [configLocation, testCase] of getTestCases(testSetName)) {
const userJson = JSON.stringify(testCase.user ?? null).replace(/"/g, "'");
it(`${testSetName} - ${configLocation} | ${testCase.key} | ${testCase.defaultValue} | ${userJson}`, () => runTest(testSetName, configLocation, testCase));
}
}
function* getTestCases(testSetName: string): Generator<[ConfigLocation, TestCase], void, undefined> {
const data = fs.readFileSync(path.join(testDataBasePath, testSetName + ".json"), "utf8");
const testSet: TestSet = JSON.parse(data);
const configLocation = testSet.sdkKey
? new CdnConfigLocation(testSet.sdkKey, testSet.baseUrl)
: new LocalFileConfigLocation(testDataBasePath, "_overrides", testSet.jsonOverride!);
for (const testCase of testSet.tests ?? []) {
yield [configLocation, testCase];
}
}
function createUser(userRaw?: Readonly<{ [key: string]: string }>): User | undefined {
if (!userRaw) {
return;
}
const identifierAttribute: WellKnownUserObjectAttribute = "Identifier";
const emailAttribute: WellKnownUserObjectAttribute = "Email";
const countryAttribute: WellKnownUserObjectAttribute = "Country";
const user = new User(userRaw[identifierAttribute]);
const email = userRaw[emailAttribute];
if (email) {
user.email = email;
}
const country = userRaw[countryAttribute];
if (country) {
user.country = country;
}
const wellKnownAttributes: string[] = [identifierAttribute, emailAttribute, countryAttribute];
for (const attributeName of Object.keys(userRaw)) {
if (wellKnownAttributes.indexOf(attributeName) < 0) {
user.custom[attributeName] = userRaw[attributeName];
}
}
return user;
}
function formatLogEvent(event: FakeLogger["events"][0]) {
const [level, eventId, message, exception] = event;
const levelString =
level === LogLevel.Debug ? "DEBUG" :
level === LogLevel.Info ? "INFO" :
level === LogLevel.Warn ? "WARNING" :
level === LogLevel.Error ? "ERROR" :
LogLevel[level].toUpperCase().padStart(5);
const exceptionString = exception !== void 0 ? "\n" + errorToString(exception, true) : "";
return `${levelString} [${eventId}] ${message}${exceptionString}`;
}
async function runTest(testSetName: string, configLocation: ConfigLocation, testCase: TestCase) {
const config = await configLocation.fetchConfigCachedAsync();
const fakeLogger = new FakeLogger();
const logger = new LoggerWrapper(fakeLogger);
const evaluator = new RolloutEvaluator(logger);
const user = createUser(testCase.user);
const evaluationDetails = evaluate(evaluator, config.settings, testCase.key, testCase.defaultValue, user, null, logger);
const actualReturnValue = evaluationDetails.value;
assert.strictEqual(actualReturnValue, testCase.returnValue);
const expectedLogFilePath = path.join(testDataBasePath, testSetName, testCase.expectedLog);
const expectedLogText = normalizeLineEndings(await util.promisify(fs.readFile)(expectedLogFilePath, "utf8")).replace(/(\r|\n)*$/, "");
const actualLogText = fakeLogger.events.map(e => formatLogEvent(e)).join("\n");
assert.strictEqual(actualLogText, expectedLogText);
}