munin/source/test.ts

140 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-06-27 22:07:18 +02:00
/*
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
{
2025-06-30 13:02:06 +02:00
2025-06-27 22:07:18 +02:00
/**
2025-06-30 13:02:06 +02:00
* @todo outsource?
2025-06-27 22:07:18 +02:00
*/
2025-06-30 13:02:06 +02:00
function lists_equal<type_element>(
list1 : Array<type_element>,
list2 : Array<type_element>
) : boolean
2025-06-27 22:07:18 +02:00
{
2025-06-30 13:02:06 +02:00
return (
(list1.length === list2.length)
&&
list1.every(
(element, index) => (element === list2[index])
)
2025-06-28 08:34:57 +02:00
);
2025-06-27 22:07:18 +02:00
}
2025-06-30 13:02:06 +02:00
2025-06-27 22:07:18 +02:00
/**
*/
2025-06-30 13:02:06 +02:00
async function reminder_check(
2025-06-28 08:34:57 +02:00
) : Promise<void>
2025-06-27 22:07:18 +02:00
{
2025-06-28 08:34:57 +02:00
type type_input = {
reminder : {
frequency : string;
offset : int;
from : int;
to : int;
};
2025-06-30 13:02:06 +02:00
events : Array<
{
title : string;
begin : lib_plankton.pit.type_datetime;
}
>;
2025-06-28 08:34:57 +02:00
datetime : lib_plankton.pit.type_datetime;
interval : int;
2025-06-27 22:07:18 +02:00
};
2025-06-30 13:02:06 +02:00
type type_output = (null | Array<string>);
2025-06-28 08:34:57 +02:00
const testcases : Array<
_munin.helpers.test.type_testcase<
type_input,
type_output
>
> = await _munin.helpers.test.get_data<type_input, type_output>(
2025-06-30 13:02:06 +02:00
"data/reminder_check.testdata.json",
2025-06-27 22:07:18 +02:00
{
2025-06-28 08:34:57 +02:00
}
);
2025-06-27 22:07:18 +02:00
for (const testcase of testcases)
{
2025-06-28 08:34:57 +02:00
_munin.helpers.test.start(testcase.name);
2025-06-27 22:07:18 +02:00
// execution
2025-06-30 13:02:06 +02:00
const result : (null | Array<_munin.type_event>) = _munin.logic.reminder_check(
2025-06-27 22:07:18 +02:00
{
2025-06-28 08:34:57 +02:00
"frequency": _munin.logic.frequency_decode(testcase.input.reminder.frequency),
2025-06-27 22:07:18 +02:00
"offset": testcase.input.reminder.offset,
"from": testcase.input.reminder.from,
"to": testcase.input.reminder.to,
},
2025-06-30 13:02:06 +02:00
testcase.input.events.map(
event_raw => ({
"title": event_raw.title,
"begin": event_raw.begin,
"end": null,
"description": null,
"location": null,
"tags": null,
})
),
2025-06-27 22:07:18 +02:00
{
2025-06-28 08:34:57 +02:00
"pit": lib_plankton.pit.from_datetime(testcase.input.datetime),
2025-06-30 13:02:06 +02:00
"interval": testcase.input.interval,
2025-06-27 22:07:18 +02:00
}
);
// assertions
2025-06-30 13:02:06 +02:00
if (
(
(testcase.output === null)
&&
(result === null)
)
||
(
(testcase.output !== null)
&&
lists_equal<string>(
result.map(event => event.title),
testcase.output
)
)
)
{
// success
}
else
2025-06-27 22:07:18 +02:00
{
2025-06-28 08:34:57 +02:00
_munin.helpers.test.fail(testcase.name);
2025-06-27 22:07:18 +02:00
}
}
}
2025-06-28 08:34:57 +02:00
2025-06-27 22:07:18 +02:00
/**
*/
2025-06-28 08:34:57 +02:00
export async function all(
) : Promise<void>
2025-06-27 22:07:18 +02:00
{
2025-06-30 13:02:06 +02:00
await reminder_check();
2025-06-27 22:07:18 +02:00
}
}