From f5a356e020d98bcc447059ec48b16af7e283c21a Mon Sep 17 00:00:00 2001 From: Naoki Kishi Date: Mon, 10 Aug 2020 17:36:27 +0900 Subject: [PATCH] fix test --- usecase/session_state.go | 32 +-- usecase/session_state_test.go | 381 ++++++++++++++++++++++++++++++++++ usecase/session_timer_test.go | 57 ----- web/handler/session_test.go | 240 +-------------------- 4 files changed, 409 insertions(+), 301 deletions(-) create mode 100644 usecase/session_state_test.go diff --git a/usecase/session_state.go b/usecase/session_state.go index ba5344f1..5241ad30 100644 --- a/usecase/session_state.go +++ b/usecase/session_state.go @@ -70,7 +70,16 @@ func (s *SessionStateUseCase) nextTrackInPlay(ctx context.Context, sessionID str // nextTrackInPause はsessionのstateがPAUSEの時のnextTrackの処理を行います func (s *SessionStateUseCase) nextTrackInPause(ctx context.Context, sessionID string) error { - _, err := s.sessionRepo.DoInTx(ctx, func(ctx context.Context) (interface{}, error) { + _, err := s.sessionRepo.DoInTx(ctx, s.nextTrackInPauseTx(sessionID)) + if err != nil { + return fmt.Errorf("nextTrackInPause transaction: %w", err) + } + + return nil +} + +func (s *SessionStateUseCase) nextTrackInPauseTx(sessionID string) func(ctx context.Context) (interface{}, error) { + return func(ctx context.Context) (interface{}, error) { session, err := s.sessionRepo.FindByIDForUpdate(ctx, sessionID) if err != nil { return nil, fmt.Errorf("find session: %w", err) @@ -108,18 +117,22 @@ func (s *SessionStateUseCase) nextTrackInPause(ctx context.Context, sessionID st Msg: entity.NewEventNextTrack(session.QueueHead), }) return nil, nil - }) - if err != nil { - return fmt.Errorf("nextTrackInPause transaction: %w", err) } - - return nil } // nextTrackInStop はsessionのstateがSTOPの時のnextTrackの処理を行います // stopToPlayで曲がResetされ、再度Spotifyのキューに積まれるため、Enqueueを行っていません func (s *SessionStateUseCase) nextTrackInStop(ctx context.Context, sessionID string) error { - _, err := s.sessionRepo.DoInTx(ctx, func(ctx context.Context) (interface{}, error) { + _, err := s.sessionRepo.DoInTx(ctx, s.nextTrackInStopTx(sessionID)) + if err != nil { + return fmt.Errorf("nextTrackInStop transaction: %w", err) + } + + return nil +} + +func (s *SessionStateUseCase) nextTrackInStopTx(sessionID string) func(ctx context.Context) (interface{}, error) { + return func(ctx context.Context) (interface{}, error) { session, err := s.sessionRepo.FindByIDForUpdate(ctx, sessionID) if err != nil { return nil, fmt.Errorf("find session :%w", err) @@ -146,12 +159,7 @@ func (s *SessionStateUseCase) nextTrackInStop(ctx context.Context, sessionID str }) return nil, nil - }) - if err != nil { - return fmt.Errorf("nextTrackInStop transaction: %w", err) } - - return nil } // ChangeSessionState は与えられたセッションのstateを操作します。 diff --git a/usecase/session_state_test.go b/usecase/session_state_test.go new file mode 100644 index 00000000..4b8aebcd --- /dev/null +++ b/usecase/session_state_test.go @@ -0,0 +1,381 @@ +package usecase + +import ( + "context" + "testing" + "time" + + "github.com/camphor-/relaym-server/domain/service" + + "github.com/camphor-/relaym-server/domain/entity" + "github.com/camphor-/relaym-server/domain/event" + "github.com/camphor-/relaym-server/domain/mock_event" + "github.com/camphor-/relaym-server/domain/mock_repository" + "github.com/camphor-/relaym-server/domain/mock_spotify" + "github.com/golang/mock/gomock" +) + +func TestSessionStateUseCase_nextTrackInPauseTx(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + sessionID string + userID string + addToTimerSessionID string + prepareMockPlayerCliFn func(m *mock_spotify.MockPlayer) + prepareMockSessionRepoFn func(m *mock_repository.MockSession) + prepareMockPusherFn func(m *mock_event.MockPusher) + prepareMockTrackCliFn func(m *mock_spotify.MockTrackClient) + prepareMockUserRepoFn func(m *mock_repository.MockUser) + wantErr bool + }{ + { + name: "Pauseかつ次の曲が存在すると次の曲に遷移し、202", + sessionID: "sessionID", + userID: "userID", + addToTimerSessionID: "sessionID", + prepareMockPlayerCliFn: func(m *mock_spotify.MockPlayer) { + m.EXPECT().GoNextTrack(gomock.Any(), "deviceID").Return(nil) + m.EXPECT().Pause(gomock.Any(), "deviceID").Return(nil) + }, + prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { + m.EXPECT().FindByIDForUpdate(gomock.Any(), "sessionID").Return( + &entity.Session{ + ID: "sessionID", + Name: "name", + CreatorID: "creatorID", + DeviceID: "deviceID", + StateType: "PAUSE", + QueueHead: 0, + QueueTracks: []*entity.QueueTrack{ + { + Index: 0, + URI: "spotify:track:track_uri1", + SessionID: "sessionID", + }, + { + Index: 1, + URI: "spotify:track:track_uri2", + SessionID: "sessionID", + }, + }, + ExpiredAt: time.Time{}, + AllowToControlByOthers: true, + ProgressWhenPaused: 0, + }, nil) + m.EXPECT().Update(gomock.Any(), &entity.Session{ + ID: "sessionID", + Name: "name", + CreatorID: "creatorID", + DeviceID: "deviceID", + StateType: "PAUSE", + QueueHead: 1, + QueueTracks: []*entity.QueueTrack{ + { + Index: 0, + URI: "spotify:track:track_uri1", + SessionID: "sessionID", + }, + { + Index: 1, + URI: "spotify:track:track_uri2", + SessionID: "sessionID", + }, + }, + ExpiredAt: time.Time{}, + AllowToControlByOthers: true, + ProgressWhenPaused: 0, + }) + }, + prepareMockPusherFn: func(m *mock_event.MockPusher) { + m.EXPECT().Push(&event.PushMessage{ + SessionID: "sessionID", + Msg: entity.NewEventNextTrack(1), + }) + }, + prepareMockTrackCliFn: func(m *mock_spotify.MockTrackClient) {}, + prepareMockUserRepoFn: func(m *mock_repository.MockUser) {}, + wantErr: false, + }, + { + name: "Pauseかつ次の曲が3曲存在すると次の曲に遷移し、三曲先がEnqueueされ、202", + sessionID: "sessionID", + userID: "userID", + addToTimerSessionID: "sessionID", + prepareMockPlayerCliFn: func(m *mock_spotify.MockPlayer) { + m.EXPECT().GoNextTrack(gomock.Any(), "deviceID").Return(nil) + m.EXPECT().Pause(gomock.Any(), "deviceID").Return(nil) + m.EXPECT().Enqueue(gomock.Any(), "spotify:track:track_uri4", "deviceID").Return(nil) + }, + prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { + m.EXPECT().FindByIDForUpdate(gomock.Any(), "sessionID").Return( + &entity.Session{ + ID: "sessionID", + Name: "name", + CreatorID: "creatorID", + DeviceID: "deviceID", + StateType: "PAUSE", + QueueHead: 0, + QueueTracks: []*entity.QueueTrack{ + { + Index: 0, + URI: "spotify:track:track_uri1", + SessionID: "sessionID", + }, + { + Index: 1, + URI: "spotify:track:track_uri2", + SessionID: "sessionID", + }, + { + Index: 2, + URI: "spotify:track:track_uri3", + SessionID: "sessionID", + }, + { + Index: 3, + URI: "spotify:track:track_uri4", + SessionID: "sessionID", + }, + }, + ExpiredAt: time.Time{}, + AllowToControlByOthers: true, + ProgressWhenPaused: 0, + }, nil) + m.EXPECT().Update(gomock.Any(), &entity.Session{ + ID: "sessionID", + Name: "name", + CreatorID: "creatorID", + DeviceID: "deviceID", + StateType: "PAUSE", + QueueHead: 1, + QueueTracks: []*entity.QueueTrack{ + { + Index: 0, + URI: "spotify:track:track_uri1", + SessionID: "sessionID", + }, + { + Index: 1, + URI: "spotify:track:track_uri2", + SessionID: "sessionID", + }, + { + Index: 2, + URI: "spotify:track:track_uri3", + SessionID: "sessionID", + }, + { + Index: 3, + URI: "spotify:track:track_uri4", + SessionID: "sessionID", + }, + }, + ExpiredAt: time.Time{}, + AllowToControlByOthers: true, + ProgressWhenPaused: 0, + }) + }, + prepareMockPusherFn: func(m *mock_event.MockPusher) { + m.EXPECT().Push(&event.PushMessage{ + SessionID: "sessionID", + Msg: entity.NewEventNextTrack(1), + }) + }, + prepareMockTrackCliFn: func(m *mock_spotify.MockTrackClient) {}, + prepareMockUserRepoFn: func(m *mock_repository.MockUser) {}, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // モックの準備 + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + uc := newSessionStateUseCaseForTest(t, ctrl, tt.prepareMockPlayerCliFn, tt.prepareMockTrackCliFn, + tt.prepareMockPusherFn, tt.prepareMockUserRepoFn, tt.prepareMockSessionRepoFn, tt.addToTimerSessionID) + + ctx := context.Background() + ctx = service.SetUserIDToContext(ctx, tt.userID) + + _, err := uc.nextTrackInPauseTx(tt.sessionID)(ctx) + if (err != nil) != tt.wantErr { + t.Errorf("NextTrack() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestSessionStateUseCase_nextTrackInStopTx(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + sessionID string + userID string + addToTimerSessionID string + prepareMockPlayerCliFn func(m *mock_spotify.MockPlayer) + prepareMockSessionRepoFn func(m *mock_repository.MockSession) + prepareMockPusherFn func(m *mock_event.MockPusher) + prepareMockTrackCliFn func(m *mock_spotify.MockTrackClient) + prepareMockUserRepoFn func(m *mock_repository.MockUser) + wantErr bool + }{ + { + name: "STOPかつ次の曲が存在する時に次の曲にSTOPのまま遷移,202", + sessionID: "sessionID", + userID: "userID", + addToTimerSessionID: "sessionID", + prepareMockPlayerCliFn: func(m *mock_spotify.MockPlayer) {}, + prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { + m.EXPECT().FindByIDForUpdate(gomock.Any(), "sessionID").Return( + &entity.Session{ + ID: "sessionID", + Name: "name", + CreatorID: "creatorID", + DeviceID: "deviceID", + StateType: "STOP", + QueueHead: 0, + QueueTracks: []*entity.QueueTrack{ + { + Index: 0, + URI: "spotify:track:track_uri1", + SessionID: "sessionID", + }, + { + Index: 1, + URI: "spotify:track:track_uri2", + SessionID: "sessionID", + }, + }, + ExpiredAt: time.Time{}, + AllowToControlByOthers: true, + ProgressWhenPaused: 0, + }, nil) + m.EXPECT().Update(gomock.Any(), &entity.Session{ + ID: "sessionID", + Name: "name", + CreatorID: "creatorID", + DeviceID: "deviceID", + StateType: "STOP", + QueueHead: 1, + QueueTracks: []*entity.QueueTrack{ + { + Index: 0, + URI: "spotify:track:track_uri1", + SessionID: "sessionID", + }, + { + Index: 1, + URI: "spotify:track:track_uri2", + SessionID: "sessionID", + }, + }, + ExpiredAt: time.Time{}, + AllowToControlByOthers: true, + ProgressWhenPaused: 0, + }).Return(nil) + }, + prepareMockPusherFn: func(m *mock_event.MockPusher) { + m.EXPECT().Push(&event.PushMessage{ + SessionID: "sessionID", + Msg: entity.NewEventNextTrack(1), + }) + }, + prepareMockTrackCliFn: func(m *mock_spotify.MockTrackClient) {}, + prepareMockUserRepoFn: func(m *mock_repository.MockUser) {}, + wantErr: false, + }, + { + name: "STOPかつ次の曲が存在しない時にErrNextQueueTrackNotFound,400", + sessionID: "sessionID", + userID: "userID", + addToTimerSessionID: "sessionID", + prepareMockPlayerCliFn: func(m *mock_spotify.MockPlayer) {}, + prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { + m.EXPECT().FindByIDForUpdate(gomock.Any(), "sessionID").Return( + &entity.Session{ + ID: "sessionID", + Name: "name", + CreatorID: "creatorID", + DeviceID: "deviceID", + StateType: "STOP", + QueueHead: 2, + QueueTracks: []*entity.QueueTrack{ + { + Index: 0, + URI: "spotify:track:track_uri1", + SessionID: "sessionID", + }, + { + Index: 1, + URI: "spotify:track:track_uri2", + SessionID: "sessionID", + }, + }, + ExpiredAt: time.Time{}, + AllowToControlByOthers: true, + ProgressWhenPaused: 0, + }, nil) + }, + prepareMockPusherFn: func(m *mock_event.MockPusher) {}, + prepareMockTrackCliFn: func(m *mock_spotify.MockTrackClient) {}, + prepareMockUserRepoFn: func(m *mock_repository.MockUser) {}, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // モックの準備 + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + uc := newSessionStateUseCaseForTest(t, ctrl, tt.prepareMockPlayerCliFn, tt.prepareMockTrackCliFn, + tt.prepareMockPusherFn, tt.prepareMockUserRepoFn, tt.prepareMockSessionRepoFn, tt.addToTimerSessionID) + + ctx := context.Background() + ctx = service.SetUserIDToContext(ctx, tt.userID) + + _, err := uc.nextTrackInStopTx(tt.sessionID)(ctx) + if (err != nil) != tt.wantErr { + t.Errorf("NextTrack() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +// モックの準備 +func newSessionStateUseCaseForTest( + t *testing.T, + ctrl *gomock.Controller, + prepareMockPlayerFn func(m *mock_spotify.MockPlayer), + prepareMockTrackFun func(m *mock_spotify.MockTrackClient), + prepareMockPusherFn func(m *mock_event.MockPusher), + prepareMockUserRepoFn func(m *mock_repository.MockUser), + prepareMockSessionRepoFn func(m *mock_repository.MockSession), + sessionID string) *SessionStateUseCase { + t.Helper() + + mockPlayer := mock_spotify.NewMockPlayer(ctrl) + prepareMockPlayerFn(mockPlayer) + mockTrackCli := mock_spotify.NewMockTrackClient(ctrl) + prepareMockTrackFun(mockTrackCli) + mockPusher := mock_event.NewMockPusher(ctrl) + prepareMockPusherFn(mockPusher) + mockUserRepo := mock_repository.NewMockUser(ctrl) + prepareMockUserRepoFn(mockUserRepo) + mockSessionRepo := mock_repository.NewMockSession(ctrl) + prepareMockSessionRepoFn(mockSessionRepo) + syncCheckTimerManager := entity.NewSyncCheckTimerManager() + if sessionID != "" { + timer := syncCheckTimerManager.CreateExpiredTimer(sessionID) + timer.SetDuration(5 * time.Minute) + } + timerUC := NewSessionTimerUseCase(mockSessionRepo, mockPlayer, mockPusher, syncCheckTimerManager) + return NewSessionStateUseCase(mockSessionRepo, mockPlayer, mockPusher, timerUC) + +} diff --git a/usecase/session_timer_test.go b/usecase/session_timer_test.go index b8446d11..1fecc823 100644 --- a/usecase/session_timer_test.go +++ b/usecase/session_timer_test.go @@ -434,63 +434,6 @@ func TestSessionTimerUseCase_handleWaitTimerExpired(t *testing.T) { }, wantErr: false, }, - { - name: "Spotifyとの同期が取れていることが確認されると、新しく追加すべき曲がSpotifyのキューに追加される", - sessionID: "sessionID", - currentOperation: "NextTrack", - prepareMockPlayerFn: func(m *mock_spotify.MockPlayer) { - m.EXPECT().CurrentlyPlaying(gomock.Any()).Return(&entity.CurrentPlayingInfo{ - Playing: true, - Progress: 10000000, - Track: &entity.Track{ - URI: "spotify:track:06QTSGUEgcmKwiEJ0IMPig", - ID: "06QTSGUEgcmKwiEJ0IMPig", - Name: "Borderland", - Duration: 213066000000, - Artists: []*entity.Artist{{Name: "MONOEYES"}}, - URL: "https://open.spotify.com/track/06QTSGUEgcmKwiEJ0IMPig", - Album: &entity.Album{ - Name: "Interstate 46 E.P.", - Images: []*entity.AlbumImage{ - { - URL: "https://i.scdn.co/image/ab67616d0000b273b48630d6efcebca2596120c4", - Height: 640, - Width: 640, - }, - }, - }, - }, - }, nil) - m.EXPECT().Enqueue(gomock.Any(), "spotify:track:track3", "deviceID").Return(nil) - }, - prepareMockPusherFn: func(m *mock_event.MockPusher) { - m.EXPECT().Push(&event.PushMessage{ - SessionID: "sessionID", - Msg: entity.NewEventNextTrack(1), - }) - }, - prepareMockUserRepoFn: func(m *mock_repository.MockUser) {}, - prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { - m.EXPECT().FindByID(gomock.Any(), "sessionID").Return(&entity.Session{ - ID: "sessionID", - Name: "name", - CreatorID: "creatorID", - DeviceID: "deviceID", - StateType: "PLAY", - QueueHead: 1, - QueueTracks: []*entity.QueueTrack{ - {Index: 0, URI: "spotify:track:5uQ0vKy2973Y9IUCd1wMEF"}, - {Index: 1, URI: "spotify:track:06QTSGUEgcmKwiEJ0IMPig"}, - {Index: 2, URI: "spotify:track:track2"}, - {Index: 3, URI: "spotify:track:track3"}, - }, - ExpiredAt: time.Time{}, - AllowToControlByOthers: false, - ProgressWhenPaused: 0, - }, nil) - }, - wantErr: false, - }, { name: "Spotifyとの同期が取れていないとhandleInterruptが呼び出されErrorが返る", sessionID: "sessionID", diff --git a/web/handler/session_test.go b/web/handler/session_test.go index 757612a0..dd010d7b 100644 --- a/web/handler/session_test.go +++ b/web/handler/session_test.go @@ -828,7 +828,8 @@ func TestUserHandler_GetActiveDevices(t *testing.T) { } } -func TestUserHandler_NextTrack(t *testing.T) { +// 正常系のテストケースはトランザクションの中を確かめる必要があるので、 usecase/session_state_test.go で行っている +func TestSessionHandler_NextTrack(t *testing.T) { t.Parallel() tests := []struct { @@ -844,72 +845,6 @@ func TestUserHandler_NextTrack(t *testing.T) { wantErr bool wantCode int }{ - { - name: "STOPかつ次の曲が存在する時に次の曲にSTOPのまま遷移,202", - sessionID: "sessionID", - userID: "userID", - addToTimerSessionID: "sessionID", - prepareMockPlayerCliFn: func(m *mock_spotify.MockPlayer) {}, - prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { - m.EXPECT().FindByID(gomock.Any(), "sessionID").Return( - &entity.Session{ - ID: "sessionID", - Name: "name", - CreatorID: "creatorID", - DeviceID: "deviceID", - StateType: "STOP", - QueueHead: 0, - QueueTracks: []*entity.QueueTrack{ - { - Index: 0, - URI: "spotify:track:track_uri1", - SessionID: "sessionID", - }, - { - Index: 1, - URI: "spotify:track:track_uri2", - SessionID: "sessionID", - }, - }, - ExpiredAt: time.Time{}, - AllowToControlByOthers: true, - ProgressWhenPaused: 0, - }, nil) - m.EXPECT().Update(gomock.Any(), &entity.Session{ - ID: "sessionID", - Name: "name", - CreatorID: "creatorID", - DeviceID: "deviceID", - StateType: "STOP", - QueueHead: 1, - QueueTracks: []*entity.QueueTrack{ - { - Index: 0, - URI: "spotify:track:track_uri1", - SessionID: "sessionID", - }, - { - Index: 1, - URI: "spotify:track:track_uri2", - SessionID: "sessionID", - }, - }, - ExpiredAt: time.Time{}, - AllowToControlByOthers: true, - ProgressWhenPaused: 0, - }).Return(nil) - }, - prepareMockPusherFn: func(m *mock_event.MockPusher) { - m.EXPECT().Push(&event.PushMessage{ - SessionID: "sessionID", - Msg: entity.NewEventNextTrack(1), - }) - }, - prepareMockTrackCliFn: func(m *mock_spotify.MockTrackClient) {}, - prepareMockUserRepoFn: func(m *mock_repository.MockUser) {}, - wantErr: false, - wantCode: http.StatusAccepted, - }, { name: "STOPかつ次の曲が存在しない時にErrNextQueueTrackNotFound,400", sessionID: "sessionID", @@ -917,7 +852,7 @@ func TestUserHandler_NextTrack(t *testing.T) { addToTimerSessionID: "sessionID", prepareMockPlayerCliFn: func(m *mock_spotify.MockPlayer) {}, prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { - m.EXPECT().FindByID(gomock.Any(), "sessionID").Return( + m.EXPECT().FindByIDForUpdate(gomock.Any(), "sessionID").Return( &entity.Session{ ID: "sessionID", Name: "name", @@ -941,6 +876,7 @@ func TestUserHandler_NextTrack(t *testing.T) { AllowToControlByOthers: true, ProgressWhenPaused: 0, }, nil) + m.EXPECT().DoInTx(gomock.Any(), gomock.Any()).Return(nil, entity.ErrNextQueueTrackNotFound) }, prepareMockPusherFn: func(m *mock_event.MockPusher) {}, prepareMockTrackCliFn: func(m *mock_spotify.MockTrackClient) {}, @@ -955,7 +891,7 @@ func TestUserHandler_NextTrack(t *testing.T) { addToTimerSessionID: "sessionID", prepareMockPlayerCliFn: func(m *mock_spotify.MockPlayer) {}, prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { - m.EXPECT().FindByID(gomock.Any(), "sessionID").Return( + m.EXPECT().FindByIDForUpdate(gomock.Any(), "sessionID").Return( &entity.Session{ ID: "sessionID", Name: "name", @@ -981,10 +917,9 @@ func TestUserHandler_NextTrack(t *testing.T) { userID: "userID", addToTimerSessionID: "sessionID", prepareMockPlayerCliFn: func(m *mock_spotify.MockPlayer) { - m.EXPECT().GoNextTrack(gomock.Any(), "deviceID").Return(nil) }, prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { - m.EXPECT().FindByID(gomock.Any(), "sessionID").Return( + m.EXPECT().FindByIDForUpdate(gomock.Any(), "sessionID").Return( &entity.Session{ ID: "sessionID", Name: "name", @@ -1011,7 +946,7 @@ func TestUserHandler_NextTrack(t *testing.T) { addToTimerSessionID: "sessionID", prepareMockPlayerCliFn: func(m *mock_spotify.MockPlayer) {}, prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { - m.EXPECT().FindByID(gomock.Any(), "sessionID").Return( + m.EXPECT().FindByIDForUpdate(gomock.Any(), "sessionID").Return( &entity.Session{ ID: "sessionID", Name: "name", @@ -1031,165 +966,6 @@ func TestUserHandler_NextTrack(t *testing.T) { wantErr: true, wantCode: http.StatusBadRequest, }, - { - name: "Pauseかつ次の曲が存在すると次の曲に遷移し、202", - sessionID: "sessionID", - userID: "userID", - addToTimerSessionID: "sessionID", - prepareMockPlayerCliFn: func(m *mock_spotify.MockPlayer) { - m.EXPECT().GoNextTrack(gomock.Any(), "deviceID").Return(nil) - m.EXPECT().Pause(gomock.Any(), "deviceID").Return(nil) - }, - prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { - m.EXPECT().FindByID(gomock.Any(), "sessionID").Return( - &entity.Session{ - ID: "sessionID", - Name: "name", - CreatorID: "creatorID", - DeviceID: "deviceID", - StateType: "PAUSE", - QueueHead: 0, - QueueTracks: []*entity.QueueTrack{ - { - Index: 0, - URI: "spotify:track:track_uri1", - SessionID: "sessionID", - }, - { - Index: 1, - URI: "spotify:track:track_uri2", - SessionID: "sessionID", - }, - }, - ExpiredAt: time.Time{}, - AllowToControlByOthers: true, - ProgressWhenPaused: 0, - }, nil) - m.EXPECT().Update(gomock.Any(), &entity.Session{ - ID: "sessionID", - Name: "name", - CreatorID: "creatorID", - DeviceID: "deviceID", - StateType: "PAUSE", - QueueHead: 1, - QueueTracks: []*entity.QueueTrack{ - { - Index: 0, - URI: "spotify:track:track_uri1", - SessionID: "sessionID", - }, - { - Index: 1, - URI: "spotify:track:track_uri2", - SessionID: "sessionID", - }, - }, - ExpiredAt: time.Time{}, - AllowToControlByOthers: true, - ProgressWhenPaused: 0, - }) - }, - prepareMockPusherFn: func(m *mock_event.MockPusher) { - m.EXPECT().Push(&event.PushMessage{ - SessionID: "sessionID", - Msg: entity.NewEventNextTrack(1), - }) - }, - prepareMockTrackCliFn: func(m *mock_spotify.MockTrackClient) {}, - prepareMockUserRepoFn: func(m *mock_repository.MockUser) {}, - wantErr: false, - wantCode: http.StatusAccepted, - }, - { - name: "Pauseかつ次の曲が3曲存在すると次の曲に遷移し、三曲先がEnqueueされ、202", - sessionID: "sessionID", - userID: "userID", - addToTimerSessionID: "sessionID", - prepareMockPlayerCliFn: func(m *mock_spotify.MockPlayer) { - m.EXPECT().GoNextTrack(gomock.Any(), "deviceID").Return(nil) - m.EXPECT().Pause(gomock.Any(), "deviceID").Return(nil) - m.EXPECT().Enqueue(gomock.Any(), "spotify:track:track_uri4", "deviceID").Return(nil) - }, - prepareMockSessionRepoFn: func(m *mock_repository.MockSession) { - m.EXPECT().FindByID(gomock.Any(), "sessionID").Return( - &entity.Session{ - ID: "sessionID", - Name: "name", - CreatorID: "creatorID", - DeviceID: "deviceID", - StateType: "PAUSE", - QueueHead: 0, - QueueTracks: []*entity.QueueTrack{ - { - Index: 0, - URI: "spotify:track:track_uri1", - SessionID: "sessionID", - }, - { - Index: 1, - URI: "spotify:track:track_uri2", - SessionID: "sessionID", - }, - { - Index: 2, - URI: "spotify:track:track_uri3", - SessionID: "sessionID", - }, - { - Index: 3, - URI: "spotify:track:track_uri4", - SessionID: "sessionID", - }, - }, - ExpiredAt: time.Time{}, - AllowToControlByOthers: true, - ProgressWhenPaused: 0, - }, nil) - m.EXPECT().Update(gomock.Any(), &entity.Session{ - ID: "sessionID", - Name: "name", - CreatorID: "creatorID", - DeviceID: "deviceID", - StateType: "PAUSE", - QueueHead: 1, - QueueTracks: []*entity.QueueTrack{ - { - Index: 0, - URI: "spotify:track:track_uri1", - SessionID: "sessionID", - }, - { - Index: 1, - URI: "spotify:track:track_uri2", - SessionID: "sessionID", - }, - { - Index: 2, - URI: "spotify:track:track_uri3", - SessionID: "sessionID", - }, - { - Index: 3, - URI: "spotify:track:track_uri4", - SessionID: "sessionID", - }, - }, - ExpiredAt: time.Time{}, - AllowToControlByOthers: true, - ProgressWhenPaused: 0, - }) - }, - prepareMockPusherFn: func(m *mock_event.MockPusher) { - m.EXPECT().Push(&event.PushMessage{ - SessionID: "sessionID", - Msg: entity.NewEventNextTrack(1), - }) - }, - prepareMockTrackCliFn: func(m *mock_spotify.MockTrackClient) {}, - prepareMockUserRepoFn: func(m *mock_repository.MockUser) {}, - wantErr: false, - wantCode: http.StatusAccepted, - }, } for _, tt := range tests { @@ -1198,7 +974,7 @@ func TestUserHandler_NextTrack(t *testing.T) { req := httptest.NewRequest(http.MethodPut, "/", nil) rec := httptest.NewRecorder() c := e.NewContext(req, rec) - c.SetPath("/sessions/:id/devices") + c.SetPath("/sessions/:id/next") c.SetParamNames("id") c.SetParamValues(tt.sessionID) c = setToContext(c, tt.userID, nil)