Files: 7e7895afa684f692c8d3b741dfc512571ef88c23 / test / position / sync / chooseOneResults.test.js
3825 bytesRaw
1 | const test = require('tape') |
2 | const ChooseOne = require('../../../position/sync/chooseOne') |
3 | const ChooseOnePoll = require('../../../poll/sync/chooseOne') |
4 | const Position = require('../../../position/sync/position') |
5 | const chooseOneResults = require('../../../position/sync/chooseOneResults') |
6 | const {ERROR_POSITION_CHOICE, ERROR_POSITION_TYPE, ERROR_POSITION_LATE} = require('../../../types') |
7 | |
8 | const pietId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/0=.ed25519' |
9 | const mixId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/1=.ed25519' |
10 | const mikeyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/2=.ed25519' |
11 | const timmyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/3=.ed25519' |
12 | const tommyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/4=.ed25519' |
13 | const sallyId = '@Mq8D3YC6VdErKQzV3oi2oK5hHSoIwR0hUQr4M46wr/5=.ed25519' |
14 | |
15 | const poll = '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256' |
16 | |
17 | const now = Number(new Date()) |
18 | |
19 | const validPoll = ChooseOnePoll({ |
20 | choices: [1, 2, 'three'], |
21 | title: 'how many food', |
22 | closesAt: now |
23 | }) |
24 | |
25 | test('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 | |
42 | test('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 | |
52 | test('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 | |
62 | test('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 | |
72 | test('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 | |
82 | test('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