|
| 1 | +import { DnaSource, Record, ActionHash, EntryHash, AppEntryDef, Create, AgentPubKey, encodeHashToBase64 } from "@holochain/client"; |
| 2 | +import { |
| 3 | + pause, |
| 4 | + runScenario, |
| 5 | + Scenario, |
| 6 | + createConductor, |
| 7 | + addAllAgentsToAllConductors, |
| 8 | + cleanAllConductors, |
| 9 | +} from "@holochain/tryorama"; |
| 10 | +import { decode } from "@msgpack/msgpack"; |
| 11 | +import { AppletConfig, Assessment, AssessmentWithDimensionAndResource, CreateAppletConfigInput, CreateAssessmentInput, Method, RangeValueInteger } from "@neighbourhoods/client"; |
| 12 | +import { ok } from "assert"; |
| 13 | +import test from "tape-promise/tape"; |
| 14 | +import { installAgent, sampleAppletConfig } from "../utils"; |
| 15 | + |
| 16 | +interface TestPost { |
| 17 | + title: string; |
| 18 | + content: string; |
| 19 | +} |
| 20 | + |
| 21 | +export const setUpAliceandBob = async ( |
| 22 | + with_config: boolean = false, |
| 23 | + resource_base_type?: any |
| 24 | +) => { |
| 25 | + const alice = await createConductor(); |
| 26 | + const bob = await createConductor(); |
| 27 | + const { |
| 28 | + agentsHapps: alice_happs, |
| 29 | + agent_key: alice_agent_key, |
| 30 | + ss_cell_id: ss_cell_id_alice, |
| 31 | + provider_cell_id: provider_cell_id_alice, |
| 32 | + } = await installAgent( |
| 33 | + alice, |
| 34 | + "alice", |
| 35 | + undefined, |
| 36 | + with_config, |
| 37 | + resource_base_type |
| 38 | + ); |
| 39 | + const { |
| 40 | + agentsHapps: bob_happs, |
| 41 | + agent_key: bob_agent_key, |
| 42 | + ss_cell_id: ss_cell_id_bob, |
| 43 | + provider_cell_id: provider_cell_id_bob, |
| 44 | + } = await installAgent( |
| 45 | + bob, |
| 46 | + "bob", |
| 47 | + alice_agent_key, |
| 48 | + with_config, |
| 49 | + resource_base_type |
| 50 | + ); |
| 51 | + await addAllAgentsToAllConductors([alice, bob]); |
| 52 | + return { |
| 53 | + alice, |
| 54 | + bob, |
| 55 | + alice_happs, |
| 56 | + bob_happs, |
| 57 | + alice_agent_key, |
| 58 | + bob_agent_key, |
| 59 | + ss_cell_id_alice, |
| 60 | + ss_cell_id_bob, |
| 61 | + provider_cell_id_alice, |
| 62 | + provider_cell_id_bob, |
| 63 | + }; |
| 64 | +}; |
| 65 | + |
| 66 | +export default () => { |
| 67 | + test("test registering agent", async (t) => { |
| 68 | + await runScenario(async (scenario) => { |
| 69 | + const { |
| 70 | + alice, |
| 71 | + bob, |
| 72 | + alice_happs, |
| 73 | + bob_happs, |
| 74 | + alice_agent_key, |
| 75 | + bob_agent_key, |
| 76 | + ss_cell_id_alice, |
| 77 | + ss_cell_id_bob, |
| 78 | + provider_cell_id_alice, |
| 79 | + provider_cell_id_bob, |
| 80 | + } = await setUpAliceandBob(); |
| 81 | + |
| 82 | + const callZomeAlice = async ( |
| 83 | + zome_name, |
| 84 | + fn_name, |
| 85 | + payload, |
| 86 | + is_ss = false |
| 87 | + ) => { |
| 88 | + return await alice.appWs().callZome({ |
| 89 | + cap_secret: null, |
| 90 | + cell_id: is_ss ? ss_cell_id_alice : provider_cell_id_alice, |
| 91 | + zome_name, |
| 92 | + fn_name, |
| 93 | + payload, |
| 94 | + provenance: alice_agent_key, |
| 95 | + }); |
| 96 | + }; |
| 97 | + const callZomeBob = async ( |
| 98 | + zome_name, |
| 99 | + fn_name, |
| 100 | + payload, |
| 101 | + is_ss = false |
| 102 | + ) => { |
| 103 | + return await bob.appWs().callZome({ |
| 104 | + cap_secret: null, |
| 105 | + cell_id: is_ss ? ss_cell_id_bob : provider_cell_id_bob, |
| 106 | + zome_name, |
| 107 | + fn_name, |
| 108 | + payload, |
| 109 | + provenance: bob_agent_key, |
| 110 | + }); |
| 111 | + }; |
| 112 | + try { |
| 113 | + const pauseDuration = 1000 |
| 114 | + await scenario.shareAllAgents(); |
| 115 | + await pause(pauseDuration*2); |
| 116 | + |
| 117 | + let agents: Array<AgentPubKey> = await callZomeAlice( |
| 118 | + "sensemaker", |
| 119 | + "get_all_agents", |
| 120 | + null, |
| 121 | + true |
| 122 | + ); |
| 123 | + t.ok(agents); |
| 124 | + t.equal(agents.length, 0); |
| 125 | + console.log("agents", agents); |
| 126 | + // Wait for the created entry to be propagated to the other node. |
| 127 | + await pause(pauseDuration); |
| 128 | + |
| 129 | + agents = await callZomeBob( |
| 130 | + "sensemaker", |
| 131 | + "get_all_agents", |
| 132 | + null, |
| 133 | + true |
| 134 | + ); |
| 135 | + t.ok(agents); |
| 136 | + t.equal(agents.length, 1); |
| 137 | + t.equal(encodeHashToBase64(agents[0]), encodeHashToBase64(alice_agent_key)); |
| 138 | + console.log("agents", agents); |
| 139 | + await pause(pauseDuration); |
| 140 | + |
| 141 | + agents = await callZomeAlice( |
| 142 | + "sensemaker", |
| 143 | + "get_all_agents", |
| 144 | + null, |
| 145 | + true |
| 146 | + ); |
| 147 | + t.ok(agents); |
| 148 | + t.equal(agents.length, 1); |
| 149 | + t.equal(encodeHashToBase64(agents[0]), encodeHashToBase64(bob_agent_key)); |
| 150 | + console.log("agents", agents); |
| 151 | + } catch (e) { |
| 152 | + console.log(e); |
| 153 | + t.ok(null); |
| 154 | + } |
| 155 | + |
| 156 | + await alice.shutDown(); |
| 157 | + await bob.shutDown(); |
| 158 | + await cleanAllConductors(); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}; |
0 commit comments