Skip to content

Commit

Permalink
Add integration tests from real-time-server
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Sobo <[email protected]>
  • Loading branch information
Antonio Scandurra authored and Nathan Sobo committed Jun 6, 2017
1 parent 39f26f4 commit d9bddbe
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST_DATABASE_URL=postgres://localhost:5432/real-time-server-test
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
"description": "",
"main": "lib/real-time-client.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha **/*.test.js --ui=tdd"
},
"author": "",
"license": "MIT",
"private": true,
"dependencies": {
"pusher-js": "^4.1.0"
},
"devDependencies": {
"dotenv": "^4.0.0",
"mocha": "^3.4.2"
}
}
72 changes: 72 additions & 0 deletions test/helpers/buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const assert = require('assert')

module.exports =
class Buffer {
constructor (text) {
this.text = text
this.textEqualityResolvers = new Map()
}

getText () {
return this.text
}

whenTextEquals (text) {
if (text === this.text) {
return Promise.resolve()
} else {
return new Promise((resolve) => {
let resolvers = this.textEqualityResolvers.get(text)
if (!resolvers) {
resolvers = []
this.textEqualityResolvers.set(text, resolvers)
}
resolvers.push(resolve)
})
}
}

setText (text) {
this.text = text
}

applyMany (operations) {
assert(Array.isArray(operations))

for (let i = operations.length - 1; i >= 0; i--) {
this.apply(operations[i])
}
}

apply (operation) {
if (operation.type === 'delete') {
this.delete(operation.position, operation.extent)
} else if (operation.type === 'insert') {
this.insert(operation.position, operation.text)
} else {
throw new Error('Unknown operation type')
}
}

insert (position, text) {
this.text = this.text.slice(0, position) + text + this.text.slice(position)
this.resolveOnTextEquality()
return {type: 'insert', position, text}
}

delete (position, extent) {
assert(position < this.text.length)
assert(position + extent <= this.text.length)
this.text = this.text.slice(0, position) + this.text.slice(position + extent)
this.resolveOnTextEquality()
return {type: 'delete', position, extent}
}

resolveOnTextEquality () {
const resolvers = this.textEqualityResolvers.get(this.text) || []
for (const resolve of resolvers) {
resolve()
}
this.textEqualityResolvers.delete(this.text)
}
}
53 changes: 53 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require('./setup')
const assert = require('assert')
const Buffer = require('./helpers/buffer')
const Client = require('../lib/real-time-client')
const PusherPubSubGateway = require('../lib/pusher-pub-sub-gateway')
const {startTestServer} = require('real-time-server')

suite('Client Integration', () => {
let server

suiteSetup(async () => {
const params = {databaseURL: process.env.TEST_DATABASE_URL}
// Uncomment and provide credentials to test against Pusher.
// params.pusherCredentials = {
// appId: '123',
// key: '123',
// secret: '123'
// }
server = await startTestServer(params)
})

suiteTeardown(() => {
return server.stop()
})

setup(() => {
return server.reset()
})

test('sharing a buffer from a host and fetching its initial state from a guest', async () => {
const host = new Client({
restGateway: server.restGateway,
pubSubGateway: server.pubSubGateway || new PusherPubSubGateway(server.pusherCredentials)
})
const hostBuffer = new Buffer('hello world')
const hostSharedBuffer = await host.createSharedBuffer(hostBuffer)

const guest = new Client({
restGateway: server.restGateway,
pubSubGateway: server.pubSubGateway || new PusherPubSubGateway(server.pusherCredentials)
})
const guestBuffer = new Buffer('')
const guestSharedBuffer = await guest.joinSharedBuffer(hostSharedBuffer.id, guestBuffer)
assert.equal(guestBuffer.getText(), 'hello world')

hostSharedBuffer.apply(hostBuffer.insert(5, ' cruel'))
guestSharedBuffer.apply(guestBuffer.delete(0, 5))
guestSharedBuffer.apply(guestBuffer.insert(0, 'goodbye'))

await hostBuffer.whenTextEquals('goodbye cruel world')
await guestBuffer.whenTextEquals('goodbye cruel world')
})
})
5 changes: 5 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require('dotenv').config()

process.on('unhandledRejection', (reason) => {
console.error(reason.stack)
})

0 comments on commit d9bddbe

Please sign in to comment.