rhizome/__tests__/run/002-two-nodes.ts

87 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-12-25 19:27:36 -06:00
import Debug from 'debug';
import {App} from '../../util/app.js';
2024-12-25 19:27:36 -06:00
const debug = Debug('test:two');
describe('Run', () => {
const apps: App[] = [];
beforeAll(async () => {
apps[0] = new App({
httpEnable: true,
2024-12-31 12:40:26 -06:00
peerId: 'app-002-A',
});
apps[1] = new App({
httpEnable: true,
2024-12-31 12:40:26 -06:00
peerId: 'app-002-B',
// Make the apps use the same pubsub topic so they can talk to each other
pubSubTopic: apps[0].config.pubSubTopic,
});
debug('app[0].config.seedPeers before adding:', JSON.stringify(apps[0].config.seedPeers));
2024-12-25 19:27:36 -06:00
apps[0].config.seedPeers.push(apps[1].myRequestAddr);
debug('app[0].config.seedPeers after adding:', JSON.stringify(apps[0].config.seedPeers));
debug('app[1].config.seedPeers before adding:', JSON.stringify(apps[1].config.seedPeers));
2024-12-25 19:27:36 -06:00
apps[1].config.seedPeers.push(apps[0].myRequestAddr);
debug('app[1].config.seedPeers after adding:', JSON.stringify(apps[1].config.seedPeers));
debug('app[0].config.seedPeers after adding:', JSON.stringify(apps[0].config.seedPeers));
await Promise.all(apps.map((app) => app.start()));
});
afterAll(async () => {
await Promise.all(apps.map((app) => app.stop()));
});
it('can create a record on app0 and read it on app1', async () => {
2024-12-25 19:27:36 -06:00
debug('apps[0].apiUrl', apps[0].apiUrl);
debug('apps[1].apiUrl', apps[1].apiUrl);
// Create a new record on app0
{
const res = await fetch(`${apps[0].apiUrl}/user`, {
method: 'PUT',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
id: "peon-1",
properties: {
name: "Peon",
age: 741
}
})
});
const data = await res.json();
expect(data).toMatchObject({
id: "peon-1",
2024-12-25 19:27:36 -06:00
properties: {
name: "Peon",
age: 741
2024-12-25 19:27:36 -06:00
}
});
}
// TODO remove delay
2024-12-25 17:32:20 -06:00
await new Promise((resolve) => setTimeout(resolve, 100));
// Read from app1
{
const res = await fetch(`${apps[1].apiUrl}/user/peon-1`);
const data = await res.json();
debug('data', data);
expect(data).toMatchObject({
id: "peon-1",
properties: {
name: "Peon",
age: 741
}
});
}
// Verify our record is also in the index
for (const app of apps) {
const res = await fetch(`${app.apiUrl}/user/ids`);
const data = await res.json();
expect(data).toMatchObject({ids: ["peon-1"]});
}
});
});