Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Oct 11, 2023
1 parent 7f8d12b commit 8c4bab7
Show file tree
Hide file tree
Showing 26 changed files with 294 additions and 349 deletions.
Original file line number Diff line number Diff line change
@@ -1,50 +1,36 @@
import { assert, test } from "vitest";

import { runScenario, pause, CallableCell } from '@holochain/tryorama';
import { runScenario, dhtSync } from '@holochain/tryorama';
import { NewEntryAction, ActionHash, Record, AppBundleSource, fakeActionHash, fakeAgentPubKey, fakeEntryHash } from '@holochain/client';
import { EntryRecord } from '@holochain-open-dev/utils';
import { decode } from '@msgpack/msgpack';
import { EntryRecord } from '@holochain-open-dev/utils';
import { toPromise } from '@holochain-open-dev/stores';

import { create{{pascal_case referenceable.name}} } from './common.js';
import { {{pascal_case referenceable.name}} } from '../../../../ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/types.js';
import { setup, sample{{pascal_case referenceable.name}} } from './common.js';

test('create a {{pascal_case referenceable.name}} and get {{lower_case collection_name}}', async () => {
await runScenario(async scenario => {
// Construct proper paths for your app.
// This assumes app bundle created by the `hc app pack` command.
const testAppPath = process.cwd() + '/../workdir/{{app_name}}.happ';

// Set up the app to be installed
const appSource = { appBundleSource: { path: testAppPath } };

// Add 2 players with the test app to the Scenario. The returned players
// can be destructured.
const [alice, bob] = await scenario.addPlayersWithApps([appSource, appSource]);

// Shortcut peer discovery through gossip and register all agents in every
// conductor of the scenario.
await scenario.shareAllAgents();
const { alice, bob } = await setup(scenario);

// Bob gets {{lower_case collection_name}}
let collectionOutput: Record[] = await bob.cells[0].callZome({
zome_name: "{{coordinator_zome_manifest.name}}",
fn_name: "get_{{snake_case collection_name}}",
payload: {{#if (eq collection_type.type "Global")}}null{{else}}alice.agentPubKey{{/if}}
});
let collectionOutput: ActionHash[] = await toPromise(bob.store.{{camel_case collection_name}});
assert.equal(collectionOutput.length, 0);

// Alice creates a {{pascal_case referenceable.name}}
const createdRecord: Record = await create{{pascal_case referenceable.name}}(alice.cells[0]);
assert.ok(createdRecord);
const {{camel_case referenceable.name}}: EntryRecord<{{pascal_case referenceable.name}}> = await bob.store.create{{pascal_case referenceable.name}}(sample{{pascal_case referenceable.name}}());
assert.ok({{camel_case referenceable.name}});

await pause(1200);
await dhtSync(
[alice.player, bob.player],
alice.player.cells[0].cell_id[0]
);

// Bob gets {{lower_case collection_name}} again
collectionOutput = await bob.cells[0].callZome({
zome_name: "{{coordinator_zome_manifest.name}}",
fn_name: "get_{{snake_case collection_name}}",
payload: {{#if (eq collection_type.type "Global")}}null{{else}}alice.agentPubKey{{/if}}
});
collectionOutput = await toPromise(bob.store.{{camel_case collection_name}});
assert.equal(collectionOutput.length, 1);
assert.deepEqual(createdRecord, collectionOutput[0]);
assert.deepEqual({{camel_case_case}}.actionHash, collectionOutput[0]);
});
});

Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
import { CallableCell } from '@holochain/tryorama';
import { NewEntryAction, ActionHash, Record, AppBundleSource, fakeActionHash, fakeAgentPubKey, fakeEntryHash, fakeDnaHash } from '@holochain/client';
import {
NewEntryAction,
encodeHashToBase64,
ActionHash,
Record,
AppBundleSource,
fakeActionHash,
fakeAgentPubKey,
fakeEntryHash,
fakeDnaHash,
AppAgentCallZomeRequest,
AppAgentWebsocket
} from '@holochain/client';
import { encode } from '@msgpack/msgpack';
import { Scenario, dhtSync } from '@holochain/tryorama';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { {{pascal_case app_name}}Client } from '../../ui/src/{{kebab_case zome_manifest.name}}-client.js';
import { {{pascal_case app_name}}Store } from '../../ui/src/{{kebab_case zome_manifest.name}}-store.js';

export async function setup(scenario: Scenario) {
const testHappUrl =
dirname(fileURLToPath(import.meta.url)) + '/../../../../workdir/{{app_name}}.happ';

// Add 2 players with the test hApp to the Scenario. The returned players
// can be destructured.
const [alice, bob] = await scenario.addPlayersWithApps([
{ appBundleSource: { path: testHappUrl } },
{ appBundleSource: { path: testHappUrl } },
]);

// Shortcut peer discovery through gossip and register all agents in every
// conductor of the scenario.
await scenario.shareAllAgents();

const aliceStore = new {{pascal_case zome_manifest.name}}Store(
new {{pascal_case zome_manifest.name}}Client(alice.appAgentWs as any, '{{dna_role_name}}', '{{snake_case zome_manifest.name}}')
);

const bobStore = new {{pascal_case zome_manifest.name}}Store(
new {{pascal_case zome_manifest.name}}Client(bob.appAgentWs as any, '{{dna_role_name}}', '{{snake_case zome_manifest.name}}')
);

// Shortcut peer discovery through gossip and register all agents in every
// conductor of the scenario.
await scenario.shareAllAgents();

return {
alice: {
player: alice,
store: aliceStore,
},
bob: {
player: bob,
store: bobStore,
},
};
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { {{pascal_case entry_type.name}} } from '../../../ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/types.js';

{{previous_file_content}}

export async function sample{{pascal_case entry_type.name}}(cell: CallableCell, partial{{pascal_case entry_type.name}} = {}) {
export async function sample{{pascal_case entry_type.name}}(client: {{pascal_case app_name}}Client, partial{{pascal_case entry_type.name}}: Partial<{{pascal_case entry_type.name}}> = {}): Promise<{{ pascal_case entry_type.name }}> {
return {
...{
{{#each entry_type.fields}}
Expand All @@ -11,30 +13,30 @@ export async function sample{{pascal_case entry_type.name}}(cell: CallableCell,
{{field_name}}: [],
{{else}}
{{#if (eq linked_from.hash_type "ActionHash")}}
{{field_name}}: [(await create{{pascal_case linked_from.name}}(cell)).signed_action.hashed.hash],
{{field_name}}: partial{{pascal_case ../entry_type.name}}.{{field_name}} || [(await create{{pascal_case linked_from.name}}(client)).actionHash],
{{else}}
{{field_name}}: [((await create{{pascal_case linked_from.name}}(cell)).signed_action.hashed.content as NewEntryAction).entry_hash],
{{field_name}}: partial{{pascal_case ../entry_type.name}}.{{field_name}} || [(await create{{pascal_case linked_from.name}}(client)).entryHash],
{{/if}}
{{/if}}
{{else}}
{{#if (eq (pascal_case linked_from.name) (pascal_case ../entry_type.name))}}
{{field_name}}: null,
{{else}}
{{#if (eq linked_from.hash_type "ActionHash")}}
{{field_name}}: (await create{{pascal_case linked_from.name}}(cell)).signed_action.hashed.hash,
{{field_name}}: partial{{pascal_case ../entry_type.name}}.{{field_name}} || (await create{{pascal_case linked_from.name}}(client)).actionHash,
{{else}}
{{field_name}}: ((await create{{pascal_case linked_from.name}}(cell)).signed_action.hashed.content as NewEntryAction).entry_hash,
{{field_name}}: partial{{pascal_case entry_type.name}}.{{field_name}} || (await create{{pascal_case linked_from.name}}(client)).entryHash,
{{/if}}
{{/if}}
{{/if}}
{{else}}
{{field_name}}: cell.cell_id[1],
{{field_name}}: client.client.myPubKey,
{{/if}}
{{else}}
{{#if (eq cardinality "vector")}}
{{field_name}}: [{{> (concat field_type.type "/sample") field_type=field_type}}],
{{field_name}}: [{{> (concat field_type.type "/sample-js") field_type=field_type}}],
{{else}}
{{field_name}}: {{> (concat field_type.type "/sample") field_type=field_type}},
{{field_name}}: {{> (concat field_type.type "/sample-js") field_type=field_type}},
{{/if}}
{{/if}}
{{/each}}
Expand All @@ -43,11 +45,7 @@ export async function sample{{pascal_case entry_type.name}}(cell: CallableCell,
};
}

export async function create{{pascal_case entry_type.name}}(cell: CallableCell, {{camel_case entry_type.name}} = undefined): Promise<Record> {
return cell.callZome({
zome_name: "{{coordinator_zome_manifest.name}}",
fn_name: "create_{{snake_case entry_type.name}}",
payload: {{camel_case entry_type.name}} || await sample{{pascal_case entry_type.name}}(cell),
});
export async function create{{pascal_case entry_type.name}}(client: {{pascal_case app_name}}Client, {{camel_case entry_type.name}}: {{pascal_case entry_type.name}} | undefined): Promise<EntryRecord<{{pascal_case entry_type.name}}>> {
return client.create{{pascal_case entry_type.name}}({{camel_case entry_type.name}} || await sample{{pascal_case entry_type.name}}(client));
}

Loading

0 comments on commit 8c4bab7

Please sign in to comment.