feat(frontend): add stations library reader with tdd

This commit is contained in:
devilreef 2026-04-30 09:31:59 +06:00
parent a1e24c6a81
commit a7256bc13c
Signed by: devilreef
SSH key fingerprint: SHA256:UZisRr4iuXx+IhkbZnR655L2RWAT6o2rgbGv5F/6m3Y
7 changed files with 153 additions and 0 deletions

View file

@ -0,0 +1,4 @@
name: Alpha
description: alpha station
color: '#ff0000'
tags: [test, alpha]

View file

@ -0,0 +1,2 @@
name: "unterminated
description: never closes

View file

@ -0,0 +1,4 @@
name: Beta
description: beta station
color: '#00ff00'
tags: [test, beta]

View file

@ -0,0 +1 @@
fake cover

View file

View file

@ -0,0 +1,56 @@
import { describe, expect, it } from 'vitest';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { listStations, readMeta } from '@lib/stations';
const HERE = path.dirname(fileURLToPath(import.meta.url));
const FIXTURES = path.resolve(HERE, 'fixtures/library');
describe('readMeta', () => {
it('returns null for missing _meta.yml', async () => {
expect(await readMeta(FIXTURES, 'empty')).toBeNull();
});
it('returns null for malformed yaml (logged, not thrown)', async () => {
expect(await readMeta(FIXTURES, 'bad-yaml')).toBeNull();
});
it('returns null when the id has uppercase letters (regex rejection, no fs read)', async () => {
expect(await readMeta(FIXTURES, 'NotLower')).toBeNull();
});
it('parses a valid station', async () => {
const s = await readMeta(FIXTURES, 'alpha');
expect(s).toEqual({
id: 'alpha',
name: 'Alpha',
description: 'alpha station',
color: '#ff0000',
tags: ['test', 'alpha'],
mounts: { mp3: '/alpha.mp3', opus: '/alpha.opus' },
cover: null,
});
});
it('sets cover when a cover file exists', async () => {
const s = await readMeta(FIXTURES, 'beta');
expect(s?.cover).toBe('/api/stations/beta/cover');
});
});
describe('listStations', () => {
it('returns only valid stations, sorted by id', async () => {
const ss = await listStations(FIXTURES);
expect(ss.map((s) => s.id)).toEqual(['alpha', 'beta']);
});
it('skips empty and bad-yaml folders', async () => {
const ss = await listStations(FIXTURES);
expect(ss.find((s) => s.id === 'empty')).toBeUndefined();
expect(ss.find((s) => s.id === 'bad-yaml')).toBeUndefined();
});
it('handles a non-existent root gracefully', async () => {
expect(await listStations('/no/such/dir')).toEqual([]);
});
});