Add handling and tests for new background schedule

This commit is contained in:
Phillip Thelen
2024-04-30 13:13:09 +02:00
parent 4c2cdfe5b8
commit 9dabe79d5e
2 changed files with 62 additions and 8 deletions

View File

@@ -79,4 +79,45 @@ describe('Content Schedule', () => {
} }
} }
}); });
describe('backgrounds matcher', () => {
it('allows background matching the month for new backgrounds', () => {
const date = new Date('2024-07-08');
const matcher = getAllScheduleMatchingGroups(date).backgrounds;
expect(matcher.match('backgroundkey072024')).to.be.true;
});
it('disallows background in the future', () => {
const date = new Date('2024-07-08');
const matcher = getAllScheduleMatchingGroups(date).backgrounds;
expect(matcher.match('backgroundkey072025')).to.be.false;
});
it('disallows background for the inverse month for new backgrounds', () => {
const date = new Date('2024-07-08');
const matcher = getAllScheduleMatchingGroups(date).backgrounds;
expect(matcher.match('backgroundkey012024')).to.be.false;
});
it('allows background for the inverse month for old backgrounds', () => {
const date = new Date('2024-08-08');
const matcher = getAllScheduleMatchingGroups(date).backgrounds;
expect(matcher.match('backgroundkey022023')).to.be.true;
expect(matcher.match('backgroundkey022021')).to.be.true;
});
it('allows background even yeared backgrounds in first half of year', () => {
const date = new Date('2025-02-08');
const matcher = getAllScheduleMatchingGroups(date).backgrounds;
expect(matcher.match('backgroundkey022024')).to.be.true;
expect(matcher.match('backgroundkey082022')).to.be.true;
});
it('allows background odd yeared backgrounds in second half of year', () => {
const date = new Date('2024-08-08');
const matcher = getAllScheduleMatchingGroups(date).backgrounds;
expect(matcher.match('backgroundkey022023')).to.be.true;
expect(matcher.match('backgroundkey082021')).to.be.true;
});
});
}); });

View File

@@ -2,12 +2,24 @@ import moment from 'moment';
import SEASONAL_SETS from './seasonalSets'; import SEASONAL_SETS from './seasonalSets';
import { getRepeatingEvents } from './events'; import { getRepeatingEvents } from './events';
function isAfterNewSchedule (year, month) {
if (year >= 2025) {
return true;
} if (year === 2024) {
return month >= 6;
}
return false;
}
function backgroundMatcher (month1, month2, oddYear) { function backgroundMatcher (month1, month2, oddYear) {
return function call (key) { return function call (key, date) {
const keyLength = key.length; const keyLength = key.length;
const month = parseInt(key.substring(keyLength - 6, keyLength - 4), 10); const month = parseInt(key.substring(keyLength - 6, keyLength - 4), 10);
return (month === month1 || month === month2) const year = parseInt(key.substring(keyLength - 4, keyLength), 10);
&& parseInt(key.substring(keyLength - 2, keyLength), 10) % 2 === (oddYear ? 1 : 0); if (isAfterNewSchedule(year, month)) {
return month === month1 && date.getFullYear() >= year && (date.getMonth() + 1) >= month;
}
return (month === month1 || month === month2) && year % 2 === (oddYear ? 1 : 0);
}; };
} }
@@ -832,21 +844,22 @@ export function assembleScheduledMatchers (date) {
let cachedScheduleMatchers = null; let cachedScheduleMatchers = null;
let cacheDate = null; let cacheDate = null;
function makeMatcherClass () { function makeMatcherClass (date) {
return { return {
matchers: [], matchers: [],
end: new Date(), end: new Date(),
items: [], items: [],
matchingDate: date,
match (key) { match (key) {
if (this.matchers.length === 0) { if (this.matchers.length === 0) {
if (this.items.length > 0) { if (this.items.length > 0) {
return inListMatcher(this.items)(key); return inListMatcher(this.items)(key, this.matchingDate);
} }
} else { } else {
if (this.items.length > 0 && !inListMatcher(this.items)(key)) { if (this.items.length > 0 && !inListMatcher(this.items)(key, this.matchingDate)) {
return false; return false;
} }
return this.matchers.every(m => m(key)); return this.matchers.every(m => m(key, this.matchingDate));
} }
return false; return false;
}, },
@@ -878,7 +891,7 @@ export function getAllScheduleMatchingGroups (date) {
cachedScheduleMatchers = {}; cachedScheduleMatchers = {};
assembleScheduledMatchers(checkedDate).forEach(matcher => { assembleScheduledMatchers(checkedDate).forEach(matcher => {
if (!cachedScheduleMatchers[matcher.type]) { if (!cachedScheduleMatchers[matcher.type]) {
cachedScheduleMatchers[matcher.type] = makeMatcherClass(); cachedScheduleMatchers[matcher.type] = makeMatcherClass(date);
} }
cachedScheduleMatchers[matcher.type].end = makeEndDate(checkedDate, matcher); cachedScheduleMatchers[matcher.type].end = makeEndDate(checkedDate, matcher);
if (matcher.matcher instanceof Function) { if (matcher.matcher instanceof Function) {