@featurevisor/core
Version:
Core package of Featurevisor for Node.js usage
71 lines • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.testSegment = testSegment;
const sdk_1 = require("@featurevisor/sdk");
async function testSegment(datasource, test, options = {}) {
const testStartTime = Date.now();
const segmentKey = test.segment;
const testResult = {
type: "segment",
key: segmentKey,
// to be updated later
notFound: false,
duration: 0,
passed: true,
assertions: [],
};
const segmentExists = await datasource.segmentExists(segmentKey);
if (!segmentExists) {
testResult.notFound = true;
testResult.passed = false;
return testResult;
}
let logLevel = "warn";
if (options.verbose) {
logLevel = "debug";
}
else if (options.quiet) {
logLevel = "fatal";
}
const parsedSegment = await datasource.readSegment(segmentKey);
const conditions = parsedSegment.conditions;
const logger = (0, sdk_1.createLogger)({
level: logLevel,
});
const datafileReader = new sdk_1.DatafileReader({
datafile: {
schemaVersion: "2",
revision: "tester",
segments: {},
features: {},
},
logger,
});
test.assertions.forEach(function (assertion) {
const assertionStartTime = Date.now();
const testResultAssertion = {
description: assertion.description,
duration: 0,
passed: true,
errors: [],
};
const expected = assertion.expectedToMatch;
const actual = datafileReader.allConditionsAreMatched(conditions, assertion.context);
const passed = actual === expected;
if (!passed) {
const testResultAssertionError = {
type: "segment",
expected,
actual,
};
testResultAssertion.errors.push(testResultAssertionError);
testResult.passed = false;
testResultAssertion.passed = passed;
}
testResult.assertions.push(testResultAssertion);
testResultAssertion.duration = Date.now() - assertionStartTime;
});
testResult.duration = Date.now() - testStartTime;
return testResult;
}
//# sourceMappingURL=testSegment.js.map