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

Add workflow to test scripts printed in README #249

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ jobs:
- name: Run tests
run: nix develop -c $SHELL -c "npm t"

- name: Run doc test of app websocket
run: nix develop -c $SHELL -c "./doc-test-app-ws.sh"

- name: Run doc test of signal
run: nix develop -c $SHELL -c "./doc-test-signal.sh"

- name: Setup tmate session if a previous step failed
if: ${{ failure() }}
uses: mxschmitt/action-tmate@v3
Expand Down
76 changes: 52 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,64 +30,92 @@ npm install --save-exact @holochain/client

## Sample usage

### Use AppAgentWebsocket with implicit zome call signing
### Use AppWebsocket with implicit zome call signing
```typescript
import { ActionHash, AdminWebsocket, AppAgentWebsocket, CellType } from "@holochain/client";

const adminWs = await AdminWebsocket.connect({url: "ws://127.0.0.1:65000"});
import {
AdminWebsocket,
AppWebsocket,
CellType,
type ActionHash,
type CallZomeRequest,
} from "./lib/index.js";

const adminWs = await AdminWebsocket.connect({
url: new URL("ws://127.0.0.1:65000"),
wsClientOptions: { origin: "my-happ" },
});
const agent_key = await adminWs.generateAgentPubKey();
const role_name = "role";
const role_name = "foo";
const installed_app_id = "test-app";
const appInfo = await adminWs.installApp({
agent_key,
path: "path/to/happ/file",
path: "./test/e2e/fixture/test.happ",
installed_app_id,
membrane_proofs: {},
});
await adminWs.enableApp({ installed_app_id });
if (!(CellType.Provisioned in appInfo.cell_info[role_name][0])) {
process.exit();
throw new Error(`No cell found under role name ${role_name}`);
}
const { cell_id } = appInfo.cell_info[role_name][0][CellType.Provisioned];
await adminWs.authorizeSigningCredentials(cell_id);
await adminWs.attachAppInterface({ port: 65001 });
const appAgentWs = await AppAgentWebsocket.connect(installed_app_id, {url: "ws://127.0.0.1:65001"});
await adminWs.attachAppInterface({ port: 65001, allowed_origins: "my-happ" });
const issuedToken = await adminWs.issueAppAuthenticationToken({
installed_app_id,
});
const appWs = await AppWebsocket.connect({
url: new URL("ws://127.0.0.1:65001"),
token: issuedToken.token,
wsClientOptions: { origin: "my-happ" },
});

const zomeCallPayload: CallZomeRequest = {
cell_id,
zome_name: "zome_name",
fn_name: "create_entry",
zome_name: "foo",
fn_name: "foo",
provenance: agent_key,
payload: "some_content",
payload: null,
};

const response: ActionHash = await appAgentWs.callZome(zomeCallPayload, 30000);
const response: ActionHash = await appWs.callZome(zomeCallPayload, 30000);
console.log("zome call response is", response);

await appAgentWs.appWebsocket.client.close();
await appWs.client.close();
await adminWs.client.close();
```

### Use AppWebsocket with implicit zome call signing
### Subscribe to signals
```typescript
import { AdminWebsocket, AppWebsocket, CellType } from "@holochain/client";

const adminWs = await AdminWebsocket.connect({url: "ws://127.0.0.1:65000"});
const adminWs = await AdminWebsocket.connect({
url: new URL("ws://127.0.0.1:65000"),
wsClientOptions: { origin: "my-happ" },
});
const agent_key = await adminWs.generateAgentPubKey();
const role_name = "foo";
const installed_app_id = "test-app";
const appInfo = await adminWs.installApp({
agent_key,
path: "path/to/happ/file",
path: "./test/e2e/fixture/test.happ",
installed_app_id,
membrane_proofs: {},
});
await adminWs.enableApp({ installed_app_id });
if (!(CellType.Provisioned in appInfo.cell_info["role"][0])) {
process.exit();
if (!(CellType.Provisioned in appInfo.cell_info[role_name][0])) {
throw new Error(`No cell found under role name ${role_name}`);
}
const { cell_id } = appInfo.cell_info["role"][0][CellType.Provisioned];
const { cell_id } = appInfo.cell_info[role_name][0][CellType.Provisioned];
await adminWs.authorizeSigningCredentials(cell_id);
await adminWs.attachAppInterface({ port: 65001 });
const appWs = await AppWebsocket.connect({url: "ws://127.0.0.1:65001"});
await adminWs.attachAppInterface({ port: 65001, allowed_origins: "my-happ" });
const issuedToken = await adminWs.issueAppAuthenticationToken({
installed_app_id,
});
const appWs = await AppWebsocket.connect({
url: new URL("ws://127.0.0.1:65001"),
token: issuedToken.token,
wsClientOptions: { origin: "my-happ" },
});

let signalCb;
const signalReceived = new Promise<void>((resolve) => {
Expand All @@ -103,7 +131,7 @@ appWs.on("signal", signalCb);
// trigger an emit_signal
await appWs.callZome({
cell_id,
zome_name: "zome",
zome_name: "foo",
fn_name: "emitter",
provenance: agent_key,
payload: null,
Expand Down Expand Up @@ -165,7 +193,7 @@ Holochain is an open source project. We welcome all sorts of participation and

[![License: CAL 1.0](https://img.shields.io/badge/License-CAL%201.0-blue.svg)](https://github.com/holochain/cryptographic-autonomy-license)

Copyright (C) 2020-2023, Holochain Foundation
Copyright (C) 2020-2024, Holochain Foundation

This program is free software: you can redistribute it and/or modify it under the terms of the license
provided in the LICENSE file (CAL-1.0). This program is distributed in the hope that it will be useful,
Expand Down
16 changes: 16 additions & 0 deletions doc-test-app-ws.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
set -xe

hc s clean
echo "" | hc s --piped create
echo "" | hc s --piped -f=65000 run &
HC_ID=$!
echo "HC_ID is $HC_ID"
sleep 5

set +e

npm run build
npx tsx doc-test-app-ws.ts

pkill -15 -P $HC_ID
50 changes: 50 additions & 0 deletions doc-test-app-ws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
AdminWebsocket,
AppWebsocket,
CellType,
type ActionHash,
type CallZomeRequest,
} from "./lib/index.js";

const adminWs = await AdminWebsocket.connect({
url: new URL("ws://127.0.0.1:65000"),
wsClientOptions: { origin: "my-happ" },
});
const agent_key = await adminWs.generateAgentPubKey();
const role_name = "foo";
const installed_app_id = "test-app";
const appInfo = await adminWs.installApp({
agent_key,
path: "./test/e2e/fixture/test.happ",
installed_app_id,
membrane_proofs: {},
});
await adminWs.enableApp({ installed_app_id });
if (!(CellType.Provisioned in appInfo.cell_info[role_name][0])) {
throw new Error(`No cell found under role name ${role_name}`);
}
const { cell_id } = appInfo.cell_info[role_name][0][CellType.Provisioned];
await adminWs.authorizeSigningCredentials(cell_id);
await adminWs.attachAppInterface({ port: 65001, allowed_origins: "my-happ" });
const issuedToken = await adminWs.issueAppAuthenticationToken({
installed_app_id,
});
const appWs = await AppWebsocket.connect({
url: new URL("ws://127.0.0.1:65001"),
token: issuedToken.token,
wsClientOptions: { origin: "my-happ" },
});

const zomeCallPayload: CallZomeRequest = {
cell_id,
zome_name: "foo",
fn_name: "foo",
provenance: agent_key,
payload: null,
};

const response: ActionHash = await appWs.callZome(zomeCallPayload, 30000);
console.log("zome call response is", response);

await appWs.client.close();
await adminWs.client.close();
16 changes: 16 additions & 0 deletions doc-test-signal.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
set -xe

hc s clean
echo "" | hc s --piped create
echo "" | hc s --piped -f=65000 run &
HC_ID=$!
echo "HC_ID is $HC_ID"
sleep 5

set +e

npm run build
npx tsx doc-test-signal.ts

pkill -15 -P $HC_ID
54 changes: 54 additions & 0 deletions doc-test-signal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { AdminWebsocket, AppWebsocket, CellType } from "./lib/index.js";

const adminWs = await AdminWebsocket.connect({
url: new URL("ws://127.0.0.1:65000"),
wsClientOptions: { origin: "my-happ" },
});
const agent_key = await adminWs.generateAgentPubKey();
const role_name = "foo";
const installed_app_id = "test-app";
const appInfo = await adminWs.installApp({
agent_key,
path: "./test/e2e/fixture/test.happ",
installed_app_id,
membrane_proofs: {},
});
await adminWs.enableApp({ installed_app_id });
if (!(CellType.Provisioned in appInfo.cell_info[role_name][0])) {
throw new Error(`No cell found under role name ${role_name}`);
}
const { cell_id } = appInfo.cell_info[role_name][0][CellType.Provisioned];
await adminWs.authorizeSigningCredentials(cell_id);
await adminWs.attachAppInterface({ port: 65001, allowed_origins: "my-happ" });
const issuedToken = await adminWs.issueAppAuthenticationToken({
installed_app_id,
});
const appWs = await AppWebsocket.connect({
url: new URL("ws://127.0.0.1:65001"),
token: issuedToken.token,
wsClientOptions: { origin: "my-happ" },
});

let signalCb;
const signalReceived = new Promise<void>((resolve) => {
signalCb = (signal) => {
console.log("signal received", signal);
// act on signal
resolve();
};
});

appWs.on("signal", signalCb);

// trigger an emit_signal
await appWs.callZome({
cell_id,
zome_name: "foo",
fn_name: "emitter",
provenance: agent_key,
payload: null,
});
await signalReceived;

await appWs.client.close();
await adminWs.client.close();
44 changes: 22 additions & 22 deletions flake.lock

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

Loading