1+ import { Test } from '@nestjs/testing' ;
2+ import { mock } from 'jest-mock-extended' ;
3+ import { ItemActionType } from 'src/database/entity' ;
4+ import { ChapterRepository , ChoiceOptionRepository , EndingRecordRepository , EndingRepository , ItemRepository , PageRepository , PlayRecordRepository , PlayStatusRepository } from 'src/database/repository' ;
5+ import { ArrayUtil } from 'src/util/array-util.service' ;
6+ import { GameService } from './game.service' ;
7+
8+ describe ( 'GameService' , ( ) => {
9+ let gameService : GameService ;
10+
11+ const mockChapterRepository = mock < ChapterRepository > ( ) ;
12+ const mockPageRepository = mock < PageRepository > ( ) ;
13+ const mockItemRepository = mock < ItemRepository > ( ) ;
14+ const mockPlayRecordRepository = mock < PlayRecordRepository > ( ) ;
15+ const mockEndingRepository = mock < EndingRepository > ( ) ;
16+ const mockEndingRecordRepository = mock < EndingRecordRepository > ( ) ;
17+ const mockPlayStatusRepository = mock < PlayStatusRepository > ( ) ;
18+ const mockChoiceOptionRepository = mock < ChoiceOptionRepository > ( ) ;
19+ const arrayUtil = new ArrayUtil ( ) ;
20+
21+ beforeEach ( async ( ) => {
22+ const module = await Test . createTestingModule ( {
23+ providers : [
24+ GameService ,
25+ { provide : ChapterRepository , useValue : mockChapterRepository } ,
26+ { provide : PageRepository , useValue : mockPageRepository } ,
27+ { provide : ItemRepository , useValue : mockItemRepository } ,
28+ { provide : PlayRecordRepository , useValue : mockPlayRecordRepository } ,
29+ { provide : EndingRepository , useValue : mockEndingRepository } ,
30+ { provide : EndingRecordRepository , useValue : mockEndingRecordRepository } ,
31+ { provide : PlayStatusRepository , useValue : mockPlayStatusRepository } ,
32+ { provide : ChoiceOptionRepository , useValue : mockChoiceOptionRepository } ,
33+ { provide : ArrayUtil , useValue : arrayUtil }
34+ ]
35+ } ) . compile ( ) ;
36+
37+ gameService = module . get < GameService > ( GameService ) ;
38+ } ) ;
39+
40+ describe ( 'Test calculateItems' , ( ) => {
41+ test ( '소유 O, 랜덤 획득 O' , ( ) => {
42+ const ownedItems = [
43+ { itemId : 1 , count : 1 }
44+ ] ;
45+ const itemMappings = [ {
46+ choiceOptionId : 1 ,
47+ itemId : 1 ,
48+ actionType : ItemActionType . RandomGain ,
49+ createdAt : new Date ( ) ,
50+ updatedAt : new Date ( )
51+ } ] ;
52+
53+ const result = ( gameService as any ) . calculateItems ( ownedItems , itemMappings ) ;
54+ expect ( result ) . toStrictEqual ( [ { itemId : 1 , count : 2 } ] ) ;
55+ } ) ;
56+
57+ test ( '소유 O, 매핑 O, 획득' , ( ) => {
58+ const ownedItems = [
59+ { itemId : 1 , count : 1 }
60+ ] ;
61+ const itemMappings = [ {
62+ choiceOptionId : 1 ,
63+ itemId : 1 ,
64+ actionType : ItemActionType . Gain ,
65+ createdAt : new Date ( ) ,
66+ updatedAt : new Date ( )
67+ } ] ;
68+
69+ const result = ( gameService as any ) . calculateItems ( ownedItems , itemMappings ) ;
70+ expect ( result ) . toStrictEqual ( [ { itemId : 1 , count : 2 } ] ) ;
71+ } ) ;
72+
73+ test ( '소유 O, 매핑 O, 소모 - 아이템이 없으면 null' , ( ) => {
74+ const ownedItems = [
75+ { itemId : 1 , count : 1 }
76+ ] ;
77+ const itemMappings = [ {
78+ choiceOptionId : 1 ,
79+ itemId : 1 ,
80+ actionType : ItemActionType . Loss ,
81+ createdAt : new Date ( ) ,
82+ updatedAt : new Date ( )
83+ } ] ;
84+
85+ const result = ( gameService as any ) . calculateItems ( ownedItems , itemMappings ) ;
86+ expect ( result ) . toBeNull ( ) ;
87+ } ) ;
88+
89+ test ( '소유 O, 매핑 O, 소모 - 다른 아이템 있는 경우' , ( ) => {
90+ const ownedItems = [
91+ { itemId : 1 , count : 1 } ,
92+ { itemId : 2 , count : 1 }
93+ ] ;
94+ const itemMappings = [ {
95+ choiceOptionId : 1 ,
96+ itemId : 1 ,
97+ actionType : ItemActionType . Loss ,
98+ createdAt : new Date ( ) ,
99+ updatedAt : new Date ( )
100+ } ] ;
101+
102+ const result = ( gameService as any ) . calculateItems ( ownedItems , itemMappings ) ;
103+ expect ( result ) . toStrictEqual ( [ { itemId : 2 , count : 1 } ] ) ;
104+ } ) ;
105+
106+ test ( '소유 O, 매핑 X' , ( ) => {
107+ const ownedItems = [
108+ { itemId : 1 , count : 1 }
109+ ] ;
110+
111+ const result = ( gameService as any ) . calculateItems ( ownedItems ) ;
112+ expect ( result ) . toStrictEqual ( ownedItems ) ;
113+ } ) ;
114+
115+ test ( '소유 X, 랜덤 획득 O' , ( ) => {
116+ const ownedItems = null ;
117+ const itemMappings = [ {
118+ choiceOptionId : 1 ,
119+ itemId : 1 ,
120+ actionType : ItemActionType . RandomGain ,
121+ createdAt : new Date ( ) ,
122+ updatedAt : new Date ( )
123+ } ] ;
124+
125+ const result = ( gameService as any ) . calculateItems ( ownedItems , itemMappings ) ;
126+ expect ( result ) . toStrictEqual ( [ { itemId : 1 , count : 1 } ] ) ;
127+ } ) ;
128+
129+ test ( '소유 X, 매핑 O - 아이템 획득인 경우' , ( ) => {
130+ const ownedItems = null ;
131+ const itemMappings = [ {
132+ choiceOptionId : 1 ,
133+ itemId : 1 ,
134+ actionType : ItemActionType . Gain ,
135+ createdAt : new Date ( ) ,
136+ updatedAt : new Date ( )
137+ } ] ;
138+
139+ const result = ( gameService as any ) . calculateItems ( ownedItems , itemMappings ) ;
140+ expect ( result ) . toStrictEqual ( [ { itemId : 1 , count : 1 } ] ) ;
141+ } ) ;
142+
143+ test ( '소유 X, 매핑 O - 아이템 소모인 경우' , ( ) => {
144+ const ownedItems = null ;
145+ const itemMappings = [ {
146+ choiceOptionId : 1 ,
147+ itemId : 1 ,
148+ actionType : ItemActionType . Loss ,
149+ createdAt : new Date ( ) ,
150+ updatedAt : new Date ( )
151+ } ] ;
152+
153+ expect ( ( ) => {
154+ ( gameService as any ) . calculateItems ( ownedItems , itemMappings ) ;
155+ } ) . toThrow ( ) ;
156+ } ) ;
157+ } ) ;
158+ } ) ;
0 commit comments