114 lines
4.9 KiB
TypeScript
114 lines
4.9 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { InMemoryMemoryStore, Persona } from '../src';
|
|
|
|
describe('Persona schedules and availability', () => {
|
|
it('creates tomorrow as a ten-minute daily schedule and persists it in memory', async () => {
|
|
const memory = new InMemoryMemoryStore();
|
|
const persona = new Persona('Mina', 'Mina works weekdays and studies at night.', {
|
|
memory,
|
|
now: '2026-05-01T10:00:00.000Z',
|
|
models: {
|
|
schedule: {
|
|
async generateDailySchedule() {
|
|
return [
|
|
{ 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 },
|
|
{ startTime: '09:00', endTime: '18:00', activity: 'deep work', title: 'Deep work', availabilityMode: 'do-not-disturb' as const },
|
|
{ startTime: '18:00', endTime: '24:00', activity: 'free time', title: 'Free time', availabilityMode: 'online' as const },
|
|
];
|
|
},
|
|
async generateMonthlySchedule() {
|
|
return [];
|
|
},
|
|
},
|
|
},
|
|
});
|
|
const space = await persona.ready();
|
|
|
|
const entries = await persona.createDailySchedule('2026-05-01T10:00:00.000Z', 'Keep a normal work day.');
|
|
|
|
expect(entries).toHaveLength(4);
|
|
expect(entries[0]).toMatchObject({
|
|
spaceId: space.id,
|
|
startAt: '2026-05-02T00:00:00.000Z',
|
|
endAt: '2026-05-02T07:00:00.000Z',
|
|
granularity: 'ten-minute',
|
|
activity: 'sleep',
|
|
});
|
|
expect(entries.at(-1)?.endAt).toBe('2026-05-03T00:00:00.000Z');
|
|
await expect(
|
|
memory.listScheduleEntries(space.id, '2026-05-02T00:00:00.000Z', '2026-05-03T00:00:00.000Z'),
|
|
).resolves.toHaveLength(4);
|
|
});
|
|
|
|
it('derives online, do-not-disturb, and offline availability from the in-memory schedule window', async () => {
|
|
const memory = new InMemoryMemoryStore();
|
|
const persona = new Persona('Mina', 'Mina works weekdays and studies at night.', {
|
|
memory,
|
|
now: '2026-05-01T10:00:00.000Z',
|
|
models: {
|
|
schedule: {
|
|
async generateDailySchedule() {
|
|
return [
|
|
{ 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 },
|
|
{ startTime: '09:00', endTime: '18:00', activity: 'deep work', title: 'Deep work', availabilityMode: 'do-not-disturb' as const },
|
|
];
|
|
},
|
|
async generateMonthlySchedule() {
|
|
return [];
|
|
},
|
|
},
|
|
},
|
|
});
|
|
await persona.ready();
|
|
|
|
await persona.createDailySchedule('2026-05-01T10:00:00.000Z', 'Keep a normal work day.');
|
|
const availability = await persona.getTodayScheduledAvailability('2026-05-01T12:00:00.000Z');
|
|
|
|
expect(availability.windowStartAt).toBe('2026-05-01T00:00:00.000Z');
|
|
expect(availability.windowEndAt).toBe('2026-05-03T00:00:00.000Z');
|
|
expect(new Set(availability.ranges.map((range) => range.mode))).toEqual(
|
|
new Set(['offline', 'online', 'do-not-disturb']),
|
|
);
|
|
expect(availability.ranges.find((range) => range.mode === 'offline')?.startAt).toBe(
|
|
'2026-05-02T00:00:00.000Z',
|
|
);
|
|
});
|
|
|
|
it('prunes schedule entries before a caller-provided cutoff', async () => {
|
|
const memory = new InMemoryMemoryStore();
|
|
const persona = new Persona('Mina', 'Mina works weekdays.', {
|
|
memory,
|
|
now: '2026-05-01T10:00:00.000Z',
|
|
models: {
|
|
schedule: {
|
|
async generateDailySchedule() {
|
|
return [
|
|
{ startTime: '00:00', endTime: '06:00', activity: 'sleep', title: 'Sleep', availabilityMode: 'offline' as const },
|
|
{ startTime: '06:00', endTime: '12:00', activity: 'work', title: 'Work', availabilityMode: 'do-not-disturb' as const },
|
|
{ startTime: '12:00', endTime: '18:00', activity: 'study', title: 'Study', availabilityMode: 'do-not-disturb' as const },
|
|
{ startTime: '18:00', endTime: '24:00', activity: 'rest', title: 'Rest', availabilityMode: 'online' as const },
|
|
];
|
|
},
|
|
async generateMonthlySchedule() {
|
|
return [];
|
|
},
|
|
},
|
|
},
|
|
});
|
|
const space = await persona.ready();
|
|
await persona.createDailySchedule('2026-05-01T10:00:00.000Z', 'Keep a normal work day.');
|
|
|
|
const deleted = await persona.deleteSchedulesBefore('2026-05-02T12:00:00.000Z');
|
|
|
|
expect(deleted).toBe(2);
|
|
await expect(
|
|
memory.listScheduleEntries(space.id, '2026-05-02T00:00:00.000Z', '2026-05-03T00:00:00.000Z'),
|
|
).resolves.toHaveLength(2);
|
|
const deletionFacts = await memory.findFacts(space.id, ['persona.schedule.deleted']);
|
|
expect(deletionFacts[0]?.metadata?.['deleted']).toBe(2);
|
|
});
|
|
|
|
});
|