Skip to content

Commit

Permalink
feat(common): add convenience zome caller
Browse files Browse the repository at this point in the history
  • Loading branch information
jost-s committed Jun 7, 2022
1 parent 9a5aef8 commit 0358e3d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 13 deletions.
17 changes: 15 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed
### Fixed

## [0.5.3]

### Added
- Convenience function getter to call zome of a given cell.

## [0.5.2]

### Added
- Conductor options to runScenario fn.

## [0.5.1]

### Added
- Missing library files.

## [0.5.0]
- Re-write Tryorama

Expand All @@ -19,8 +34,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Middleware
- Conductor configuration
- Implicit usage of `tape` as test harness
### Changed
### Fixed

## [0.4.10]
### Fixed
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,48 @@ const createEntryHash: EntryHash = await aliceHapps.cells[0].callZome({
await conductor.shutDown();
```
### Convenience function for Zome calls
When testing a Zome, there are usually a lot of calls to the cell with this
particular Zome. Specifying the Cell and the Zome name for every call is
repetitive. It is therefore convenient to use a handle to a particular
combination of Cell and Zome.
Instead of
```typescript
const [aliceHapps] = await conductor.installAgentsHapps({
agentsDnas: [dnas],
});
const createEntryHash: EntryHash = await aliceHapps.cells[0].callZome({
zome_name: "crud",
fn_name: "create",
payload: entryContent,
});
const readEntryHash: string = await aliceHapps.cells[0].callZome({
zome_name: "crud",
fn_name: "read",
payload: createEntryHash,
});
```
the shorthand access to the Zome can be called
```typescript
const [aliceHapps] = await conductor.installAgentsHapps({
agentsDnas: [dnas],
});
const aliceCrudZomeCall = getZomeCaller(aliceHapps.cells[0], "crud");
const entryHeaderHash: HeaderHash = await crudZomeCall(
"create",
"test-entry"
);
const readEntryHash: string = await crudZomeCall(
"read",
entryHeaderHash
);
```
## Signals
`Scenario.addPlayer` as well as `Conductor.installAgentsHapps` allow for an
Expand Down
5 changes: 2 additions & 3 deletions package-lock.json

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

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@holochain/tryorama",
"description": "Toolset to manage Holochain conductors and facilitate running test scenarios",
"version": "0.5.2",
"version": "0.5.3",
"author": "Holochain Foundation",
"keywords": [
"holochain",
Expand All @@ -17,7 +17,7 @@
"license": "CAL-1.0",
"type": "module",
"engines": {
"node": "^14.13.1 || >=16.0.0"
"node": "^14.13.1 || >=16.0.0 || >=18.0.0"
},
"workspaces": [
"crates/trycp_server/test"
Expand All @@ -27,7 +27,7 @@
},
"types": "./lib/index.d.ts",
"files": [
"lib/"
"lib"
],
"scripts": {
"build": "rimraf ./lib && tsc -p tsconfig.build.json && npm run build:docs",
Expand Down Expand Up @@ -57,7 +57,6 @@
"@microsoft/api-extractor": "^7.24.2",
"@msgpack/msgpack": "^2.7.2",
"@types/lodash": "^4.14.182",
"@types/node": "^16.11.36",
"@types/ramda": "^0.26.44",
"@types/tape": "^4.13.2",
"@types/tape-promise": "^4.0.1",
Expand All @@ -77,4 +76,4 @@
"ts-node": "^10.8.0",
"typescript": "^4.7.2"
}
}
}
16 changes: 15 additions & 1 deletion ts/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import {
InstalledAppInfo,
InstalledCell,
} from "@holochain/client";
import { AgentHapp, CellZomeCallRequest, IConductor } from "./types.js";
import {
AgentHapp,
CallableCell,
CellZomeCallRequest,
IConductor,
} from "./types.js";

export const addAllAgentsToAllConductors = async (conductors: IConductor[]) => {
await Promise.all(
Expand Down Expand Up @@ -80,3 +85,12 @@ const getCallableCell = (
return callZomeResponse;
},
});

export const getZomeCaller =
(cell: CallableCell, zomeName: string) =>
<T>(fnName: string, payload: unknown): Promise<T> =>
cell.callZome<T>({
zome_name: zomeName,
fn_name: fnName,
payload,
});
2 changes: 1 addition & 1 deletion ts/src/local/conductor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export class Conductor implements IConductor {
*
* @param options - An array of DNAs for each agent, resulting in a
* 2-dimensional array, and a UID for the DNAs (optional).
* @returns An array with each agent's hApp.
* @returns An array with each agent's hApps.
*/
async installAgentsHapps(options: {
agentsDnas: DnaSource[][];
Expand Down
26 changes: 25 additions & 1 deletion ts/test/local/conductor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import {
import { readFileSync } from "node:fs";
import test from "tape-promise/tape.js";
import { URL } from "node:url";
import { addAllAgentsToAllConductors } from "../../src/common.js";
import {
addAllAgentsToAllConductors,
getZomeCaller,
} from "../../src/common.js";
import {
cleanAllConductors,
createConductor,
NetworkType,
} from "../../src/index.js";
import { pause } from "../../src/util.js";
import { FIXTURE_DNA_URL, FIXTURE_HAPP_URL } from "../fixture/index.js";
import { HeaderHash } from "@holochain/client";

test("Local Conductor - Spawn a conductor with QUIC network", async (t) => {
const conductor = await createConductor({
Expand Down Expand Up @@ -199,6 +203,26 @@ test("Local Conductor - Install and call a hApp bundle", async (t) => {
await cleanAllConductors();
});

test("Local Conductor - Get a convenience function for zome calls", async (t) => {
const conductor = await createConductor();
const [aliceHapps] = await conductor.installAgentsHapps({
agentsDnas: [[{ path: FIXTURE_DNA_URL.pathname }]],
});
const crudZomeCall = getZomeCaller(aliceHapps.cells[0], "crud");
t.equal(typeof crudZomeCall, "function", "getZomeCaller returns a function");

const entryHeaderHash: HeaderHash = await crudZomeCall(
"create",
"test-entry"
);
const entryHeaderHashB64 = Buffer.from(entryHeaderHash).toString("base64");
t.equal(entryHeaderHash.length, 39, "HeaderHash is 39 bytes long");
t.ok(entryHeaderHashB64.startsWith("hCkk"), "HeaderHash starts with hCkk");

await conductor.shutDown();
await cleanAllConductors();
});

test("Local Conductor - Install multiple agents and DNAs and get access to agents and cells", async (t) => {
const conductor = await createConductor();
const [aliceHapps, bobHapps] = await conductor.installAgentsHapps({
Expand Down

0 comments on commit 0358e3d

Please sign in to comment.