Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-jadhav committed Aug 19, 2023
1 parent 9773157 commit 390453e
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion src/playlistAudioTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ export class PlaylistAudiotrack {
client,
}: {
audioContext: IAudioContext;

audioData: IAudioTrackData;
playlist: Playlist;
client: Roundware;
Expand Down
2 changes: 1 addition & 1 deletion src/types/audioTrack.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export interface IAudioTrackData {
banned_duration: number;
tag_filters: any[];
project_id: number;
timed_asset_priority: number;
timed_asset_priority: string;
}
9 changes: 9 additions & 0 deletions tests/__mocks__/assetPool.ts
Original file line number Diff line number Diff line change
@@ -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 };
5 changes: 5 additions & 0 deletions tests/__mocks__/mixer/TrackOptions.ts
Original file line number Diff line number Diff line change
@@ -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 };
17 changes: 17 additions & 0 deletions tests/__mocks__/playlist.ts
Original file line number Diff line number Diff line change
@@ -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 };
14 changes: 14 additions & 0 deletions tests/__mocks__/playlistAudioTrack.ts
Original file line number Diff line number Diff line change
@@ -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 };
23 changes: 23 additions & 0 deletions tests/__mocks__/roundware.ts
Original file line number Diff line number Diff line change
@@ -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,
});
13 changes: 13 additions & 0 deletions tests/__tests__/TrackStates.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
19 changes: 7 additions & 12 deletions tests/__tests__/assetFilters.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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(
Expand Down
55 changes: 55 additions & 0 deletions tests/__tests__/assetPool.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof AssetSorter>;

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);
});
});

0 comments on commit 390453e

Please sign in to comment.