« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/data-tests/index.js125
-rw-r--r--test/data-tests/test-no-short-tracks.js25
-rw-r--r--test/data-tests/test-order-of-album-groups.js55
3 files changed, 205 insertions, 0 deletions
diff --git a/test/data-tests/index.js b/test/data-tests/index.js
new file mode 100644
index 00000000..1b9ec990
--- /dev/null
+++ b/test/data-tests/index.js
@@ -0,0 +1,125 @@
+import chokidar from 'chokidar';
+import * as path from 'path';
+import {fileURLToPath} from 'url';
+
+import {quickLoadAllFromYAML} from '../../src/data/yaml.js';
+import {isMain} from '../../src/util/node-utils.js';
+import {getContextAssignments} from '../../src/repl.js';
+
+import {
+  color,
+  logError,
+  logInfo,
+  logWarn,
+  parseOptions,
+} from '../../src/util/cli.js';
+
+import {
+  bindOpts,
+  showAggregate,
+} from '../../src/util/sugar.js';
+
+async function main() {
+  const miscOptions = await parseOptions(process.argv.slice(2), {
+    'data-path': {
+      type: 'value',
+    },
+  });
+
+  const dataPath = miscOptions['data-path'] || process.env.HSMUSIC_DATA;
+
+  if (!dataPath) {
+    logError`Expected --data-path option or HSMUSIC_DATA to be set`;
+    return;
+  }
+
+  console.log(`HSMusic automated data tests`);
+  console.log(`${color.bright(color.yellow(`:star:`))} Now featuring quick-reloading! ${color.bright(color.cyan(`:earth:`))}`);
+
+  // Watch adjacent files in data-tests directory
+  const metaPath = fileURLToPath(import.meta.url);
+  const metaDirname = path.dirname(metaPath);
+  const watcher = chokidar.watch(metaDirname);
+
+  const wikiData = await quickLoadAllFromYAML(dataPath, {
+    showAggregate: bindOpts(showAggregate, {
+      showTraces: false,
+    }),
+  });
+
+  const context = await getContextAssignments({
+    wikiData,
+  });
+
+  let resolveNext;
+
+  const queue = [];
+
+  watcher.on('all', (event, path) => {
+    if (!['add', 'change'].includes(event)) return;
+    if (path === metaPath) return;
+    if (resolveNext) {
+      resolveNext(path);
+    } else if (!queue.includes(path)) {
+      queue.push(path);
+    }
+  });
+
+  logInfo`Awaiting file changes.`;
+
+  /* eslint-disable-next-line no-constant-condition */
+  while (true) {
+    const testPath = (queue.length
+      ? queue.shift()
+      : await new Promise(resolve => {
+          resolveNext = resolve;
+        }));
+
+    resolveNext = null;
+
+    const shortPath = path.basename(testPath);
+
+    logInfo`Path updated: ${shortPath} - running this test!`;
+
+    let imp;
+    try {
+      imp = await import(`${testPath}?${Date.now()}`)
+    } catch (error) {
+      logWarn`Failed to import ${shortPath} - ${error.constructor.name} details below:`;
+      console.error(error);
+      continue;
+    }
+
+    const {default: testFn} = imp;
+
+    if (!testFn) {
+      logWarn`No default export for ${shortPath}`;
+      logWarn`Skipping this test for now!`;
+      continue;
+    }
+
+    if (typeof testFn !== 'function') {
+      logWarn`Default export for ${shortPath} is ${typeof testFn}, not function`;
+      logWarn`Skipping this test for now!`;
+      continue;
+    }
+
+    try {
+      await testFn(context);
+    } catch (error) {
+      showAggregate(error, {
+        pathToFileURL: f => path.relative(metaDirname, fileURLToPath(f)),
+      });
+    }
+  }
+}
+
+if (isMain(import.meta.url)) {
+  main().catch((error) => {
+    if (error instanceof AggregateError) {
+      showAggregate(error);
+    } else {
+      console.error(error);
+    }
+  });
+}
diff --git a/test/data-tests/test-no-short-tracks.js b/test/data-tests/test-no-short-tracks.js
new file mode 100644
index 00000000..76356099
--- /dev/null
+++ b/test/data-tests/test-no-short-tracks.js
@@ -0,0 +1,25 @@
+export default function({
+  albumData,
+  getTotalDuration,
+}) {
+  const shortAlbums = albumData
+    .filter(album => album.tracks.length > 1)
+    .map(album => ({
+      album,
+      duration: getTotalDuration(album.tracks),
+    }))
+    .filter(album => album.duration)
+    .filter(album => album.duration < 60 * 15);
+
+  if (!shortAlbums.length) return true;
+
+  shortAlbums.sort((a, b) => a.duration - b.duration);
+
+  console.log(`Found ${shortAlbums.length} short albums! Oh nooooooo!`);
+  console.log(`Here are the shortest 10:`);
+  for (const {duration, album} of shortAlbums.slice(0, 10)) {
+    console.log(`- (${duration}s)`, album);
+  }
+
+  return false;
+}
diff --git a/test/data-tests/test-order-of-album-groups.js b/test/data-tests/test-order-of-album-groups.js
new file mode 100644
index 00000000..de2fcbed
--- /dev/null
+++ b/test/data-tests/test-order-of-album-groups.js
@@ -0,0 +1,55 @@
+import * as util from 'util';
+
+export default function({
+  albumData,
+  groupCategoryData,
+}) {
+  const groupSchemaTemplate = [
+    ['Projects beyond Homestuck', 'Fandom projects'],
+    ['Solo musicians', 'Fan-musician groups'],
+    ['HSMusic'],
+  ];
+
+  const groupSchema =
+    groupSchemaTemplate.map(names => names.flatMap(
+      name => groupCategoryData
+        .find(gc => gc.name === name)
+        .groups));
+
+  const badAlbums = albumData.filter(album => {
+    const groups = album.groups.slice();
+    const disallowed = [];
+    for (const allowed of groupSchema) {
+      while (groups.length) {
+        if (disallowed.includes(groups[0]))
+          return true;
+        else if (allowed.includes(groups[0]))
+          groups.shift();
+        else break;
+      }
+      disallowed.push(...allowed);
+    }
+    return false;
+  });
+
+  if (!badAlbums.length) return true;
+
+  console.log(`Some albums don't list their groups in the right order:`);
+  for (const album of badAlbums) {
+    console.log('-', album);
+    for (const group of album.groups) {
+      console.log(`  - ${util.inspect(group)}`)
+    }
+  }
+
+  console.log(`Here's the group schema they should be updated to match:`);
+  for (const section of groupSchemaTemplate) {
+    if (section.length > 1) {
+      console.log(`- Groups from any of: ${section.join(', ')}`);
+    } else {
+      console.log(`- Groups from: ${section}`);
+    }
+  }
+
+  return false;
+}