116 lines
3.5 KiB
TypeScript
116 lines
3.5 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { performance } from 'node:perf_hooks';
|
|
import test from 'node:test';
|
|
|
|
import {
|
|
buildSuggestions,
|
|
type ClassOption,
|
|
normalizeExport,
|
|
type PlannerWeights,
|
|
} from './planner.ts';
|
|
|
|
const weights: PlannerWeights = {
|
|
compact: 'زیاد',
|
|
fewerDays: 'متوسط',
|
|
location: 'متوسط',
|
|
capacity: 'متوسط',
|
|
};
|
|
|
|
function option(
|
|
id: string,
|
|
lesson: string,
|
|
teacher: string,
|
|
start: number,
|
|
end: number,
|
|
): ClassOption {
|
|
return {
|
|
id,
|
|
lesson,
|
|
teacher,
|
|
semester: 'نیمسال اول',
|
|
hours: 20,
|
|
capacity: 15,
|
|
availability: 'قابل انتخاب',
|
|
location: 'مکان ساختگی',
|
|
classTitle: lesson,
|
|
scope: '',
|
|
meetings: [{ day: 'شنبه', start, end, label: 'زمان ساختگی' }],
|
|
};
|
|
}
|
|
|
|
void test('normalizes scraper JSON and Persian digits', () => {
|
|
const classes = normalizeExport({
|
|
classes: [{
|
|
lesson: 'درس ساختگی',
|
|
class_title: 'درس ساختگی: نیمسال اول',
|
|
hours: '۲۰',
|
|
teacher: 'استاد ساختگی',
|
|
remaining_capacity: '۱۵',
|
|
days: ['شنبه'],
|
|
time: '08:00-09:00',
|
|
}],
|
|
});
|
|
|
|
assert.equal(classes.length, 1);
|
|
assert.equal(classes[0].hours, 20);
|
|
assert.equal(classes[0].capacity, 15);
|
|
assert.equal(classes[0].meetings[0].start, 480);
|
|
});
|
|
|
|
void test('honors teacher locks and rejects time conflicts', () => {
|
|
const classes = [
|
|
option('lesson-1-a', 'درس یک', 'استاد الف', 480, 540),
|
|
option('lesson-1-b', 'درس یک', 'استاد ب', 540, 600),
|
|
option('lesson-2-c', 'درس دو', 'استاد ج', 510, 570),
|
|
option('lesson-2-d', 'درس دو', 'استاد د', 600, 660),
|
|
];
|
|
|
|
const plans = buildSuggestions(
|
|
classes,
|
|
[{ id: 'required', lesson: 'درس یک', teacher: 'استاد الف' }],
|
|
[40, 40],
|
|
weights,
|
|
);
|
|
|
|
assert.ok(plans.length > 0);
|
|
for (const plan of plans) {
|
|
assert.ok(plan.classes.some((item) => item.id === 'lesson-1-a'));
|
|
assert.ok(plan.classes.some((item) => item.id === 'lesson-2-d'));
|
|
assert.ok(!plan.classes.some((item) => item.id === 'lesson-2-c'));
|
|
}
|
|
});
|
|
|
|
void test('keeps a locked teacher even when a lesson has many choices', () => {
|
|
const crowdedLesson = Array.from({ length: 80 }, (_, index) =>
|
|
option(`crowded-${index}`, 'درس پرتعداد', `استاد ${index}`, 480 + index, 540 + index));
|
|
const required = crowdedLesson.at(-1)!;
|
|
const plans = buildSuggestions(
|
|
[...crowdedLesson, option('second', 'درس دوم', 'استاد دوم', 700, 760)],
|
|
[{ id: 'required', lesson: required.lesson, teacher: required.teacher }],
|
|
[40, 40],
|
|
weights,
|
|
);
|
|
|
|
assert.ok(plans.length > 0);
|
|
assert.ok(plans.every((plan) => plan.classes.some((item) => item.id === required.id)));
|
|
});
|
|
|
|
void test('handles a multi-thousand-class export without an exhaustive expansion', () => {
|
|
const classes = Array.from({ length: 38 }, (_, lessonIndex) =>
|
|
Array.from({ length: 60 }, (_, choiceIndex): ClassOption => ({
|
|
...option(`large-${lessonIndex}-${choiceIndex}`, `درس ${lessonIndex}`, `استاد ${choiceIndex}`, 0, 0),
|
|
hours: 20,
|
|
capacity: 5 + (choiceIndex % 30),
|
|
location: `مکان ${choiceIndex % 8}`,
|
|
meetings: [],
|
|
})),
|
|
).flat();
|
|
|
|
const startedAt = performance.now();
|
|
const plans = buildSuggestions(classes, [], [500, 600], weights);
|
|
const elapsed = performance.now() - startedAt;
|
|
|
|
assert.ok(plans.length > 0);
|
|
assert.ok(elapsed < 1500, `large dataset took ${elapsed.toFixed(0)}ms`);
|
|
});
|