From 45a95b68af9d0c6d1ca541d78d400899e9f20bfa Mon Sep 17 00:00:00 2001 From: freearhey <7253922+freearhey@users.noreply.github.com> Date: Mon, 14 Apr 2025 21:53:00 +0300 Subject: [PATCH] Update store.test.js --- tests/store.test.js | 274 ++++++++++++++++++++++---------------------- 1 file changed, 138 insertions(+), 136 deletions(-) diff --git a/tests/store.test.js b/tests/store.test.js index c60bc8a12..7f83ff4aa 100644 --- a/tests/store.test.js +++ b/tests/store.test.js @@ -1,33 +1,54 @@ -import { search, fetchChannels, filteredChannels } from '../src/store' +import { loadData, search, searchResults } from '../src/store' +import { expect, it, describe, beforeEach, afterEach, vi } from 'vitest' import { get } from 'svelte/store' -import fs from 'fs' import path from 'path' -import { jest } from '@jest/globals' - -const API_ENDPOINT = 'https://iptv-org.github.io/api' +import fs from 'fs' +import AxiosMockAdapter from 'axios-mock-adapter' +import axios from 'axios' +import { ApiClient, DataProcessor } from '../src/core' beforeEach(async () => { - global.fetch = mockFetch() - await fetchChannels() + const client = new ApiClient() + const processor = new DataProcessor() + + client.instance = axios.create({ + baseURL: 'https://iptv-org.github.io/api' + }) + + const mockAxios = new AxiosMockAdapter(client.instance) + + mockAxios.onGet(`categories.json`).reply(200, loadJson('categories.json')) + mockAxios.onGet(`countries.json`).reply(200, loadJson('countries.json')) + mockAxios.onGet(`languages.json`).reply(200, loadJson('languages.json')) + mockAxios.onGet(`blocklist.json`).reply(200, loadJson('blocklist.json')) + mockAxios.onGet(`timezones.json`).reply(200, loadJson('timezones.json')) + mockAxios.onGet(`channels.json`).reply(200, loadJson('channels.json')) + mockAxios.onGet(`regions.json`).reply(200, loadJson('regions.json')) + mockAxios.onGet(`streams.json`).reply(200, loadJson('streams.json')) + mockAxios.onGet(`guides.json`).reply(200, loadJson('guides.json')) + mockAxios.onGet(`feeds.json`).reply(200, loadJson('feeds.json')) + mockAxios.onGet(`subdivisions.json`).reply(200, loadJson('subdivisions.json')) + + await loadData({ client, processor }) }) describe('search', () => { it('return all channels by default', () => { - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(15) }) it('returns empty list if there is no such channel', () => { search('lorem') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(0) }) it('can find channel by name', () => { search('name:002') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: '002RadioTV.do' @@ -37,7 +58,7 @@ describe('search', () => { it('can find channels by multiple words', () => { search('Xtrema Cartoons') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(2) expect(results[0]).toMatchObject({ id: 'XtremaCartoons.ar' @@ -50,7 +71,7 @@ describe('search', () => { it('can search for one of two words', () => { search('Johannesburg,002') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(2) expect(results[0]).toMatchObject({ id: '002RadioTV.do' @@ -63,7 +84,7 @@ describe('search', () => { it('can search for exact word matches', () => { search('"Xtrema Cartoons"') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: 'XtremaCartoons.ar' @@ -73,7 +94,7 @@ describe('search', () => { it('can find channels by id', () => { search('id:002RadioTV.do') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: '002RadioTV.do' @@ -83,7 +104,7 @@ describe('search', () => { it('can find channels by alternative names', () => { search('alt_names:التلفزيون') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: 'TV1.dz' @@ -93,7 +114,7 @@ describe('search', () => { it('can find channels by network', () => { search('network:Hope') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: 'K11UUD1.as' @@ -103,7 +124,7 @@ describe('search', () => { it('can find channels without the owner', () => { search('owners:^$') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(7) expect(results[0]).toMatchObject({ id: '002RadioTV.do' @@ -113,7 +134,7 @@ describe('search', () => { it('can find channels by country code', () => { search('country:DO') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: '002RadioTV.do' @@ -123,7 +144,7 @@ describe('search', () => { it('can find channels that are broadcast from the same region', () => { search('subdivision:AR-W') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: '13MaxTelevision.ar' @@ -133,37 +154,17 @@ describe('search', () => { it('can find channels that are broadcast from the same city', () => { search('city:Corrientes') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: '13MaxTelevision.ar' }) }) - it('can find channels that are broadcast in the same region', () => { - search('broadcast_area:s/AR-W') - - const results = get(filteredChannels) - expect(results.length).toBe(1) - expect(results[0]).toMatchObject({ - id: '13MaxTelevision.ar' - }) - }) - - it('can find channels that are broadcast in the same language', () => { - search('languages:spa') - - const results = get(filteredChannels) - expect(results.length).toBe(4) - expect(results[0]).toMatchObject({ - id: '002RadioTV.do' - }) - }) - it('can find channels that have the same category', () => { search('categories:lifestyle') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: 'FashionTVJohannesburg.fr' @@ -173,7 +174,7 @@ describe('search', () => { it('can find channels with website', () => { search('website:.') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(14) expect(results[0]).toMatchObject({ id: '002RadioTV.do' @@ -183,7 +184,7 @@ describe('search', () => { it('can find channels marked as NSFW', () => { search('is_nsfw:true') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: 'Bizarre.al' @@ -193,7 +194,7 @@ describe('search', () => { it('can find closed channels', () => { search('is_closed:true') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: 'AynaTV.af' @@ -203,37 +204,17 @@ describe('search', () => { it('can find blocked channels', () => { search('is_blocked:true') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: 'Bizarre.al' }) }) - it('can find channels that have streams', () => { - search('streams:>0') - - const results = get(filteredChannels) - expect(results.length).toBe(1) - expect(results[0]).toMatchObject({ - id: 'XtremaCartoons.ar' - }) - }) - - it('can find channels that have guides', () => { - search('guides:>0') - - const results = get(filteredChannels) - expect(results.length).toBe(1) - expect(results[0]).toMatchObject({ - id: 'LaLiganaZap.ao' - }) - }) - it('can find channels by query in lower case', () => { search('tv2') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(2) expect(results[0]).toMatchObject({ id: 'SEN502.us' @@ -247,67 +228,47 @@ describe('search', () => { search('tv2') search('alt_names:tv2') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: 'SEN502.us' }) }) - it('can find channel by broadcast area name', () => { - search('broadcast_area:"dominican republic"') + it('can find channels that have streams', () => { + search('streams:>0') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ - id: '002RadioTV.do' + id: 'XtremaCartoons.ar' + }) + }) + + it('can find channels that have guides', () => { + search('guides:>0') + + const results = get(searchResults).all() + expect(results.length).toBe(1) + expect(results[0]).toMatchObject({ + id: 'LaLiganaZap.ao' }) }) it('can find channel by country name', () => { - search('country:"dominican republic"') + search('"dominican republic"') - const results = get(filteredChannels) - expect(results.length).toBe(1) + const results = get(searchResults).all() + expect(results.length).toBe(3) expect(results[0]).toMatchObject({ id: '002RadioTV.do' }) }) - it('can find channel by region code', () => { - search('broadcast_area:r/EUR') - - const results = get(filteredChannels) - expect(results.length).toBe(1) - expect(results[0]).toMatchObject({ - id: 'ORF2Europe.at' - }) - }) - - it('can find channel by region name', () => { - search('broadcast_area:europe') - - const results = get(filteredChannels) - expect(results.length).toBe(1) - expect(results[0]).toMatchObject({ - id: 'ORF2Europe.at' - }) - }) - - it('can find channel by country name from broadcast region', () => { - search('broadcast_area:france') - - const results = get(filteredChannels) - expect(results.length).toBe(3) - expect(results[2]).toMatchObject({ - id: 'ORF2Europe.at' - }) - }) - it('can find channel by display name from the guides', () => { search('La Liga HD') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: 'LaLiganaZap.ao' @@ -317,43 +278,84 @@ describe('search', () => { it('can find channel by stream url', () => { search('https://stmv6.voxtvhd.com.br/xtremacartoons/xtremacartoons/playlist.m3u8') - const results = get(filteredChannels) + const results = get(searchResults).all() expect(results.length).toBe(1) expect(results[0]).toMatchObject({ id: 'XtremaCartoons.ar' }) }) + + it('can find channels by broadcast area code', () => { + search('broadcast_area:s/AR-W') + + const results = get(searchResults).all() + expect(results.length).toBe(1) + expect(results[0]).toMatchObject({ + id: '13MaxTelevision.ar' + }) + }) + + it('can find channel by broadcast location code', () => { + search('eur') + + const results = get(searchResults).all() + expect(results.length).toBe(2) + expect(results[0]).toMatchObject({ + id: 'Bizarre.al' + }) + }) + + it('can find channel by broadcast location name', () => { + search('europe') + + const results = get(searchResults).all() + expect(results.length).toBe(2) + expect(results[0]).toMatchObject({ + id: 'Bizarre.al' + }) + }) + + it('can find channels by exact language code', () => { + search('language:fra') + + const results = get(searchResults).all() + expect(results.length).toBe(1) + expect(results[0]).toMatchObject({ + id: 'SEN502.us' + }) + }) + + it('can find channels by language name', () => { + search('french') + + const results = get(searchResults).all() + expect(results.length).toBe(1) + expect(results[0]).toMatchObject({ + id: 'SEN502.us' + }) + }) + + it('can find channels by video format', () => { + search('video_format:576i') + + const results = get(searchResults).all() + expect(results.length).toBe(1) + expect(results[0]).toMatchObject({ + id: 'Bizarre.al' + }) + }) + + it('can find channels by timezone id', () => { + search('timezone:Europe/London') + + const results = get(searchResults).all() + expect(results.length).toBe(1) + expect(results[0]).toMatchObject({ + id: 'Bizarre.al' + }) + }) }) -function mockFetch() { - return jest.fn().mockImplementation(url => - Promise.resolve({ - ok: true, - json: () => { - if (url === `${API_ENDPOINT}/channels.json`) - return loadJson('tests/__data__/input/channels.json') - if (url === `${API_ENDPOINT}/countries.json`) - return loadJson('tests/__data__/input/countries.json') - if (url === `${API_ENDPOINT}/languages.json`) - return loadJson('tests/__data__/input/languages.json') - if (url === `${API_ENDPOINT}/guides.json`) - return loadJson('tests/__data__/input/guides.json') - if (url === `${API_ENDPOINT}/regions.json`) - return loadJson('tests/__data__/input/regions.json') - if (url === `${API_ENDPOINT}/blocklist.json`) - return loadJson('tests/__data__/input/blocklist.json') - if (url === `${API_ENDPOINT}/subdivisions.json`) - return loadJson('tests/__data__/input/subdivisions.json') - if (url === `${API_ENDPOINT}/categories.json`) - return loadJson('tests/__data__/input/categories.json') - if (url === `${API_ENDPOINT}/streams.json`) - return loadJson('tests/__data__/input/streams.json') - return [] - } - }) - ) -} - function loadJson(filepath) { - return JSON.parse(fs.readFileSync(path.resolve(filepath), 'utf8')) + return JSON.parse(fs.readFileSync(path.resolve('tests/__data__/input/', filepath), 'utf8')) }