git ssb

0+

Piet / ssb-loomio



Commit b792992e79b3b28771b347e53432ec76daa47ee5

wip getting tests passing

mix irving authored on 3/20/2018, 10:13:17 AM
Piet Geursen committed on 4/11/2018, 2:44:12 AM
Parent: 9bc85a148ffd605a3657b147dbdf7e05833f5308

Files changed

package-lock.jsonchanged
package.jsonchanged
position/async/buildChooseOne.jschanged
position/async/buildPosition.jschanged
results/sync/buildResults.jschanged
test/poll/sync/buildChooseOne.test.jschanged
test/position/async/buildChooseOne.test.jschanged
package-lock.jsonView
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.jsonView
@@ -35,10 +35,10 @@
3535 "libnested": "^1.2.1",
3636 "lodash.clonedeep": "^4.5.0",
3737 "mutant": "^3.22.1",
3838 "pull-stream": "^3.6.2",
39- "ssb-msg-content": "^1.0.0",
39+ "ssb-msg-content": "^1.0.1",
4040 "ssb-msg-schemas": "^6.3.0",
41- "ssb-poll-schema": "^1.1.0",
41+ "ssb-poll-schema": "^1.1.3",
4242 "ssb-ref": "^2.9.0"
4343 }
4444 }
position/async/buildChooseOne.jsView
@@ -1,14 +1,15 @@
11 const getContent = require('ssb-msg-content')
2-const { isPosition } = require('ssb-poll-schema')
2+const { isPoll, isPosition } = require('ssb-poll-schema')
33 const buildPosition = require('./buildPosition')
44 const { CHOOSE_ONE, ERROR_POSITION_TYPE } = require('../../types')
55
66 module.exports = function (server) {
77 const Position = buildPosition(server)
88
99 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'}))
1112
1213 const opts = {
1314 poll,
1415 details: {
position/async/buildPosition.jsView
@@ -1,11 +1,14 @@
11 const pull = require('pull-stream')
22 const pullAsync = require('pull-async')
33 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')
56
67 module.exports = function (server) {
78 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+
811 // NOTE - getPoll has to be required here to avoid circular deps
912 const getPoll = require('../../poll/async/get')(server)
1013
1114 pull(
@@ -29,10 +32,9 @@
2932 root: poll.key,
3033 details
3134 }
3235
33- const branch = heads(poll.positions)
34- if (branch && branch.length) content.branch = branch
36+ content.branch = heads(poll.positions || [])
3537
3638 if (reason) content.reason = reason
3739 if (poll.channel) content.channel = poll.channel
3840 if (mentions) content.mentions = mentions
results/sync/buildResults.jsView
@@ -13,8 +13,10 @@
1313 // }
1414 // }
1515 //
1616 // 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)
1719 module.exports = function ({positions, poll}) {
1820 if (isChooseOnePoll(poll)) {
1921 return chooseOneResults({positions, poll})
2022 }
@@ -31,10 +33,12 @@
3133 }
3234 })
3335
3436 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)
3741
3842 if (isInvalidChoice({position, poll})) {
3943 acc.errors.push(PositionChoiceError({position}))
4044 return acc
test/poll/sync/buildChooseOne.test.jsView
@@ -1,9 +1,9 @@
11 const test = require('tape')
22 const ChooseOne = require('../../../poll/sync/buildChooseOne')
33 const {isPoll, isChooseOnePoll} = require('ssb-poll-schema')
44
5-test('Position - ChooseOne', function (t) {
5+test('poll.sync.buildeChooseOne', function (t) {
66 var invalidPoll = ChooseOne({
77 })
88 t.false(isPoll(invalidPoll), 'invalid')
99
test/position/async/buildChooseOne.test.jsView
@@ -5,77 +5,84 @@
55 const Server = require('../../../lib/testServer')
66
77 var server = Server()
88
9+const publishChooseOne = require('../../../poll/async/publishChooseOne')(server)
910 const ChooseOne = require('../../../position/async/buildChooseOne')(server)
1011
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)
2214
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
3318
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+ )
4529
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')
5740
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+ }
7078 }
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+ )
7884
7985 // NOTE - we might want an isChooseOnePosition in future
8086 // t.true(isChooseOnePosition(fullPositionMsg), 'simple (full msg)')
87+ })
8188 })

Built with git-ssb-web