249 lines
10 KiB
TypeScript
249 lines
10 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
addUtcDays,
|
|
availabilityModeForEntry,
|
|
blocksToDailySchedule,
|
|
blocksToMonthlySchedule,
|
|
buildAvailabilitySnapshot,
|
|
dateKeysAround,
|
|
daysInMonth,
|
|
scheduleTargetDay,
|
|
startOfUtcDay,
|
|
toDate,
|
|
toIso,
|
|
} from '../src';
|
|
|
|
describe('toDate', () => {
|
|
it('accepts a Date instance', () => {
|
|
const original = new Date('2026-05-01T12:34:56.789Z');
|
|
const result = toDate(original);
|
|
expect(result.toISOString()).toBe('2026-05-01T12:34:56.789Z');
|
|
});
|
|
|
|
it('accepts an ISO string', () => {
|
|
const result = toDate('2026-05-01T12:00:00.000Z');
|
|
expect(result.toISOString()).toBe('2026-05-01T12:00:00.000Z');
|
|
});
|
|
|
|
it('accepts a timestamp number', () => {
|
|
const ts = new Date('2026-05-01T00:00:00.000Z').getTime();
|
|
const result = toDate(ts);
|
|
expect(result.toISOString()).toBe('2026-05-01T00:00:00.000Z');
|
|
});
|
|
|
|
it('throws on invalid input', () => {
|
|
expect(() => toDate('not-a-date')).toThrow('Invalid datetime: not-a-date');
|
|
expect(() => toDate(NaN)).toThrow('Invalid datetime: NaN');
|
|
});
|
|
});
|
|
|
|
describe('toIso', () => {
|
|
it('converts any DateTimeInput to an ISO string', () => {
|
|
expect(toIso(new Date('2026-05-01T12:00:00.000Z'))).toBe('2026-05-01T12:00:00.000Z');
|
|
expect(toIso('2026-05-01T12:00:00.000Z')).toBe('2026-05-01T12:00:00.000Z');
|
|
});
|
|
});
|
|
|
|
describe('startOfUtcDay', () => {
|
|
it('strips time to 00:00:00 UTC', () => {
|
|
const result = startOfUtcDay('2026-05-01T15:30:45.000Z');
|
|
expect(result.toISOString()).toBe('2026-05-01T00:00:00.000Z');
|
|
});
|
|
|
|
it('works across month boundaries', () => {
|
|
const result = startOfUtcDay('2026-03-31T23:59:59.999Z');
|
|
expect(result.toISOString()).toBe('2026-03-31T00:00:00.000Z');
|
|
});
|
|
});
|
|
|
|
describe('addUtcDays', () => {
|
|
it('adds days', () => {
|
|
const result = addUtcDays('2026-05-01T12:00:00.000Z', 5);
|
|
expect(result.toISOString()).toBe('2026-05-06T00:00:00.000Z');
|
|
});
|
|
|
|
it('subtracts days', () => {
|
|
const result = addUtcDays('2026-05-01T12:00:00.000Z', -1);
|
|
expect(result.toISOString()).toBe('2026-04-30T00:00:00.000Z');
|
|
});
|
|
});
|
|
|
|
describe('scheduleTargetDay', () => {
|
|
it('returns tomorrow at UTC midnight', () => {
|
|
const result = scheduleTargetDay('2026-05-01T10:00:00.000Z');
|
|
expect(result.toISOString()).toBe('2026-05-02T00:00:00.000Z');
|
|
});
|
|
});
|
|
|
|
describe('daysInMonth', () => {
|
|
it('returns 31 for January', () => {
|
|
expect(daysInMonth(new Date('2026-01-15T00:00:00.000Z'))).toBe(31);
|
|
});
|
|
|
|
it('returns 28 for February in a non-leap year', () => {
|
|
expect(daysInMonth(new Date('2026-02-15T00:00:00.000Z'))).toBe(28);
|
|
});
|
|
|
|
it('returns 29 for February in a leap year', () => {
|
|
expect(daysInMonth(new Date('2024-02-15T00:00:00.000Z'))).toBe(29);
|
|
});
|
|
|
|
it('returns 30 for April', () => {
|
|
expect(daysInMonth(new Date('2026-04-15T00:00:00.000Z'))).toBe(30);
|
|
});
|
|
|
|
it('returns 31 for December', () => {
|
|
expect(daysInMonth(new Date('2026-12-15T00:00:00.000Z'))).toBe(31);
|
|
});
|
|
});
|
|
|
|
describe('blocksToDailySchedule', () => {
|
|
it('converts schedule blocks to entries for the target day', () => {
|
|
const persona = { id: 'space-1', displayName: 'Mina', createdAt: '2026-05-01T00:00:00.000Z', metadata: {} };
|
|
const blocks = [
|
|
{ startTime: '00:00', endTime: '07:00', activity: 'sleep', title: 'Sleep', availabilityMode: 'offline' as const },
|
|
{ startTime: '07:00', endTime: '09:00', activity: 'morning routine', title: 'Morning routine', availabilityMode: 'online' as const },
|
|
];
|
|
const entries = blocksToDailySchedule({ persona, targetDay: '2026-05-02T10:00:00.000Z', message: 'msg', blocks });
|
|
|
|
expect(entries).toHaveLength(2);
|
|
expect(entries[0]!.startAt).toBe('2026-05-02T00:00:00.000Z');
|
|
expect(entries[0]!.endAt).toBe('2026-05-02T07:00:00.000Z');
|
|
expect(entries[0]!.activity).toBe('sleep');
|
|
expect(entries[0]!.granularity).toBe('ten-minute');
|
|
expect(entries[0]!.metadata['availabilityMode']).toBe('offline');
|
|
expect(entries[1]!.startAt).toBe('2026-05-02T07:00:00.000Z');
|
|
expect(entries[1]!.endAt).toBe('2026-05-02T09:00:00.000Z');
|
|
});
|
|
|
|
it('uses block description when provided', () => {
|
|
const persona = { id: 'space-1', displayName: 'Mina', createdAt: '2026-05-01T00:00:00.000Z', metadata: {} };
|
|
const blocks = [
|
|
{ startTime: '09:00', endTime: '18:00', activity: 'work', title: 'Work', description: 'Custom desc', availabilityMode: 'do-not-disturb' as const },
|
|
];
|
|
const entries = blocksToDailySchedule({ persona, targetDay: '2026-05-02T00:00:00.000Z', message: 'msg', blocks });
|
|
expect(entries[0]!.description).toBe('Custom desc');
|
|
});
|
|
|
|
it('supports 24:00 as end time to reach next midnight', () => {
|
|
const persona = { id: 'space-1', displayName: 'Mina', createdAt: '2026-05-01T00:00:00.000Z', metadata: {} };
|
|
const blocks = [
|
|
{ startTime: '18:00', endTime: '24:00', activity: 'rest', title: 'Rest', availabilityMode: 'online' as const },
|
|
];
|
|
const entries = blocksToDailySchedule({ persona, targetDay: '2026-05-02T00:00:00.000Z', message: 'msg', blocks });
|
|
expect(entries[0]!.endAt).toBe('2026-05-03T00:00:00.000Z');
|
|
});
|
|
});
|
|
|
|
describe('blocksToMonthlySchedule', () => {
|
|
it('maps each block to consecutive days starting from fromDay', () => {
|
|
const persona = { id: 'space-1', displayName: 'Mina', createdAt: '2026-05-01T00:00:00.000Z', metadata: {} };
|
|
const blocks = [
|
|
{ startTime: '00:00', endTime: '24:00', activity: 'work', title: 'Work', availabilityMode: 'do-not-disturb' as const },
|
|
{ startTime: '00:00', endTime: '24:00', activity: 'rest', title: 'Rest', availabilityMode: 'online' as const },
|
|
];
|
|
const entries = blocksToMonthlySchedule({ persona, fromDay: '2026-05-02T00:00:00.000Z', message: 'msg', blocks });
|
|
|
|
expect(entries).toHaveLength(2);
|
|
expect(entries[0]!.startAt).toBe('2026-05-02T00:00:00.000Z');
|
|
expect(entries[0]!.endAt).toBe('2026-05-03T00:00:00.000Z');
|
|
expect(entries[1]!.startAt).toBe('2026-05-03T00:00:00.000Z');
|
|
expect(entries[1]!.endAt).toBe('2026-05-04T00:00:00.000Z');
|
|
expect(entries[0]!.granularity).toBe('day');
|
|
});
|
|
});
|
|
|
|
describe('availabilityModeForEntry', () => {
|
|
it('reads availabilityMode from metadata', () => {
|
|
expect(
|
|
availabilityModeForEntry({
|
|
id: '1', spaceId: 's', startAt: '', endAt: '', activity: 'anything', title: '', granularity: 'day', metadata: { availabilityMode: 'offline' },
|
|
}),
|
|
).toBe('offline');
|
|
expect(
|
|
availabilityModeForEntry({
|
|
id: '1', spaceId: 's', startAt: '', endAt: '', activity: 'anything', title: '', granularity: 'day', metadata: { availabilityMode: 'do-not-disturb' },
|
|
}),
|
|
).toBe('do-not-disturb');
|
|
expect(
|
|
availabilityModeForEntry({
|
|
id: '1', spaceId: 's', startAt: '', endAt: '', activity: 'anything', title: '', granularity: 'day', metadata: { availabilityMode: 'online' },
|
|
}),
|
|
).toBe('online');
|
|
});
|
|
|
|
it('falls back to online when metadata is missing or invalid', () => {
|
|
expect(
|
|
availabilityModeForEntry({
|
|
id: '1', spaceId: 's', startAt: '', endAt: '', activity: 'work', title: '', granularity: 'day', metadata: {},
|
|
}),
|
|
).toBe('online');
|
|
expect(
|
|
availabilityModeForEntry({
|
|
id: '1', spaceId: 's', startAt: '', endAt: '', activity: 'sleep', title: '', granularity: 'day', metadata: { availabilityMode: 'invalid' },
|
|
}),
|
|
).toBe('online');
|
|
});
|
|
});
|
|
|
|
describe('buildAvailabilitySnapshot', () => {
|
|
it('filters, sorts, and merges contiguous ranges with the same mode', () => {
|
|
const entries = [
|
|
{
|
|
id: 'e1', spaceId: 's', startAt: '2026-05-01T00:00:00.000Z', endAt: '2026-05-01T06:00:00.000Z',
|
|
activity: 'sleep', title: 'Sleep', granularity: 'ten-minute' as const, metadata: { availabilityMode: 'offline' },
|
|
},
|
|
{
|
|
id: 'e2', spaceId: 's', startAt: '2026-05-01T06:00:00.000Z', endAt: '2026-05-01T12:00:00.000Z',
|
|
activity: 'work', title: 'Work', granularity: 'ten-minute' as const, metadata: { availabilityMode: 'do-not-disturb' },
|
|
},
|
|
{
|
|
id: 'e3', spaceId: 's', startAt: '2026-05-01T12:00:00.000Z', endAt: '2026-05-01T18:00:00.000Z',
|
|
activity: 'work', title: 'Work', granularity: 'ten-minute' as const, metadata: { availabilityMode: 'do-not-disturb' },
|
|
},
|
|
{
|
|
id: 'e4', spaceId: 's', startAt: '2026-05-01T18:00:00.000Z', endAt: '2026-05-02T00:00:00.000Z',
|
|
activity: 'rest', title: 'Rest', granularity: 'ten-minute' as const, metadata: { availabilityMode: 'online' },
|
|
},
|
|
];
|
|
|
|
const snapshot = buildAvailabilitySnapshot({ now: '2026-05-01T12:00:00.000Z', entries });
|
|
|
|
expect(snapshot.windowStartAt).toBe('2026-05-01T00:00:00.000Z');
|
|
expect(snapshot.windowEndAt).toBe('2026-05-03T00:00:00.000Z');
|
|
expect(snapshot.ranges).toHaveLength(3);
|
|
expect(snapshot.ranges[0]).toMatchObject({ startAt: '2026-05-01T00:00:00.000Z', endAt: '2026-05-01T06:00:00.000Z', mode: 'offline' });
|
|
expect(snapshot.ranges[1]).toMatchObject({ startAt: '2026-05-01T06:00:00.000Z', endAt: '2026-05-01T18:00:00.000Z', mode: 'do-not-disturb' });
|
|
expect(snapshot.ranges[2]).toMatchObject({ startAt: '2026-05-01T18:00:00.000Z', endAt: '2026-05-02T00:00:00.000Z', mode: 'online' });
|
|
});
|
|
|
|
it('excludes entries outside the 2-day window', () => {
|
|
const entries = [
|
|
{
|
|
id: 'e1', spaceId: 's', startAt: '2026-04-30T00:00:00.000Z', endAt: '2026-04-30T23:59:59.000Z',
|
|
activity: 'sleep', title: 'Sleep', granularity: 'day' as const, metadata: { availabilityMode: 'offline' },
|
|
},
|
|
{
|
|
id: 'e2', spaceId: 's', startAt: '2026-05-01T00:00:00.000Z', endAt: '2026-05-02T00:00:00.000Z',
|
|
activity: 'work', title: 'Work', granularity: 'day' as const, metadata: { availabilityMode: 'do-not-disturb' },
|
|
},
|
|
];
|
|
|
|
const snapshot = buildAvailabilitySnapshot({ now: '2026-05-01T12:00:00.000Z', entries });
|
|
expect(snapshot.ranges).toHaveLength(1);
|
|
expect(snapshot.ranges[0]!.startAt).toBe('2026-05-01T00:00:00.000Z');
|
|
});
|
|
});
|
|
|
|
describe('dateKeysAround', () => {
|
|
it('returns yesterday, today, and tomorrow as YYYY-MM-DD strings', () => {
|
|
const keys = dateKeysAround('2026-05-01T12:00:00.000Z');
|
|
expect(keys).toEqual(['2026-04-30', '2026-05-01', '2026-05-02']);
|
|
});
|
|
|
|
it('handles month boundaries correctly', () => {
|
|
const keys = dateKeysAround('2026-03-01T00:00:00.000Z');
|
|
expect(keys).toEqual(['2026-02-28', '2026-03-01', '2026-03-02']);
|
|
});
|
|
});
|