git ssb

0+

Piet / ssb-loomio



Tree: 7610206161f8ec304e4fb592fc4c9861f6990e80

Files: 7610206161f8ec304e4fb592fc4c9861f6990e80 / test / position / sync / chooseOneResults.test.js

3733 bytesRaw
1const test = require('tape')
2const ChooseOne = require('../../../position/sync/chooseOne')
3const ChooseOnePoll = require('../../../poll/sync/chooseOne')
4const Position = require('../../../position/sync/position')
5const chooseOneResults = require('../../../position/sync/chooseOneResults')
6
7const pietId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/0=.ed25519'
8const mixId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/1=.ed25519'
9const mikeyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/2=.ed25519'
10const timmyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/3=.ed25519'
11const tommyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/4=.ed25519'
12const sallyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/5=.ed25519'
13
14const poll = '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256'
15
16const now = Number(new Date())
17
18const validPoll = ChooseOnePoll({
19 choices: [1, 2, 'three'],
20 title: 'how many food',
21 closesAt: now
22})
23
24test('ChooseOneResults - ChooseOneResults', function (t) {
25 const positions = [
26 { value: { content: Position(ChooseOne({choice: 0, poll})), author: pietId } },
27 { value: { content: Position(ChooseOne({choice: 0, poll})), author: mixId } },
28 { value: { content: Position(ChooseOne({choice: 0, poll})), author: mikeyId } },
29 { value: { content: Position(ChooseOne({choice: 1, poll})), author: timmyId } },
30 { value: { content: Position(ChooseOne({choice: 1, poll})), author: tommyId } },
31 { value: { content: Position(ChooseOne({choice: 2, poll})), author: sallyId } }
32 ]
33
34 const actual = chooseOneResults({positions, poll: validPoll})
35 t.deepEqual(actual[0], [pietId, mixId, mikeyId], 'correct voters for choice 0')
36 t.deepEqual(actual[1], [timmyId, tommyId], 'correct voters for choice 1')
37 t.deepEqual(actual[2], [sallyId], 'correct voters for choice 2')
38 t.end()
39})
40
41test('ChooseOneResults - a position stated for an invalid choice index is not counted', function (t) {
42 const positions = [
43 { value: { content: Position(ChooseOne({choice: 3, poll})), author: pietId } }
44 ]
45
46 const actual = chooseOneResults({positions, poll: validPoll})
47 t.false(actual[3], 'invalid vote is not counted')
48 t.end()
49})
50
51test('ChooseOneResults - a position stated for an invalid choice index is included in the errors object', function (t) {
52 const positions = [
53 { value: { content: Position(ChooseOne({choice: 3, poll})), author: pietId } }
54 ]
55
56 const actual = chooseOneResults({positions, poll: validPoll})
57 t.deepEqual(actual.errors.invalidPositions[0], positions[0], 'invalid vote is on error object')
58 t.end()
59})
60
61test('ChooseOneResults - A position stated before the closing time of the poll is counted', function (t) {
62 const positions = [
63 { value: { content: Position(ChooseOne({choice: 0, poll})), author: pietId, timestamp: now - 1} }
64 ]
65
66 const actual = chooseOneResults({positions, poll: validPoll})
67 t.true(actual[0], 'valid vote is counted')
68 t.end()
69})
70
71test('ChooseOneResults - A position stated after the closing time of the poll is not counted', function (t) {
72 const positions = [
73 { value: { content: Position(ChooseOne({choice: 0, poll})), author: pietId, timestamp: now + 1} }
74 ]
75
76 const actual = chooseOneResults({positions, poll: validPoll})
77 t.false(actual[0], 'invalid vote is not counted')
78 t.end()
79})
80
81test('ChooseOneResults - A position stated after the closing time of the poll is included in the error object', function (t) {
82 const positions = [
83 { value: { content: Position(ChooseOne({choice: 0, poll})), author: pietId, timestamp: now + 1} }
84 ]
85
86 const actual = chooseOneResults({positions, poll: validPoll})
87 t.deepEqual(actual.errors.invalidPositions[0], positions[0], 'invalid vote is on error object')
88 t.end()
89})
90

Built with git-ssb-web