261 lines
8.5 KiB
TypeScript
261 lines
8.5 KiB
TypeScript
import assert from 'assert';
|
||
import { LocalDatabase } from '../src/services/db';
|
||
import { InsightService } from '../src/services/insight';
|
||
import { ScraperService } from '../src/services/scraper';
|
||
import { Assignment } from '../src/types';
|
||
|
||
async function runAllTests() {
|
||
console.log('========================================================');
|
||
console.log(' BeKultur - Automated Test Suite');
|
||
console.log('========================================================\n');
|
||
|
||
let passed = 0;
|
||
let failed = 0;
|
||
|
||
function test(name: string, fn: () => void | Promise<void>) {
|
||
return (async () => {
|
||
try {
|
||
await fn();
|
||
console.log(` ✓ ${name}`);
|
||
passed++;
|
||
} catch (err: any) {
|
||
console.error(` ✗ ${name}`);
|
||
console.error(` Error: ${err.message}\n`);
|
||
failed++;
|
||
}
|
||
})();
|
||
}
|
||
|
||
// 1. Date Parsing
|
||
await test('InsightService: parsePortalDate parses Turkish DD.MM.YYYY correctly', () => {
|
||
const d1 = InsightService.parsePortalDate('20.10.2026');
|
||
assert(d1 !== null, 'Date should not be null');
|
||
assert.strictEqual(d1.getFullYear(), 2026);
|
||
assert.strictEqual(d1.getMonth(), 9);
|
||
assert.strictEqual(d1.getDate(), 20);
|
||
|
||
const d2 = InsightService.parsePortalDate('8.11.2026');
|
||
assert(d2 !== null);
|
||
assert.strictEqual(d2.getMonth(), 10);
|
||
assert.strictEqual(d2.getDate(), 8);
|
||
|
||
const invalid = InsightService.parsePortalDate('');
|
||
assert.strictEqual(invalid, null);
|
||
});
|
||
|
||
// 2. Teacher Note Extraction
|
||
await test('InsightService: extractTeacherCheckNote detects various teacher notes', () => {
|
||
const n1 = InsightService.extractTeacherCheckNote('fenomen ayt 237-242 kontrol tarihi 26-30 ekim');
|
||
assert.strictEqual(n1?.toLowerCase(), '26-30 ekim');
|
||
|
||
const n2 = InsightService.extractTeacherCheckNote('3D TYT sf. 99-118 + 3D AYT sf. 123-130 (kontrol tarihi 27 Ekim)');
|
||
assert.strictEqual(n2?.toLowerCase(), '27 ekim');
|
||
|
||
const n3 = InsightService.extractTeacherCheckNote('12 MF:X YAYINCILIK (ÖDEVLER 26-30 EKİM ARASINDA KONTROL EDİLECEKTİR.)');
|
||
assert.strictEqual(n3?.toLowerCase(), '26-30 ekim');
|
||
|
||
const n4 = InsightService.extractTeacherCheckNote('AYT 1. KİTAP [26-56] ÇÖZÜLECEKTİR. 26 10 KONTROL');
|
||
assert.strictEqual(n4?.toLowerCase(), '26 ekim');
|
||
|
||
const nNone = InsightService.extractTeacherCheckNote('Toprak Yayınları Bölüm 4 çözülecek.');
|
||
assert.strictEqual(nNone, null);
|
||
});
|
||
|
||
// 3. Status Computation
|
||
await test('InsightService: computeStatus correctly handles completed, next-week, and overdue', () => {
|
||
const refDate = new Date(2026, 9, 21);
|
||
|
||
const s1 = InsightService.computeStatus({
|
||
id: 'test1',
|
||
subject: 'Matematik',
|
||
type: 'Yayın',
|
||
unit: '',
|
||
topic: '',
|
||
startDate: '10.10.2026',
|
||
endDate: '25.10.2026',
|
||
parsedStartDate: '2026-10-10',
|
||
parsedEndDate: '2026-10-25',
|
||
description: 'Test',
|
||
detectedCheckDate: null,
|
||
completed: true,
|
||
completedAt: '2026-10-21T10:00:00Z'
|
||
}, refDate);
|
||
assert.strictEqual(s1, 'completed');
|
||
|
||
const s2 = InsightService.computeStatus({
|
||
id: 'test2',
|
||
subject: 'Biyoloji',
|
||
type: 'Yayın',
|
||
unit: '',
|
||
topic: '',
|
||
startDate: '01.10.2026',
|
||
endDate: '19.10.2026',
|
||
parsedStartDate: '2026-10-01',
|
||
parsedEndDate: '2026-10-19',
|
||
description: 'Old test',
|
||
detectedCheckDate: null,
|
||
completed: false,
|
||
completedAt: null
|
||
}, refDate);
|
||
assert.strictEqual(s2, 'overdue');
|
||
|
||
const s3 = InsightService.computeStatus({
|
||
id: 'test3',
|
||
subject: 'AYT Kimya',
|
||
type: 'Yayın',
|
||
unit: '',
|
||
topic: '',
|
||
startDate: '20.10.2026',
|
||
endDate: '26.10.2026',
|
||
parsedStartDate: '2026-10-20',
|
||
parsedEndDate: '2026-10-26',
|
||
description: 'ayt 237-242 kontrol tarihi 26-30 ekim',
|
||
detectedCheckDate: '26-30 ekim',
|
||
completed: false,
|
||
completedAt: null
|
||
}, refDate);
|
||
assert.strictEqual(s3, 'next-week');
|
||
});
|
||
|
||
// 4. Statistics Calculation
|
||
await test('InsightService: calculateStats computes accurate completion rates and subject breakdown', () => {
|
||
const sampleAssignments: Assignment[] = [
|
||
{
|
||
id: '1',
|
||
subject: 'Kimya',
|
||
type: 'Yayın',
|
||
unit: '',
|
||
topic: '',
|
||
startDate: '20.10.2026',
|
||
endDate: '26.10.2026',
|
||
parsedStartDate: '2026-10-20',
|
||
parsedEndDate: '2026-10-26',
|
||
description: 'Test 1',
|
||
detectedCheckDate: null,
|
||
status: 'next-week',
|
||
completed: false,
|
||
completedAt: null
|
||
},
|
||
{
|
||
id: '2',
|
||
subject: 'Kimya',
|
||
type: 'Yayın',
|
||
unit: '',
|
||
topic: '',
|
||
startDate: '10.10.2026',
|
||
endDate: '15.10.2026',
|
||
parsedStartDate: '2026-10-10',
|
||
parsedEndDate: '2026-10-15',
|
||
description: 'Test 2',
|
||
detectedCheckDate: null,
|
||
status: 'completed',
|
||
completed: true,
|
||
completedAt: '2026-10-21T10:00:00Z'
|
||
},
|
||
{
|
||
id: '3',
|
||
subject: 'Fizik',
|
||
type: 'Yayın',
|
||
unit: '',
|
||
topic: '',
|
||
startDate: '01.10.2026',
|
||
endDate: '10.10.2026',
|
||
parsedStartDate: '2026-10-01',
|
||
parsedEndDate: '2026-10-10',
|
||
description: 'Test 3',
|
||
detectedCheckDate: null,
|
||
status: 'overdue',
|
||
completed: false,
|
||
completedAt: null
|
||
}
|
||
];
|
||
|
||
const stats = InsightService.calculateStats(sampleAssignments);
|
||
assert.strictEqual(stats.total, 3);
|
||
assert.strictEqual(stats.completed, 1);
|
||
assert.strictEqual(stats.checkedNextWeek, 1);
|
||
assert.strictEqual(stats.overdue, 1);
|
||
assert.strictEqual(stats.completionRate, 33);
|
||
assert.strictEqual(stats.bySubject['Kimya'].total, 2);
|
||
assert.strictEqual(stats.bySubject['Kimya'].completed, 1);
|
||
assert.strictEqual(stats.bySubject['Kimya'].percentage, 50);
|
||
});
|
||
|
||
// 5. Local Database
|
||
await test('LocalDatabase: save, merge, and toggle checkboxes in db.json', () => {
|
||
const db = LocalDatabase.getInstance();
|
||
const testStudentId = `test_student_${Date.now()}`;
|
||
|
||
const testItem: Assignment = {
|
||
id: `hash_${Date.now()}`,
|
||
subject: 'Matematik',
|
||
type: 'Yayın',
|
||
unit: 'Fonksiyonlar',
|
||
topic: 'Test',
|
||
startDate: '20.10.2026',
|
||
endDate: '26.10.2026',
|
||
parsedStartDate: '2026-10-20',
|
||
parsedEndDate: '2026-10-26',
|
||
description: 'Sayfa 50-60',
|
||
detectedCheckDate: null,
|
||
status: 'active',
|
||
completed: false,
|
||
completedAt: null
|
||
};
|
||
|
||
db.mergeAssignments(testStudentId, [testItem]);
|
||
let store = db.getStudentStore(testStudentId);
|
||
assert(store.assignments[testItem.id]);
|
||
assert.strictEqual(store.assignments[testItem.id].completed, false);
|
||
|
||
const updated = db.toggleAssignment(testStudentId, testItem.id, true);
|
||
assert.strictEqual(updated?.completed, true);
|
||
assert(updated?.completedAt !== null);
|
||
|
||
db.mergeAssignments(testStudentId, [{
|
||
...testItem,
|
||
completed: false
|
||
}]);
|
||
store = db.getStudentStore(testStudentId);
|
||
assert.strictEqual(
|
||
store.assignments[testItem.id].completed,
|
||
true,
|
||
'mergeAssignments MUST preserve user checked status!'
|
||
);
|
||
|
||
db.saveCredentials(testStudentId, 'test_pass');
|
||
const creds = db.getRememberedCredentials(testStudentId);
|
||
assert.strictEqual(creds?.password, 'test_pass');
|
||
db.clearCredentials(testStudentId);
|
||
assert.strictEqual(db.getRememberedCredentials(testStudentId), null);
|
||
});
|
||
|
||
// 6. Optional Live Portal Test (via environment variables only)
|
||
const testStudentId = process.env.TEST_STUDENT_ID;
|
||
const testPassword = process.env.TEST_PASSWORD;
|
||
|
||
if (testStudentId && testPassword) {
|
||
await test('ScraperService: Live portal authentication and scraping', async () => {
|
||
const loginRes = await ScraperService.login(testStudentId, testPassword);
|
||
assert.strictEqual(loginRes.success, true, `Login failed: ${loginRes.error}`);
|
||
|
||
const scrapeRes = await ScraperService.scrapePage(loginRes.cookies, 1, testStudentId);
|
||
assert(scrapeRes.assignments.length > 0, 'Page 1 must contain homework cards');
|
||
});
|
||
} else {
|
||
console.log(' - Live portal test skipped (set TEST_STUDENT_ID and TEST_PASSWORD to run)');
|
||
}
|
||
|
||
console.log('\n--------------------------------------------------------');
|
||
console.log(`Test Results: ${passed} passed, ${failed} failed`);
|
||
console.log('--------------------------------------------------------\n');
|
||
|
||
if (failed > 0) {
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
runAllTests().catch(err => {
|
||
console.error('Fatal test error:', err);
|
||
process.exit(1);
|
||
});
|