Commit b792992e79b3b28771b347e53432ec76daa47ee5
wip getting tests passing
mix irving authored on 3/20/2018, 10:13:17 AMPiet Geursen committed on 4/11/2018, 2:44:12 AM
Parent: 9bc85a148ffd605a3657b147dbdf7e05833f5308
Files changed
package-lock.json | ||
---|---|---|
The diff is too large to show. Use a local git client to view these changes. Old file size: 193392 bytes New file size: 193428 bytes |
package.json | ||
---|---|---|
@@ -35,10 +35,10 @@ | ||
35 | 35 | "libnested": "^1.2.1", |
36 | 36 | "lodash.clonedeep": "^4.5.0", |
37 | 37 | "mutant": "^3.22.1", |
38 | 38 | "pull-stream": "^3.6.2", |
39 | - "ssb-msg-content": "^1.0.0", | |
39 | + "ssb-msg-content": "^1.0.1", | |
40 | 40 | "ssb-msg-schemas": "^6.3.0", |
41 | - "ssb-poll-schema": "^1.1.0", | |
41 | + "ssb-poll-schema": "^1.1.3", | |
42 | 42 | "ssb-ref": "^2.9.0" |
43 | 43 | } |
44 | 44 | } |
position/async/buildChooseOne.js | ||
---|---|---|
@@ -1,14 +1,15 @@ | ||
1 | 1 | const getContent = require('ssb-msg-content') |
2 | -const { isPosition } = require('ssb-poll-schema') | |
2 | +const { isPoll, isPosition } = require('ssb-poll-schema') | |
3 | 3 | const buildPosition = require('./buildPosition') |
4 | 4 | const { CHOOSE_ONE, ERROR_POSITION_TYPE } = require('../../types') |
5 | 5 | |
6 | 6 | module.exports = function (server) { |
7 | 7 | const Position = buildPosition(server) |
8 | 8 | |
9 | 9 | return function ChooseOne ({ poll, choice, reason, mentions }, cb) { |
10 | - if (choice > getContent(poll).details.choices.length - 1) throw new Error({type: ERROR_POSITION_TYPE, message: 'choice outside valid choices range'}) | |
10 | + if (!isPoll(poll)) return cb(new Error('ChooseOne position factory requires a valid poll')) | |
11 | + if (choice > getContent(poll).details.choices.length - 1) return cb(new Error({type: ERROR_POSITION_TYPE, message: 'choice outside valid choices range'})) | |
11 | 12 | |
12 | 13 | const opts = { |
13 | 14 | poll, |
14 | 15 | details: { |
position/async/buildPosition.js | ||
---|---|---|
@@ -1,11 +1,14 @@ | ||
1 | 1 | const pull = require('pull-stream') |
2 | 2 | const pullAsync = require('pull-async') |
3 | 3 | const { heads } = require('ssb-sort') |
4 | -const { isPosition, versionStrings: {V1_SCHEMA_VERSION_STRING} } = require('ssb-poll-schema') | |
4 | +const { isMsg } = require('ssb-ref') | |
5 | +const { isPoll, isPosition, versionStrings: {V1_SCHEMA_VERSION_STRING} } = require('ssb-poll-schema') | |
5 | 6 | |
6 | 7 | module.exports = function (server) { |
7 | 8 | return function Position ({ poll, details, reason, mentions }, cb) { |
9 | + if (!isPoll(poll) && !isMsg(poll)) return cb(new Error('Position factory expects a valid poll')) | |
10 | + | |
8 | 11 | // NOTE - getPoll has to be required here to avoid circular deps |
9 | 12 | const getPoll = require('../../poll/async/get')(server) |
10 | 13 | |
11 | 14 | pull( |
@@ -29,10 +32,9 @@ | ||
29 | 32 | root: poll.key, |
30 | 33 | details |
31 | 34 | } |
32 | 35 | |
33 | - const branch = heads(poll.positions) | |
34 | - if (branch && branch.length) content.branch = branch | |
36 | + content.branch = heads(poll.positions || []) | |
35 | 37 | |
36 | 38 | if (reason) content.reason = reason |
37 | 39 | if (poll.channel) content.channel = poll.channel |
38 | 40 | if (mentions) content.mentions = mentions |
results/sync/buildResults.js | ||
---|---|---|
@@ -13,8 +13,10 @@ | ||
13 | 13 | // } |
14 | 14 | // } |
15 | 15 | // |
16 | 16 | // postions must be of the correct type ie: type checked by the caller. |
17 | + | |
18 | +// TODO find a better home for this (it is not strongly in poll nor position domain) | |
17 | 19 | module.exports = function ({positions, poll}) { |
18 | 20 | if (isChooseOnePoll(poll)) { |
19 | 21 | return chooseOneResults({positions, poll}) |
20 | 22 | } |
@@ -31,10 +33,12 @@ | ||
31 | 33 | } |
32 | 34 | }) |
33 | 35 | |
34 | 36 | return positions.reduce(function (acc, position) { |
35 | - const { author, content } = position.value | |
36 | - const { choice } = content.details | |
37 | + console.log(position) | |
38 | + const { author } = position.value | |
39 | + const { choice } = getContent(position).details | |
40 | + console.log(author, choice) | |
37 | 41 | |
38 | 42 | if (isInvalidChoice({position, poll})) { |
39 | 43 | acc.errors.push(PositionChoiceError({position})) |
40 | 44 | return acc |
test/poll/sync/buildChooseOne.test.js | ||
---|---|---|
@@ -1,9 +1,9 @@ | ||
1 | 1 | const test = require('tape') |
2 | 2 | const ChooseOne = require('../../../poll/sync/buildChooseOne') |
3 | 3 | const {isPoll, isChooseOnePoll} = require('ssb-poll-schema') |
4 | 4 | |
5 | -test('Position - ChooseOne', function (t) { | |
5 | +test('poll.sync.buildeChooseOne', function (t) { | |
6 | 6 | var invalidPoll = ChooseOne({ |
7 | 7 | }) |
8 | 8 | t.false(isPoll(invalidPoll), 'invalid') |
9 | 9 |
test/position/async/buildChooseOne.test.js | ||
---|---|---|
@@ -5,77 +5,84 @@ | ||
5 | 5 | const Server = require('../../../lib/testServer') |
6 | 6 | |
7 | 7 | var server = Server() |
8 | 8 | |
9 | +const publishChooseOne = require('../../../poll/async/publishChooseOne')(server) | |
9 | 10 | const ChooseOne = require('../../../position/async/buildChooseOne')(server) |
10 | 11 | |
11 | -test('Position - ChooseOne', function (t) { | |
12 | - pull( | |
13 | - pullAsync(cb => { | |
14 | - ChooseOne({ | |
15 | - poll: '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256' | |
16 | - }, cb) | |
17 | - }), | |
18 | - pull.drain((missingChoice) => { | |
19 | - t.false(isPosition(missingChoice), 'missing a choice') | |
20 | - }) | |
21 | - ) | |
12 | +test('position.async.buildChooseOne', function (t) { | |
13 | + t.plan(5) | |
22 | 14 | |
23 | - pull( | |
24 | - pullAsync(cb => { | |
25 | - ChooseOne({ | |
26 | - choice: 0 | |
27 | - }, cb) | |
28 | - }), | |
29 | - pull.drain((missingPoll) => { | |
30 | - t.false(isPosition(missingPoll), 'missing a poll') | |
31 | - }) | |
32 | - ) | |
15 | + const pollOpts = {title: 'are you reading this', choices: ['yes', 'no'], closesAt: new Date().toISOString()} | |
16 | + publishChooseOne(pollOpts, (err, poll) => { | |
17 | + if (err) throw err | |
33 | 18 | |
34 | - pull( | |
35 | - pullAsync(cb => { | |
36 | - ChooseOne({ | |
37 | - poll: 'dog?', | |
38 | - choice: 0 | |
39 | - }, cb) | |
40 | - }), | |
41 | - pull.drain((missingPoll) => { | |
42 | - t.false(isPosition(missingPoll), 'does not reference a poll') | |
43 | - }) | |
44 | - ) | |
19 | + pull( | |
20 | + pullAsync(cb => { | |
21 | + ChooseOne({ | |
22 | + poll | |
23 | + }, cb) | |
24 | + }), | |
25 | + pull.drain((missingChoice) => { | |
26 | + t.false(isPosition(missingChoice), 'missing a choice') | |
27 | + }) | |
28 | + ) | |
45 | 29 | |
46 | - pull( | |
47 | - pullAsync(cb => { | |
48 | - ChooseOne({ | |
49 | - poll: '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256', | |
50 | - choice: 0 | |
51 | - }, cb) | |
52 | - }), | |
53 | - pull.drain((poll) => { | |
54 | - t.true(isPosition(poll), 'simple') | |
55 | - }) | |
56 | - ) | |
30 | + t.throws(() => { | |
31 | + pull( | |
32 | + pullAsync(cb => { | |
33 | + ChooseOne({ | |
34 | + choice: 0 | |
35 | + }, cb) | |
36 | + }), | |
37 | + pull.drain((missingPoll) => {}) | |
38 | + ) | |
39 | + }, 'missing a poll') | |
57 | 40 | |
58 | - pull( | |
59 | - pullAsync(cb => { | |
60 | - ChooseOne({ | |
61 | - poll: '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256', | |
62 | - choice: 0 | |
63 | - }, cb) | |
64 | - }), | |
65 | - pull.map((validPosition) => { | |
66 | - return { | |
67 | - key: '%somekey', | |
68 | - value: { | |
69 | - content: validPosition | |
41 | + t.throws(() => { | |
42 | + pull( | |
43 | + pullAsync(cb => { | |
44 | + ChooseOne({ | |
45 | + poll: 'dog?', | |
46 | + choice: 0 | |
47 | + }, cb) | |
48 | + }), | |
49 | + pull.drain((missingPoll) => { }) | |
50 | + ) | |
51 | + }, 'does not reference a poll') | |
52 | + | |
53 | + pull( | |
54 | + pullAsync(cb => { | |
55 | + ChooseOne({ | |
56 | + poll, | |
57 | + choice: 0 | |
58 | + }, cb) | |
59 | + }), | |
60 | + pull.drain((poll) => { | |
61 | + t.true(isPosition(poll), 'simple') | |
62 | + }) | |
63 | + ) | |
64 | + | |
65 | + pull( | |
66 | + pullAsync(cb => { | |
67 | + ChooseOne({ | |
68 | + poll, | |
69 | + choice: 0 | |
70 | + }, cb) | |
71 | + }), | |
72 | + pull.map((validPosition) => { | |
73 | + return { | |
74 | + key: '%somekey', | |
75 | + value: { | |
76 | + content: validPosition | |
77 | + } | |
70 | 78 | } |
71 | - } | |
72 | - }), | |
73 | - pull.drain((poll) => { | |
74 | - t.true(isPosition(poll), 'simple (full msg)') | |
75 | - t.end() | |
76 | - }) | |
77 | - ) | |
79 | + }), | |
80 | + pull.drain((poll) => { | |
81 | + t.true(isPosition(poll), 'simple (full msg)') | |
82 | + }) | |
83 | + ) | |
78 | 84 | |
79 | 85 | // NOTE - we might want an isChooseOnePosition in future |
80 | 86 | // t.true(isChooseOnePosition(fullPositionMsg), 'simple (full msg)') |
87 | + }) | |
81 | 88 | }) |
Built with git-ssb-web