Branchwell

PulseCanvas / app · pulsecanvas.ai / scripts / test-radio-stations.py

Raw 3.4 KB on 23ffa4e102ea595ec83f2b18683b75332fc815c3
1#!/usr/bin/env python3
2"""Check grouped stations preserve music and reject incomplete publications."""
3import contextlib
4import importlib.util
5import io
6import json
7from pathlib import Path
8import tempfile
9import unittest
10
11ROOT = Path(__file__).resolve().parents[1]
12spec = importlib.util.spec_from_file_location('station_builder', ROOT / 'scripts/build-radio-stations.py')
13builder = importlib.util.module_from_spec(spec)
14spec.loader.exec_module(builder)
15
16
17class StationTests(unittest.TestCase):
18 def setUp(self):
19 self.temporary = tempfile.TemporaryDirectory()
20 self.addCleanup(self.temporary.cleanup)
21 self.root = Path(self.temporary.name)
22 self.catalog = self.root / 'catalog.json'
23 self.names = self.root / 'stations-config.json'
24 self.output = self.root / 'ready'
25 self.groups = [dict(id='pop', title='Pop', description='Pop music', genres=['K-pop', 'Disco pop']),
26 dict(id='folk', title='Folk & Sea Shanties', description='Folk music', genres=['Bluegrass', 'Sea shanty'])]
27 self.tracks = [dict(id=str(i), title=f'Song {i}', genre=genre, audio=f'audio/{i}.m4a',
28 sha256='a' * 64, byteCount=123, duration=45.0)
29 for i, genre in enumerate(['K-pop', 'Disco pop', 'Bluegrass', 'Sea shanty'])]
30 self.catalog.write_text(json.dumps(dict(version=1, title='All music', tracks=self.tracks)))
31
32 def build(self):
33 self.names.write_text(json.dumps(self.groups))
34 with contextlib.redirect_stdout(io.StringIO()):
35 builder.build(self.catalog, self.output, self.names)
36
37 def test_grouped_metadata_and_relative_paths(self):
38 self.build()
39 self.assertEqual((self.output / 'catalog.json').read_bytes(), self.catalog.read_bytes())
40 index = json.loads((self.output / 'stations.json').read_text())
41 self.assertEqual([x['id'] for x in index['stations']], ['all-music', 'pop', 'folk'])
42 gathered = []
43 for station in index['stations'][1:]:
44 catalog = json.loads((self.output / station['catalog']).read_text())
45 self.assertEqual(catalog['title'], station['title'])
46 gathered += catalog['tracks']
47 self.assertEqual(gathered, self.tracks)
48
49 def test_invalid_mapping_never_replaces_published_index(self):
50 for mode in ['unknown', 'duplicate', 'too-many']:
51 with self.subTest(mode=mode):
52 self.output.mkdir(exist_ok=True)
53 index = self.output / 'stations.json'
54 index.write_text('existing publication')
55 original = json.loads(json.dumps(self.groups))
56 if mode == 'unknown': self.groups[0]['genres'] = ['Other']
57 if mode == 'duplicate': self.groups[1]['genres'].append('K-pop')
58 if mode == 'too-many':
59 self.groups += [dict(id=f'extra-{i}', title='Extra', genres=[f'Genre {i}'], description='') for i in range(6)]
60 with self.assertRaises(AssertionError): self.build()
61 self.assertEqual(index.read_text(), 'existing publication')
62 self.groups = original
63
64 def test_shipped_configuration_caps_total_choices_at_eight(self):
65 groups = json.loads((ROOT / 'radio/station-names.json').read_text())
66 self.assertLessEqual(len(groups) + 1, 8)
67 self.assertTrue(any(len(group['genres']) > 1 for group in groups))
68
69
70if __name__ == '__main__': unittest.main()