Skip to content

Commit

Permalink
feat: add jest for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
KagChi committed Apr 4, 2022
1 parent ec5004b commit ec4ed07
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"lint:fix": "turbo run lint:fix",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"prepareForRelease": "turbo run prepareForRelease",
"release": "turbo run release"
"release": "turbo run release",
"test": "turbo run test"
},
"devDependencies": {
"prettier": "^2.5.1",
Expand Down
7 changes: 6 additions & 1 deletion packages/deezer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,28 @@
"lint:fix": "eslint src --fix",
"format": "prettier --write {src,tests}/**/*.ts",
"prepareForRelease": "npm run build && npm run format && npm run lint && npm run lint:fix",
"test": "jest test",
"release": "npm publish --access public"
},
"devDependencies": {
"@babel/preset-env": "^7.16.11",
"@babel/preset-typescript": "^7.16.7",
"@babel/types": "7.17.0",
"@sapphire/eslint-config": "4.3.2",
"@sapphire/prettier-config": "1.4.1",
"@sapphire/ts-config": "3.3.3",
"@types/jest": "^27.4.1",
"@types/ws": "^8.5.3",
"eslint": "8.11.0",
"jest": "^27.5.1",
"prettier": "2.6.0",
"rimraf": "3.0.2",
"typescript": "4.6.2",
"unbuild": "0.7.0"
},
"prettier": "@sapphire/prettier-config",
"dependencies": {
"@kirishima/core": "^0.6.3",
"@kirishima/core": "^0.6.4",
"@kirishima/fetch": "^0.2.1",
"lavalink-api-types": "^0.1.9",
"undici": "^4.16.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/deezer/src/Structures/KirisihimaPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export class KirishimaPlayer extends Structure.get('KirishimaPlayer') {
return this.kirishima.resolveTracks(track.isrc);
}

return this.kirishima.resolveTracks(`${track.info.title} - ${track.info.author ? track.info.author : ''}`);
return this.kirishima.resolveTracks(`${track.info?.title} - ${track.info?.author ? track.info.author : ''}`);
}
}
38 changes: 38 additions & 0 deletions packages/deezer/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Kirishima } from '@kirishima/core';
import { LoadTypeEnum } from 'lavalink-api-types';
import crypto from 'node:crypto';
import { KirishimaDeezer } from '..';

const kirishima = new Kirishima({
send: () => {
/** Do nothing */
},
plugins: [new KirishimaDeezer()],
nodes: [
{
url: 'usui-linku.kadantte.moe:443',
password: 'Usui#0256'
}
]
});

test('Do search tracks', async () => {
if (!kirishima.options.clientId && !kirishima.nodes.size) await kirishima.initialize(crypto.randomBytes(10).toString('hex'));
const tracks = await kirishima.resolveTracks({ source: 'deezer', query: 'yoasobi' }, kirishima.nodes.first());
expect(tracks.loadType).toBe(LoadTypeEnum.SEARCH_RESULT);
expect(tracks.tracks.length).not.toBe(0);
});

test('Do get single track', async () => {
if (!kirishima.options.clientId && !kirishima.nodes.size) await kirishima.initialize(crypto.randomBytes(10).toString('hex'));
const tracks = await kirishima.resolveTracks('https://www.deezer.com/en/track/1053803772', kirishima.nodes.first());
expect(tracks.loadType).toBe(LoadTypeEnum.TRACK_LOADED);
expect(tracks.tracks.length).not.toBe(0);
});

test('Do get playlist tracks', async () => {
if (!kirishima.options.clientId && !kirishima.nodes.size) await kirishima.initialize(crypto.randomBytes(10).toString('hex'));
const tracks = await kirishima.resolveTracks('https://www.deezer.com/en/playlist/9314027622', kirishima.nodes.first());
expect(tracks.loadType).toBe(LoadTypeEnum.PLAYLIST_LOADED);
expect(tracks.tracks.length).not.toBe(0);
});
16 changes: 5 additions & 11 deletions packages/deezer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Kirishima, KirishimaNode, KirishimaPlugin, LoadTrackResponse, KirishimaTrack, Structure } from '@kirishima/core';
import { Kirishima, KirishimaNode, KirishimaPlugin, LoadTrackResponse, Structure } from '@kirishima/core';
import { fetch, FetchResultTypes } from '@kirishima/fetch';
import { LoadTypeEnum } from 'lavalink-api-types';
import { KirishimaPartialTrack } from './Structures/KirishimaPartialTrack';
Expand All @@ -14,10 +14,7 @@ export class KirishimaDeezer extends KirishimaPlugin {

private baseURL = 'https://api.deezer.com/';
private regex = /^(?:https?:\/\/|)?(?:www\.)?deezer\.com\/(?:\w{2}\/)?(?<type>track|album|playlist)\/(?<id>\d+)/;
private _resolveTracks!: (
options: string | { source?: string | undefined; query: string },
node?: KirishimaNode
) => Promise<LoadTrackResponse<KirishimaPartialTrack | KirishimaTrack>>;
private _resolveTracks!: (options: string | { source?: string | undefined; query: string }, node?: KirishimaNode) => Promise<LoadTrackResponse>;

public constructor() {
super({
Expand All @@ -31,22 +28,19 @@ export class KirishimaDeezer extends KirishimaPlugin {
kirishima.resolveTracks = this.resolveTracks.bind(this);
}

public resolveTracks(
options: string | { source?: string | undefined; query: string },
node?: KirishimaNode
): Promise<LoadTrackResponse<KirishimaPartialTrack | KirishimaTrack>> {
public resolveTracks(options: string | { source?: string | undefined; query: string }, node?: KirishimaNode): Promise<LoadTrackResponse> {
const query = typeof options === 'string' ? options : options.query;
const source = typeof options === 'string' ? undefined : options.source;
if (source === 'deezer') {
return this.searchTracks(query) as unknown as Promise<LoadTrackResponse<KirishimaPartialTrack | KirishimaTrack>>;
return this.searchTracks(query) as unknown as Promise<LoadTrackResponse>;
}

const [, type, id] = query.match(this.regex) ?? [];

if (type in this.resolvers) {
const resolver = this.resolvers[type as keyof typeof this.resolvers];
if (resolver) {
return resolver(id) as unknown as Promise<LoadTrackResponse<KirishimaPartialTrack | KirishimaTrack>>;
return resolver(id) as unknown as Promise<LoadTrackResponse>;
}
}

Expand Down
3 changes: 3 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"release": {
"outputs": []
},
"test": {
"outputs": []
},
"dev": {
"cache": false
}
Expand Down

0 comments on commit ec4ed07

Please sign in to comment.