forked from atom/teletype-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration tests from real-time-server
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
Showing
5 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TEST_DATABASE_URL=postgres://localhost:5432/real-time-server-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |