git ssb

0+

Piet / ssb-loomio



Tree: 9bf2bbbd3ecdc8c850b63aad19ada17d6bf593c7

Files: 9bf2bbbd3ecdc8c850b63aad19ada17d6bf593c7 / test / poll / obs / get.test.js

3841 bytesRaw
1const test = require('tape')
2const Server = require('../../../lib/testServer')
3const pull = require('pull-stream')
4
5const ChooseOnePoll = require('../../../poll/sync/buildChooseOne')
6const ChooseOnePosition = require('../../../position/async/buildChooseOne')
7const UpdatedClosingTime = require('../../../poll/async/buildUpdatedClosingTime')
8const getPoll = require('../../../poll/obs/get')
9
10const {ERROR_POSITION_CHOICE} = require('../../../types.js')
11
12const server = Server()
13
14const katie = server.createFeed()
15const piet = server.createFeed()
16
17const pollContent = ChooseOnePoll({
18 title: "what's our mascott team?",
19 choices: ['prairie dog', 'kea', 'hermit crab'],
20 closesAt: nDaysTime(2)
21})
22
23const agesAway = nDaysTime(100)
24const soSoon = nDaysTime(1)
25
26test('poll.obs.get', t => {
27 t.plan(15)
28 piet.publish(pollContent, (err, poll) => {
29 t.error(err)
30 const pollDoc = getPoll(server)(poll.key)
31
32 pollDoc.sync(function (sync) {
33 t.ok(sync, 'sync gets set')
34 t.equal(pollDoc.key(), poll.key, 'has valid key once sync is true')
35 t.deepEqual(pollDoc.value(), poll.value, 'has value once sync is true')
36 t.equal(pollDoc.author(), poll.value.author, 'has author once sync is true')
37 t.equal(pollDoc.title(), poll.value.content.title, 'has title once sync is true')
38 })
39
40 pollDoc.positions(function (positions) {
41 if (positions.length === 2) {
42 t.ok(true, 'got 2 positions')
43 t.deepEqual(positions[0].value.content.branch, [poll.key], 'first published position has poll as branch')
44 t.deepEqual(positions[1].value.content.branch, [positions[0].key], 'second published branch has first position as branch')
45 t.equal(positions[0].choice, pollContent.details.choices[1], 'choice is the value from the poll, not the index.')
46 }
47 })
48
49 pollDoc.closesAt(function (closesAt) {
50 if (closesAt === agesAway) {
51 t.ok(true, 'closesAt is eventually set to agesAway')
52 }
53 })
54
55 pollDoc.myPosition(function (position) {
56 t.equal(position.value.content.reason, 'mine', 'my position is eventually observed')
57 })
58
59 pollDoc.results(function (results) {
60 // I hate this but I need to keep going.
61 if (results.length && results[1].voters[katie.id] && results[2].voters[piet.id] && Object.keys(results[1].voters).length === 2) {
62 t.ok(true, 'results eventually are correct')
63 }
64 })
65
66 pollDoc.errors(function (errors) {
67 if (errors.length > 0) {
68 t.equal(errors[0].type, ERROR_POSITION_CHOICE, 'errors are eventually observed')
69 }
70 })
71
72 pull(
73 pull.values([
74 { author: katie, opts: { poll, choice: 1, reason: 'they are sick!' } },
75 { author: piet, opts: { poll, choice: 2, reason: 'scuttles 4life' } },
76 { author: piet, opts: { poll, choice: 2, reason: 'INVALID' } },
77 { author: server, opts: { poll, choice: 1, reason: 'mine' } }
78 ]),
79 pull.asyncMap((t, cb) => {
80 // NOTE: piet.get does not exist, so have to build using the master server
81 ChooseOnePosition(server)(t.opts, (err, position) => {
82 if (err) return cb(err)
83 if (position.reason === 'INVALID') {
84 position.details.choice = 1000
85 }
86 t.position = position
87 cb(null, t)
88 })
89 }),
90 pull.asyncMap((t, cb) => t.author.publish(t.position, cb)),
91 pull.asyncMap((m, cb) => UpdatedClosingTime(server)({poll, closesAt: soSoon}, cb)),
92 pull.asyncMap((m, cb) => UpdatedClosingTime(server)({poll, closesAt: agesAway}, cb)),
93 pull.asyncMap((t, cb) => piet.publish(t, cb)),
94 pull.drain(
95 m => {},
96 onDone
97 )
98 )
99
100 function onDone () {
101 server.close()
102 }
103 })
104})
105
106function nDaysTime (n) {
107 const d = new Date()
108 d.setDate(d.getDate() + n)
109
110 return d.toISOString()
111}
112

Built with git-ssb-web