feat: edit mode + steal price entry
This commit is contained in:
217
tests/helpers.test.js
Normal file
217
tests/helpers.test.js
Normal file
@@ -0,0 +1,217 @@
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
|
||||
function monthToYear(month) {
|
||||
const match = String(month || "").match(/(20\d{2})/);
|
||||
return match ? Number(match[1]) : new Date().getFullYear();
|
||||
}
|
||||
|
||||
function buildHistoryEntry(surcharges, baseSS) {
|
||||
const month =
|
||||
surcharges["316"]?.month ||
|
||||
surcharges["304"]?.month ||
|
||||
surcharges["444"]?.month ||
|
||||
"";
|
||||
|
||||
return {
|
||||
m: month,
|
||||
y: monthToYear(month),
|
||||
ss: Number.isFinite(Number(baseSS)) ? Number(baseSS) : null,
|
||||
lz304: Number(surcharges["304"]?.value) || 0,
|
||||
lz316: Number(surcharges["316"]?.value) || 0,
|
||||
lz444: Number(surcharges["444"]?.value) || 0,
|
||||
};
|
||||
}
|
||||
|
||||
function getSaveMonth(surcharges, editMode, editMonth) {
|
||||
if (editMode) return editMonth;
|
||||
|
||||
return (
|
||||
surcharges["316"]?.month ||
|
||||
surcharges["304"]?.month ||
|
||||
surcharges["444"]?.month ||
|
||||
""
|
||||
);
|
||||
}
|
||||
|
||||
function cloneSurcharges(surcharges) {
|
||||
return {
|
||||
304: {
|
||||
...(surcharges["304"] || { month: "", value: 0 }),
|
||||
},
|
||||
316: {
|
||||
...(surcharges["316"] || { month: "", value: 0 }),
|
||||
},
|
||||
444: {
|
||||
...(surcharges["444"] || { month: "", value: 0 }),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function shouldUseEditedCurrentValues(editMode, editMonth, editSnapshot) {
|
||||
if (!editMode || !editSnapshot) return true;
|
||||
|
||||
return getSaveMonth(editSnapshot.surcharges, false, "") === editMonth;
|
||||
}
|
||||
|
||||
function buildSaveEntry(surcharges, baseSS, saveMonth) {
|
||||
const entry = buildHistoryEntry(surcharges, baseSS);
|
||||
|
||||
return {
|
||||
...entry,
|
||||
m: saveMonth,
|
||||
y: monthToYear(saveMonth),
|
||||
};
|
||||
}
|
||||
|
||||
function upsertHistory(history, entry) {
|
||||
if (!entry.m) return history;
|
||||
|
||||
const existingIndex = history.findIndex(
|
||||
(historyEntry) => historyEntry.m === entry.m,
|
||||
);
|
||||
if (existingIndex >= 0) {
|
||||
const nextHistory = [...history];
|
||||
nextHistory[existingIndex] = { ...nextHistory[existingIndex], ...entry };
|
||||
return nextHistory;
|
||||
}
|
||||
|
||||
return [...history, entry];
|
||||
}
|
||||
|
||||
test("monthToYear extracts year", () => {
|
||||
assert.equal(monthToYear("Apr. 2026"), 2026);
|
||||
assert.equal(monthToYear("Jan 2025"), 2025);
|
||||
});
|
||||
|
||||
test("buildSaveEntry persists base price and surcharge values", () => {
|
||||
const surcharges = {
|
||||
304: { month: "Apr. 2026", value: 2663 },
|
||||
316: { month: "Apr. 2026", value: 4591 },
|
||||
444: { month: "Apr. 2026", value: 2817 },
|
||||
};
|
||||
|
||||
const entry = buildSaveEntry(surcharges, 813, "Apr. 2026");
|
||||
|
||||
assert.equal(entry.m, "Apr. 2026");
|
||||
assert.equal(entry.y, 2026);
|
||||
assert.equal(entry.ss, 813);
|
||||
assert.equal(entry.lz304, 2663);
|
||||
assert.equal(entry.lz316, 4591);
|
||||
assert.equal(entry.lz444, 2817);
|
||||
});
|
||||
|
||||
test("buildSaveEntry uses explicit save month", () => {
|
||||
const surcharges = {
|
||||
304: { month: "Apr. 2026", value: 2663 },
|
||||
316: { month: "Apr. 2026", value: 4591 },
|
||||
444: { month: "Apr. 2026", value: 2817 },
|
||||
};
|
||||
|
||||
const entry = buildSaveEntry(surcharges, 755, "Feb 2026");
|
||||
|
||||
assert.equal(entry.m, "Feb 2026");
|
||||
assert.equal(entry.y, 2026);
|
||||
assert.equal(entry.ss, 755);
|
||||
assert.equal(entry.lz316, 4591);
|
||||
});
|
||||
|
||||
test("shouldUseEditedCurrentValues returns false for older month edit", () => {
|
||||
const editSnapshot = {
|
||||
baseSS: 813,
|
||||
surcharges: cloneSurcharges({
|
||||
304: { month: "Apr. 2026", value: 2663 },
|
||||
316: { month: "Apr. 2026", value: 4591 },
|
||||
444: { month: "Apr. 2026", value: 2817 },
|
||||
}),
|
||||
};
|
||||
|
||||
assert.equal(
|
||||
shouldUseEditedCurrentValues(true, "Feb 2026", editSnapshot),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("shouldUseEditedCurrentValues returns true for current month edit", () => {
|
||||
const editSnapshot = {
|
||||
baseSS: 813,
|
||||
surcharges: cloneSurcharges({
|
||||
304: { month: "Apr. 2026", value: 2663 },
|
||||
316: { month: "Apr. 2026", value: 4591 },
|
||||
444: { month: "Apr. 2026", value: 2817 },
|
||||
}),
|
||||
};
|
||||
|
||||
assert.equal(
|
||||
shouldUseEditedCurrentValues(true, "Apr. 2026", editSnapshot),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test("shouldUseEditedCurrentValues returns true outside edit mode", () => {
|
||||
const editSnapshot = {
|
||||
baseSS: 813,
|
||||
surcharges: cloneSurcharges({
|
||||
304: { month: "Apr. 2026", value: 2663 },
|
||||
316: { month: "Apr. 2026", value: 4591 },
|
||||
444: { month: "Apr. 2026", value: 2817 },
|
||||
}),
|
||||
};
|
||||
|
||||
assert.equal(
|
||||
shouldUseEditedCurrentValues(false, "Feb 2026", editSnapshot),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test("upsertHistory appends entry for new month", () => {
|
||||
const history = [
|
||||
{ m: "Feb 2026", y: 2026, ss: 755, lz304: 2541, lz316: 4179, lz444: 2522 },
|
||||
{ m: "Mrz 2026", y: 2026, ss: 792, lz304: 2571, lz316: 4367, lz444: 2637 },
|
||||
];
|
||||
const entry = {
|
||||
m: "Apr 2026",
|
||||
y: 2026,
|
||||
ss: 813,
|
||||
lz304: 2663,
|
||||
lz316: 4591,
|
||||
lz444: 2817,
|
||||
};
|
||||
|
||||
const result = upsertHistory(history, entry);
|
||||
|
||||
assert.equal(result.length, 3);
|
||||
assert.equal(result[2].m, "Apr 2026");
|
||||
});
|
||||
|
||||
test("upsertHistory updates existing month without duplicate", () => {
|
||||
const history = [
|
||||
{ m: "Feb 2026", y: 2026, ss: 755, lz304: 2541, lz316: 4179, lz444: 2522 },
|
||||
{ m: "Mrz 2026", y: 2026, ss: 792, lz304: 2571, lz316: 4367, lz444: 2637 },
|
||||
];
|
||||
const entry = {
|
||||
m: "Mrz 2026",
|
||||
ss: 800,
|
||||
lz304: 2600,
|
||||
lz316: 4400,
|
||||
lz444: 2650,
|
||||
};
|
||||
|
||||
const result = upsertHistory(history, entry);
|
||||
|
||||
assert.equal(result.length, 2);
|
||||
assert.equal(result[1].m, "Mrz 2026");
|
||||
assert.equal(result[1].y, 2026);
|
||||
assert.equal(result[1].ss, 800);
|
||||
assert.equal(result[1].lz444, 2650);
|
||||
});
|
||||
|
||||
test("upsertHistory keeps history unchanged for missing month", () => {
|
||||
const history = [
|
||||
{ m: "Feb 2026", y: 2026, ss: 755, lz304: 2541, lz316: 4179, lz444: 2522 },
|
||||
];
|
||||
|
||||
const result = upsertHistory(history, { m: "", ss: 700 });
|
||||
|
||||
assert.deepEqual(result, history);
|
||||
});
|
||||
Reference in New Issue
Block a user