Files: be17b27d1ff6966e3d7d922f5e612f4d2477dce4 / test / position / sync / chooseOneResults.test.js
4023 bytesRaw
1 | const test = require('tape') |
2 | const ChooseOne = require('../../../position/sync/chooseOne') |
3 | const ChooseOnePoll = require('../../../poll/sync/chooseOne') |
4 | const chooseOneResults = require('../../../position/sync/chooseOneResults') |
5 | const {ERROR_POSITION_CHOICE, ERROR_POSITION_TYPE, ERROR_POSITION_LATE} = require('../../../types') |
6 | |
7 | const pietId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/0=.ed25519' |
8 | const mixId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/1=.ed25519' |
9 | const mikeyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/2=.ed25519' |
10 | const timmyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/3=.ed25519' |
11 | const tommyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/4=.ed25519' |
12 | const sallyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/5=.ed25519' |
13 | |
14 | const poll = '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256' |
15 | |
16 | const now = Number(new Date()) |
17 | |
18 | const 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 | |
29 | test('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 | |
71 | test('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[3], 'invalid vote is not counted') |
78 | t.end() |
79 | }) |
80 | |
81 | test('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 | |
91 | test('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[0], 'valid vote is counted') |
98 | t.end() |
99 | }) |
100 | |
101 | test('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.false(actual[0], 'invalid vote is not counted') |
108 | t.end() |
109 | }) |
110 | |
111 | test('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