UNPKG

4.88 kBJavaScriptView Raw
1'use strict'
2
3/* global describe, beforeEach, it */
4
5const chai = require('chai')
6const sinon = require('sinon')
7chai.use(require('sinon-chai'))
8
9const expect = chai.expect
10
11const Brain = require('../src/brain')
12const InMemoryDataStore = require('../src/datastores/memory')
13
14describe('Datastore', function () {
15 beforeEach(function () {
16 this.clock = sinon.useFakeTimers()
17 this.robot = {
18 emit () {},
19 on () {},
20 receive: sinon.spy()
21 }
22
23 // This *should* be callsArgAsync to match the 'on' API, but that makes
24 // the tests more complicated and seems irrelevant.
25 sinon.stub(this.robot, 'on').withArgs('running').callsArg(1)
26
27 this.robot.brain = new Brain(this.robot)
28 this.robot.datastore = new InMemoryDataStore(this.robot)
29 this.robot.brain.userForId('1', { name: 'User One' })
30 this.robot.brain.userForId('2', { name: 'User Two' })
31 })
32
33 describe('global scope', function () {
34 it('returns undefined for values not in the datastore', function () {
35 return this.robot.datastore.get('blah').then(function (value) {
36 expect(value).to.be.an('undefined')
37 })
38 })
39
40 it('can store simple values', function () {
41 return this.robot.datastore.set('key', 'value').then(() => {
42 return this.robot.datastore.get('key').then((value) => {
43 expect(value).to.equal('value')
44 })
45 })
46 })
47
48 it('can store arbitrary JavaScript values', function () {
49 const object = {
50 name: 'test',
51 data: [1, 2, 3]
52 }
53 return this.robot.datastore.set('key', object).then(() => {
54 return this.robot.datastore.get('key').then((value) => {
55 expect(value.name).to.equal('test')
56 expect(value.data).to.deep.equal([1, 2, 3])
57 })
58 })
59 })
60
61 it('can dig inside objects for values', function () {
62 const object = {
63 a: 'one',
64 b: 'two'
65 }
66 return this.robot.datastore.set('key', object).then(() => {
67 return this.robot.datastore.getObject('key', 'a').then((value) => {
68 expect(value).to.equal('one')
69 })
70 })
71 })
72
73 it('can set individual keys inside objects', function () {
74 const object = {
75 a: 'one',
76 b: 'two'
77 }
78 return this.robot.datastore.set('object', object).then(() => {
79 return this.robot.datastore.setObject('object', 'c', 'three').then(() => {
80 return this.robot.datastore.get('object').then((value) => {
81 expect(value.a).to.equal('one')
82 expect(value.b).to.equal('two')
83 expect(value.c).to.equal('three')
84 })
85 })
86 })
87 })
88
89 it('creates an object from scratch when none exists', function () {
90 return this.robot.datastore.setObject('object', 'key', 'value').then(() => {
91 return this.robot.datastore.get('object').then((value) => {
92 const expected = { key: 'value' }
93 expect(value).to.deep.equal(expected)
94 })
95 })
96 })
97
98 it('can append to an existing array', function () {
99 return this.robot.datastore.set('array', [1, 2, 3]).then(() => {
100 return this.robot.datastore.setArray('array', 4).then(() => {
101 return this.robot.datastore.get('array').then((value) => {
102 expect(value).to.deep.equal([1, 2, 3, 4])
103 })
104 })
105 })
106 })
107
108 it('creates an array from scratch when none exists', function () {
109 return this.robot.datastore.setArray('array', 4).then(() => {
110 return this.robot.datastore.get('array').then((value) => {
111 expect(value).to.deep.equal([4])
112 })
113 })
114 })
115 })
116
117 describe('User scope', function () {
118 it('has access to the robot object', function () {
119 const user = this.robot.brain.userForId('1')
120 expect(user._getRobot()).to.equal(this.robot)
121 })
122
123 it('can store user data which is separate from global data', function () {
124 const user = this.robot.brain.userForId('1')
125 return user.set('blah', 'blah').then(() => {
126 return user.get('blah').then((userBlah) => {
127 return this.robot.datastore.get('blah').then((datastoreBlah) => {
128 expect(userBlah).to.not.equal(datastoreBlah)
129 expect(userBlah).to.equal('blah')
130 expect(datastoreBlah).to.be.an('undefined')
131 })
132 })
133 })
134 })
135
136 it('stores user data separate per-user', function () {
137 const userOne = this.robot.brain.userForId('1')
138 const userTwo = this.robot.brain.userForId('2')
139 return userOne.set('blah', 'blah').then(() => {
140 return userOne.get('blah').then((valueOne) => {
141 return userTwo.get('blah').then((valueTwo) => {
142 expect(valueOne).to.not.equal(valueTwo)
143 expect(valueOne).to.equal('blah')
144 expect(valueTwo).to.be.an('undefined')
145 })
146 })
147 })
148 })
149 })
150})