UNPKG

6.15 kBJavaScriptView Raw
1var StreamTopic = require('zetta-events-stream-protocol').StreamTopic;
2var assert = require('assert');
3var Query = require('calypso').Query;
4
5describe('Stream Topic', function() {
6 it('will correctly parse a topic of all valid strings', function() {
7 var t = new StreamTopic();
8 t.parse('Detroit/led/1234/state');
9 assert.equal(t.serverName(), 'Detroit');
10 assert.equal(t.pubsubIdentifier(), 'led/1234/state');
11 assert.equal(t.streamQuery, null);
12 });
13
14 it('will correctly parse a topic with RegExp throughout', function() {
15 var t = new StreamTopic();
16 t.parse('{^Det.+$}/**/{^stream.+$}');
17 assert(t.serverName().test);
18 assert.equal(t.pubsubIdentifier(), '**/{^stream.+$}');
19 assert.equal(t.streamQuery, null);
20 });
21
22 it('will correctly parse a topic with regex and *', function() {
23 var t = new StreamTopic();
24 t.parse('{^Det.+$}/{^zigbee.+$}/*/{^stream.+$}');
25 assert(t.serverName().test);
26 assert.equal(t.pubsubIdentifier(), '{^zigbee.+$}/*/{^stream.+$}');
27 assert.equal(t.streamQuery, null);
28 });
29
30 it('will correctly parse a topic with regex and * for all paths', function() {
31 var t = new StreamTopic();
32 t.parse('{^Det.+$}/*/*/*');
33 assert(t.serverName().test);
34 assert.equal(t.pubsubIdentifier(), '*/*/*');
35 assert.equal(t.streamQuery, null);
36 });
37
38 it('will correctly parse a topic with regex and ** for paths', function() {
39 var t = new StreamTopic();
40 t.parse('{^Det.+$}/**');
41 assert(t.serverName().test);
42 assert.equal(t.pubsubIdentifier(), '**');
43 assert.equal(t.streamQuery, null);
44 });
45
46 it('will correctly parse a regex out of a topic string', function() {
47 var t = new StreamTopic();
48 t.parse('{^Det.+$}/led/1234/state');
49 assert(t.serverName().test);
50 assert.equal(t.pubsubIdentifier(), 'led/1234/state');
51 assert.equal(t.streamQuery, null);
52 });
53
54 it('will correctly parse a query out of a topic string', function() {
55 var t = new StreamTopic();
56 t.parse('Detroit/led/1234/state?select * where data > 80');
57 assert.equal(t.serverName(), 'Detroit');
58 assert.equal(t.pubsubIdentifier(), 'led/1234/state');
59 assert.equal(t.streamQuery, 'select * where data > 80');
60 });
61
62 it('will correctly parse topics without the leading server name', function() {
63 var t = new StreamTopic();
64 t.parse('led/1234/state');
65 assert.equal(t.serverName(), 'led');
66 assert.equal(t.pubsubIdentifier(), '1234/state');
67 assert.equal(t.streamQuery, null);
68 })
69
70 it('will correctly parse a topic with double star', function() {
71 var t = new StreamTopic();
72 t.parse('hub/led/**');
73 assert.equal(t.serverName(), 'hub');
74 assert.equal(t.pubsubIdentifier(), 'led/**');
75 assert.equal(t.streamQuery, null);
76 });
77
78 it('will correctly parse a topic hub/**/state', function() {
79 var t = new StreamTopic();
80 t.parse('hub/**/state');
81 assert.equal(t.serverName(), 'hub');
82 assert.equal(t.pubsubIdentifier(), '**/state');
83 assert.equal(t.streamQuery, null);
84 });
85
86 it('will correctly parse a regex topic with a ? in it', function() {
87 var t = new StreamTopic();
88 t.parse('{Detroit-?123}/**/state');
89 assert(t.serverName().test);
90 assert.equal(t.pubsubIdentifier(), '**/state');
91 assert.equal(t.streamQuery, null);
92 });
93
94 it('will correctly parse a regex topic with a ? in it and a query', function() {
95 var t = new StreamTopic();
96 t.parse('{Detroit-?123}/**/state?select * where data > 80');
97 assert(t.serverName().test);
98 assert.equal(t.pubsubIdentifier(), '**/state');
99 assert.equal(t.streamQuery, 'select * where data > 80');
100 });
101
102 it('will correctly parse **/some-topic', function() {
103 var t = new StreamTopic();
104 t.parse('**/some-topic');
105 assert.equal(t.serverName(), '*');
106 assert.equal(t.pubsubIdentifier(), '**/some-topic');
107 assert.equal(t.streamQuery, null);
108 });
109
110 it('will correctly parse **/led/123/state', function() {
111 var t = new StreamTopic();
112 t.parse('**/led/123/state');
113 assert.equal(t.serverName(), '*');
114 assert.equal(t.pubsubIdentifier(), 'led/123/state');
115 assert.equal(t.streamQuery, null);
116 });
117
118 it('will correctly parse **/123/state', function() {
119 var t = new StreamTopic();
120 t.parse('**/123/state');
121 assert.equal(t.serverName(), '*');
122 assert.equal(t.pubsubIdentifier(), '**/123/state');
123 assert.equal(t.streamQuery, null);
124 });
125
126
127 function checkSpecial(topic) {
128 it('will correctly parse special topic ' + topic, function() {
129 var t = new StreamTopic();
130 t.parse(topic);
131 assert.equal(t.serverName(), null);
132 assert.equal(t.isSpecial, true);
133 assert.equal(t.pubsubIdentifier(), topic);
134 })
135 }
136
137 checkSpecial('_peer/connect');
138 checkSpecial('_peer/disconnect');
139 checkSpecial('_peer/*');
140 checkSpecial('_peer/**');
141 checkSpecial('query:where type="led"');
142 checkSpecial('query/where type="led"');
143
144 it('hash() will return the original input', function() {
145 var t = new StreamTopic();
146 var topic = '{^Det.+$}/led/1234/state?select * where data > 80';
147 t.parse(topic);
148 assert.equal(t.hash(), topic);
149 })
150
151 describe('.match()', function() {
152
153 function matchTest(query, topic, eval) {
154 it('should return ' + eval + ' for query ' + query + ' on topic ' + topic, function() {
155 var t = StreamTopic.parse(query);
156 assert.equal(t.match(topic), eval);
157 })
158 }
159
160 matchTest('led/123/*', 'led/123/state', true);
161 matchTest('led/321/*', 'led/123/state', false);
162 matchTest('Detroit/led/123/*', 'led/123/state', false);
163 matchTest('led/**', 'led/123/state', true);
164 matchTest('{^Det.+$}/led/123/state', 'Detroit-123/led/123/state', true);
165 matchTest('{^Det.+$}/led/123/state', 'hub/led/123/state', false);
166 matchTest('{^Det.+$}/led/**', 'Detroit-123/led/123/stream', true);
167 matchTest('{^Det.+$}/led/123/{^stream.+$}', 'Detroit-123/led/123/stream-123', true);
168 matchTest('{^Det.+$}/**/{^stream.+$}', 'Detroit-123/led/123/stream-123', true);
169 matchTest('{^Det.+$}/**', 'Detroit-123/led/123/stream', true);
170 matchTest('*/led/**', 'hub/led/123/state', true);
171 });
172
173});