Skip to content

Commit

Permalink
updated tests, code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
nphias committed Jun 13, 2024
1 parent 7328537 commit 8cbe4ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 42 deletions.
36 changes: 6 additions & 30 deletions tests/src/map_holons/holons/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@ import { CallableCell } from '@holochain/tryorama';
import { NewEntryAction, ActionHash, Record, AppBundleSource, fakeActionHash, fakeAgentPubKey, fakeEntryHash, fakeDnaHash } from '@holochain/client';
import { BaseValue, BaseValueList, DanceRequestObject, Holon, WithPropertyInput, DanceTypeEnum, RequestBodyEnum, DanceResponseObject, DanceType, RequestBody, DanceTypeObject, RequestBodyObject, TargetHolons, StagingArea, PropertyMap, ResponseStatusCode, ResponseBody, HolonReference, ResponseBodyEnum } from './types';


export function send_dance_request(cell: CallableCell, name:string, type:DanceTypeObject, body:RequestBodyObject):Promise<DanceResponseObject> {
const data:DanceRequestObject = {
dance_name:name,
dance_type:type,
body: body,
staging_area:{staged_holons:[],index:{}}

}
return cell.callZome({zome_name: "dances", fn_name: "dance", payload: data})
}


export function createHolon(props:PropertyMap):Holon {
return {
state: { New: null },
Expand All @@ -29,18 +16,7 @@ export function createHolon(props:PropertyMap):Holon {
}


export async function withProperty(cell: CallableCell, emptyholon: Holon, property:string = undefined, propertyvalue:BaseValueList = undefined): Promise<Holon> {
const propertyObject: WithPropertyInput = { holon: emptyholon, property_name:property, value:propertyvalue }
return cell.callZome({
zome_name: "holons",
fn_name: "with_property_value",
payload: propertyObject //|| await sampleHolon(cell),
});
}



// alternative helper class
// helper class
export class DanceResponse {
public status_code: ResponseStatusCode
public description: string
Expand All @@ -64,11 +40,11 @@ export class DanceResponse {
return this.staging_area.index
}
//wip
findIndexbyKey(key:string):number{
if (this.body.type === ResponseBodyEnum.Holons)
return 0
return 0
}
//findIndexbyKey(key:string):number{
// if (this.body.type === ResponseBodyEnum.Holons)
// return 0
// return 0
// }

}

Expand Down
25 changes: 13 additions & 12 deletions tests/src/map_holons/holons/test-case-1.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { assert, test } from "vitest";

import { runScenario, pause, CallableCell, dhtSync } from '@holochain/tryorama';
import { NewEntryAction, ActionHash, AppBundleSource, fakeActionHash, fakeAgentPubKey, fakeEntryHash } from '@holochain/client';
import { } from '@holochain/client';
import { decode } from '@msgpack/msgpack';

import { DanceRequest, createHolon, send_dance_request } from './common.js';
import { BaseValueType, DanceResponseObject, Holon, DanceTypeEnum, RequestBodyEnum, PropertyMap, ResponseStatusCodeMap, ResponseBodyEnum, ResponseBodyMap } from "./types.js";
import { DanceRequest, createHolon } from './common.js';
import { BaseValueType, Holon, PropertyMap, ResponseStatusCodeMap, ResponseBodyEnum } from "./types.js";

test('TEST CASE 1, Stage, Add Properties, Commit Holons', async () => {
await runScenario(async scenario => {
Expand All @@ -24,11 +24,12 @@ test('TEST CASE 1, Stage, Add Properties, Commit Holons', async () => {
// conductor of the scenario.
await scenario.shareAllAgents();
//---------------------------------------------------------------


//create instance of the DanceRequest class
let alicerequest = new DanceRequest(alice.cells[0])

//task 1 - get all holons
console.log("---- alice gets all holons to ensure the staging area is empty\n")
let alicerequest = new DanceRequest(alice.cells[0])
let response = await alicerequest.readall("get_all_holons")
//console.log(response)
assert.equal(response.getStagedObjects().length, 0);
Expand All @@ -37,22 +38,21 @@ test('TEST CASE 1, Stage, Add Properties, Commit Holons', async () => {
//task 2 - create empty holon by not providing one
console.log('----- Alice creates a new empty Holon for Book\n')
response = await alicerequest.createOneEmpty("stage_new_holon")
//test
//assertions
assert.equal(response.getStagedObjects().length, 1);
assert.equal(Object.keys(response.body)[0], ResponseBodyEnum.Index);
let holonindex = Object.values(response.body)[0]
//console.log("index response:",holonindex )
assert.equal(holonindex, 0);



//task 3 - add a title property to holon at index 0
console.log(" -- -- Alice adds a title property to the Book Holon at index 0\n")
let properties:PropertyMap = {}
properties["title"] = {[BaseValueType.StringValue]:"mybook"}
//let index = response.findIndexbyKey()
response = await alicerequest.updateOneWithProperties("with_properties",holonindex,properties)
//console.log(response)
//assertions:
assert.equal(response.getStagedObjects().length, 1);
assert.equal(Object.keys(response.status_code)[0], ResponseStatusCodeMap.OK);
assert.equal(Object.keys(response.body)[0], ResponseBodyEnum.Index);
Expand All @@ -66,7 +66,7 @@ test('TEST CASE 1, Stage, Add Properties, Commit Holons', async () => {
properties = {}
properties["description"] = {[BaseValueType.StringValue]:"some description"}
response = await alicerequest.updateOneWithProperties("with_properties",holonindex,properties)

//assertions
//console.log("property add result:",response)
assert.equal(response.getStagedObjects().length, 1);
assert.equal(Object.keys(response.status_code)[0], ResponseStatusCodeMap.OK);
Expand All @@ -83,7 +83,7 @@ test('TEST CASE 1, Stage, Add Properties, Commit Holons', async () => {
properties["favourite number"] = {[BaseValueType.IntegerValue]:42}
let holon:Holon = createHolon(properties)
response = await alicerequest.createOne("stage_new_holon",holon)

//assertions
//console.log("New holon result",response)
assert.equal(response.getStagedObjects().length, 2);
assert.equal(Object.keys(response.status_code)[0], ResponseStatusCodeMap.OK);
Expand All @@ -96,17 +96,18 @@ test('TEST CASE 1, Stage, Add Properties, Commit Holons', async () => {
// task 6 - commit staged holons
console.log("--- Alice commits all staged holons\n")
response = await alicerequest.commit("commit")

//assertions:
console.log("commit result",response)
assert.equal(response.getStagedObjects().length, 0);
assert.equal(Object.keys(response.body)[0], "Holons");
assert.equal(Object.values(response.body)[0].length, 2); //2 holons committed



//task 7 - get all holons
console.warn(" -- alice gets all holons to ensure the staging area matches\n")
response = await alicerequest.readall("get_all_holons")

//assertions:
console.log("final response",response)
assert.equal(Object.keys(response.body)[0], "Holons");
const holons:Holon[] = Object.values(response.body)[0]
Expand Down

0 comments on commit 8cbe4ea

Please sign in to comment.