From 5e749819f08113503daf6b4630ee40d1b4666265 Mon Sep 17 00:00:00 2001 From: Isaac Hunter Date: Wed, 1 Jan 2025 18:10:06 -0500 Subject: [PATCH] format, link, and fix tests --- .eslintrc.js | 1 + src/config/cache-options.ts | 2 +- src/jukebox/dto/jukebox-action.dto.ts | 6 ++---- src/jukebox/dto/player-state.dto.ts | 2 +- src/jukebox/dto/track-player-state.dto.ts | 2 +- src/jukebox/jukebox.gateway.ts | 4 ++-- src/jukebox/tests/jukebox.controller.spec.ts | 8 +++++--- src/jukebox/tests/jukebox.gateway.spec.ts | 11 +++++++---- src/jukebox/tests/jukebox.service.spec.ts | 17 +++++++++++++++++ .../tests/track-queue.service.spec.ts | 4 ++-- src/jukebox/track-queue/track-queue.service.ts | 6 +++--- src/spotify/spotify-auth.service.ts | 2 +- src/spotify/spotify-base.service.ts | 3 +-- src/spotify/tests/spotify.service.spec.ts | 7 ++----- src/utils/index.ts | 1 + src/utils/mock/mockUser.ts | 2 +- src/utils/providers/axios.provider.ts | 5 +++++ src/utils/providers/index.ts | 1 + src/utils/testing/mocking.ts | 4 ++-- 19 files changed, 56 insertions(+), 32 deletions(-) create mode 100644 src/utils/providers/index.ts diff --git a/.eslintrc.js b/.eslintrc.js index aaee9b8..aacc093 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -26,6 +26,7 @@ module.exports = { '@typescript-eslint/no-redeclare': 'off', '@typescript-eslint/space-before-function-paren': 'off', '@typescript-eslint/no-explicit-any': 'off', + '@typescript-eslint/no-empty-object-type': 'off', 'prefer-const': 'warn', // 'prettier/prettier': 'warn', 'prettier/prettier': ['warn', { endOfLine: 'auto' }], diff --git a/src/config/cache-options.ts b/src/config/cache-options.ts index 3e31155..0b83f56 100644 --- a/src/config/cache-options.ts +++ b/src/config/cache-options.ts @@ -1,4 +1,4 @@ -import { CacheModuleAsyncOptions } from '@nestjs/cache-manager' +import type { CacheModuleAsyncOptions } from '@nestjs/cache-manager' import { ConfigModule, ConfigService } from '@nestjs/config' import { redisStore } from 'cache-manager-redis-store' diff --git a/src/jukebox/dto/jukebox-action.dto.ts b/src/jukebox/dto/jukebox-action.dto.ts index 3a82ee4..83332aa 100644 --- a/src/jukebox/dto/jukebox-action.dto.ts +++ b/src/jukebox/dto/jukebox-action.dto.ts @@ -1,9 +1,7 @@ interface IJukeboxAction { jukebox_id: number action: 'like' | 'dislike' - + queue_index: number } -export class JukeboxActionDto { - -} \ No newline at end of file +export class JukeboxActionDto {} diff --git a/src/jukebox/dto/player-state.dto.ts b/src/jukebox/dto/player-state.dto.ts index da31273..4039446 100644 --- a/src/jukebox/dto/player-state.dto.ts +++ b/src/jukebox/dto/player-state.dto.ts @@ -1,4 +1,4 @@ -import { TrackMetaDto } from './track.dto' +import type { TrackMetaDto } from './track.dto' export class PlayerStateDto implements IPlayerState { jukebox_id: number diff --git a/src/jukebox/dto/track-player-state.dto.ts b/src/jukebox/dto/track-player-state.dto.ts index 52371a1..02e8993 100644 --- a/src/jukebox/dto/track-player-state.dto.ts +++ b/src/jukebox/dto/track-player-state.dto.ts @@ -12,5 +12,5 @@ export class PlayerAuxUpdateDto implements IPlayerAuxUpdate { progress: number is_playing: boolean default_next_tracks: ITrack[] - changed_tracks?: boolean; + changed_tracks?: boolean } diff --git a/src/jukebox/jukebox.gateway.ts b/src/jukebox/jukebox.gateway.ts index 79ac2bd..243e9f4 100644 --- a/src/jukebox/jukebox.gateway.ts +++ b/src/jukebox/jukebox.gateway.ts @@ -42,7 +42,7 @@ export class JukeboxGateway { if (changed_tracks) { prevState = { - current_track: null + current_track: null, } } @@ -50,7 +50,7 @@ export class JukeboxGateway { await this.queueSvc.setPlayerState(jukebox_id, { ...prevState, jukebox_id, - current_track: {...(prevState?.current_track || {}), ...current_track}, + current_track: { ...(prevState?.current_track || {}), ...current_track }, is_playing, progress, default_next_tracks, diff --git a/src/jukebox/tests/jukebox.controller.spec.ts b/src/jukebox/tests/jukebox.controller.spec.ts index de7408f..a7bc907 100644 --- a/src/jukebox/tests/jukebox.controller.spec.ts +++ b/src/jukebox/tests/jukebox.controller.spec.ts @@ -7,11 +7,12 @@ import { SpotifyAccount } from 'src/spotify/entities/spotify-account.entity' import { SpotifyAuthService } from 'src/spotify/spotify-auth.service' import { SpotifyService } from 'src/spotify/spotify.service' import { AxiosProvider } from 'src/utils/providers/axios.provider' -import { mockCache, mockRepo } from 'src/utils/testing' +import { CacheMockProvider, mockRepo } from 'src/utils/testing' import { Jukebox, JukeboxLinkAssignment } from '../entities/jukebox.entity' import { JukeboxController } from '../jukebox.controller' import { JukeboxService } from '../jukebox.service' -import { TrackQueueService } from '../track-queue/queue.service' +import { TrackQueueService } from '../track-queue/track-queue.service' +import { JukeboxGateway } from '../jukebox.gateway' describe('JukeboxController', () => { let controller: JukeboxController @@ -22,12 +23,13 @@ describe('JukeboxController', () => { controllers: [JukeboxController], providers: [ AxiosProvider, + CacheMockProvider, SpotifyAuthService, JukeboxService, SpotifyService, TrackQueueService, + JukeboxGateway, AppGateway, - mockCache, { provide: getRepositoryToken(Jukebox), useFactory: mockRepo, diff --git a/src/jukebox/tests/jukebox.gateway.spec.ts b/src/jukebox/tests/jukebox.gateway.spec.ts index 1e53d50..385bc76 100644 --- a/src/jukebox/tests/jukebox.gateway.spec.ts +++ b/src/jukebox/tests/jukebox.gateway.spec.ts @@ -1,12 +1,14 @@ -import { Test, TestingModule } from '@nestjs/testing' +import type { TestingModule } from '@nestjs/testing' +import { Test } from '@nestjs/testing' import { getRepositoryToken } from '@nestjs/typeorm' import { AppGateway } from 'src/app.gateway' import { SpotifyService } from 'src/spotify/spotify.service' -import { mockCache, mockRepo } from 'src/utils/testing' +import { CacheMockProvider, mockRepo } from 'src/utils/testing' import { Jukebox, JukeboxLinkAssignment } from '../entities/jukebox.entity' import { JukeboxGateway } from '../jukebox.gateway' import { JukeboxService } from '../jukebox.service' -import { TrackQueueService } from '../track-queue/queue.service' +import { TrackQueueService } from '../track-queue/track-queue.service' +import { AxiosMockProvider } from 'src/utils' describe('JukeboxGateway', () => { let gateway: JukeboxGateway @@ -14,12 +16,13 @@ describe('JukeboxGateway', () => { beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ + CacheMockProvider, + AxiosMockProvider, JukeboxGateway, TrackQueueService, JukeboxService, SpotifyService, AppGateway, - mockCache, { provide: getRepositoryToken(Jukebox), useFactory: mockRepo, diff --git a/src/jukebox/tests/jukebox.service.spec.ts b/src/jukebox/tests/jukebox.service.spec.ts index cc29316..2eb8baf 100644 --- a/src/jukebox/tests/jukebox.service.spec.ts +++ b/src/jukebox/tests/jukebox.service.spec.ts @@ -1,11 +1,15 @@ +import { CACHE_MANAGER } from '@nestjs/cache-manager' import type { TestingModule } from '@nestjs/testing' import { Test } from '@nestjs/testing' import { getRepositoryToken } from '@nestjs/typeorm' +import Axios from 'axios' import { SpotifyAccount } from 'src/spotify/entities/spotify-account.entity' +import { SpotifyService } from 'src/spotify/spotify.service' import type { MockType } from 'src/utils/testing' import type { Repository } from 'typeorm' import { Jukebox, JukeboxLinkAssignment } from '../entities/jukebox.entity' import { JukeboxService } from '../jukebox.service' +import { TrackQueueService } from '../track-queue/track-queue.service' describe('JukeboxService', () => { let service: JukeboxService @@ -19,7 +23,20 @@ describe('JukeboxService', () => { const module: TestingModule = await Test.createTestingModule({ providers: [ + { + provide: Axios.Axios, + useValue: Axios.create(), + }, + { + provide: CACHE_MANAGER, + useValue: { + get: () => 'any value', + set: () => jest.fn(), + }, + }, JukeboxService, + SpotifyService, + TrackQueueService, { provide: getRepositoryToken(Jukebox), useFactory: mockRepo, diff --git a/src/jukebox/track-queue/tests/track-queue.service.spec.ts b/src/jukebox/track-queue/tests/track-queue.service.spec.ts index 5ae6118..49e6e4f 100644 --- a/src/jukebox/track-queue/tests/track-queue.service.spec.ts +++ b/src/jukebox/track-queue/tests/track-queue.service.spec.ts @@ -1,6 +1,6 @@ import type { TestingModule } from '@nestjs/testing' import { Test } from '@nestjs/testing' -import { mockCache } from 'src/utils/testing' +import { CacheMockProvider } from 'src/utils/testing' import { TrackQueueService } from '../track-queue.service' describe('TrackQueueService', () => { @@ -8,7 +8,7 @@ describe('TrackQueueService', () => { beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [TrackQueueService, mockCache], + providers: [TrackQueueService, CacheMockProvider], }).compile() service = module.get(TrackQueueService) diff --git a/src/jukebox/track-queue/track-queue.service.ts b/src/jukebox/track-queue/track-queue.service.ts index 86c46fe..321a2b9 100644 --- a/src/jukebox/track-queue/track-queue.service.ts +++ b/src/jukebox/track-queue/track-queue.service.ts @@ -4,12 +4,12 @@ import { Cache } from 'cache-manager' import { randomUUID } from 'crypto' import { PlayerMetaStateDto } from '../dto/player-state.dto' -export class QueueItem { +export class QueueItem { recommended_by: string constructor(public item: T) {} } -export class Queue { +export class Queue { constructor(readonly items: QueueItem[]) {} // Pushes a track to the end of the queue and returns the new length @@ -188,7 +188,7 @@ export class TrackQueueService { const playerState = await this.getPlayerState(jukeboxId) const updatedState = cb(playerState) await this.commitPlayerState(jukeboxId, updatedState) - + return updatedState } } diff --git a/src/spotify/spotify-auth.service.ts b/src/spotify/spotify-auth.service.ts index d4edd43..d5cb80c 100644 --- a/src/spotify/spotify-auth.service.ts +++ b/src/spotify/spotify-auth.service.ts @@ -96,7 +96,7 @@ export class SpotifyAuthService extends SpotifyBaseService { public async refreshSpotifyAccount( account: { spotify_email: string } | SpotifyAccount, - force=false, + force = false, ): Promise { let spotifyAccount: SpotifyAccount diff --git a/src/spotify/spotify-base.service.ts b/src/spotify/spotify-base.service.ts index 154dff8..1f07e87 100644 --- a/src/spotify/spotify-base.service.ts +++ b/src/spotify/spotify-base.service.ts @@ -1,10 +1,9 @@ import { SpotifyApi } from '@spotify/web-api-ts-sdk' import { SPOTIFY_CLIENT_ID } from 'src/config' -import { SpotifyTokensDto } from './dto/spotify-tokens.dto' +import type { SpotifyTokensDto } from './dto/spotify-tokens.dto' import { Axios } from 'axios' export class SpotifyBaseService { - // constructor(protected axios: Axios) {} protected getSdk(tokens: SpotifyTokensDto) { return SpotifyApi.withAccessToken(SPOTIFY_CLIENT_ID, { access_token: tokens.access_token, diff --git a/src/spotify/tests/spotify.service.spec.ts b/src/spotify/tests/spotify.service.spec.ts index 92f6937..5eb9a9c 100644 --- a/src/spotify/tests/spotify.service.spec.ts +++ b/src/spotify/tests/spotify.service.spec.ts @@ -2,8 +2,8 @@ import { getModelToken } from '@nestjs/mongoose' import type { TestingModule } from '@nestjs/testing' import { Test } from '@nestjs/testing' import { getRepositoryToken } from '@nestjs/typeorm' -import Axios from 'axios' import { Model } from 'mongoose' +import { AxiosMockProvider } from 'src/utils' import type { MockType } from 'src/utils/testing' import type { Repository } from 'typeorm' import { SpotifyAccount } from '../entities/spotify-account.entity' @@ -16,12 +16,9 @@ describe('SpotifyService', () => { const mockSpotifyLinkRepo: () => MockType> = jest.fn(() => ({})) const module: TestingModule = await Test.createTestingModule({ providers: [ + AxiosMockProvider, SpotifyAuthService, { provide: getModelToken(SpotifyAccount.name), useValue: Model }, - { - provide: Axios.Axios, - useValue: Axios.create(), - }, { provide: getRepositoryToken(SpotifyAccount), useFactory: mockSpotifyLinkRepo, diff --git a/src/utils/index.ts b/src/utils/index.ts index c55ae8f..ca9ab2b 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,6 +2,7 @@ export * from './filters' export * from './helpers' export * from './interceptors' export * from './mock' +export * from './providers' // Do not include testing in export, auto imports testing modules // export * from './testing' diff --git a/src/utils/mock/mockUser.ts b/src/utils/mock/mockUser.ts index 131b9ad..5e8ea40 100644 --- a/src/utils/mock/mockUser.ts +++ b/src/utils/mock/mockUser.ts @@ -3,5 +3,5 @@ export const mockUser: IUser = { email: 'user@example.com', first_name: 'John', last_name: 'Doe', - username: 'JohnDoe52' + username: 'JohnDoe52', } diff --git a/src/utils/providers/axios.provider.ts b/src/utils/providers/axios.provider.ts index d848bec..c20e021 100644 --- a/src/utils/providers/axios.provider.ts +++ b/src/utils/providers/axios.provider.ts @@ -4,3 +4,8 @@ export const AxiosProvider = { provide: Axios.Axios, useValue: Axios.create(), } + +export const AxiosMockProvider = { + provide: Axios.Axios, + useValue: Axios.create(), +} diff --git a/src/utils/providers/index.ts b/src/utils/providers/index.ts new file mode 100644 index 0000000..81de77c --- /dev/null +++ b/src/utils/providers/index.ts @@ -0,0 +1 @@ +export * from './axios.provider' diff --git a/src/utils/testing/mocking.ts b/src/utils/testing/mocking.ts index 96997fd..bd48451 100644 --- a/src/utils/testing/mocking.ts +++ b/src/utils/testing/mocking.ts @@ -1,12 +1,12 @@ import { jest } from '@jest/globals' import { CACHE_MANAGER } from '@nestjs/cache-manager' -import { Repository } from 'typeorm' +import type { Repository } from 'typeorm' export type MockType = { [P in keyof T]?: jest.Mock } -export const mockCache = { +export const CacheMockProvider = { provide: CACHE_MANAGER, useValue: { get: () => '',