Skip to content

Commit

Permalink
format, link, and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IkeHunter committed Jan 1, 2025
1 parent 15b8a60 commit 5e74981
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 32 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' }],
Expand Down
2 changes: 1 addition & 1 deletion src/config/cache-options.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
6 changes: 2 additions & 4 deletions src/jukebox/dto/jukebox-action.dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
interface IJukeboxAction {
jukebox_id: number
action: 'like' | 'dislike'

queue_index: number
}
export class JukeboxActionDto {

}
export class JukeboxActionDto {}
2 changes: 1 addition & 1 deletion src/jukebox/dto/player-state.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TrackMetaDto } from './track.dto'
import type { TrackMetaDto } from './track.dto'

export class PlayerStateDto implements IPlayerState {
jukebox_id: number
Expand Down
2 changes: 1 addition & 1 deletion src/jukebox/dto/track-player-state.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export class PlayerAuxUpdateDto implements IPlayerAuxUpdate {
progress: number
is_playing: boolean
default_next_tracks: ITrack[]
changed_tracks?: boolean;
changed_tracks?: boolean
}
4 changes: 2 additions & 2 deletions src/jukebox/jukebox.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ export class JukeboxGateway {

if (changed_tracks) {
prevState = {
current_track: null
current_track: null,
}
}

// Set current player state
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,
Expand Down
8 changes: 5 additions & 3 deletions src/jukebox/tests/jukebox.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,12 +23,13 @@ describe('JukeboxController', () => {
controllers: [JukeboxController],
providers: [
AxiosProvider,
CacheMockProvider,
SpotifyAuthService,
JukeboxService,
SpotifyService,
TrackQueueService,
JukeboxGateway,
AppGateway,
mockCache,
{
provide: getRepositoryToken(Jukebox),
useFactory: mockRepo<Jukebox>,
Expand Down
11 changes: 7 additions & 4 deletions src/jukebox/tests/jukebox.gateway.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
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

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
CacheMockProvider,
AxiosMockProvider,
JukeboxGateway,
TrackQueueService,
JukeboxService,
SpotifyService,
AppGateway,
mockCache,
{
provide: getRepositoryToken(Jukebox),
useFactory: mockRepo<Jukebox>,
Expand Down
17 changes: 17 additions & 0 deletions src/jukebox/tests/jukebox.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/jukebox/track-queue/tests/track-queue.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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', () => {
let service: TrackQueueService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [TrackQueueService, mockCache],
providers: [TrackQueueService, CacheMockProvider],
}).compile()

service = module.get<TrackQueueService>(TrackQueueService)
Expand Down
6 changes: 3 additions & 3 deletions src/jukebox/track-queue/track-queue.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Cache } from 'cache-manager'
import { randomUUID } from 'crypto'
import { PlayerMetaStateDto } from '../dto/player-state.dto'

export class QueueItem<T> {
export class QueueItem<T = unknown> {
recommended_by: string
constructor(public item: T) {}
}

export class Queue<T> {
export class Queue<T = unknown> {
constructor(readonly items: QueueItem<T>[]) {}

// Pushes a track to the end of the queue and returns the new length
Expand Down Expand Up @@ -188,7 +188,7 @@ export class TrackQueueService {
const playerState = await this.getPlayerState(jukeboxId)
const updatedState = cb(playerState)
await this.commitPlayerState(jukeboxId, updatedState)

return updatedState
}
}
2 changes: 1 addition & 1 deletion src/spotify/spotify-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class SpotifyAuthService extends SpotifyBaseService {

public async refreshSpotifyAccount(
account: { spotify_email: string } | SpotifyAccount,
force=false,
force = false,
): Promise<SpotifyAccount> {
let spotifyAccount: SpotifyAccount

Expand Down
3 changes: 1 addition & 2 deletions src/spotify/spotify-base.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 2 additions & 5 deletions src/spotify/tests/spotify.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -16,12 +16,9 @@ describe('SpotifyService', () => {
const mockSpotifyLinkRepo: () => MockType<Repository<SpotifyAccount>> = jest.fn(() => ({}))
const module: TestingModule = await Test.createTestingModule({
providers: [
AxiosMockProvider,
SpotifyAuthService,
{ provide: getModelToken(SpotifyAccount.name), useValue: Model<SpotifyAccount> },
{
provide: Axios.Axios,
useValue: Axios.create(),
},
{
provide: getRepositoryToken(SpotifyAccount),
useFactory: mockSpotifyLinkRepo,
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 1 addition & 1 deletion src/utils/mock/mockUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export const mockUser: IUser = {
email: '[email protected]',
first_name: 'John',
last_name: 'Doe',
username: 'JohnDoe52'
username: 'JohnDoe52',
}
5 changes: 5 additions & 0 deletions src/utils/providers/axios.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export const AxiosProvider = {
provide: Axios.Axios,
useValue: Axios.create(),
}

export const AxiosMockProvider = {
provide: Axios.Axios,
useValue: Axios.create(),
}
1 change: 1 addition & 0 deletions src/utils/providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './axios.provider'
4 changes: 2 additions & 2 deletions src/utils/testing/mocking.ts
Original file line number Diff line number Diff line change
@@ -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<T> = {
[P in keyof T]?: jest.Mock<any>
}

export const mockCache = {
export const CacheMockProvider = {
provide: CACHE_MANAGER,
useValue: {
get: () => '',
Expand Down

0 comments on commit 5e74981

Please sign in to comment.