|
| 1 | +import { describe, it, expect, beforeEach } from 'bun:test'; |
| 2 | +import { |
| 3 | + clearConfigCache, |
| 4 | + getAvailableSourcesFromConfig, |
| 5 | + getBookConfig, |
| 6 | + getSourceConfig, |
| 7 | + loadSourcesConfig, |
| 8 | +} from '../src/utils/sourceConfig'; |
| 9 | +import { DocumentSource } from '../src/types'; |
| 10 | + |
| 11 | +describe('sourceConfig', () => { |
| 12 | + beforeEach(() => { |
| 13 | + clearConfigCache(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('loads sources.json and exposes known sources', () => { |
| 17 | + const config = loadSourcesConfig(); |
| 18 | + const cairoConfig = config.sources[DocumentSource.CAIRO_BOOK]; |
| 19 | + |
| 20 | + expect(cairoConfig).toBeDefined(); |
| 21 | + expect(cairoConfig.name).toBeTruthy(); |
| 22 | + expect(cairoConfig.config.fileExtensions).toBeInstanceOf(Array); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns per-source config and book config helpers', () => { |
| 26 | + const config = loadSourcesConfig(); |
| 27 | + const sourceConfig = getSourceConfig(DocumentSource.CAIRO_BOOK); |
| 28 | + const bookConfig = getBookConfig(DocumentSource.CAIRO_BOOK); |
| 29 | + |
| 30 | + expect(sourceConfig).toEqual(config.sources[DocumentSource.CAIRO_BOOK]); |
| 31 | + expect(bookConfig).toEqual(config.sources[DocumentSource.CAIRO_BOOK].config); |
| 32 | + }); |
| 33 | + |
| 34 | + it('returns all available sources from config', () => { |
| 35 | + const sources = getAvailableSourcesFromConfig().slice().sort(); |
| 36 | + const expected = Object.values(DocumentSource).slice().sort(); |
| 37 | + |
| 38 | + expect(sources).toEqual(expected); |
| 39 | + }); |
| 40 | + |
| 41 | + it('caches config between loads and can be cleared', () => { |
| 42 | + const first = loadSourcesConfig(); |
| 43 | + const second = loadSourcesConfig(); |
| 44 | + |
| 45 | + expect(first).toBe(second); |
| 46 | + |
| 47 | + clearConfigCache(); |
| 48 | + const third = loadSourcesConfig(); |
| 49 | + |
| 50 | + expect(third).not.toBe(first); |
| 51 | + }); |
| 52 | +}); |
0 commit comments