munin/source/test.ts
2025-07-01 17:36:23 +00:00

140 lines
2.8 KiB
TypeScript

/*
This file is part of »munin«.
Copyright 2025 'Fenris Wolf' <fenris@folksprak.org>
»munin« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»munin« 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with »munin«. If not, see <http://www.gnu.org/licenses/>.
*/
namespace _munin.test
{
/**
* @todo outsource?
*/
function lists_equal<type_element>(
list1 : Array<type_element>,
list2 : Array<type_element>
) : boolean
{
return (
(list1.length === list2.length)
&&
list1.every(
(element, index) => (element === list2[index])
)
);
}
/**
*/
async function reminder_check(
) : Promise<void>
{
type type_input = {
reminder : {
frequency : string;
offset : int;
from : int;
to : int;
};
events : Array<
{
title : string;
begin : lib_plankton.pit.type_datetime;
}
>;
datetime : lib_plankton.pit.type_datetime;
interval : int;
};
type type_output = (null | Array<string>);
const testcases : Array<
_munin.helpers.test.type_testcase<
type_input,
type_output
>
> = await _munin.helpers.test.get_data<type_input, type_output>(
"data/test/reminder_check.testdata.json",
{
}
);
for (const testcase of testcases)
{
_munin.helpers.test.start(testcase.name);
// execution
const result : (null | Array<_munin.type_event>) = _munin.logic.reminder_check(
{
"frequency": _munin.logic.frequency_decode(testcase.input.reminder.frequency),
"offset": testcase.input.reminder.offset,
"from": testcase.input.reminder.from,
"to": testcase.input.reminder.to,
},
testcase.input.events.map(
event_raw => ({
"title": event_raw.title,
"begin": event_raw.begin,
"end": null,
"description": null,
"location": null,
"tags": null,
})
),
{
"pit": lib_plankton.pit.from_datetime(testcase.input.datetime),
"interval": testcase.input.interval,
}
);
// assertions
if (
(
(testcase.output === null)
&&
(result === null)
)
||
(
(testcase.output !== null)
&&
lists_equal<string>(
result.map(event => event.title),
testcase.output
)
)
)
{
// success
}
else
{
_munin.helpers.test.fail(testcase.name);
}
}
}
/**
*/
export async function all(
) : Promise<void>
{
await reminder_check();
}
}