utxo-prague/utils/test.js

59 řádky
1.5 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
Deno.test(`UTXO.${entryId}: index.yaml`, () => {
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)) {
Deno.test(`UTXO.${entryId}: ${specId}`, () => {
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
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-03 13:44:08 +01:00
}
}