Serving roles per event or session, sign-ups and assigning with an overlap check, swaps that happen on accept (notify recipients emailed), QR and manual check-in (kids-room roles start rostered duty), optional free volunteer ticket, day-before reminder job, hours served, privacy hooks. The kids check-in and volunteer tickets are left out of public event pages and public registration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
4.8 KiB
JavaScript
93 lines
4.8 KiB
JavaScript
const v = require('../src/utils/volunteers');
|
|
const { isBlocked } = require('../src/hub/readOnly');
|
|
|
|
const event = {
|
|
id: 'e1', startDate: new Date('2026-11-08T06:00:00Z'), endDate: new Date('2026-11-08T10:00:00Z'),
|
|
sessions: [{ id: 's1', startsAt: new Date('2026-11-15T16:30:00Z'), endsAt: new Date('2026-11-15T18:30:00Z') }],
|
|
};
|
|
|
|
describe('shifts', () => {
|
|
test("a role's shift: its own times, else its session's, else the event's", () => {
|
|
expect(v.shiftOf({}, event)).toEqual({ startsAt: event.startDate, endsAt: event.endDate });
|
|
expect(v.shiftOf({ sessionId: 's1' }, event)).toEqual({ startsAt: event.sessions[0].startsAt, endsAt: event.sessions[0].endsAt });
|
|
const own = { startsAt: '2026-11-08T05:30:00Z', endsAt: '2026-11-08T07:00:00Z' };
|
|
expect(v.shiftOf(own, event)).toEqual({ startsAt: new Date(own.startsAt), endsAt: new Date(own.endsAt) });
|
|
});
|
|
|
|
test('overlapping shifts; touching ends are fine', () => {
|
|
const a = { startsAt: '2026-11-08T06:00:00Z', endsAt: '2026-11-08T08:00:00Z' };
|
|
expect(v.overlaps(a, { startsAt: '2026-11-08T07:00:00Z', endsAt: '2026-11-08T09:00:00Z' })).toBe(true);
|
|
expect(v.overlaps(a, { startsAt: '2026-11-08T08:00:00Z', endsAt: '2026-11-08T09:00:00Z' })).toBe(false);
|
|
expect(v.overlaps(a, { startsAt: '2026-11-08T05:00:00Z', endsAt: '2026-11-08T06:00:00Z' })).toBe(false);
|
|
});
|
|
|
|
test('hours to the nearest quarter hour', () => {
|
|
expect(v.shiftHours({ startsAt: '2026-11-08T06:00:00Z', endsAt: '2026-11-08T08:30:00Z' })).toBe(2.5);
|
|
expect(v.shiftHours({ startsAt: '2026-11-08T06:00:00Z', endsAt: '2026-11-08T06:50:00Z' })).toBe(0.75);
|
|
expect(v.shiftHours({ startsAt: '2026-11-08T06:00:00Z', endsAt: '2026-11-08T06:00:00Z' })).toBe(0);
|
|
});
|
|
|
|
test('fill', () => {
|
|
expect(v.fillOf(4, 3)).toEqual({ needed: 4, filled: 3, open: 1, full: false });
|
|
expect(v.fillOf(2, 3)).toEqual({ needed: 2, filled: 3, open: 0, full: true });
|
|
});
|
|
});
|
|
|
|
describe('check-in window', () => {
|
|
const shift = { startsAt: new Date('2026-11-08T06:00:00Z'), endsAt: new Date('2026-11-08T08:00:00Z') };
|
|
test('open on the day of the shift until it ends', () => {
|
|
expect(v.checkInOpen(shift, new Date('2026-11-08T04:00:00Z'))).toBe(true); // early, same day (SAST)
|
|
expect(v.checkInOpen(shift, new Date('2026-11-08T07:59:00Z'))).toBe(true);
|
|
});
|
|
test('closed the day before and after the shift ends', () => {
|
|
expect(v.checkInOpen(shift, new Date('2026-11-07T12:00:00Z'))).toBe(false);
|
|
expect(v.checkInOpen(shift, new Date('2026-11-08T08:01:00Z'))).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('reminders', () => {
|
|
const shift = { startsAt: new Date('2026-11-08T06:00:00Z'), endsAt: new Date('2026-11-08T08:00:00Z') };
|
|
const signedUp = new Date('2026-11-01T10:00:00Z');
|
|
test('due within 24 hours of the start, once', () => {
|
|
expect(v.reminderDue({ remindedAt: null, createdAt: signedUp }, shift, new Date('2026-11-07T07:00:00Z'))).toBe(true);
|
|
expect(v.reminderDue({ remindedAt: new Date(), createdAt: signedUp }, shift, new Date('2026-11-07T07:00:00Z'))).toBe(false);
|
|
});
|
|
test('not too early, not after the start', () => {
|
|
expect(v.reminderDue({ remindedAt: null, createdAt: signedUp }, shift, new Date('2026-11-07T05:00:00Z'))).toBe(false);
|
|
expect(v.reminderDue({ remindedAt: null, createdAt: signedUp }, shift, new Date('2026-11-08T06:00:00Z'))).toBe(false);
|
|
});
|
|
test('not for someone who signed up minutes ago', () => {
|
|
const now = new Date('2026-11-07T20:00:00Z');
|
|
expect(v.reminderDue({ remindedAt: null, createdAt: new Date(now.getTime() - 10 * 60 * 1000) }, shift, now)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('kids rooms', () => {
|
|
test('a session role counts for its session; otherwise for the day', () => {
|
|
expect(v.kidsOccasion({ sessionId: 's1' })).toBe('s1');
|
|
expect(v.kidsOccasion({ sessionId: null }, new Date('2026-11-08T06:00:00Z'))).toBe('2026-11-08');
|
|
});
|
|
});
|
|
|
|
describe('reserved tickets', () => {
|
|
test('the kids and volunteer tickets never show publicly', () => {
|
|
const ev = { kidsOptionId: 'k', volunteerOptionId: 'vol', eventOptions: [{ id: 'adult' }, { id: 'k' }, { id: 'vol' }] };
|
|
expect(v.reservedOptionIds(ev)).toEqual(['k', 'vol']);
|
|
expect(v.publicOptions(ev).map(o => o.id)).toEqual(['adult']);
|
|
expect(v.publicOptions({ eventOptions: [{ id: 'a' }] }).map(o => o.id)).toEqual(['a']);
|
|
});
|
|
});
|
|
|
|
describe('read-only licence', () => {
|
|
test.each([
|
|
['POST', '/api/volunteers/roles/r1/sign-up'],
|
|
['POST', '/api/volunteers/manage/events/e1/roles'],
|
|
['POST', '/api/volunteers/manage/roles/r1/assign'],
|
|
])('%s %s is blocked', (m, p) => expect(isBlocked(m, p)).toBe(true));
|
|
test.each([
|
|
['POST', '/api/volunteers/check-in'],
|
|
['POST', '/api/volunteers/swaps/w1/accept'],
|
|
['DELETE', '/api/volunteers/slots/s1'],
|
|
])('%s %s keeps working', (m, p) => expect(isBlocked(m, p)).toBe(false));
|
|
});
|