diff --git a/src/scripts/background/ifidf.ts b/src/scripts/background/ifidf.ts index ec173dd..2367f85 100644 --- a/src/scripts/background/ifidf.ts +++ b/src/scripts/background/ifidf.ts @@ -1,5 +1,5 @@ import { dot, norm } from '../helpers/shared'; -import { normalizeString } from './misc'; +import { normalizeString } from './normalize'; type SearchableVideo = { title: string, diff --git a/src/scripts/background/index.ts b/src/scripts/background/index.ts index 014e6e2..cc9e2a1 100644 --- a/src/scripts/background/index.ts +++ b/src/scripts/background/index.ts @@ -1,5 +1,6 @@ export * from './misc'; export * from './nebula'; +export * from './normalize'; export * from './youtube'; export type Creator = { diff --git a/src/scripts/background/misc.ts b/src/scripts/background/misc.ts index fd2dbab..8721eca 100644 --- a/src/scripts/background/misc.ts +++ b/src/scripts/background/misc.ts @@ -53,8 +53,3 @@ export const loadCreators: () => Promise = (() => { return getInformation; })(); - -// @ts-expect-error regex works, don't touch -export const normalizeString = (str: string) => str.toLowerCase().normalize('NFD').replace(/\p{Pd}/g, '-') - /* eslint-disable-next-line no-misleading-character-class */ - .replace(/["'()[\]{}\u0300-\u036f]/g, ''); diff --git a/src/scripts/background/nebula.ts b/src/scripts/background/nebula.ts index 23cc631..2a1b177 100644 --- a/src/scripts/background/nebula.ts +++ b/src/scripts/background/nebula.ts @@ -2,7 +2,7 @@ import type { Video } from '.'; import { getChannelVideos, searchVideos } from '../helpers/api'; import { ytvideo } from '../helpers/shared'; import { creatorHasVideo } from './ifidf'; -import { normalizeString } from './misc'; +import { normalizeString } from './normalize'; const plistcache: { [key: string]: Video[]; } = {}; export const loadNebulaChannelVideos = async (channel: string, num: number) => { diff --git a/src/scripts/background/normalize.ts b/src/scripts/background/normalize.ts new file mode 100644 index 0000000..53da741 --- /dev/null +++ b/src/scripts/background/normalize.ts @@ -0,0 +1,4 @@ +// @ts-expect-error regex works, don't touch +export const normalizeString = (str: string) => str.toLowerCase().normalize('NFD').replace(/\p{Pd}/g, '-') + /* eslint-disable-next-line no-misleading-character-class */ + .replace(/["'()[\]{}\u0300-\u036f]/g, ''); diff --git a/src/scripts/background/youtube.ts b/src/scripts/background/youtube.ts index d2d659e..5ed2c07 100644 --- a/src/scripts/background/youtube.ts +++ b/src/scripts/background/youtube.ts @@ -1,7 +1,7 @@ import type { Video } from '.'; import { getBase, ytvideo } from '../helpers/shared'; import { creatorHasVideo } from './ifidf'; -import { normalizeString } from './misc'; +import { normalizeString } from './normalize'; const plistcache: { [key: string]: Video[]; } = {}; export const loadYTVideos = async (playlist: string, num: number) => { diff --git a/src/scripts/background_script.ts b/src/scripts/background_script.ts index 5d412cb..afd1584 100644 --- a/src/scripts/background_script.ts +++ b/src/scripts/background_script.ts @@ -44,11 +44,11 @@ getBrowserInstance().runtime.onMessage.addListener((message: string | { [key: st if (ret) { ret.then( res => { - console.dev.log('Result for', msg.type, ':', res); + if (__DEV__) console.log('Result for', msg.type, ':', res); sendResponse({ res }); }, err => { - console.dev.log('Error running', msg.type, ':', err); + if (__DEV__) console.log('Error running', msg.type, ':', err); sendResponse({ err }); }, ); diff --git a/tests/unit/creators.ts b/tests/unit/creators.ts index b67a1b3..7862090 100644 --- a/tests/unit/creators.ts +++ b/tests/unit/creators.ts @@ -1,8 +1,8 @@ import { jest } from '@jest/globals'; import fetch from 'node-fetch'; -import { loadCreators } from '../../src/scripts/background'; import { matchVideoConfidence } from '../../src/scripts/background/ifidf'; import '../../src/scripts/helpers/shared/prototype'; +import { getInformation as loadCreators } from '../../src/scripts/page/offscreen'; global.fetch = fetch as unknown as typeof global.fetch; diff --git a/tests/unit/nebula.ts b/tests/unit/nebula.ts index 350221d..1d19d4b 100644 --- a/tests/unit/nebula.ts +++ b/tests/unit/nebula.ts @@ -5,7 +5,7 @@ import '../../src/scripts/helpers/shared/prototype'; global.fetch = fetch as unknown as typeof global.fetch; -const { creatorHasNebulaVideo, loadNebulaChannelVideos, existsNebulaVideo, loadNebulaSearchVideos } = await import('../../src/scripts/background'); +const { creatorHasNebulaVideo, loadNebulaChannelVideos, existsNebulaVideo, loadNebulaSearchVideos } = await import('../../src/scripts/background/nebula'); describe('loading nebula videos', () => { const consoleError = console.error; @@ -40,9 +40,9 @@ describe('loading nebula videos', () => { const fetchMock = jest.fn(); global.fetch = fetchMock; await expect(creatorHasNebulaVideo('hai', vid.title, 50)).resolves.toEqual({ confidence: 1, video: vid.videoId }); - expect(fetchMock).not.toBeCalled(); + expect(fetchMock).not.toHaveBeenCalled(); await expect(loadNebulaChannelVideos('hai', 100)).resolves.toHaveLength(100); - expect(fetchMock).not.toBeCalled(); + expect(fetchMock).not.toHaveBeenCalled(); global.fetch = fetch as unknown as typeof global.fetch; }); @@ -60,9 +60,9 @@ describe('loading nebula videos', () => { const fetchMock = jest.fn(); global.fetch = fetchMock; await expect(existsNebulaVideo(vid.title, 50)).resolves.toEqual({ confidence: 1, video: vid.videoId }); - expect(fetchMock).not.toBeCalled(); + expect(fetchMock).not.toHaveBeenCalled(); await expect(loadNebulaSearchVideos('test', 100)).resolves.toHaveLength(100); - expect(fetchMock).not.toBeCalled(); + expect(fetchMock).not.toHaveBeenCalled(); global.fetch = fetch as unknown as typeof global.fetch; }); }); diff --git a/tests/unit/shared.ts b/tests/unit/shared.ts index c76e1cc..f464f61 100644 --- a/tests/unit/shared.ts +++ b/tests/unit/shared.ts @@ -3,7 +3,7 @@ import { jest } from '@jest/globals'; import { JSDOM } from 'jsdom'; import { dirname } from 'path'; import { fileURLToPath } from 'url'; -import { Event, Message, arrFromLengthy, clone, debounce, dot, getCookie, injectFunction, injectScript, isMobile, isVideoListPage, isVideoPage, norm, parseMaybeJSON, parseTypeObject, replyMessage, sendEventHandler, sendMessage } from '../../src/scripts/helpers/shared'; +import { Event, Message, arrFromLengthy, clone, debounce, dot, getCookie, injectScript, isMobile, isVideoListPage, isVideoPage, norm, parseMaybeJSON, parseTypeObject, replyMessage, sendEventHandler, sendMessage } from '../../src/scripts/helpers/shared'; import '../../src/scripts/helpers/shared/prototype'; const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -102,7 +102,7 @@ describe('other', () => { expect(isMobile()).toBe(true); expect(isMobile()).toBe(false); - expect(mock).toBeCalledTimes(2); + expect(mock).toHaveBeenCalledTimes(2); mock.mock.calls.forEach(c => { expect(c[0]).toMatch(/pointer/); }); @@ -121,12 +121,12 @@ describe('other', () => { for (let i = 0; i < 10; i++) m(); jest.runAllTimers(); - expect(mock).toBeCalledTimes(1); + expect(mock).toHaveBeenCalledTimes(1); for (let i = 0; i < 10; i++) m(); jest.runAllTimers(); - expect(mock).toBeCalledTimes(2); + expect(mock).toHaveBeenCalledTimes(2); }); test('arrFromLengthy converts to array', () => { @@ -234,17 +234,8 @@ describe('script injection', () => { const wrapper = dom.window.document.head; await expect(injectScript('../fixtures/waitForEvent.js', wrapper, 'test', 'data', dom.window as never as Window)).resolves.toBe(void 0); - expect(mock).toBeCalledTimes(1); - expect(mock).toHaveBeenCalledWith('data'); - console.log = log; - }); - - test('injecting function works with arguments', () => { - const { log } = console; - const mock = console.log = jest.fn(); - injectFunction(document.body, data => console.log(data), 'test-data'); expect(mock).toHaveBeenCalledTimes(1); - expect(mock).toHaveBeenCalledWith('test-data'); + expect(mock).toHaveBeenCalledWith('data'); console.log = log; }); }); @@ -358,7 +349,7 @@ describe('message event listeners', () => { cb.mockImplementationOnce(resolve); window.postMessage({ type: name, res: i }, '*'); })).resolves.toBe(i); - expect(cb).toBeCalledTimes(i + 1); + expect(cb).toHaveBeenCalledTimes(i + 1); } }); }); diff --git a/tests/unit/youtube.ts b/tests/unit/youtube.ts index f57a0c8..3bfbda8 100644 --- a/tests/unit/youtube.ts +++ b/tests/unit/youtube.ts @@ -1,6 +1,7 @@ import { jest } from '@jest/globals'; import fetch from 'node-fetch'; -import { Video, creatorHasYTVideo, loadYTVideos } from '../../src/scripts/background'; +import type { Video } from '../../src/scripts/background'; +import { creatorHasYTVideo, loadYTVideos } from '../../src/scripts/background/youtube'; import '../../src/scripts/helpers/shared/prototype'; global.fetch = fetch as unknown as typeof global.fetch; @@ -36,9 +37,9 @@ describe('loading youtube videos', () => { const fetchMock = jest.fn(); global.fetch = fetchMock; await expect(creatorHasYTVideo('UUuCkxoKLYO_EQ2GeFtbM_bw', vid.title, 50)).resolves.toEqual({ confidence: 1, video: vid.videoId }); - expect(fetchMock).not.toBeCalled(); + expect(fetchMock).not.toHaveBeenCalled(); await expect(loadYTVideos('UUuCkxoKLYO_EQ2GeFtbM_bw', 100)).resolves.toHaveLength(100); - expect(fetchMock).not.toBeCalled(); + expect(fetchMock).not.toHaveBeenCalled(); global.fetch = fetch as unknown as typeof global.fetch; }); }); \ No newline at end of file