diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-06-06 21:06:23 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-06-06 21:06:23 -0500 |
| commit | 1e713798fd4324d4513e66f7dbeb1a1bb16691a5 (patch) | |
| tree | a342f66a0b3be69adb9f0319a38f1ec7fcfe576c /src/web | |
| parent | d7abce6360907738c29999f6b2700f7fe64b998d (diff) | |
| download | slskd-1e713798fd4324d4513e66f7dbeb1a1bb16691a5.tar.gz | |
add more search tests
Diffstat (limited to 'src/web')
| -rw-r--r-- | src/web/src/lib/search.js | 26 | ||||
| -rw-r--r-- | src/web/src/lib/search.test.js | 91 |
2 files changed, 97 insertions, 20 deletions
diff --git a/src/web/src/lib/search.js b/src/web/src/lib/search.js index 6d59197a..c56a94c4 100644 --- a/src/web/src/lib/search.js +++ b/src/web/src/lib/search.js @@ -1,8 +1,5 @@ import api from './api'; -export const CBR = 'CBR'; -export const VBR = 'VBR'; - export const search = ({ id, searchText }) => { return api.post(`/searches`, { id, searchText }); }; @@ -16,8 +13,6 @@ export const getResponses = async ({ id }) => { }; export const isConstantBitRate = (bitRate) => { - // because the caller most likely already O^2 this uses an ugly but fast approach - // https://en.wikipedia.org/wiki/MP3#Bit_rate switch (bitRate) { case 8: case 16: @@ -55,6 +50,14 @@ export const parseFiltersFromString = (string) => { isCBR: false }; + const getNthMatch = (string, regex, n) => { + const match = string.match(regex); + + if (match) { + return parseInt(match[n], 10); + } + } + filters.minBitRate = getNthMatch(string, /(minbr|minbitrate):([0-9]+)/i, 2) || filters.minBitRate; filters.minFileSize = getNthMatch(string, /(minfs|minfilesize):([0-9]+)/i, 2) || filters.minFileSize; filters.minLength = getNthMatch(string, /(minlen|minlength):([0-9]+)/i, 2) || filters.minLength; @@ -63,9 +66,8 @@ export const parseFiltersFromString = (string) => { filters.isVBR = !!string.match(/isvbr/i); filters.isCBR = !!string.match(/iscbr/i); - const isReservedWord = (word) => word.includes(':') || word === 'isvbr' || word === 'iscbr'; - - let terms = string.toLowerCase().split(' ').filter(term => !isReservedWord(term)); + let terms = string.toLowerCase().split(' ') + .filter(term => !term.includes(':') && term !== 'isvbr' && term !== 'iscbr'); filters.include = terms.filter(term => !term.startsWith('-')); filters.exclude = terms.filter(term => term.startsWith('-')).map(term => term.slice(1)); @@ -73,14 +75,6 @@ export const parseFiltersFromString = (string) => { return filters; }; -const getNthMatch = (string, regex, n) => { - const match = string.match(regex); - - if (match) { - return parseInt(match[n], 10); - } -} - export const filterResponse = ({ filters = { minBitRate: 0, diff --git a/src/web/src/lib/search.test.js b/src/web/src/lib/search.test.js index 9461a2df..02c8cf99 100644 --- a/src/web/src/lib/search.test.js +++ b/src/web/src/lib/search.test.js @@ -34,7 +34,7 @@ describe('filterResponse', () => { const filters = { isCBR: true }; - expect(search.filterResponse({ response, filters })).toStrictEqual({ + expect(search.filterResponse({ response, filters })).toMatchObject({ files: [ { bitRate: 320 } ] @@ -51,7 +51,7 @@ describe('filterResponse', () => { const filters = { isVBR: true }; - expect(search.filterResponse({ response, filters })).toStrictEqual({ + expect(search.filterResponse({ response, filters })).toMatchObject({ files: [ { bitRate: 123 } ] @@ -68,12 +68,95 @@ describe('filterResponse', () => { const filters = { isCBR: true, isVBR: true }; - expect(search.filterResponse({ response, filters })).toStrictEqual({ + expect(search.filterResponse({ response, filters })).toMatchObject({ files: [] }); }); - // todo: more tests + it('removes files with bitRate less than minBitRate', () => { + const response = { + files: [ + { bitRate: 100 }, + { bitRate: 99 } + ] + }; + + const filters = { minBitRate: 100 }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { bitRate: 100 } + ] + }); + }); + + it('removes files with size less than minFileSize', () => { + const response = { + files: [ + { size: 100 }, + { size: 99 } + ] + }; + + const filters = { minFileSize: 100 }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { size: 100 } + ] + }); + }); + + it('removes files with length less than minLength', () => { + const response = { + files: [ + { length: 100 }, + { length: 99 } + ] + }; + + const filters = { minLength: 100 }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { length: 100 } + ] + }); + }); + + it('removes files with filenames not containing included phrases', () => { + const response = { + files: [ + { filename: '/path/to/foo.mp3' }, + { filename: '/path/to/bar.mp3' } + ] + }; + + const filters = { include: ['foo'] }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { filename: '/path/to/foo.mp3' } + ] + }); + }); + + it('removes files with filenames containing excluded phrases', () => { + const response = { + files: [ + { filename: '/path/to/foo.mp3' }, + { filename: '/path/to/bar.mp3' } + ] + }; + + const filters = { exclude: ['foo'] }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { filename: '/path/to/bar.mp3' } + ] + }); + }); }); describe('parseFiltersFromString', () => { |
