utxo-prague/utils/test.js

127 řádky
3.8 KiB
JavaScript
Surový Normální zobrazení Historie

2022-01-14 17:56:42 +01:00
import { assertEquals } from "https://deno.land/std@0.119.0/testing/asserts.ts";
import { UTXOEngine } from "./engine.js";
2022-01-03 13:44:08 +01:00
// initialize ajv JSON Schema validator
2022-01-14 17:56:42 +01:00
import Ajv from "https://esm.sh/ajv@8.8.1?pin=v58";
import addFormats from "https://esm.sh/ajv-formats@2.1.1";
2022-01-03 20:30:13 +01:00
2022-01-14 17:56:42 +01:00
const ajv = new Ajv();
addFormats(ajv);
2022-01-03 13:44:08 +01:00
2022-01-14 17:56:42 +01:00
const utxo = new UTXOEngine({ silent: true });
await utxo.init();
const schemas = await utxo.schemas();
2022-01-03 20:24:19 +01:00
2022-01-14 17:56:42 +01:00
const validators = {};
2022-01-03 13:44:08 +01:00
for (const item of schemas) {
2022-01-14 17:56:42 +01:00
validators[item.name] = ajv.compile(item.schema);
2022-01-03 13:44:08 +01:00
}
// check entries
for (const entryId of utxo.entriesList()) {
2022-01-14 17:56:42 +01:00
const entry = utxo.entries[entryId];
2022-01-03 13:44:08 +01:00
// check index
2022-01-27 08:56:34 +01:00
Deno.test(`UTXO.${entryId}: index[schema]`, () => {
2022-01-03 13:44:08 +01:00
if (!validators.index(entry.index)) {
2022-01-14 17:56:42 +01:00
throw validators.index.errors;
2022-01-03 13:44:08 +01:00
}
2022-01-14 17:56:42 +01:00
});
2022-01-03 13:44:08 +01:00
// check specific specs
for (const specId of Object.keys(entry.specs)) {
2022-01-27 08:56:34 +01:00
Deno.test(`UTXO.${entryId}: ${specId}[schema]`, () => {
2022-01-03 13:44:08 +01:00
if (!validators[specId]) {
2022-01-14 17:56:42 +01:00
return null;
2022-01-03 13:44:08 +01:00
}
if (!validators[specId](entry.specs[specId])) {
2022-01-14 17:56:42 +01:00
throw validators[specId].errors;
2022-01-03 13:44:08 +01:00
}
2022-01-14 17:56:42 +01:00
});
2022-01-19 08:18:15 +01:00
2022-02-08 09:25:11 +01:00
if (!["team"].includes(specId)) {
Deno.test(`UTXO.${entryId}: ${specId}[id-duplicates]`, () => {
const used = [];
for (const item of entry.specs[specId]) {
if (!item.id) {
return null;
}
if (used.includes(item.id)) {
throw `Duplicate key: ${item.id}`;
}
used.push(item.id);
2022-01-27 08:56:34 +01:00
}
2022-02-08 09:25:11 +01:00
});
}
2022-01-27 08:56:34 +01:00
2022-01-19 08:18:15 +01:00
if (["speakers", "projects"].includes(specId)) {
Deno.test(`UTXO.${entryId}: ${specId}[tracks-links]`, () => {
const tracks = entry.specs.tracks.map((t) => t.id);
for (const item of entry.specs[specId]) {
if (!item.tracks) {
continue;
}
for (const t of item.tracks) {
if (!tracks.includes(t)) {
throw new Error(`Track not exists: ${t}`);
}
}
}
});
}
2022-01-27 08:56:34 +01:00
if (["events"].includes(specId)) {
Deno.test(`UTXO.${entryId}: ${specId}[speakers-links]`, () => {
const speakers = entry.specs.speakers.map((t) => t.id);
for (const item of entry.specs[specId]) {
if (!item.speakers || item.speakers.length === 0) {
continue;
}
for (const t of item.speakers) {
if (!speakers.includes(t)) {
throw new Error(`Speaker not exists: ${t}`);
}
}
}
});
}
2022-02-08 09:25:11 +01:00
if (["team"].includes(specId)) {
Deno.test(`UTXO.${entryId}: ${specId}[persons-links]`, () => {
const persons = Object.keys(entry.specs.team.persons);
for (const teamId of Object.keys(entry.specs[specId].teams)) {
const team = entry.specs[specId].teams[teamId];
2022-02-09 09:59:52 +01:00
if (
team.parent &&
!Object.keys(entry.specs.team.teams).includes(team.parent)
) {
throw new Error(`Parent not found: ${team.parent}`);
}
2022-02-08 09:25:11 +01:00
if (team.lead && !persons.includes(team.lead)) {
throw new Error(`Lead not found: ${team.lead}`);
}
if (!team.members || team.members.length === 0) {
continue;
}
for (const m of team.members) {
if (!persons.includes(m)) {
throw new Error(`Person not exists: ${m}`);
}
}
}
});
}
2022-01-27 08:56:34 +01:00
if (["events"].includes(specId)) {
Deno.test(`UTXO.${entryId}: ${specId}[speakers-tracks]`, () => {
const tracks = entry.specs.tracks.map((t) => t.id);
for (const item of entry.specs[specId]) {
if (!item.track) {
continue;
}
if (!tracks.includes(item.track)) {
throw new Error(`Track not exists: ${item.track}`);
}
}
});
}
2022-01-03 13:44:08 +01:00
}
}