« get me outta code hell

test-order-of-album-groups.js « data-tests - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/data-tests/test-order-of-album-groups.js
blob: 57500e33f149b01efd6abb2d78c9cf013b9774c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {inspect} from 'node: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(`  - ${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;
}