Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
Multidevice, tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Oct 17, 2024
1 parent e5e3c5f commit f2f1134
Show file tree
Hide file tree
Showing 22 changed files with 2,996 additions and 1,097 deletions.
1,260 changes: 588 additions & 672 deletions Cargo.lock

Large diffs are not rendered by default.

2,117 changes: 1,845 additions & 272 deletions flake.lock

Large diffs are not rendered by default.

48 changes: 44 additions & 4 deletions pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"@msgpack/msgpack": "^2.7.0",
"@holochain/client": "^0.17.1",
"@holochain/tryorama": "^v0.16.0",
"@holochain-open-dev/signals": "^0.300.6",
"@holochain-open-dev/signals": "^0.300.9",
"@holochain-open-dev/utils": "^0.300.3",
"@holochain-open-dev/profiles": "github:holochain-open-dev/profiles#3c854292c1d30247d05764b974ccb145350b144a&path:ui",
"typescript": "^5.4.5",
"vitest": "^1.4.0"
},
Expand Down
37 changes: 32 additions & 5 deletions tests/src/notification-lifecycle.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toPromise } from '@holochain-open-dev/signals';
import { EntryRecord } from '@holochain-open-dev/utils';
import { dhtSync, runScenario } from '@holochain/tryorama';
import { dhtSync, pause, runScenario } from '@holochain/tryorama';
import { assert, test } from 'vitest';

import { sampleNotification } from '../../ui/src/mocks.js';
Expand All @@ -11,6 +11,18 @@ test('create notifications, read it, and dismiss it', async () => {
await runScenario(async scenario => {
const { alice, bob } = await setup(scenario);

const aliceProfile =
await alice.store.client.profilesStore.client.createProfile({
nickname: 'alice',
fields: {},
});

const bobProfile =
await bob.store.client.profilesStore.client.createProfile({
nickname: 'bob',
fields: {},
});

// Wait for the created entry to be propagated to the other node.
await dhtSync([alice.player, bob.player], alice.player.cells[0].cell_id[0]);

Expand All @@ -26,12 +38,12 @@ test('create notifications, read it, and dismiss it', async () => {
// Alice creates a Notification
await alice.store.client.createNotification(
await sampleNotification(alice.store.client, {
recipients: [bob.player.agentPubKey],
recipients_profiles_hashes: [bobProfile.actionHash],
}),
);
await alice.store.client.createNotification(
await sampleNotification(alice.store.client, {
recipients: [bob.player.agentPubKey],
recipients_profiles_hashes: [bobProfile.actionHash],
}),
);

Expand Down Expand Up @@ -92,6 +104,21 @@ test('create notifications and dismiss it directly', async () => {
await runScenario(async scenario => {
const { alice, bob } = await setup(scenario);

const aliceProfile =
await alice.store.client.profilesStore.client.createProfile({
nickname: 'alice',
fields: {},
});

const bobProfile =
await bob.store.client.profilesStore.client.createProfile({
nickname: 'bob',
fields: {},
});

// Wait for the created entry to be propagated to the other node.
await dhtSync([alice.player, bob.player], alice.player.cells[0].cell_id[0]);

let unreadNotifications = await toPromise(bob.store.unreadNotifications);
assert.equal(unreadNotifications.size, 0);
let readNotifications = await toPromise(bob.store.readNotifications);
Expand All @@ -104,12 +131,12 @@ test('create notifications and dismiss it directly', async () => {
// Alice creates a Notification
await alice.store.client.createNotification(
await sampleNotification(alice.store.client, {
recipients: [bob.player.agentPubKey],
recipients_profiles_hashes: [bobProfile.actionHash],
}),
);
await alice.store.client.createNotification(
await sampleNotification(alice.store.client, {
recipients: [bob.player.agentPubKey],
recipients_profiles_hashes: [bobProfile.actionHash],
}),
);

Expand Down
42 changes: 40 additions & 2 deletions tests/src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ProfilesClient, ProfilesStore } from '@holochain-open-dev/profiles';
import { AppWebsocket } from '@holochain/client';
import { Scenario } from '@holochain/tryorama';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
Expand All @@ -21,22 +23,41 @@ export async function setup(scenario: Scenario) {
// Shortcut peer discovery through gossip and register all agents in every
// conductor of the scenario.
await scenario.shareAllAgents();
await alice.conductor
.adminWs()
.authorizeSigningCredentials(alice.cells[0].cell_id);
patchCallZome(alice.appWs as AppWebsocket);

await bob.conductor
.adminWs()
.authorizeSigningCredentials(bob.cells[0].cell_id);
patchCallZome(bob.appWs as AppWebsocket);

const config: NotificationsConfig = {
types: {},
};

const aliceProfilesStore = new ProfilesStore(
new ProfilesClient(alice.appWs as any, 'notifications_test', 'profiles'),
);

const aliceStore = new NotificationsStore(
new NotificationsClient(
aliceProfilesStore,
alice.appWs as any,
'notifications_test',
'notifications',
),
config,
);

const bobProfilesStore = new ProfilesStore(
new ProfilesClient(bob.appWs as any, 'notifications_test', 'profiles'),
);

const bobStore = new NotificationsStore(
new NotificationsClient(
bobProfilesStore,
bob.appWs as any,
'notifications_test',
'notifications',
Expand All @@ -49,8 +70,8 @@ export async function setup(scenario: Scenario) {
await scenario.shareAllAgents();

// Prevent race condition when two zome calls are made instantly at the beginning of the lifecycle that cause a ChainHeadMoved error because they trigger 2 parallel init workflows
await aliceStore.client.getUndismissedNotifications();
await bobStore.client.getUndismissedNotifications();
await aliceStore.client.profilesStore.client.getAllProfiles();
await bobStore.client.profilesStore.client.getAllProfiles();

return {
alice: {
Expand All @@ -63,3 +84,20 @@ export async function setup(scenario: Scenario) {
},
};
}
export function patchCallZome(appWs: AppWebsocket) {
const callZome = appWs.callZome;

appWs.callZome = async req => {
try {
const result = await callZome(req);
return result;
} catch (e) {
if (
!e.toString().includes('Socket is not open') &&
!e.toString().includes('ClientClosedWithPendingRequests')
) {
throw e;
}
}
};
}
Loading

0 comments on commit f2f1134

Please sign in to comment.