Commit be47c4200236fb818783a444f2a01b0707ec0a46
Move to async. Pass chooseOne tests
Piet Geursen committed on 3/11/2018, 9:58:16 PMParent: 37adf78cdbbd4382daca8b71e27337cdacfe5570
Files changed
package-lock.json | ||
---|---|---|
The diff is too large to show. Use a local git client to view these changes. Old file size: 174788 bytes New file size: 174754 bytes |
package.json | ||
---|---|---|
@@ -23,8 +23,9 @@ | ||
23 | 23 … | "url": "https://github.com/ssbc/scuttle-poll/issues" |
24 | 24 … | }, |
25 | 25 … | "homepage": "https://github.com/ssbc/scuttle-poll#readme", |
26 | 26 … | "devDependencies": { |
27 … | + "pull-async": "^1.0.0", | |
27 | 28 … | "scuttle-testbot": "^1.1.4", |
28 | 29 … | "ssb-backlinks": "^0.6.1", |
29 | 30 … | "tap-spec": "^4.1.1", |
30 | 31 … | "tape": "^4.8.0" |
position/sync/chooseOne.js | ||
---|---|---|
@@ -1,17 +1,0 @@ | ||
1 | -const Position = require('./position') | |
2 | -const { CHOOSE_ONE } = require('../../types') | |
3 | - | |
4 | -function ChooseOne ({ poll, choice, reason, channel, mentions }) { | |
5 | - return Position({ | |
6 | - poll, | |
7 | - details: { | |
8 | - type: CHOOSE_ONE, | |
9 | - choice | |
10 | - }, | |
11 | - reason, | |
12 | - channel, | |
13 | - mentions | |
14 | - }) | |
15 | -} | |
16 | - | |
17 | -module.exports = ChooseOne |
position/sync/position.js | ||
---|---|---|
@@ -1,38 +1,0 @@ | ||
1 | -const { isMsg: isMsgRef } = require('ssb-ref') | |
2 | -// var { link } = require('ssb-msg-schemas/util') | |
3 | - | |
4 | -function Position ({ poll = {}, details, reason, channel, mentions }) { | |
5 | - const content = { | |
6 | - type: 'position', | |
7 | - root: typeof poll === 'string' ? poll : poll.key, | |
8 | - details | |
9 | - } | |
10 | - | |
11 | - if (reason) content.reason = reason | |
12 | - | |
13 | - // TODO branch should be calculated | |
14 | - // ... this needs to be async... unless all messages are passed in | |
15 | - | |
16 | - // // NOTE mentions can be derived from text, | |
17 | - // // or we could leave it so you can manually notify people without having to at-mention spam the text | |
18 | - // if (mentions && (!Array.isArray(mentions) || mentions.length)) { | |
19 | - // mentions = links(mentions) | |
20 | - // if (!mentions || !mentions.length) { throw new Error('mentions are not valid links') } | |
21 | - // content.mentions = mentions | |
22 | - // } | |
23 | - | |
24 | - // // NOTE recps should be derived from the poll I think | |
25 | - // if (recps && (!Array.isArray(recps) || recps.length)) { | |
26 | - // recps = links(recps) | |
27 | - // if (!recps || !recps.length) { throw new Error('recps are not valid links') } | |
28 | - // content.recps = recps | |
29 | - // } | |
30 | - if (channel) { | |
31 | - if (typeof channel !== 'string') { throw new Error('channel must be a string') } | |
32 | - content.channel = channel | |
33 | - } | |
34 | - | |
35 | - return content | |
36 | -} | |
37 | - | |
38 | -module.exports = Position |
position/async/chooseOne.js | |||
---|---|---|---|
@@ -1,0 +1,17 @@ | |||
1 … | +const Position = require('./position') | ||
2 … | +const { CHOOSE_ONE } = require('../../types') | ||
3 … | + | ||
4 … | +function ChooseOne ({ poll, choice, reason, channel, mentions }, cb) { | ||
5 … | + Position({ | ||
6 … | + poll, | ||
7 … | + details: { | ||
8 … | + type: CHOOSE_ONE, | ||
9 … | + choice | ||
10 … | + }, | ||
11 … | + reason, | ||
12 … | + channel, | ||
13 … | + mentions | ||
14 … | + }, cb) | ||
15 … | +} | ||
16 … | + | ||
17 … | +module.exports = ChooseOne |
position/async/position.js | ||
---|---|---|
@@ -1,0 +1,38 @@ | ||
1 … | +const { isMsg: isMsgRef } = require('ssb-ref') | |
2 … | +// var { link } = require('ssb-msg-schemas/util') | |
3 … | + | |
4 … | +function Position ({ poll = {}, details, reason, channel, mentions }, cb) { | |
5 … | + const content = { | |
6 … | + type: 'position', | |
7 … | + root: typeof poll === 'string' ? poll : poll.key, | |
8 … | + details | |
9 … | + } | |
10 … | + | |
11 … | + if (reason) content.reason = reason | |
12 … | + | |
13 … | + // TODO branch should be calculated | |
14 … | + // ... this needs to be async... unless all messages are passed in | |
15 … | + | |
16 … | + // // NOTE mentions can be derived from text, | |
17 … | + // // or we could leave it so you can manually notify people without having to at-mention spam the text | |
18 … | + // if (mentions && (!Array.isArray(mentions) || mentions.length)) { | |
19 … | + // mentions = links(mentions) | |
20 … | + // if (!mentions || !mentions.length) { throw new Error('mentions are not valid links') } | |
21 … | + // content.mentions = mentions | |
22 … | + // } | |
23 … | + | |
24 … | + // // NOTE recps should be derived from the poll I think | |
25 … | + // if (recps && (!Array.isArray(recps) || recps.length)) { | |
26 … | + // recps = links(recps) | |
27 … | + // if (!recps || !recps.length) { throw new Error('recps are not valid links') } | |
28 … | + // content.recps = recps | |
29 … | + // } | |
30 … | + if (channel) { | |
31 … | + if (typeof channel !== 'string') { throw new Error('channel must be a string') } | |
32 … | + content.channel = channel | |
33 … | + } | |
34 … | + | |
35 … | + cb(null, content) | |
36 … | +} | |
37 … | + | |
38 … | +module.exports = Position |
test/position/sync/chooseOne.test.js | ||
---|---|---|
@@ -1,39 +1,0 @@ | ||
1 | -const test = require('tape') | |
2 | -const ChooseOne = require('../../../position/sync/chooseOne') | |
3 | -const isPosition = require('../../../isPosition') | |
4 | - | |
5 | -test('Position - ChooseOne', function (t) { | |
6 | - var missingChoice = ChooseOne({ | |
7 | - poll: '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256' | |
8 | - }) | |
9 | - t.false(isPosition(missingChoice), 'missing a choice') | |
10 | - | |
11 | - var missingPoll = ChooseOne({ | |
12 | - choice: 0 | |
13 | - }) | |
14 | - t.false(isPosition(missingPoll), 'missing a poll') | |
15 | - | |
16 | - var brokenPoll = ChooseOne({ | |
17 | - poll: 'dog?', | |
18 | - choice: 0 | |
19 | - }) | |
20 | - t.false(isPosition(missingPoll), 'does not reference a poll') | |
21 | - | |
22 | - var validPosition = ChooseOne({ | |
23 | - poll: '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256', | |
24 | - choice: 0 | |
25 | - }) | |
26 | - t.true(isPosition(validPosition), 'simple') | |
27 | - | |
28 | - var fullPositionMsg = { | |
29 | - key: '%somekey', | |
30 | - value: { | |
31 | - content: validPosition | |
32 | - } | |
33 | - } | |
34 | - t.true(isPosition(fullPositionMsg), 'simple (full msg)') | |
35 | - // NOTE - we might want an isChooseOnePosition in future | |
36 | - // t.true(isChooseOnePosition(fullPositionMsg), 'simple (full msg)') | |
37 | - | |
38 | - t.end() | |
39 | -}) |
test/position/async/chooseOne.test.js | ||
---|---|---|
@@ -1,0 +1,77 @@ | ||
1 … | +const test = require('tape') | |
2 … | +var pull = require('pull-stream') | |
3 … | +var pullAsync = require('pull-async') | |
4 … | +const ChooseOne = require('../../../position/async/chooseOne') | |
5 … | +const isPosition = require('../../../isPosition') | |
6 … | + | |
7 … | +test('Position - ChooseOne', function (t) { | |
8 … | + pull( | |
9 … | + pullAsync(cb => { | |
10 … | + ChooseOne({ | |
11 … | + poll: '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256' | |
12 … | + }, cb) | |
13 … | + }), | |
14 … | + pull.drain((missingChoice) => { | |
15 … | + t.false(isPosition(missingChoice), 'missing a choice') | |
16 … | + }) | |
17 … | + ) | |
18 … | + | |
19 … | + pull( | |
20 … | + pullAsync(cb => { | |
21 … | + ChooseOne({ | |
22 … | + choice: 0 | |
23 … | + }, cb) | |
24 … | + }), | |
25 … | + pull.drain((missingPoll) => { | |
26 … | + t.false(isPosition(missingPoll), 'missing a poll') | |
27 … | + }) | |
28 … | + ) | |
29 … | + | |
30 … | + pull( | |
31 … | + pullAsync(cb => { | |
32 … | + ChooseOne({ | |
33 … | + poll: 'dog?', | |
34 … | + choice: 0 | |
35 … | + }, cb) | |
36 … | + }), | |
37 … | + pull.drain((missingPoll) => { | |
38 … | + t.false(isPosition(missingPoll), 'does not reference a poll') | |
39 … | + }) | |
40 … | + ) | |
41 … | + | |
42 … | + pull( | |
43 … | + pullAsync(cb => { | |
44 … | + ChooseOne({ | |
45 … | + poll: '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256', | |
46 … | + choice: 0 | |
47 … | + }, cb) | |
48 … | + }), | |
49 … | + pull.drain((poll) => { | |
50 … | + t.true(isPosition(poll), 'simple') | |
51 … | + }) | |
52 … | + ) | |
53 … | + | |
54 … | + pull( | |
55 … | + pullAsync(cb => { | |
56 … | + ChooseOne({ | |
57 … | + poll: '%t+PhrNxxXkw/jMo6mnwUWfFjJapoPWxzsQoe0Np+nYw=.sha256', | |
58 … | + choice: 0 | |
59 … | + }, cb) | |
60 … | + }), | |
61 … | + pull.map((validPosition) => { | |
62 … | + return { | |
63 … | + key: '%somekey', | |
64 … | + value: { | |
65 … | + content: validPosition | |
66 … | + } | |
67 … | + } | |
68 … | + }), | |
69 … | + pull.drain((poll) => { | |
70 … | + t.true(isPosition(poll), 'simple (full msg)') | |
71 … | + t.end() | |
72 … | + }) | |
73 … | + ) | |
74 … | + | |
75 … | + // NOTE - we might want an isChooseOnePosition in future | |
76 … | + // t.true(isChooseOnePosition(fullPositionMsg), 'simple (full msg)') | |
77 … | +}) |
Built with git-ssb-web