1+ 'use strict' ;
2+
3+ // Prime NodeBB require paths + use the mock DB (so this test doesn't hit real Redis)
4+ require ( '../require-main' ) ;
5+ require ( './mocks/databasemock' ) ;
6+
7+ const assert = require ( 'assert' ) ;
8+ const Polls = require ( '../src/polls/model' ) ;
9+ const Votes = require ( '../src/polls/votes' ) ;
10+
11+ describe ( 'Poll Vote API (Issue 4)' , function ( ) {
12+ let pollId ;
13+ const testUid = 'user123' ;
14+ const testPid = 'post456' ;
15+
16+ before ( async function ( ) {
17+ // Create a test poll attached to a post
18+ const poll = await Polls . createPoll ( {
19+ postId : testPid ,
20+ question : 'Test Poll Question' ,
21+ options : [ 'Option A' , 'Option B' , 'Option C' ] ,
22+ createdBy : 'admin' ,
23+ } ) ;
24+ pollId = poll . pollId ;
25+ } ) ;
26+
27+ it ( 'should allow a user to vote on a poll' , async function ( ) {
28+ // Test the core voting logic
29+ const result = await Votes . recordVote ( { pollId, userId : testUid , optionIndex : 0 } ) ;
30+ assert . strictEqual ( result , true ) ;
31+
32+ // Verify the vote was recorded
33+ const hasVoted = await Votes . hasUserVoted ( pollId , testUid ) ;
34+ assert . strictEqual ( hasVoted , true ) ;
35+
36+ // Check the user's choice
37+ const choice = await Votes . getUserChoice ( pollId , testUid ) ;
38+ assert . strictEqual ( choice , 0 ) ;
39+
40+ // Check vote counts
41+ const counts = await Votes . getCounts ( pollId ) ;
42+ assert . strictEqual ( counts [ 0 ] , 1 ) ;
43+ assert . strictEqual ( counts [ 1 ] || 0 , 0 ) ;
44+ assert . strictEqual ( counts [ 2 ] || 0 , 0 ) ;
45+ } ) ;
46+
47+ it ( 'should prevent double voting by the same user' , async function ( ) {
48+ let err ;
49+ try {
50+ await Votes . recordVote ( { pollId, userId : testUid , optionIndex : 1 } ) ;
51+ } catch ( e ) { err = e ; }
52+ assert . ok ( err , 'Expected an error for double voting' ) ;
53+ assert . strictEqual ( err . code , 'ALREADY_VOTED' ) ;
54+ } ) ;
55+
56+ it ( 'should handle multiple users voting on different options' , async function ( ) {
57+ const anotherUser = 'user456' ;
58+ const thirdUser = 'user789' ;
59+
60+ // Second user votes for option 1
61+ const result1 = await Votes . recordVote ( { pollId, userId : anotherUser , optionIndex : 1 } ) ;
62+ assert . strictEqual ( result1 , true ) ;
63+
64+ // Third user votes for option 2
65+ const result2 = await Votes . recordVote ( { pollId, userId : thirdUser , optionIndex : 2 } ) ;
66+ assert . strictEqual ( result2 , true ) ;
67+
68+ // Check final vote counts
69+ const counts = await Votes . getCounts ( pollId ) ;
70+ assert . strictEqual ( counts [ 0 ] , 1 ) ; // First user's vote
71+ assert . strictEqual ( counts [ 1 ] , 1 ) ; // Second user's vote
72+ assert . strictEqual ( counts [ 2 ] , 1 ) ; // Third user's vote
73+ } ) ;
74+
75+ it ( 'should validate vote parameters' , async function ( ) {
76+ // Test missing pollId
77+ let err ;
78+ try {
79+ await Votes . recordVote ( { userId : 'test' , optionIndex : 0 } ) ;
80+ } catch ( e ) { err = e ; }
81+ assert . ok ( err , 'Expected an error for missing pollId' ) ;
82+ assert . strictEqual ( err . code , 'BAD_INPUT' ) ;
83+
84+ // Test missing userId
85+ err = null ;
86+ try {
87+ await Votes . recordVote ( { pollId, optionIndex : 0 } ) ;
88+ } catch ( e ) { err = e ; }
89+ assert . ok ( err , 'Expected an error for missing userId' ) ;
90+ assert . strictEqual ( err . code , 'BAD_INPUT' ) ;
91+
92+ // Test invalid optionIndex
93+ err = null ;
94+ try {
95+ await Votes . recordVote ( { pollId, userId : 'newuser' , optionIndex : 'invalid' } ) ;
96+ } catch ( e ) { err = e ; }
97+ assert . ok ( err , 'Expected an error for invalid optionIndex' ) ;
98+ assert . strictEqual ( err . code , 'BAD_INPUT' ) ;
99+ } ) ;
100+ } ) ;
0 commit comments