diff --git a/package.json b/package.json index 3671bc99..11fcb55b 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ ], "types": "dist/roundware.d.ts", "scripts": { - "test": "jest --silent", + "test": "jest", "build": "rm -rf dist && NODE_ENV=production && npm-run-all --parallel build:*", "build:main": "babel src --out-dir dist --extensions '.ts' --copy-files src/types/*.d.ts && tsc --declaration --outDir dist/ --emitDeclarationOnly", "build:umd": "webpack --mode=production --output-filename roundware.umd.js", diff --git a/src/playlistAudioTrack.ts b/src/playlistAudioTrack.ts index 6b8b7997..390c73a8 100644 --- a/src/playlistAudioTrack.ts +++ b/src/playlistAudioTrack.ts @@ -164,7 +164,6 @@ export class PlaylistAudiotrack { client, }: { audioContext: IAudioContext; - audioData: IAudioTrackData; playlist: Playlist; client: Roundware; diff --git a/src/types/audioTrack.d.ts b/src/types/audioTrack.d.ts index 2f119715..3321bbca 100644 --- a/src/types/audioTrack.d.ts +++ b/src/types/audioTrack.d.ts @@ -21,5 +21,5 @@ export interface IAudioTrackData { banned_duration: number; tag_filters: any[]; project_id: number; - timed_asset_priority: number; + timed_asset_priority: string; } diff --git a/tests/__mocks__/assetPool.ts b/tests/__mocks__/assetPool.ts new file mode 100644 index 00000000..11d623e3 --- /dev/null +++ b/tests/__mocks__/assetPool.ts @@ -0,0 +1,9 @@ +import { AssetPool } from "../../src/assetPool"; +import { MOCK_ASSET_DATA } from "./mock_api_responses"; +const mockAssetPool = new AssetPool({ + assets: MOCK_ASSET_DATA, + filterChain: undefined, + sortMethods: undefined, + mixParams: undefined, +}); +export { mockAssetPool }; diff --git a/tests/__mocks__/mixer/TrackOptions.ts b/tests/__mocks__/mixer/TrackOptions.ts new file mode 100644 index 00000000..e7b8eb36 --- /dev/null +++ b/tests/__mocks__/mixer/TrackOptions.ts @@ -0,0 +1,5 @@ +import { TrackOptions } from "../../../src/mixer/TrackOptions"; +import { MOCK_AUDIO_TRACKS_DATA } from "../mock_api_responses"; + +const mockTrackOptions = new TrackOptions(() => "", MOCK_AUDIO_TRACKS_DATA[0]); +export { mockTrackOptions }; diff --git a/tests/__mocks__/playlist.ts b/tests/__mocks__/playlist.ts new file mode 100644 index 00000000..596e2f8c --- /dev/null +++ b/tests/__mocks__/playlist.ts @@ -0,0 +1,17 @@ +import { AudioContext } from "standardized-audio-context-mock"; +import { Playlist } from "../../src/playlist"; +import { coordsToPoints } from "../../src/utils"; +import { mockAssetPool } from "./assetPool"; +import { mockRoundware } from "./roundware"; +import { faker } from "@faker-js/faker"; +const mockPlaylist = new Playlist({ + client: mockRoundware, + assetPool: mockAssetPool, + audioContext: new AudioContext(), + listenerPoint: coordsToPoints({ + latitude: +faker.address.latitude(), + longitude: +faker.address.longitude(), + }), +}); + +export { mockPlaylist }; diff --git a/tests/__mocks__/playlistAudioTrack.ts b/tests/__mocks__/playlistAudioTrack.ts new file mode 100644 index 00000000..f5f022b2 --- /dev/null +++ b/tests/__mocks__/playlistAudioTrack.ts @@ -0,0 +1,14 @@ +import { AudioContext } from "standardized-audio-context-mock"; +import { PlaylistAudiotrack } from "../../src/playlistAudioTrack"; +import { MOCK_AUDIO_TRACKS_DATA } from "./mock_api_responses"; +import { mockPlaylist } from "./playlist"; +import { mockRoundware } from "./roundware"; + +const mockPlaylistAudiotrack = new PlaylistAudiotrack({ + audioContext: new AudioContext(), + audioData: MOCK_AUDIO_TRACKS_DATA[0], + playlist: mockPlaylist, + client: mockRoundware, +}); + +export { mockPlaylistAudiotrack }; diff --git a/tests/__mocks__/roundware.ts b/tests/__mocks__/roundware.ts new file mode 100644 index 00000000..779abbb1 --- /dev/null +++ b/tests/__mocks__/roundware.ts @@ -0,0 +1,23 @@ +import { Roundware } from "../../src/roundware"; +import config from "./roundware.config"; +import { faker } from "@faker-js/faker"; +import { IAssetFilters } from "../../src/types/asset"; +export const mockRoundware = new Roundware({ + deviceId: "test", + geoListenMode: 1, + listenerLocation: { + latitude: +faker.address.latitude(), + longitude: +faker.address.longitude(), + }, + projectId: 1, + serverUrl: config.baseServerUrl, + speakerConfig: { + acceptableDelayMs: 100, + length: 100, + loop: true, + prefetch: true, + sync: true, + syncCheckInterval: 100, + }, + assetFilters: [] as IAssetFilters, +}); diff --git a/tests/__tests__/TrackStates.test.ts b/tests/__tests__/TrackStates.test.ts new file mode 100644 index 00000000..c115b6c5 --- /dev/null +++ b/tests/__tests__/TrackStates.test.ts @@ -0,0 +1,13 @@ +import { LoadingState } from "../../src/TrackStates"; +import { mockTrackOptions } from "../__mocks__/mixer/TrackOptions"; +import { mockPlaylistAudiotrack } from "../__mocks__/playlistAudioTrack"; +jest.mock("standardized-audio-context", () => + require("standardized-audio-context-mock") +); +describe("LoadingState", () => { + test("should initialize", () => { + const state = new LoadingState(mockPlaylistAudiotrack, mockTrackOptions); + expect(state).toBeTruthy(); + expect(state.asset).toBeNull(); + }); +}); diff --git a/tests/__tests__/assetFilters.test.ts b/tests/__tests__/assetFilters.test.ts index f3352435..4a01ceef 100644 --- a/tests/__tests__/assetFilters.test.ts +++ b/tests/__tests__/assetFilters.test.ts @@ -1,3 +1,8 @@ +import { faker } from "@faker-js/faker"; +import { lineString, point } from "@turf/helpers"; +import lineToPolygon from "@turf/line-to-polygon"; +import { addDays, subDays } from "date-fns"; +import { omit } from "lodash"; import { rankForGeofilteringEligibility } from "../../src/assetFilters"; import { assetDecorationMapper } from "../../src/assetPool"; import { @@ -15,22 +20,12 @@ import { timedAssetFilter, timedRepeatFilter, } from "../../src/roundware"; +import { IDecoratedAsset } from "../../src/types/asset"; import { getRandomAssetData, getRandomDecoratedAssetData, } from "../__mocks__/assetData"; -import { point } from "@turf/helpers"; -import { faker } from "@faker-js/faker"; -import { IDecoratedAsset } from "../../src/types/asset"; -import { - InvalidArgumentError, - RoundwareFrameworkError, -} from "../../src/errors/app.errors"; -import { addDays, subDays } from "date-fns"; -import { omit } from "lodash"; -import lineToPolygon from "@turf/line-to-polygon"; -import { lineString } from "@turf/helpers"; -import distance from "@turf/distance"; + describe("rankForGeofilteringEligibility", () => { test("should return false if geo listen mode is disabled", () => { expect( diff --git a/tests/__tests__/assetPool.test.ts b/tests/__tests__/assetPool.test.ts new file mode 100644 index 00000000..68d2eb59 --- /dev/null +++ b/tests/__tests__/assetPool.test.ts @@ -0,0 +1,55 @@ +import AssetPool, { assetDecorationMapper } from "../../src/assetPool"; +import { ITimedAssetData } from "../../src/types/index"; +import { getRandomAssetData } from "../__mocks__/assetData"; + +import { AssetSorter } from "../../src/assetSorter"; +import { silenceAudioBase64 } from "../../src/playlistAudioTrack"; +import { InvalidArgumentError } from "../../src/errors/app.errors"; +jest.mock(`../../src/assetSorter`); +const mockAssetSorter = AssetSorter as jest.MockedClass; + +describe("AssetPool", () => { + // instantiation; + test("should instantiate asset pool", () => { + const assetPool = new AssetPool({ + assets: getRandomAssetData(10), + }); + + expect(mockAssetSorter).toHaveBeenCalledTimes(1); + console.log(mockAssetSorter.mock.calls); + + expect(assetPool).toBeInstanceOf(AssetPool); + silenceAudioBase64; + }); + + test("should throw error if arguments of updateAssets() is not array", () => { + const assetPool = new AssetPool({ + assets: getRandomAssetData(10), + timedAssets: [], + }); + + expect(() => { + // @ts-expect-error + assetPool.updateAssets(1, 2); + }).toThrowError(InvalidArgumentError); + }); + + test("should decorate assets", () => { + const testAssetData = getRandomAssetData(10); + const timedAssets: ITimedAssetData[] = [ + { + asset_id: testAssetData[0].id, + + start: 23, + end: 10, + }, + ]; + const decoratedAssets = testAssetData.map( + assetDecorationMapper(timedAssets) + ); + + expect(decoratedAssets[0].timedAssetStart).toBe(23); + + expect(decoratedAssets[0].timedAssetEnd).toBe(10); + }); +});