refactor: Rewrite to typescript
All checks were successful
Remote Deploy / deploy (push) Successful in 14s
All checks were successful
Remote Deploy / deploy (push) Successful in 14s
This commit is contained in:
310
tests/test.ts
Normal file
310
tests/test.ts
Normal file
@@ -0,0 +1,310 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Jakub Žitník
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
import fs from "fs";
|
||||
import parseAbsence from "../scrape/utils/parseAbsence.js";
|
||||
|
||||
const teachermap: Record<string, string> = JSON.parse(fs.readFileSync("./tests/teachermap.json", "utf8"));
|
||||
|
||||
test("Me", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null,
|
||||
},
|
||||
]);
|
||||
|
||||
test("ad", [
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "wholeDay",
|
||||
hours: null,
|
||||
},
|
||||
]);
|
||||
|
||||
test("me ad", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null,
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "wholeDay",
|
||||
hours: null,
|
||||
},
|
||||
]);
|
||||
|
||||
test("me 3", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "single",
|
||||
hours: 3,
|
||||
},
|
||||
]);
|
||||
|
||||
test("ad 1", [
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "single",
|
||||
hours: 1,
|
||||
},
|
||||
]);
|
||||
|
||||
test("me 2-4", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "range",
|
||||
hours: { from: 2, to: 4 },
|
||||
},
|
||||
]);
|
||||
|
||||
test("ad 5,6", [
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "range",
|
||||
hours: { from: 5, to: 6 },
|
||||
},
|
||||
]);
|
||||
|
||||
test("me 7+", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "range",
|
||||
hours: { from: 7, to: 10 },
|
||||
},
|
||||
]);
|
||||
|
||||
test("me,ad 3-5", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "range",
|
||||
hours: { from: 3, to: 5 },
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "range",
|
||||
hours: { from: 3, to: 5 },
|
||||
},
|
||||
]);
|
||||
|
||||
test("me;ad 4", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null,
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "single",
|
||||
hours: 4,
|
||||
},
|
||||
]);
|
||||
|
||||
test("me;ad;bo 2-3", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null,
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "wholeDay",
|
||||
hours: null,
|
||||
},
|
||||
{
|
||||
teacher: "Ing. Anna Bodnárová",
|
||||
teacherCode: "bo",
|
||||
type: "range",
|
||||
hours: { from: 2, to: 3 },
|
||||
},
|
||||
]);
|
||||
|
||||
test("me 2 ad 4-5", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "single",
|
||||
hours: 2,
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "range",
|
||||
hours: { from: 4, to: 5 },
|
||||
},
|
||||
]);
|
||||
|
||||
test("3", [
|
||||
{
|
||||
type: "invalid",
|
||||
teacher: null,
|
||||
teacherCode: null,
|
||||
hours: null,
|
||||
original: "3",
|
||||
},
|
||||
]);
|
||||
|
||||
test("2-4", [
|
||||
{
|
||||
type: "invalid",
|
||||
teacher: null,
|
||||
teacherCode: null,
|
||||
hours: null,
|
||||
original: "2-4",
|
||||
},
|
||||
]);
|
||||
|
||||
test("me Xx 3", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null,
|
||||
},
|
||||
{ teacher: null, teacherCode: "xx", type: "single", hours: 3 },
|
||||
]);
|
||||
|
||||
test("me,ad;bo 1-2 ad 5+", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "wholeDay",
|
||||
hours: null,
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "wholeDay",
|
||||
hours: null,
|
||||
},
|
||||
{
|
||||
teacher: "Ing. Anna Bodnárová",
|
||||
teacherCode: "bo",
|
||||
type: "range",
|
||||
hours: { from: 1, to: 2 },
|
||||
},
|
||||
{
|
||||
teacher: "Bc. Daniel Adámek",
|
||||
teacherCode: "ad",
|
||||
type: "range",
|
||||
hours: { from: 5, to: 10 },
|
||||
},
|
||||
]);
|
||||
|
||||
test("Me-exk", [
|
||||
{
|
||||
teacher: "Michaela Meitnerová",
|
||||
teacherCode: "me",
|
||||
type: "exkurze",
|
||||
hours: null,
|
||||
},
|
||||
]);
|
||||
|
||||
test("za Vn zastupuje Jk", [
|
||||
{
|
||||
teacher: "Ing. Zdeněk Vondra",
|
||||
teacherCode: "vn",
|
||||
type: "zastoupen",
|
||||
hours: null,
|
||||
zastupuje: {
|
||||
teacher: "David Janoušek",
|
||||
teacherCode: "jk",
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
test("Vc 1. h", [
|
||||
{
|
||||
teacher: "Ing. Antonín Vobecký",
|
||||
teacherCode: "vc",
|
||||
type: "single",
|
||||
hours: 1,
|
||||
},
|
||||
]);
|
||||
|
||||
test("Sv-5-exk", [
|
||||
{
|
||||
teacher: "Ing. Jan Šváb",
|
||||
teacherCode: "sv",
|
||||
type: "exkurze",
|
||||
hours: { from: 5, to: 10 },
|
||||
},
|
||||
]);
|
||||
|
||||
function test(input: string, expectedOutput: any[]) {
|
||||
const res = parseAbsence(input, teachermap);
|
||||
|
||||
if (!deepEqual(res, expectedOutput)) {
|
||||
console.error("ERROR for input: " + input);
|
||||
console.log(JSON.stringify(res, null, 2));
|
||||
console.log(JSON.stringify(expectedOutput, null, 2));
|
||||
console.log();
|
||||
}
|
||||
}
|
||||
|
||||
function deepEqual(a: any, b: any): boolean {
|
||||
if (a === b) return true;
|
||||
|
||||
if (
|
||||
typeof a !== "object" ||
|
||||
a === null ||
|
||||
typeof b !== "object" ||
|
||||
b === null
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle arrays (ignore order)
|
||||
if (Array.isArray(a) && Array.isArray(b)) {
|
||||
if (a.length !== b.length) return false;
|
||||
|
||||
const used = new Array(b.length).fill(false);
|
||||
|
||||
return a.every((itemA) =>
|
||||
b.some((itemB, i) => {
|
||||
if (used[i]) return false; // don't reuse elements
|
||||
if (deepEqual(itemA, itemB)) {
|
||||
used[i] = true; // mark element as used
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
||||
|
||||
const keysA = Object.keys(a);
|
||||
const keysB = Object.keys(b);
|
||||
|
||||
if (keysA.length !== keysB.length) return false;
|
||||
|
||||
return keysA.every((key) => keysB.includes(key) && deepEqual(a[key], b[key]));
|
||||
}
|
||||
Reference in New Issue
Block a user