@@ -18,6 +18,9 @@ vi.mock('$lib/server/database', () => ({
1818 votedGradeStatistics : {
1919 upsert : vi . fn ( ) ,
2020 } ,
21+ task : {
22+ findUnique : vi . fn ( ) ,
23+ } ,
2124 $transaction : vi . fn ( ) ,
2225 } ,
2326} ) ) ;
@@ -48,13 +51,16 @@ function setupTransaction() {
4851 voteGrade : { findUnique : vi . fn ( ) , upsert : vi . fn ( ) } ,
4952 votedGradeCounter : { updateMany : vi . fn ( ) , upsert : vi . fn ( ) , findMany : vi . fn ( ) } ,
5053 votedGradeStatistics : { upsert : vi . fn ( ) } ,
54+ task : { findUnique : vi . fn ( ) } ,
5155 } ;
5256 vi . mocked ( prisma . $transaction ) . mockImplementation ( async ( callback : unknown ) =>
5357 ( callback as ( tx : typeof mockTx ) => Promise < unknown > ) ( mockTx ) ,
5458 ) ;
5559 return mockTx ;
5660}
5761
62+ type MockTx = ReturnType < typeof setupTransaction > ;
63+
5864// ---------------------------------------------------------------------------
5965// Tests
6066// ---------------------------------------------------------------------------
@@ -117,6 +123,7 @@ describe('upsertVoteGradeTables', () => {
117123 { grade : TaskGrade . Q5 , count : 1 } ,
118124 { grade : TaskGrade . Q4 , count : 0 } ,
119125 ] ) ;
126+ tx . task . findUnique . mockResolvedValue ( { grade : TaskGrade . Q3 } ) ;
120127
121128 await upsertVoteGradeTables ( 'user-1' , 'abc001_a' , TaskGrade . Q5 ) ;
122129
@@ -134,7 +141,7 @@ describe('upsertVoteGradeTables', () => {
134141 update : { count : { increment : 1 } } ,
135142 } ) ,
136143 ) ;
137- // Total = 1 vote (Q5:1 + Q4:0), below MIN_VOTES_FOR_STATISTICS — statistics must not be updated
144+ // Total = 1 vote (Q5:1 + Q4:0), below MIN_VOTES_FOR_STATISTICS (3) for non-PENDING task — statistics must not be updated
138145 expect ( tx . votedGradeStatistics . upsert ) . not . toHaveBeenCalled ( ) ;
139146 } ) ;
140147
@@ -144,6 +151,7 @@ describe('upsertVoteGradeTables', () => {
144151 tx . voteGrade . upsert . mockResolvedValue ( { } ) ;
145152 tx . votedGradeCounter . upsert . mockResolvedValue ( { } ) ;
146153 tx . votedGradeCounter . findMany . mockResolvedValue ( [ { grade : TaskGrade . Q5 , count : 1 } ] ) ;
154+ tx . task . findUnique . mockResolvedValue ( { grade : TaskGrade . Q3 } ) ;
147155
148156 await upsertVoteGradeTables ( 'user-1' , 'abc001_a' , TaskGrade . Q5 ) ;
149157
@@ -159,17 +167,18 @@ describe('upsertVoteGradeTables', () => {
159167 update : { count : { increment : 1 } } ,
160168 } ) ,
161169 ) ;
162- // Total = 1 vote (Q5:1), below MIN_VOTES_FOR_STATISTICS — statistics must not be updated
170+ // Total = 1 vote (Q5:1), below MIN_VOTES_FOR_STATISTICS (3) for non-PENDING task — statistics must not be updated
163171 expect ( tx . votedGradeStatistics . upsert ) . not . toHaveBeenCalled ( ) ;
164172 } ) ;
165173
166- test ( 'upserts VotedGradeStatistics when total votes reaches 3' , async ( ) => {
174+ test ( 'upserts VotedGradeStatistics when total votes reaches 3 for non-PENDING task ' , async ( ) => {
167175 const tx = setupTransaction ( ) ;
168176 tx . voteGrade . findUnique . mockResolvedValue ( null ) ;
169177 tx . voteGrade . upsert . mockResolvedValue ( { } ) ;
170178 tx . votedGradeCounter . upsert . mockResolvedValue ( { } ) ;
171179 // 3 votes all on Q5 → median = Q5
172180 tx . votedGradeCounter . findMany . mockResolvedValue ( [ { grade : TaskGrade . Q5 , count : 3 } ] ) ;
181+ tx . task . findUnique . mockResolvedValue ( { grade : TaskGrade . Q3 } ) ;
173182 tx . votedGradeStatistics . upsert . mockResolvedValue ( { } ) ;
174183
175184 await upsertVoteGradeTables ( 'user-1' , 'abc001_a' , TaskGrade . Q5 ) ;
@@ -182,15 +191,35 @@ describe('upsertVoteGradeTables', () => {
182191 ) ;
183192 } ) ;
184193
185- test ( 'does not upsert VotedGradeStatistics when total votes is below 3' , async ( ) => {
194+ test ( 'does not upsert VotedGradeStatistics when total votes is below 3 for non-PENDING task ' , async ( ) => {
186195 const tx = setupTransaction ( ) ;
187196 tx . voteGrade . findUnique . mockResolvedValue ( null ) ;
188197 tx . voteGrade . upsert . mockResolvedValue ( { } ) ;
189198 tx . votedGradeCounter . upsert . mockResolvedValue ( { } ) ;
190199 tx . votedGradeCounter . findMany . mockResolvedValue ( [ { grade : TaskGrade . Q5 , count : 2 } ] ) ;
200+ tx . task . findUnique . mockResolvedValue ( { grade : TaskGrade . Q3 } ) ;
191201
192202 await upsertVoteGradeTables ( 'user-1' , 'abc001_a' , TaskGrade . Q5 ) ;
193203
194204 expect ( tx . votedGradeStatistics . upsert ) . not . toHaveBeenCalled ( ) ;
195205 } ) ;
206+
207+ test ( 'upserts VotedGradeStatistics after 1 vote for PENDING task' , async ( ) => {
208+ const tx = setupTransaction ( ) ;
209+ tx . voteGrade . findUnique . mockResolvedValue ( null ) ;
210+ tx . voteGrade . upsert . mockResolvedValue ( { } ) ;
211+ tx . votedGradeCounter . upsert . mockResolvedValue ( { } ) ;
212+ tx . votedGradeCounter . findMany . mockResolvedValue ( [ { grade : TaskGrade . Q5 , count : 1 } ] ) ;
213+ tx . task . findUnique . mockResolvedValue ( { grade : TaskGrade . PENDING } ) ;
214+ tx . votedGradeStatistics . upsert . mockResolvedValue ( { } ) ;
215+
216+ await upsertVoteGradeTables ( 'user-1' , 'abc001_a' , TaskGrade . Q5 ) ;
217+
218+ expect ( tx . votedGradeStatistics . upsert ) . toHaveBeenCalledWith (
219+ expect . objectContaining ( {
220+ where : { taskId : 'abc001_a' } ,
221+ update : { grade : TaskGrade . Q5 } ,
222+ } ) ,
223+ ) ;
224+ } ) ;
196225} ) ;
0 commit comments