Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get ready for the asyncronous storage peers #112

Closed
wants to merge 10 commits into from
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@emotion/react": "^11.1.5",
"@localfirst/relay-client": "^3.2.0",
"automerge": "^1.0.1-preview.3",
"automerge-sync": "^1.0.0",
"bip39": "^3.0.4",
"bson": "^4.4.0",
"buffer": "^6.0.3",
Expand Down
143 changes: 0 additions & 143 deletions src/backend/AutomergeDiscovery.ts

This file was deleted.

43 changes: 31 additions & 12 deletions src/backend/backchannel.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Backchannel, EVENTS } from './backchannel';
import { generateKey } from './crypto';
import { randomBytes } from 'crypto';
import { DiscoveryKey } from './types';
import { FileProgress } from './blobs';
import { FileMessage, FileState, TextMessage } from './types';

let doc,
petbob_id,
android_id,
petalice_id = null;
let alice, bob, android: Backchannel;
let alice, bob: Backchannel, android: Backchannel;
let server,
port = 3001;
let relay = `ws://localhost:${port}`;
Expand All @@ -22,6 +23,16 @@ async function connected(device: Backchannel, id: string) {
return p;
}

async function ondownload(d) {
return new Promise<void>((resolve) => {
let onPatch = async (event: FileProgress) => {
d.removeListener(EVENTS.FILE_DOWNLOAD, onPatch);
resolve();
};
d.on(EVENTS.FILE_DOWNLOAD, onPatch);
});
}

async function onmessage(device, id) {
return new Promise<any>((resolve) => {
let onPatch = async ({ contactId, docId }) => {
Expand All @@ -47,7 +58,7 @@ function multidevice(done) {
let synced_bob = android.db.getContactById(alices_bob.id);
expect(synced_bob.key).toBe(alices_bob.key);

let messages = android.getMessagesByContactId(alices_bob.id);
let messages = await android.getMessagesByContactId(alices_bob.id);
expect(messages).toStrictEqual([]);

let msgText = 'hey alice';
Expand All @@ -59,8 +70,8 @@ function multidevice(done) {
await bob.sendMessage(petalice_id, msgText);

await allPatched;
let msgs = android.getMessagesByContactId(alices_bob.id);
let og = alice.getMessagesByContactId(alices_bob.id);
let msgs = await android.getMessagesByContactId(alices_bob.id);
let og = await alice.getMessagesByContactId(alices_bob.id);
expect(msgs).toStrictEqual(og);

done({
Expand Down Expand Up @@ -149,8 +160,8 @@ test('getMessagesByContactId', async () => {
let msgs = ['hey .... whats up', 'h4x the planet', 'ok bob'];

let contact = bob.db.getContactById(petalice_id);
msgs.map((msg) => bob.sendMessage(contact.id, msg));
let messages = bob.getMessagesByContactId(petalice_id);
await Promise.all(msgs.map((msg) => bob.sendMessage(contact.id, msg)));
let messages = await bob.getMessagesByContactId(petalice_id);
expect(messages.length).toBe(msgs.length);
});

Expand All @@ -168,9 +179,9 @@ test('integration send a message', async () => {
let p = onmessage(bob, docId);
await alice.sendMessage(outgoing.contact, outgoing.text);
await p;
let messages = bob.getMessagesByContactId(petalice_id);
let messages = await bob.getMessagesByContactId(petalice_id);
expect(messages.length).toBe(1);
expect(messages[0].text).toBe(outgoing.text);
expect((messages[0] as TextMessage).text).toBe(outgoing.text);
});

test('presence', (done) => {
Expand Down Expand Up @@ -255,19 +266,27 @@ test('integration send multiple messages', async () => {
await alice.sendMessage(outgoing.contact, outgoing.text);
await p;

let messages = bob.getMessagesByContactId(petalice_id);
let messages = await bob.getMessagesByContactId(petalice_id);
expect(messages.length).toBe(1);

p = onmessage(alice, docId);
await bob.sendMessage(response.contact, response.text);
await p;

p = ondownload(alice);
let blob = new File(['test text'], 'boop.txt', { type: 'text/plain' });
let msg = await bob.createFileMessage(petalice_id, blob);
await bob.sendFile(msg, blob);
await p;

docId = alice.db.getContactById(petbob_id).discoveryKey;
let alices = alice.getMessagesByContactId(petbob_id);
let alices = await alice.getMessagesByContactId(petbob_id);
expect(alices[0].text).toBe(outgoing.text);
expect(alices[1].text).toBe(response.text);
let bobs = bob.getMessagesByContactId(petalice_id);
let bobs = await bob.getMessagesByContactId(petalice_id);
expect(alices).toStrictEqual(bobs);
expect(alices[2].state).toEqual(FileState.SUCCESS);
expect((bobs[2] as FileMessage).state).toEqual(FileState.SUCCESS);
});

test('unlink device', (done) => {
Expand Down
Loading