Implementation of the customer consumption display
This commit is contained in:
174
tests/consumptionTrend.test.cjs
Normal file
174
tests/consumptionTrend.test.cjs
Normal file
@@ -0,0 +1,174 @@
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const vm = require("node:vm");
|
||||
|
||||
const root = path.resolve(__dirname, "..");
|
||||
|
||||
function loadContentHelpers() {
|
||||
const source = fs.readFileSync(path.join(root, "src", "content.js"), "utf8");
|
||||
const marker = "\n if (insertTile()) {";
|
||||
const instrumented = source.replace(marker, `
|
||||
globalThis.__consumptionTrendTest = {
|
||||
normalizeConsumptionTrendItems,
|
||||
formatConsumptionPointLabel,
|
||||
createNiceConsumptionMaximum,
|
||||
calculateAverageConsumption
|
||||
};
|
||||
${marker}`);
|
||||
const context = {
|
||||
chrome: { runtime: { onMessage: { addListener() {} } } },
|
||||
document: { getElementById: () => ({}) },
|
||||
window: {
|
||||
location: {
|
||||
hostname: "eeho.fa.us2.oraclecloud.com",
|
||||
pathname: "/fscmUI/faces/FuseWelcome"
|
||||
}
|
||||
},
|
||||
Intl,
|
||||
Date,
|
||||
Map,
|
||||
Set,
|
||||
Number,
|
||||
String,
|
||||
Array,
|
||||
Object,
|
||||
Math
|
||||
};
|
||||
|
||||
vm.runInNewContext(instrumented, context);
|
||||
return context.__consumptionTrendTest;
|
||||
}
|
||||
|
||||
function loadBackground(fetchCalls) {
|
||||
let listener;
|
||||
const source = fs.readFileSync(path.join(root, "src", "background.js"), "utf8");
|
||||
const headers = {
|
||||
get(name) {
|
||||
return name.toLowerCase() === "content-type" ? "application/json" : null;
|
||||
},
|
||||
forEach(callback) {
|
||||
callback("application/json", "content-type");
|
||||
}
|
||||
};
|
||||
const context = {
|
||||
chrome: {
|
||||
runtime: {
|
||||
onMessage: {
|
||||
addListener(callback) {
|
||||
listener = callback;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
fetch: async (url, options) => {
|
||||
fetchCalls.push({ url, options });
|
||||
return {
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers,
|
||||
text: async () => JSON.stringify([{ fyDate: "2026-09-01", usedAmount: 25 }])
|
||||
};
|
||||
},
|
||||
URL,
|
||||
Date,
|
||||
JSON,
|
||||
Map,
|
||||
Set,
|
||||
Promise,
|
||||
String,
|
||||
Array,
|
||||
Object,
|
||||
Number,
|
||||
Math,
|
||||
setTimeout,
|
||||
clearTimeout
|
||||
};
|
||||
|
||||
vm.runInNewContext(source, context);
|
||||
return listener;
|
||||
}
|
||||
|
||||
function dispatch(listener, message) {
|
||||
return new Promise((resolve) => {
|
||||
const asynchronous = listener(message, {}, resolve);
|
||||
assert.equal(asynchronous, true);
|
||||
});
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const helpers = loadContentHelpers();
|
||||
const points = helpers.normalizeConsumptionTrendItems([
|
||||
{
|
||||
fyDate: "2026-08-22",
|
||||
fiscalTime: ["22-Aug-26"],
|
||||
fiscalQtr: "FY27-Q1",
|
||||
usageCategory: "Funded Allocation",
|
||||
usedAmount: 31000,
|
||||
arrCd: 120000,
|
||||
currencyCode: "BRL"
|
||||
},
|
||||
{
|
||||
fyDate: "2026-08-22",
|
||||
fiscalTime: ["22-Aug-26"],
|
||||
usageCategory: "OVERAGE",
|
||||
usedAmount: 4200,
|
||||
currencyCode: "BRL"
|
||||
},
|
||||
{
|
||||
fyDate: "2026-08-15",
|
||||
fiscalTime: ["15-Aug-26"],
|
||||
usageCategory: null,
|
||||
usedAmount: 29000,
|
||||
currencyCode: "BRL"
|
||||
}
|
||||
]);
|
||||
|
||||
assert.equal(points.length, 2);
|
||||
assert.equal(points[0].dateKey, "2026-08-15");
|
||||
assert.equal(points[0].funded, 29000);
|
||||
assert.equal(points[1].funded, 31000);
|
||||
assert.equal(points[1].overage, 4200);
|
||||
assert.equal(points[1].total, 35200);
|
||||
assert.equal(points[1].arr, 120000);
|
||||
assert.equal(helpers.formatConsumptionPointLabel(points[1], "weekly"), "22-Aug-26");
|
||||
assert.equal(helpers.createNiceConsumptionMaximum(35200), 50000);
|
||||
assert.equal(helpers.calculateAverageConsumption(points, true), 29000);
|
||||
assert.equal(helpers.calculateAverageConsumption(points, false), 32100);
|
||||
assert.equal(helpers.calculateAverageConsumption(points, true, new Set(["funded"])), 29000);
|
||||
assert.equal(helpers.calculateAverageConsumption(points, false, new Set(["overage"])), 2100);
|
||||
|
||||
const fetchCalls = [];
|
||||
const listener = loadBackground(fetchCalls);
|
||||
const periods = ["quarterly", "monthly", "weekly", "daily"];
|
||||
|
||||
for (const period of periods) {
|
||||
const response = await dispatch(listener, {
|
||||
type: "opportunitiesExtension.requestConsumptionTrend",
|
||||
subPlanNum: "11276098",
|
||||
period,
|
||||
authorization: { name: "g2m-authorization", value: "ocicons-secret" }
|
||||
});
|
||||
|
||||
assert.equal(response.ok, true);
|
||||
assert.equal(response.period, period);
|
||||
assert.equal(response.items.length, 1);
|
||||
assert.equal(response.debugRequests[0].name, `requestConsumptionService${period[0].toUpperCase()}${period.slice(1)}`);
|
||||
}
|
||||
|
||||
assert.equal(fetchCalls.length, 4);
|
||||
fetchCalls.forEach((call, index) => {
|
||||
assert.equal(call.options.method, "GET");
|
||||
assert.equal(call.options.headers["G2m-Authorization"], "ocicons-secret");
|
||||
assert.match(call.url, /\/11276098\?quarter=All&timePeriod=/);
|
||||
assert.ok(call.url.endsWith(periods[index]));
|
||||
});
|
||||
|
||||
console.log("Consumption trend tests passed.");
|
||||
}
|
||||
|
||||
run().catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in New Issue
Block a user