git ssb

0+

Piet / ssb-loomio



Tree: 7e7895afa684f692c8d3b741dfc512571ef88c23

Files: 7e7895afa684f692c8d3b741dfc512571ef88c23 / test / position / sync / chooseOneResults.test.js

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

Built with git-ssb-web