From 9032ee2ea2ba1a737a80a37f4645a498bc794e41 Mon Sep 17 00:00:00 2001 From: Script Raccoon Date: Fri, 26 Jun 2026 22:59:16 +0200 Subject: [PATCH] check that link targets exist --- databases/catdat/scripts/test.ts | 93 +++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/databases/catdat/scripts/test.ts b/databases/catdat/scripts/test.ts index 00dd440f..ae5a9484 100644 --- a/databases/catdat/scripts/test.ts +++ b/databases/catdat/scripts/test.ts @@ -13,7 +13,10 @@ import decided_categories from './expected-data/decided-categories.json' import decided_functors from './expected-data/decided-functors.json' import { capitalize } from './utils/helpers' import { get_client } from './utils/db' -import { PLURALS, StructureType } from './config' +import { PLURALS, STRUCTURES, type StructureType } from './config' +import fs from 'node:fs' +import path from 'node:path' +import { decode_property_ID } from '../../../src/lib/commons/property.url' const db = get_client() @@ -24,6 +27,9 @@ execute_tests() */ function execute_tests() { try { + console.info('--- Test link targets ---') + check_link_targets_exist() + console.info('\n--- Test categories ---') test_mutual_structure_duals('category') test_positivity('1', 'category') @@ -188,3 +194,88 @@ function test_properties_of_selected_structures( console.info(`✅ Properties of ${structure_id} are correct`) } } + +/** + * Checks if all links in proofs refer to existing entries. + */ +function check_link_targets_exist() { + const content_ids = new Set( + fs + .readdirSync(path.resolve('content')) + .filter((file) => file.endsWith('.md')) + .map((file) => path.basename(file, '.md')), + ) + + const proofs: { proof: string; error_prefix: string }[] = [ + ...db + + .prepare( + `SELECT + structure_id AS structure, + property_id AS property, + proof + FROM property_assignments`, + ) + .all() + .map((x) => ({ + proof: x.proof, + error_prefix: `❌ The proof of (${x.structure}, ${x.property})`, + })), + ...db + .prepare( + `SELECT id, proof FROM implications`, + ) + .all() + .map((x) => ({ + proof: x.proof, + error_prefix: `❌ The proof of the implication (${x.id})`, + })), + ] + + for (const { proof, error_prefix } of proofs) { + const link_regex = new RegExp( + `(`SELECT id FROM ${sort} WHERE id = ? AND type = ?`) + .get(decoded_id, type) + + if (!exists) { + throw new Error( + `${error_prefix} has a link to "${id}" which does not exist:\n"${proof}"`, + ) + } + } + + const content_link_regex = /