diff --git a/src/storage.js b/src/storage.js index 422db13..733ebcb 100644 --- a/src/storage.js +++ b/src/storage.js @@ -24,6 +24,10 @@ function isFiniteNumber(value) { return typeof value === 'number' && Number.isFinite(value); } +function isPositiveInteger(value) { + return typeof value === 'number' && Number.isInteger(value) && value >= 1; +} + function isUsableStack(stack) { return ( isPlainObject(stack) @@ -36,7 +40,7 @@ function isUsableStack(stack) { function isUsableEntry(entry) { return ( isPlainObject(entry) - && isFiniteNumber(entry.entryId) + && isPositiveInteger(entry.entryId) && typeof entry.stackId === 'string' && isPlainObject(entry.spec) ); @@ -65,7 +69,7 @@ export function loadSession(store) { if (!raw) return null; const parsed = JSON.parse(raw); if (!Array.isArray(parsed.stacks) || !Array.isArray(parsed.entries)) return null; - if (!isFiniteNumber(parsed.nextEntryId)) return null; + if (!isPositiveInteger(parsed.nextEntryId)) return null; if (!parsed.stacks.every(isUsableStack)) return null; if (!parsed.entries.every(isUsableEntry)) return null; diff --git a/test/storage.test.js b/test/storage.test.js index 9aec02c..f022e4c 100644 --- a/test/storage.test.js +++ b/test/storage.test.js @@ -191,3 +191,43 @@ test('nach Sichern und Laden funktioniert Weiterarbeiten weiterhin', () => { assert.equal(wieder.entries.find((e) => e.entryId === zweiterEintrag.entryId), undefined); assert.equal(wieder.stacks.find((s) => s.id === 'B'), undefined); }); + +test('nextEntryId als negative Zahl liefert null', () => { + const store = fakeStore(); + store.setItem('ram-sortierhilfe:session', JSON.stringify({ + stacks: [], + entries: [], + nextEntryId: -5, + })); + assert.equal(loadSession(store), null); +}); + +test('nextEntryId als gebrochene Zahl liefert null', () => { + const store = fakeStore(); + store.setItem('ram-sortierhilfe:session', JSON.stringify({ + stacks: [], + entries: [], + nextEntryId: 1.5, + })); + assert.equal(loadSession(store), null); +}); + +test('nextEntryId als Null liefert null', () => { + const store = fakeStore(); + store.setItem('ram-sortierhilfe:session', JSON.stringify({ + stacks: [], + entries: [], + nextEntryId: 0, + })); + assert.equal(loadSession(store), null); +}); + +test('Eintrag mit gebrochener entryId liefert null', () => { + const store = fakeStore(); + store.setItem('ram-sortierhilfe:session', JSON.stringify({ + stacks: [{ id: 'A', count: 1, spec: emptySpec() }], + entries: [{ entryId: 1.5, stackId: 'A', spec: emptySpec(), source: 'barcode' }], + nextEntryId: 2, + })); + assert.equal(loadSession(store), null); +});