git ssb

0+

Piet / ssb-loomio



Tree: 6d51512c0a6ee2e9c2d4efe211c22333055c8c03

Files: 6d51512c0a6ee2e9c2d4efe211c22333055c8c03 / test / position / sync / chooseOneResults.test.js

3725 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_TYPE, 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 actual = chooseOneResults({positions, poll: validPoll})
40 t.deepEqual(actual[0], [pietId, mixId, mikeyId], 'correct voters for choice 0')
41 t.deepEqual(actual[1], [timmyId, tommyId], 'correct voters for choice 1')
42 t.deepEqual(actual[2], [sallyId], 'correct voters for choice 2')
43 t.end()
44})
45
46test('ChooseOneResults - a position stated for an invalid choice index is not counted', function (t) {
47 const positions = [
48 { value: { content: ChooseOne({choice: 3, poll}), author: pietId } }
49 ]
50
51 const actual = chooseOneResults({positions, poll: validPoll})
52 t.false(actual[3], 'invalid vote is not counted')
53 t.end()
54})
55
56test('ChooseOneResults - a position stated for an invalid choice index is included in the errors object', function (t) {
57 const positions = [
58 { value: { content: ChooseOne({choice: 3, poll}), author: pietId } }
59 ]
60
61 const actual = chooseOneResults({positions, poll: validPoll})
62 t.deepEqual(actual.errors[0].type, ERROR_POSITION_CHOICE, 'invalid vote is on error object')
63 t.end()
64})
65
66test('ChooseOneResults - A position stated before the closing time of the poll is counted', function (t) {
67 const positions = [
68 { value: { content: ChooseOne({choice: 0, poll}), author: pietId, timestamp: now - 1 } }
69 ]
70
71 const actual = chooseOneResults({positions, poll: validPoll})
72 t.true(actual[0], 'valid vote is counted')
73 t.end()
74})
75
76test('ChooseOneResults - A position stated after the closing time of the poll is not counted', function (t) {
77 const positions = [
78 { value: { content: ChooseOne({choice: 0, poll}), author: pietId, timestamp: now + 1 } }
79 ]
80
81 const actual = chooseOneResults({positions, poll: validPoll})
82 t.false(actual[0], 'invalid vote is not counted')
83 t.end()
84})
85
86test('ChooseOneResults - A position stated after the closing time of the poll is included in the error object', function (t) {
87 const positions = [
88 { value: { content: ChooseOne({choice: 0, poll}), author: pietId, timestamp: now + 1 } }
89 ]
90
91 const actual = chooseOneResults({positions, poll: validPoll})
92 t.deepEqual(actual.errors[0].type, ERROR_POSITION_LATE, 'invalid vote is on error object')
93 t.end()
94})
95

Built with git-ssb-web