Ensures you are the winning bidder if you want to be.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

321 lines
14 KiB

//Setup some enum's
const Presidence = Object.freeze({
General: "general",
Row: "row",
Highest: "highest",
Lowest: "lowest"
})
const AppliesTo = Object.freeze({
All: "all",
None: "none",
Capped: "capped",
Uncapped: "uncapped"
})
const Oversight = Object.freeze({
Win: "win",
Tie: "tie",
Watch: "watch",
Ignore: "ignore"
})
const Capsource = Object.freeze({
Row: "row",
General: "general"
})
const TestResult = Object.freeze({
Pass: "Pass",
Fail: "Fail"
})
//Declare some readability constants
const CapIsTriggered = true; //myBid is over the targetCap the cap is enabled in some way for the row)
const GeneralCapIsEnabled = true; //The generalCap checkbox is checked
const RowCapIsEnabled = true; //The rowCap checkBox is checked for the county
function newResult(capIsTriggered, reasonNum, trueCapNum, capSource, myBidNum) {
let result = {};
result.capIsTriggered = capIsTriggered;
result.reason = reasonNum;
result.trueCap = trueCapNum;
result.capSource = capSource;
result.myBid = myBidNum;
return result;
}
function getBidCapReasonForScenario(scenario) {
return getBidCapReason(scenario[0], scenario[1], scenario[2], scenario[3], scenario[4], scenario[5], scenario[6], scenario[7], scenario[8]);
}
//Stuff to Testing the actual functionality
function Assert(testName, thingsToCheck) {
let allChecksPassed = true;
let testCount = thingsToCheck.length;
let testResults = [];
for(let i = 0; i < testCount; i++ ){
let result = (thingsToCheck[i] == true);
testResults.push(result);
if(!result) {
allChecksPassed = false;
}
}
let output = {};
output.testName = testName;
output.result = (allChecksPassed == true) ? TestResult.Pass: TestResult.Fail;
output.individualResults = testResults;
return output;
}
function executeTests(scenarios, expectations, verboseLevel) {
///////////////////////////////////////////////////////////////////////////////
//Run thru the scenarios
const scenariosCount = scenarios.length;
console.log("VerboseLevel: "+ verboseLevel);
console.log("Generating Scenarios... \t[generalCapIsEnabled, generalCap, capPresidence, applyPresidenceTo, oversightAction, rowCapIsEnabled, rowCap, tieBid, myBid]");
console.log("Generating ExpectedValues...\t[capIsTriggered, reason, trueCap, capSource, myBid]");
if(verboseLevel >= 2) { console.log("Test \tScenarioValues -> ExpectedValues"); }
console.log("--------------------------------------------------------");
let scenarioReasons = [];
for(let i = 0; i < scenariosCount; i++ ){
if(verboseLevel >= 2) { console.log(i+"\t["+ scenarios[i]+"] -> "+ "["+expectations[i]+"]");}
let reason = getBidCapReasonForScenario(scenarios[i]);
scenarioReasons.push(reason);
}
////////////////////////////////////////////////////////////////////////////////
//Do the actual assertions/tests to confirm we got the results we were expecting
const reasonsCount = scenarioReasons.length;
console.log("");
console.log("Running tests on each scenario...");
if(verboseLevel >= 1){
console.log("Test\tOutome\t||trgrd\t|reason\t|truCap\t|capSrc\t|myBid\t|| ScenarioValues -> ExpectedValues");
} else {
console.log("Test\tOutome\t|| ScenarioValues -> ExpectedValues");
}
console.log("-----------------------------------------------------------------");
let allTestsPassed = true;
let failCount = 0;
for(let i = 0; i < reasonsCount; i++ ){
//console.log(JSON.stringify(scenarioReasons[i]));
let assertions = [
(scenarioReasons[i].capIsTriggered == expectations[i][0]),
(scenarioReasons[i].reason == expectations[i][1]),
(scenarioReasons[i].trueCap == expectations[i][2]),
(scenarioReasons[i].capSource == expectations[i][3]),
(scenarioReasons[i].myBid == expectations[i][4])
];
let testResult = Assert(i, assertions);
if(verboseLevel >= 1){
let individualResults = testResult.individualResults.toString().replaceAll(",","\t| ");
console.log(testResult.testName + "\t"+ testResult.result + "\t||"+ individualResults +"\t|| ["+scenarios[i]+"]\t-> ["+ expectations[i] +"]");
} else {
console.log(testResult.testName + "\t"+ testResult.result + "\t|| ["+scenarios[i]+"]\t-> ["+ expectations[i] +"]");
}
if(verboseLevel >= 2) {
const ePad =" \t |";
const TestResult_CapIsTriggered = (assertions[0] == true)? TestResult.Pass : TestResult.Fail;
const TestResult_Reason = (assertions[1] == true)? TestResult.Pass : TestResult.Fail;
const TestResult_TrueCap = (assertions[2] == true)? TestResult.Pass : TestResult.Fail;
const TestResult_CapSource = (assertions[3] == true)? TestResult.Pass : TestResult.Fail;
const TestResult_MyBid = (assertions[4] == true)? TestResult.Pass : TestResult.Fail;
console.log(" +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
console.log(ePad +" ITEM RESULT \t| ACTUAL \t| EXPECTED");
console.log(" +---------------------------------------------------");
console.log(ePad +"capIsTriggered: "+ TestResult_CapIsTriggered +" \t| "+ scenarioReasons[i].capIsTriggered +" \t| "+ expectations[i][0]);
console.log(ePad +"reason: "+ TestResult_Reason +"\t| "+ scenarioReasons[i].reason +" \t| "+ expectations[i][1]);
console.log(ePad +"trueCap: "+ TestResult_TrueCap +"\t| "+ scenarioReasons[i].trueCap +" \t| "+ expectations[i][2]);
console.log(ePad +"capSource: "+ TestResult_CapSource+"\t| "+ scenarioReasons[i].capSource +"\t| "+ expectations[i][3]);
console.log(ePad +"myBid: "+ TestResult_MyBid +"\t| "+ scenarioReasons[i].myBid +" \t| "+ expectations[i][4]);
console.log(" +---------------------------------------------------");
console.log("---------------------------------------------------------------------");
}
if(testResult.result != TestResult.Pass){
allTestsPassed = false;
failCount++;
}
}
console.log("=====================================================================");
if(allTestsPassed){
console.log("All "+reasonsCount +" tests passed");
} else {
console.log(failCount+"/"+reasonsCount+" tests failed");
}
console.log("");
return allTestsPassed;
}
function getTest(testId) {
//TODO: THIS CODE IS AN IDEA AND IS NOT USED YET
let test = {};
test.id = testId;
test.given = getUseCases()[testId];
test.expect = getExpectedOutcomes()[testId];
return test;
}
//The function from our main codebase
function getBidCapReason(generalCapIsEnabled, pGeneralCap, capPresidence, applyPresidenceTo, oversightAction, rowCapIsEnabled, pRowCap, pTieBid, pMyBid) {
let tieBid = Number(pTieBid);
let winBid = Number(pTieBid) + 25;
let generalCap = Number(pGeneralCap);
let rowCap = Number(pRowCap);
let myBid = Number(pMyBid);
//Build our response object
let results = newResult(false, 0, null, null, myBid);
//WIP - LOTS of rules here - these are just the beginning.
if(generalCapIsEnabled) {
if(capPresidence == Presidence.General) {
if(applyPresidenceTo == AppliesTo.All) {
switch(oversightAction) {
case Oversight.Win:
if(winBid > generalCap) {
results = newResult(true, 1, generalCap, Capsource.General, generalCap);
return results;
}
//winBid <= generalCap
results = newResult(false, 2, generalCap, Capsource.General, myBid);
break;
case Oversight.Tie:
if(tieBid > generalCap) {
results = newResult(true, 3, generalCap, Capsource.General, generalCap);
return results;
}
//tieBid <= generalCap
results = newResult(false, 4, generalCap, Capsource.General, myBid);
break;
case Oversight.Watch:
results = newResult(false, 5, generalCap, Capsource.General, myBid);
break;
case Oversight.Ignore:
results = newResult(false, 6, generalCap, Capsource.General, myBid);
break;
default:
//We should never get here because all oversight actions have been covered.
}
return results;
}
else if(applyPresidenceTo == AppliesTo.None){
}
else if(applyPresidenceTo == AppliesTo.Capped){
}
else if(applyPresidenceTo == AppliesTo.Uncapped){
}
return results;
}
else if(capPresidence == Presidence.Row) {
return results;
}
else if(capPresidence == Presidence.Highest){
return results;
}
else if(capPresidence == Presidence.Lowest){
return results;
}
}
//If we made it here, generalCapIsEnabled is FALSE so no conflicts should exist
//This means rowCaps are the winners if they are enabled.
if(rowCapIsEnabled) {
switch(oversightAction) {
case Oversight.Win:
if(winBid > rowCap) {
results = newResult(true, 101, rowCap, Capsource.Row, rowCap);
return results;
}
//winBid <= rowCap
results = newResult(false, 102, rowCap, Capsource.Row, myBid);
break;
case Oversight.Tie:
if(tieBid > rowCap) {
results = newResult(true, 103, rowCap, Capsource.Row, rowCap);
return results;
}
//tieBid <= generalCap
results = newResult(false, 104, rowCap, Capsource.Row, myBid);
break;
case Oversight.Watch:
results = newResult(false, 105, null, Capsource.Row, myBid);
break;
case Oversight.Ignore:
results = newResult(false, 106, null, Capsource.Row, myBid);
break;
default:
//We should never get here because all oversight actions have been covered.
}
return results;
}
//Caps are not enabled for this row.
return results;
}
//All the scenarios we could run into
function getUseCases(){
let useCases = []; //[generalCapIsEnabled, generalCap, capPresidence, applyPresidenceTo, oversightAction, rowCapIsEnabled, rowCap, tieBid, myBid];
// [generalCapIsEnabled, gCap, capPresidence , applyTo , rowAction , rowCapIsEnabled, rCap, tie, myBid]; //win
//winBid > generalCap > rowCap > myBid
useCases[0] = [GeneralCapIsEnabled, 350, Presidence.General, AppliesTo.All, Oversight.Win, !RowCapIsEnabled, null, 550, 225]; //575
useCases[1] = [GeneralCapIsEnabled, 350, Presidence.General, AppliesTo.All, Oversight.Win, RowCapIsEnabled, 450 , 550, 225]; //575
//winBid = generalCap > rowCap > myBid
useCases[2] = [GeneralCapIsEnabled, 350, Presidence.General, AppliesTo.All, Oversight.Win, !RowCapIsEnabled, null, 275, 225]; //300
useCases[3] = [GeneralCapIsEnabled, 350, Presidence.General, AppliesTo.All, Oversight.Win, RowCapIsEnabled, 450 , 275, 225]; //300
//winBid < generalCap > rowCap > myBid
useCases[4] = [GeneralCapIsEnabled, 350, Presidence.General, AppliesTo.All, Oversight.Win, !RowCapIsEnabled, null, 250, 225]; //275
useCases[5] = [GeneralCapIsEnabled, 350, Presidence.General, AppliesTo.All, Oversight.Win, RowCapIsEnabled, 450 , 250, 225]; //275
//useCases[2] = [GeneralCapIsEnabled, 300, Presidence.General, AppliesTo.All, Oversight.Tie, !RowCapIsEnabled, null, 500, 350];
//useCases[3] = [GeneralCapIsEnabled, 300, Presidence.General, AppliesTo.All, Oversight.Tie, RowCapIsEnabled, 450 , 500, 350];
return useCases;
}
//And what we expect from each scenario
function getExpectedOutcomes(){
let expectedOutcomes = [];//[capIsTriggered, reason, trueCap, capSource, myBid]);
// [ capIsTriggered, r,trueCap, capSource, myBid] //wBid,(gC||rC ) -> tCap? myBid
//winBid > generalCap > rowCap > myBid
expectedOutcomes[0] = [!CapIsTriggered, 1, 350, Capsource.General, 225]; //575,(350||null) -> 350 ? 225
expectedOutcomes[1] = [!CapIsTriggered, 1, 350, Capsource.General, 225]; //575,(350||450) -> 350 ? 225
//winBid = generalCap > rowCap > myBid
expectedOutcomes[2] = [!CapIsTriggered, 0, 350, Capsource.General, 225]; //300,(350||null) -> 350 ? 225
expectedOutcomes[3] = [!CapIsTriggered, 0, 350, Capsource.General, 225]; //300,(350||450) -> 350 ? 225
//winBid < generalCap > rowCap > myBid
expectedOutcomes[4] = [!CapIsTriggered, 0, 350, Capsource.General, 225]; //275,(350||null) -> 350 ? 225
expectedOutcomes[5] = [!CapIsTriggered, 0, 350, Capsource.General, 225]; //275,(350||450) -> 350 ? 225
//expectedOutcomes[2] = [CapIsTriggered, 3, 300, Capsource.General, 300];
//expectedOutcomes[3] = [CapIsTriggered, 4, 300, Capsource.General, 300];
return expectedOutcomes;
}
//Setup our scenarios and expected values
const useCases = getUseCases();
const expectedOutcomes = getExpectedOutcomes();
const verboseLevel = 1;
const testResults = executeTests(useCases, expectedOutcomes, verboseLevel);