@@ -10,6 +10,7 @@ import { mockAuthenticateWithApeKey } from "../../__testData__/auth";
1010import { enableRateLimitExpects } from "../../__testData__/rate-limit" ;
1111import { DBResult } from "../../../src/utils/result" ;
1212import { omit } from "../../../src/utils/misc" ;
13+ import { CompletedEvent } from "@monkeytype/schemas/results" ;
1314
1415const { mockApp, uid, mockAuth } = setup ( ) ;
1516const configuration = Configuration . getCachedConfiguration ( ) ;
@@ -588,6 +589,7 @@ describe("result controller test", () => {
588589
589590 beforeEach ( async ( ) => {
590591 await enableResultsSaving ( true ) ;
592+ await enableUsersXpGain ( true ) ;
591593
592594 [
593595 userGetMock ,
@@ -611,48 +613,15 @@ describe("result controller test", () => {
611613 it ( "should add result" , async ( ) => {
612614 //GIVEN
613615
616+ const completedEvent = buildCompletedEvent ( {
617+ funbox : [ "58008" , "read_ahead_hard" ] ,
618+ } ) ;
614619 //WHEN
615620 const { body } = await mockApp
616621 . post ( "/results" )
617622 . set ( "Authorization" , `Bearer ${ uid } ` )
618623 . send ( {
619- result : {
620- acc : 86 ,
621- afkDuration : 5 ,
622- bailedOut : false ,
623- blindMode : false ,
624- charStats : [ 100 , 2 , 3 , 5 ] ,
625- chartData : { wpm : [ 1 , 2 , 3 ] , burst : [ 50 , 55 , 56 ] , err : [ 0 , 2 , 0 ] } ,
626- consistency : 23.5 ,
627- difficulty : "normal" ,
628- funbox : [ ] ,
629- hash : "hash" ,
630- incompleteTestSeconds : 2 ,
631- incompleteTests : [ { acc : 75 , seconds : 10 } ] ,
632- keyConsistency : 12 ,
633- keyDuration : [ 0 , 3 , 5 ] ,
634- keySpacing : [ 0 , 2 , 4 ] ,
635- language : "english" ,
636- lazyMode : false ,
637- mode : "time" ,
638- mode2 : "15" ,
639- numbers : false ,
640- punctuation : false ,
641- rawWpm : 99 ,
642- restartCount : 4 ,
643- tags : [ "tagOneId" , "tagTwoId" ] ,
644- testDuration : 15.1 ,
645- timestamp : 1000 ,
646- uid,
647- wpmConsistency : 55 ,
648- wpm : 80 ,
649- stopOnLetter : false ,
650- //new required
651- charTotal : 5 ,
652- keyOverlap : 7 ,
653- lastKeyToEnd : 9 ,
654- startToFirstKey : 11 ,
655- } ,
624+ result : completedEvent ,
656625 } )
657626 . expect ( 200 ) ;
658627
@@ -662,7 +631,12 @@ describe("result controller test", () => {
662631 tagPbs : [ ] ,
663632 xp : 0 ,
664633 dailyXpBonus : false ,
665- xpBreakdown : { } ,
634+ xpBreakdown : {
635+ accPenalty : 28 ,
636+ base : 20 ,
637+ incomplete : 5 ,
638+ funbox : 80 ,
639+ } ,
666640 streak : 0 ,
667641 insertedId : insertedId . toHexString ( ) ,
668642 } ) ;
@@ -751,44 +725,9 @@ describe("result controller test", () => {
751725 . post ( "/results" )
752726 . set ( "Authorization" , `Bearer ${ uid } ` )
753727 . send ( {
754- result : {
755- acc : 86 ,
756- afkDuration : 5 ,
757- bailedOut : false ,
758- blindMode : false ,
759- charStats : [ 100 , 2 , 3 , 5 ] ,
760- chartData : { wpm : [ 1 , 2 , 3 ] , burst : [ 50 , 55 , 56 ] , err : [ 0 , 2 , 0 ] } ,
761- consistency : 23.5 ,
762- difficulty : "normal" ,
763- funbox : [ ] ,
764- hash : "hash" ,
765- incompleteTestSeconds : 2 ,
766- incompleteTests : [ { acc : 75 , seconds : 10 } ] ,
767- keyConsistency : 12 ,
768- keyDuration : [ 0 , 3 , 5 ] ,
769- keySpacing : [ 0 , 2 , 4 ] ,
770- language : "english" ,
771- lazyMode : false ,
772- mode : "time" ,
773- mode2 : "15" ,
774- numbers : false ,
775- punctuation : false ,
776- rawWpm : 99 ,
777- restartCount : 4 ,
778- tags : [ "tagOneId" , "tagTwoId" ] ,
779- testDuration : 15.1 ,
780- timestamp : 1000 ,
781- uid,
782- wpmConsistency : 55 ,
783- wpm : 80 ,
784- stopOnLetter : false ,
785- //new required
786- charTotal : 5 ,
787- keyOverlap : 7 ,
788- lastKeyToEnd : 9 ,
789- startToFirstKey : 11 ,
728+ result : buildCompletedEvent ( {
790729 extra2 : "value" ,
791- } ,
730+ } as any ) ,
792731 extra : "value" ,
793732 } )
794733 . expect ( 422 ) ;
@@ -803,6 +742,24 @@ describe("result controller test", () => {
803742 } ) ;
804743 } ) ;
805744
745+ it ( "should fail wit duplicate funboxes" , async ( ) => {
746+ //GIVEN
747+
748+ //WHEN
749+ const { body } = await mockApp
750+ . post ( "/results" )
751+ . set ( "Authorization" , `Bearer ${ uid } ` )
752+ . send ( {
753+ result : buildCompletedEvent ( {
754+ funbox : [ "58008" , "58008" ] ,
755+ } ) ,
756+ } )
757+ . expect ( 400 ) ;
758+
759+ //THEN
760+ expect ( body . message ) . toEqual ( "Duplicate funboxes" ) ;
761+ } ) ;
762+
806763 // it("should fail invalid properties ", async () => {
807764 //GIVEN
808765 //WHEN
@@ -824,6 +781,47 @@ describe("result controller test", () => {
824781 } ) ;
825782} ) ;
826783
784+ function buildCompletedEvent ( result ?: Partial < CompletedEvent > ) : CompletedEvent {
785+ return {
786+ acc : 86 ,
787+ afkDuration : 5 ,
788+ bailedOut : false ,
789+ blindMode : false ,
790+ charStats : [ 100 , 2 , 3 , 5 ] ,
791+ chartData : { wpm : [ 1 , 2 , 3 ] , burst : [ 50 , 55 , 56 ] , err : [ 0 , 2 , 0 ] } ,
792+ consistency : 23.5 ,
793+ difficulty : "normal" ,
794+ funbox : [ ] ,
795+ hash : "hash" ,
796+ incompleteTestSeconds : 2 ,
797+ incompleteTests : [ { acc : 75 , seconds : 10 } ] ,
798+ keyConsistency : 12 ,
799+ keyDuration : [ 0 , 3 , 5 ] ,
800+ keySpacing : [ 0 , 2 , 4 ] ,
801+ language : "english" ,
802+ lazyMode : false ,
803+ mode : "time" ,
804+ mode2 : "15" ,
805+ numbers : false ,
806+ punctuation : false ,
807+ rawWpm : 99 ,
808+ restartCount : 4 ,
809+ tags : [ "tagOneId" , "tagTwoId" ] ,
810+ testDuration : 15.1 ,
811+ timestamp : 1000 ,
812+ uid,
813+ wpmConsistency : 55 ,
814+ wpm : 80 ,
815+ stopOnLetter : false ,
816+ //new required
817+ charTotal : 5 ,
818+ keyOverlap : 7 ,
819+ lastKeyToEnd : 9 ,
820+ startToFirstKey : 11 ,
821+ ...result ,
822+ } ;
823+ }
824+
827825async function enablePremiumFeatures ( enabled : boolean ) : Promise < void > {
828826 const mockConfig = await configuration ;
829827 mockConfig . users . premium = { ...mockConfig . users . premium , enabled } ;
@@ -884,3 +882,11 @@ async function enableResultsSaving(enabled: boolean): Promise<void> {
884882 mockConfig
885883 ) ;
886884}
885+ async function enableUsersXpGain ( enabled : boolean ) : Promise < void > {
886+ const mockConfig = await configuration ;
887+ mockConfig . users . xp = { ...mockConfig . users . xp , enabled, funboxBonus : 1 } ;
888+
889+ vi . spyOn ( Configuration , "getCachedConfiguration" ) . mockResolvedValue (
890+ mockConfig
891+ ) ;
892+ }
0 commit comments