git ssb

0+

Piet / ssb-loomio



Commit ac1a79fec1e45652d23a6905c8540851a850a868

poll.async.get (PR review fixes)

mix irving committed on 3/8/2018, 2:50:04 AM
Parent: f368010bd5cd7dd957193a674e18bd6e6cad8233

Files changed

poll/async/get.jschanged
test/poll/async/get.jsdeleted
test/poll/async/get.test.jsadded
poll/async/get.jsView
@@ -57,23 +57,21 @@
5757 msgs = sort(msgs)
5858
5959 // filter position message into 'positions' and 'errors'
6060 const type = poll.value.content.pollDetails.type
61- msgs
61+
62+ poll.positions = msgs
6263 .filter(msg => msg.value.content.root === poll.key)
63- .forEach(position => {
64- if (isPosition[type](position)) {
65- // TODO validator checks right position shape, but needs to add e.g. acceptible position ranges based on poll
66- poll.positions.push(position)
67- return
68- }
64+ .filter(isPosition[type])
6965
70- if (isPosition(position)) {
71- poll.errors.push({
72- type: ERROR_POSITION_TYPE,
73- message: `Position responses need to be off the ${type} type for this poll`,
74- position
75- })
66+ poll.errors = msgs
67+ .filter(msg => msg.value.content.root === poll.key)
68+ .filter(msg => isPosition(msg) && !isPosition[type](msg))
69+ .map(position => {
70+ return {
71+ type: ERROR_POSITION_TYPE,
72+ message: `Position responses need to be off the ${type} type for this poll`,
73+ position
7674 }
7775 })
7876
7977 poll.results = results({ poll, positions: poll.positions })
test/poll/async/get.jsView
@@ -1,73 +1,0 @@
1-var test = require('tape')
2-var Server = require('scuttle-testbot')
3-var pull = require('pull-stream')
4-
5-var ChooseOnePoll = require('../../../poll/sync/chooseOne')
6-var ChooseOne = require('../../../position/sync/chooseOne')
7-var getPoll = require('../../../poll/async/get')
8-
9-Server
10- .use(require('ssb-backlinks'))
11-
12-var server = Server({name: 'testBotName'})
13-
14-var katie = server.createFeed()
15-var piet = server.createFeed()
16-
17-var pollContent = ChooseOnePoll({
18- title: "what's our mascott team?",
19- choices: ['prairie dog', 'kea', 'hermit crab'],
20- closesAt: nDaysTime(2)
21-})
22-
23-test('pull.async.get', t => {
24- piet.publish(pollContent, (err, poll) => {
25- if (err) throw err
26-
27- pull(
28- pull.values([
29- { author: katie, position: ChooseOne({ poll, choice: 1, reason: 'they are sick!' }) },
30- { author: piet, position: ChooseOne({ poll, choice: 2, reason: 'scuttles 4life' }) }
31- ]),
32- pull.asyncMap((t, cb) => t.author.publish(t.position, cb)),
33- pull.drain(
34- m => {}, // console.log(m.value.content.type),
35- onDone
36- )
37- )
38-
39- function onDone () {
40- getPoll(server)(poll.key, (err, data) => {
41- if (err) throw err
42-
43- // print(data)
44- t.equal(data.key, poll.key, 'has key')
45- t.deepEqual(data.value, poll.value, 'has value')
46-
47- t.equal(data.author, poll.value.author, 'has author')
48- t.equal(data.title, poll.value.content.title, 'has title')
49-
50- t.equal(data.positions.length, 2, 'has positions')
51-
52- t.deepEqual(data.results, {
53- 1: [katie.id], // TODO update this data structure
54- 2: [piet.id],
55- errors: [] // TODO prune this later
56- }, 'has results!')
57-
58- server.close()
59- t.end()
60- })
61- }
62- })
63-})
64-
65-function print (obj) {
66- console.log(JSON.stringify(obj, null, 2))
67-}
68-
69-function nDaysTime (n) {
70- var d = new Date()
71- return d.setDate(d.getDate() + n)
72- // returns integer!
73-}
test/poll/async/get.test.jsView
@@ -1,0 +1,73 @@
1+var test = require('tape')
2+var Server = require('scuttle-testbot')
3+var pull = require('pull-stream')
4+
5+var ChooseOnePoll = require('../../../poll/sync/chooseOne')
6+var ChooseOne = require('../../../position/sync/chooseOne')
7+var getPoll = require('../../../poll/async/get')
8+
9+Server
10+ .use(require('ssb-backlinks'))
11+
12+var server = Server({name: 'testBotName'})
13+
14+var katie = server.createFeed()
15+var piet = server.createFeed()
16+
17+var pollContent = ChooseOnePoll({
18+ title: "what's our mascott team?",
19+ choices: ['prairie dog', 'kea', 'hermit crab'],
20+ closesAt: nDaysTime(2)
21+})
22+
23+test('pull.async.get', t => {
24+ piet.publish(pollContent, (err, poll) => {
25+ if (err) throw err
26+
27+ pull(
28+ pull.values([
29+ { author: katie, position: ChooseOne({ poll, choice: 1, reason: 'they are sick!' }) },
30+ { author: piet, position: ChooseOne({ poll, choice: 2, reason: 'scuttles 4life' }) }
31+ ]),
32+ pull.asyncMap((t, cb) => t.author.publish(t.position, cb)),
33+ pull.drain(
34+ m => {}, // console.log(m.value.content.type),
35+ onDone
36+ )
37+ )
38+
39+ function onDone () {
40+ getPoll(server)(poll.key, (err, data) => {
41+ if (err) throw err
42+
43+ print(data)
44+ t.equal(data.key, poll.key, 'has key')
45+ t.deepEqual(data.value, poll.value, 'has value')
46+
47+ t.equal(data.author, poll.value.author, 'has author')
48+ t.equal(data.title, poll.value.content.title, 'has title')
49+
50+ t.equal(data.positions.length, 2, 'has positions')
51+
52+ t.deepEqual(data.results, {
53+ 1: [katie.id], // TODO update this data structure
54+ 2: [piet.id],
55+ errors: [] // TODO prune this later
56+ }, 'has results!')
57+
58+ server.close()
59+ t.end()
60+ })
61+ }
62+ })
63+})
64+
65+function print (obj) {
66+ console.log(JSON.stringify(obj, null, 2))
67+}
68+
69+function nDaysTime (n) {
70+ var d = new Date()
71+ return d.setDate(d.getDate() + n)
72+ // returns integer!
73+}

Built with git-ssb-web