feat(data): settings.moderatorsPerDay in load/save JSON

This commit is contained in:
ferdi2go committed 2026-04-28 10:38:51 +00:00
1 parent 7bda92a0a3
commit a825a63961
2 files changed
+58 -3

No files matched your search

+10 -2
View File
@@ -1,3 +1,9 @@
function parseSettings(raw) {
const n = raw?.moderatorsPerDay;
const valid = Number.isInteger(n) && n >= 1 && n <= 5;
return { moderatorsPerDay: valid ? n : 1 };
}
export function loadFromJSON(json) { export function loadFromJSON(json) {
let parsed; let parsed;
try { try {
@@ -16,7 +22,8 @@ export function loadFromJSON(json) {
companyClosures: Array.isArray(parsed.calendar.companyClosures) ? parsed.calendar.companyClosures : [] companyClosures: Array.isArray(parsed.calendar.companyClosures) ? parsed.calendar.companyClosures : []
} }
: { holidays: [], companyClosures: [] }, : { holidays: [], companyClosures: [] },
history: Array.isArray(parsed.history) ? parsed.history : [] history: Array.isArray(parsed.history) ? parsed.history : [],
settings: parseSettings(parsed.settings)
}; };
} }
@@ -24,7 +31,8 @@ export function saveToJSON(state) {
return JSON.stringify({ return JSON.stringify({
employees: state.employees, employees: state.employees,
calendar: state.calendar, calendar: state.calendar,
history: state.history history: state.history,
settings: state.settings
}, null, 2); }, null, 2);
} }
+48 -1
View File
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { addHistoryEntries, removeHistoryEntry } from '../js/data.js'; import { addHistoryEntries, removeHistoryEntry, loadFromJSON, saveToJSON } from '../js/data.js';
function makeState(history = []) { function makeState(history = []) {
return { return {
@@ -76,4 +76,51 @@ function makeState(history = []) {
assert.equal(state.history.length, 1, 'no change when employeeId not found'); assert.equal(state.history.length, 1, 'no change when employeeId not found');
} }
// loadFromJSON: missing settings → default {moderatorsPerDay: 1}
{
const json = JSON.stringify({
employees: [],
calendar: { holidays: [], companyClosures: [] },
history: []
});
const loaded = loadFromJSON(json);
assert.deepEqual(loaded.settings, { moderatorsPerDay: 1 }, 'default settings when missing');
}
// loadFromJSON: present settings preserved, moderatorsPerDay clamped to [1,5]
{
const json = JSON.stringify({
employees: [], calendar: { holidays: [], companyClosures: [] }, history: [],
settings: { moderatorsPerDay: 3 }
});
const loaded = loadFromJSON(json);
assert.equal(loaded.settings.moderatorsPerDay, 3, 'preserved value');
}
// loadFromJSON: invalid moderatorsPerDay (string, out of range) → fallback to 1
{
const tooHigh = loadFromJSON(JSON.stringify({
employees: [], calendar: { holidays: [], companyClosures: [] }, history: [],
settings: { moderatorsPerDay: 99 }
}));
assert.equal(tooHigh.settings.moderatorsPerDay, 1, 'out of range falls back to 1');
const wrongType = loadFromJSON(JSON.stringify({
employees: [], calendar: { holidays: [], companyClosures: [] }, history: [],
settings: { moderatorsPerDay: 'two' }
}));
assert.equal(wrongType.settings.moderatorsPerDay, 1, 'wrong type falls back to 1');
}
// saveToJSON: settings are serialized
{
const state = {
employees: [], calendar: { holidays: [], companyClosures: [] }, history: [],
settings: { moderatorsPerDay: 2 }
};
const json = saveToJSON(state);
const parsed = JSON.parse(json);
assert.deepEqual(parsed.settings, { moderatorsPerDay: 2 }, 'settings round-trip');
}
console.log('All data tests passed.'); console.log('All data tests passed.');