import { test, expect } from 'vitest'; import { filterStudies, tagCounts, isTagQuery } from './studyFilter.js'; const index = [ { id: 'a', title: 'Titus Notes', chapterSummary: 'Titus 1', tags: ['hope', 'leadership'] }, { id: 'b', title: 'New Birth', chapterSummary: 'John 3', tags: ['grace', 'hope'] }, { id: 'c', title: 'Grace Alone', chapterSummary: 'Romans 3', tags: [] }, ]; const ids = (list) => list.map((e) => e.id); test('the search box finds a study by title, passage or tag', () => { expect(ids(filterStudies(index, { query: 'titus' }))).toEqual(['a']); expect(ids(filterStudies(index, { query: 'romans' }))).toEqual(['c']); expect(ids(filterStudies(index, { query: 'lead' }))).toEqual(['a']); // a tag expect(ids(filterStudies(index, { query: 'grace' }))).toEqual(['b', 'c']); // a tag on one, a title on the other }); test('starting with # searches tags only', () => { expect(isTagQuery(' #grace')).toBe(true); expect(ids(filterStudies(index, { query: '#grace' }))).toEqual(['b']); // not the study named "Grace Alone" expect(ids(filterStudies(index, { query: '#HOP' }))).toEqual(['a', 'b']); expect(ids(filterStudies(index, { query: '#' }))).toEqual(['a', 'b', 'c']); expect(ids(filterStudies(index, { query: '#nothing' }))).toEqual([]); }); test('chosen tags must all be present, and combine with the search', () => { expect(ids(filterStudies(index, { tags: ['hope'] }))).toEqual(['a', 'b']); expect(ids(filterStudies(index, { tags: ['hope', 'grace'] }))).toEqual(['b']); expect(ids(filterStudies(index, { tags: ['hope'], query: 'john' }))).toEqual(['b']); expect(ids(filterStudies(index, { tags: ['Hope'] }))).toEqual(['a', 'b']); // case does not matter }); test('tags are counted, most used first', () => { expect(tagCounts(index)).toEqual([{ tag: 'hope', count: 2 }, { tag: 'grace', count: 1 }, { tag: 'leadership', count: 1 }]); }); import { groupBySeries, seriesList } from './studyFilter.js'; test('series are listed and studies grouped under them, with unsorted ones last', () => { const idx = [ { id: 'a', title: 'A', series: 'Romans' }, { id: 'b', title: 'B', series: '' }, { id: 'c', title: 'C', series: 'Advent' }, { id: 'd', title: 'D', series: 'Romans' }, ]; expect(seriesList(idx)).toEqual([{ name: 'Advent', count: 1 }, { name: 'Romans', count: 2 }]); expect(groupBySeries(idx).map((g) => `${g.name || '(none)'}:${g.items.map((e) => e.id).join('')}`)).toEqual(['Advent:c', 'Romans:ad', '(none):b']); expect(groupBySeries([{ id: 'x' }]).map((g) => g.name)).toEqual(['']); }); test('a series can be chosen as a filter, and the search finds studies by series name', () => { const idx = [{ id: 'a', title: 'Ch 8', series: 'Romans' }, { id: 'b', title: 'Ch 1' }, { id: 'c', title: 'Gen', series: 'Advent' }]; expect(filterStudies(idx, { series: 'Romans' }).map((e) => e.id)).toEqual(['a']); expect(filterStudies(idx, { series: '__none' }).map((e) => e.id)).toEqual(['b']); expect(filterStudies(idx, { query: 'advent' }).map((e) => e.id)).toEqual(['c']); });