« get me outta code hell

test, urls: get content-function unit test infra working, probably - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-10-15 14:52:26 -0300
committer(quasar) nebula <qznebula@protonmail.com>2025-10-15 14:52:49 -0300
commitda27189f09c185089cd77b8ea5199b66a6c031c0 (patch)
tree110aa0cfa1bdfe65c0932abfb7a8e3f77e4e673a
parent3cb93cd6f21c2df1c79107b347e501d12b451fe2 (diff)
test, urls: get content-function unit test infra working, probably
-rw-r--r--src/url-spec.js19
-rw-r--r--test/lib/content-function.js30
2 files changed, 42 insertions, 7 deletions
diff --git a/src/url-spec.js b/src/url-spec.js
index 75cd8006..2e8b9fc1 100644
--- a/src/url-spec.js
+++ b/src/url-spec.js
@@ -1,6 +1,7 @@
 // Exports defined here are re-exported through urls.js,
 // so they're generally imported from '#urls'.
 
+import {readFileSync} from 'node:fs';
 import {readFile} from 'node:fs/promises';
 import * as path from 'node:path';
 import {fileURLToPath} from 'node:url';
@@ -195,6 +196,24 @@ export async function processURLSpecFromFile(file) {
       error => annotateErrorWithFile(error, file));
   }
 
+  return processURLSpecFromFileContents(file, contents);
+}
+
+export function processURLSpecFromFileSync(file) {
+  let contents;
+
+  try {
+    contents = readFileSync(file, 'utf-8');
+  } catch (caughtError) {
+    throw annotateError(
+      new Error(`Failed to read URL spec file`, {cause: caughtError}),
+      error => annotateErrorWithFile(error, file));
+  }
+
+  return processURLSpecFromFileContents(file, contents);
+}
+
+function processURLSpecFromFileContents(file, contents) {
   let sourceSpec;
   let parseLanguage;
 
diff --git a/test/lib/content-function.js b/test/lib/content-function.js
index a46d18c9..49fe5c95 100644
--- a/test/lib/content-function.js
+++ b/test/lib/content-function.js
@@ -11,16 +11,21 @@ import {quickEvaluate} from '#content-function';
 import * as html from '#html';
 import {internalDefaultStringsFile, processLanguageFile} from '#language';
 import {empty} from '#sugar';
-import {generateURLs, thumb, urlSpec} from '#urls';
+
+import {
+  applyLocalizedWithBaseDirectory,
+  generateURLs,
+  internalDefaultURLSpecFile,
+  processURLSpecFromFileSync,
+  thumb,
+} from '#urls';
 
 import mock from './generic-mock.js';
 
 const __dirname = path.dirname(fileURLToPath(import.meta.url));
 
-function cleanURLSpec(reference) {
-  const prepared = structuredClone(reference);
-
-  for (const spec of Object.values(prepared)) {
+function cleanURLSpec(urlSpec) {
+  for (const spec of Object.values(urlSpec)) {
     if (spec.prefix) {
       // Strip out STATIC_VERSION. This updates fairly regularly and we
       // don't want it to affect snapshot tests.
@@ -28,12 +33,23 @@ function cleanURLSpec(reference) {
         .replace(/static-\d+[a-z]\d+/i, 'static');
     }
   }
+}
+
+function urlsPlease() {
+  const {aggregate: urlsAggregate, result: urlSpec} =
+    processURLSpecFromFileSync(internalDefaultURLSpecFile);
+
+  urlsAggregate.close();
+
+  applyLocalizedWithBaseDirectory(urlSpec);
+
+  cleanURLSpec(urlSpec);
 
-  return prepared;
+  return generateURLs(urlSpec);
 }
 
 export function testContentFunctions(t, message, fn) {
-  const urls = generateURLs(cleanURLSpec(urlSpec));
+  const urls = urlsPlease();
 
   t.test(message, async t => {
     let loadedContentDependencies;