108 lines
2.8 KiB
TypeScript
108 lines
2.8 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest';
|
|
import { fetchCrabfitData, filterMemberData, findMeetingOptions } from '$lib/meeting';
|
|
|
|
// vi.mock('node-fetch', async () => ({
|
|
// default: vi.fn(() =>
|
|
// Promise.resolve({
|
|
// json: () => Promise.resolve(mockData)
|
|
// })
|
|
// )
|
|
// }));
|
|
const mockData = [
|
|
{
|
|
name: 'Alice',
|
|
availability: ['1100-1', '1115-1', '1130-1', '1100-2'],
|
|
created_at: 1000000000
|
|
},
|
|
{
|
|
name: 'Bob',
|
|
availability: ['1100-1', '1400-1', '1500-1'],
|
|
created_at: 1000000001
|
|
},
|
|
// Charlie has no times on Monday, but does on Tuesdays
|
|
{
|
|
name: 'Charlie',
|
|
availability: ['2000-1', '1100-2'],
|
|
created_at: 1000000002
|
|
}
|
|
];
|
|
|
|
const testData = [
|
|
{
|
|
name: 'B',
|
|
availability: ['0900-1', '0915-1', '0930-1', '0945-1', '1000-1', '1015-1', '1030-1', '1045-1'],
|
|
created_at: 1753176466
|
|
},
|
|
{
|
|
name: 'A',
|
|
availability: ['0800-1', '0815-1', '0830-1', '0845-1', '0900-1', '0915-1', '0930-1', '0945-1'],
|
|
created_at: 1753176450
|
|
}
|
|
];
|
|
|
|
const CRABFIT_TEST_API_URL = 'https://api.crab.fit/event/schedulertestdata-850919';
|
|
|
|
describe('findMeetingOptions', () => {
|
|
it('returns options correctly', async () => {
|
|
const options = await findMeetingOptions([mockData[0], mockData[1]], {
|
|
days: ['Monday'],
|
|
number: 1
|
|
});
|
|
expect(options).toEqual([{ 'Option 1': ['Monday', '1100'] }]);
|
|
});
|
|
|
|
it('returns options correctly', async () => {
|
|
const options = await findMeetingOptions([mockData[0], mockData[2]], {
|
|
days: ['Tuesday'],
|
|
number: 1
|
|
});
|
|
expect(options).toEqual([{ 'Option 1': ['Tuesday', '1100'] }]);
|
|
});
|
|
|
|
it('returns no options for impossible constraints', async () => {
|
|
const options = await findMeetingOptions([mockData[0], mockData[2]], {
|
|
days: ['Monday'],
|
|
number: 1
|
|
});
|
|
expect(options).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('filterMemberData', () => {
|
|
it('filters member data', async () => {
|
|
const members = ['A', 'B'];
|
|
const result = await filterMemberData(members, testData);
|
|
expect(result).toEqual(expect.arrayContaining(testData));
|
|
});
|
|
});
|
|
|
|
describe('fetchCrabfitData', () => {
|
|
it('gets member data', async () => {
|
|
expect(await fetchCrabfitData(CRABFIT_TEST_API_URL)).toEqual(testData);
|
|
});
|
|
});
|
|
|
|
describe('integration', () => {
|
|
it('finds meetings with fetched data', async () => {
|
|
const crabfitData = await fetchCrabfitData(CRABFIT_TEST_API_URL);
|
|
const members = ['A', 'B'];
|
|
const memberData = await filterMemberData(members, crabfitData);
|
|
|
|
const options = await findMeetingOptions(memberData, {
|
|
days: ['Monday'],
|
|
number: 1
|
|
});
|
|
expect(options).toEqual([{ 'Option 1': ['Monday', '0900'] }]);
|
|
});
|
|
it('finds meetings with test data', async () => {
|
|
const members = ['A', 'B'];
|
|
const memberData = await filterMemberData(members, testData);
|
|
|
|
const options = await findMeetingOptions(memberData, {
|
|
days: ['Monday'],
|
|
number: 1
|
|
});
|
|
expect(options).toEqual([{ 'Option 1': ['Monday', '0900'] }]);
|
|
});
|
|
});
|