node tests: Extract topic_data.js.

This mostly moves code, and it also removes some unnecessary
coupling to stream_data.js.  The topic_data code purely
works in the stream_id space, so there's no need to set up
actual stream data for it.
This commit is contained in:
Steve Howell 2017-07-26 08:18:40 -04:00 committed by Tim Abbott
parent a9e296db74
commit 466757c3f1
2 changed files with 60 additions and 65 deletions

View File

@ -242,71 +242,6 @@ var people = global.people;
assert(!ok);
}());
(function test_process_message() {
var stream_id = 55;
var rome = {
name: 'Rome',
stream_id: stream_id,
};
stream_data.add_sub('Rome', rome);
topic_data.add_message({
stream_id: stream_id,
message_id: 101,
topic_name: 'toPic1',
});
var history = topic_data.get_recent_names(rome.stream_id);
assert.deepEqual(history, ['toPic1']);
topic_data.add_message({
stream_id: stream_id,
message_id: 102,
topic_name: 'Topic1',
});
history = topic_data.get_recent_names(rome.stream_id);
assert.deepEqual(history, ['Topic1']);
topic_data.add_message({
stream_id: stream_id,
message_id: 103,
topic_name: 'topic2',
});
history = topic_data.get_recent_names(rome.stream_id);
assert.deepEqual(history, ['topic2', 'Topic1']);
// Removing first topic1 message has no effect.
topic_data.remove_message({
stream_id: stream_id,
topic_name: 'toPic1',
});
history = topic_data.get_recent_names(rome.stream_id);
assert.deepEqual(history, ['topic2', 'Topic1']);
// Removing second topic1 message removes the topic.
topic_data.remove_message({
stream_id: stream_id,
topic_name: 'Topic1',
});
history = topic_data.get_recent_names(rome.stream_id);
assert.deepEqual(history, ['topic2']);
// Test that duplicate remove does not crash us.
topic_data.remove_message({
stream_id: stream_id,
topic_name: 'Topic1',
});
history = topic_data.get_recent_names(rome.stream_id);
assert.deepEqual(history, ['topic2']);
// get to 100% coverage for defensive code
topic_data.remove_message({
stream_id: 9999999,
});
}());
(function test_is_active() {
stream_data.clear_subscriptions();

View File

@ -0,0 +1,60 @@
var topic_data = require('js/topic_data.js');
(function test_basics() {
var stream_id = 55;
topic_data.add_message({
stream_id: stream_id,
message_id: 101,
topic_name: 'toPic1',
});
var history = topic_data.get_recent_names(stream_id);
assert.deepEqual(history, ['toPic1']);
topic_data.add_message({
stream_id: stream_id,
message_id: 102,
topic_name: 'Topic1',
});
history = topic_data.get_recent_names(stream_id);
assert.deepEqual(history, ['Topic1']);
topic_data.add_message({
stream_id: stream_id,
message_id: 103,
topic_name: 'topic2',
});
history = topic_data.get_recent_names(stream_id);
assert.deepEqual(history, ['topic2', 'Topic1']);
// Removing first topic1 message has no effect.
topic_data.remove_message({
stream_id: stream_id,
topic_name: 'toPic1',
});
history = topic_data.get_recent_names(stream_id);
assert.deepEqual(history, ['topic2', 'Topic1']);
// Removing second topic1 message removes the topic.
topic_data.remove_message({
stream_id: stream_id,
topic_name: 'Topic1',
});
history = topic_data.get_recent_names(stream_id);
assert.deepEqual(history, ['topic2']);
// Test that duplicate remove does not crash us.
topic_data.remove_message({
stream_id: stream_id,
topic_name: 'Topic1',
});
history = topic_data.get_recent_names(stream_id);
assert.deepEqual(history, ['topic2']);
// get to 100% coverage for defensive code
topic_data.remove_message({
stream_id: 9999999,
});
}());