git ssb

0+

Piet / ssb-loomio



Tree: 92693260135502dac86a07eef9c477fb0198568a

Files: 92693260135502dac86a07eef9c477fb0198568a / test / position / sync / chooseOneResults.test.js

4037 bytesRaw
1const test = require('tape')
2const ChooseOne = require('../../../position/sync/chooseOne')
3const ChooseOnePoll = require('../../../poll/sync/chooseOne')
4const chooseOneResults = require('../../../position/sync/chooseOneResults')
5const {ERROR_POSITION_CHOICE, ERROR_POSITION_LATE} = require('../../../types')
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 = {
19 key: '%schwoop',
20 value: {
21 content: ChooseOnePoll({
22 choices: [1, 2, 'three'],
23 title: 'how many food',
24 closesAt: now
25 })
26 }
27}
28
29test('ChooseOneResults - ChooseOneResults', function (t) {
30 const positions = [
31 { value: { content: ChooseOne({choice: 0, poll}), author: pietId } },
32 { value: { content: ChooseOne({choice: 0, poll}), author: mixId } },
33 { value: { content: ChooseOne({choice: 0, poll}), author: mikeyId } },
34 { value: { content: ChooseOne({choice: 1, poll}), author: timmyId } },
35 { value: { content: ChooseOne({choice: 1, poll}), author: tommyId } },
36 { value: { content: ChooseOne({choice: 2, poll}), author: sallyId } }
37 ]
38
39 const expected = {
40 results: [
41 {
42 choice: 1,
43 voters: {
44 [pietId]: positions[0],
45 [mixId]: positions[1],
46 [mikeyId]: positions[2]
47 }
48 },
49 {
50 choice: 2,
51 voters: {
52 [timmyId]: positions[3],
53 [tommyId]: positions[4]
54 }
55 },
56 {
57 choice: 'three',
58 voters: {
59 [sallyId]: positions[5]
60 }
61 }
62 ],
63 errors: {}
64 }
65
66 const actual = chooseOneResults({positions, poll: validPoll})
67 t.deepEqual(actual, expected, 'results are correct')
68 t.end()
69})
70
71test('ChooseOneResults - a position stated for an invalid choice index is not counted', function (t) {
72 const positions = [
73 { value: { content: ChooseOne({choice: 3, poll}), author: pietId } }
74 ]
75
76 const actual = chooseOneResults({positions, poll: validPoll})
77 t.false(actual.results[3], 'invalid vote is not counted')
78 t.end()
79})
80
81test('ChooseOneResults - a position stated for an invalid choice index is included in the errors object', function (t) {
82 const positions = [
83 { value: { content: ChooseOne({choice: 3, poll}), author: pietId } }
84 ]
85
86 const actual = chooseOneResults({positions, poll: validPoll})
87 t.deepEqual(actual.errors[0].type, ERROR_POSITION_CHOICE, 'invalid vote is on error object')
88 t.end()
89})
90
91test('ChooseOneResults - A position stated before the closing time of the poll is counted', function (t) {
92 const positions = [
93 { value: { content: ChooseOne({choice: 0, poll}), author: pietId, timestamp: now - 1 } }
94 ]
95
96 const actual = chooseOneResults({positions, poll: validPoll})
97 t.true(actual.results[0], 'valid vote is counted')
98 t.end()
99})
100
101test('ChooseOneResults - A position stated after the closing time of the poll is not counted', function (t) {
102 const positions = [
103 { value: { content: ChooseOne({choice: 0, poll}), author: pietId, timestamp: now + 1 } }
104 ]
105
106 const actual = chooseOneResults({positions, poll: validPoll})
107 t.deepEqual(actual.results[0].voters, {}, 'invalid vote is not counted')
108 t.end()
109})
110
111test('ChooseOneResults - A position stated after the closing time of the poll is included in the error object', function (t) {
112 const positions = [
113 { value: { content: ChooseOne({choice: 0, poll}), author: pietId, timestamp: now + 1 } }
114 ]
115
116 const actual = chooseOneResults({positions, poll: validPoll})
117 t.deepEqual(actual.errors[0].type, ERROR_POSITION_LATE, 'invalid vote is on error object')
118 t.end()
119})
120

Built with git-ssb-web