Commit ac1a79fec1e45652d23a6905c8540851a850a868
poll.async.get (PR review fixes)
mix irving committed on 3/8/2018, 2:50:04 AMParent: f368010bd5cd7dd957193a674e18bd6e6cad8233
Files changed
poll/async/get.js | changed |
test/poll/async/get.js | deleted |
test/poll/async/get.test.js | added |
poll/async/get.js | ||
---|---|---|
@@ -57,23 +57,21 @@ | ||
57 | 57 | msgs = sort(msgs) |
58 | 58 | |
59 | 59 | // filter position message into 'positions' and 'errors' |
60 | 60 | const type = poll.value.content.pollDetails.type |
61 | - msgs | |
61 | + | |
62 | + poll.positions = msgs | |
62 | 63 | .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]) | |
69 | 65 | |
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 | |
76 | 74 | } |
77 | 75 | }) |
78 | 76 | |
79 | 77 | poll.results = results({ poll, positions: poll.positions }) |
test/poll/async/get.js | ||
---|---|---|
@@ -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.js | ||
---|---|---|
@@ -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