Skip to content

Commit

Permalink
feat: handle incorrect date
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeauchamp committed Nov 26, 2024
1 parent 299e1bb commit 19a1658
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 5 additions & 1 deletion @xen-orchestra/backups/_getOldEntries.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import moment from 'moment-timezone'
import assert from 'node:assert'

function instantiateTimezonedDateCreator(timezone) {
return date => (timezone ? moment.tz(date, timezone) : moment(date))
return date => {
const transformed = timezone ? moment.tz(date, timezone) : moment(date)
assert.ok(transformed.isValid(), `date ${date} , timezone ${timezone} is invalid`)
return transformed
}
}

const LTR_DEFINITIONS = {
Expand Down
23 changes: 20 additions & 3 deletions @xen-orchestra/backups/_getOldEntries.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ describe('_getOldEntries() should fail when called incorrectly', () => {
args: [
5,
[
{ timestamp: new Date('2024-09-01'), id: 1 },
{ timestamp: new Date('2024-09-03'), id: 3 },
{ timestamp: new Date('2024-09-02'), id: 2 },
{ timestamp: +new Date('2024-09-01'), id: 1 },
{ timestamp: +new Date('2024-09-03'), id: 3 },
{ timestamp: +new Date('2024-09-02'), id: 2 },
],
{
longTermRetention: {
Expand All @@ -255,6 +255,23 @@ describe('_getOldEntries() should fail when called incorrectly', () => {
],
testLabel: 'unsorted entries ',
},

{
args: [
5,
[
{ timestamp: 'notadate', id: 1 },
{ timestamp: 'still not a date, but after', id: 3 },
],
{
longTermRetention: {
daily: { retention: 5 },
},
timezone: 'Europe/Paris',
},
],
testLabel: 'broken date ',
},
]

for (const { args, testLabel } of tests) {
Expand Down

0 comments on commit 19a1658

Please sign in to comment.