From 4e647413a7c0f8db8e798b15e1f4b16fff03e267 Mon Sep 17 00:00:00 2001 From: "guillem.cordoba" Date: Wed, 22 May 2024 13:49:38 +0200 Subject: [PATCH] Fixed utils --- packages/utils/package.json | 3 +- packages/utils/src/fake.ts | 265 ++++++++++++++++---------------- packages/utils/src/zome-mock.ts | 190 +++++++++++------------ 3 files changed, 229 insertions(+), 229 deletions(-) diff --git a/packages/utils/package.json b/packages/utils/package.json index 9576467..949183d 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -19,8 +19,7 @@ "build": "npm run lint && tsc --incremental", "build:watch": "tsc --watch --preserveWatchOutput", "lint": "eslint src", - "test": "vitest run", - "publish": "npm run build && npm publish --tag dev" + "test": "vitest run" }, "dependencies": { "@holochain/client": "^0.17.0-rc", diff --git a/packages/utils/src/fake.ts b/packages/utils/src/fake.ts index 672ab6a..df3d42f 100644 --- a/packages/utils/src/fake.ts +++ b/packages/utils/src/fake.ts @@ -1,162 +1,163 @@ -import { encode } from "@msgpack/msgpack"; import { - Record, - Action, - ActionType, - Entry, - EntryHash, - AgentPubKey, - ActionHash, - RecordEntry, - fakeEntryHash, - fakeAgentPubKey, - fakeActionHash, - randomByteArray, -} from "@holochain/client"; -import { hash, HashType } from "./hash.js"; + Action, + ActionHash, + ActionType, + AgentPubKey, + Entry, + EntryHash, + Record, + RecordEntry, + fakeActionHash, + fakeAgentPubKey, + fakeEntryHash, + randomByteArray, +} from '@holochain/client'; +import { encode } from '@msgpack/msgpack'; + +import { HashType, hash } from './hash.js'; export async function fakeCreateAction( - entry_hash?: EntryHash, - author?: AgentPubKey + entry_hash?: EntryHash, + author?: AgentPubKey, ): Promise { - if (!entry_hash) entry_hash = await fakeEntryHash(); - if (!author) author = await fakeAgentPubKey(); - return { - type: ActionType.Create, - author, - timestamp: Date.now() * 1000, - action_seq: 10, - prev_action: await fakeActionHash(), - entry_type: { - App: { - entry_index: 0, - visibility: { Public: null }, - zome_index: 0, - }, - }, - entry_hash, - }; + if (!entry_hash) entry_hash = await fakeEntryHash(); + if (!author) author = await fakeAgentPubKey(); + return { + type: ActionType.Create, + author, + timestamp: Date.now() * 1000, + action_seq: 10, + prev_action: await fakeActionHash(), + entry_type: { + App: { + entry_index: 0, + visibility: 'Public', + zome_index: 0, + }, + }, + entry_hash, + }; } -export function fakeEntry(entry: any = "some data"): Entry { - return { - entry: encode(entry), - entry_type: "App", - }; +export function fakeEntry(entry: any = 'some data'): Entry { + return { + entry: encode(entry), + entry_type: 'App', + }; } export async function fakeDeleteEntry( - deletes_address?: ActionHash, - deletes_entry_address?: EntryHash, - author?: AgentPubKey + deletes_address?: ActionHash, + deletes_entry_address?: EntryHash, + author?: AgentPubKey, ): Promise { - if (!deletes_address) deletes_address = await fakeActionHash(); - if (!deletes_entry_address) deletes_entry_address = await fakeEntryHash(); - if (!author) author = await fakeAgentPubKey(); - return { - type: ActionType.Delete, - author, - timestamp: Date.now() * 1000, - action_seq: 10, - prev_action: await fakeActionHash(), - deletes_address, - deletes_entry_address, - }; + if (!deletes_address) deletes_address = await fakeActionHash(); + if (!deletes_entry_address) deletes_entry_address = await fakeEntryHash(); + if (!author) author = await fakeAgentPubKey(); + return { + type: ActionType.Delete, + author, + timestamp: Date.now() * 1000, + action_seq: 10, + prev_action: await fakeActionHash(), + deletes_address, + deletes_entry_address, + }; } export async function fakeUpdateEntry( - original_action_address?: ActionHash, - original_entry_address?: EntryHash, - author?: AgentPubKey, - entry: Entry = fakeEntry() + original_action_address?: ActionHash, + original_entry_address?: EntryHash, + author?: AgentPubKey, + entry: Entry = fakeEntry(), ): Promise { - if (!original_action_address) - original_action_address = await fakeActionHash(); - if (!original_entry_address) original_entry_address = await fakeEntryHash(); - if (!author) author = await fakeAgentPubKey(); - return { - type: ActionType.Update, - author, - timestamp: Date.now() * 1000, - action_seq: 10, - prev_action: await fakeActionHash(), - original_entry_address, - original_action_address, - entry_hash: hash(entry, HashType.ENTRY), - entry_type: { - App: { - entry_index: 0, - visibility: { Public: null }, - zome_index: 0, - }, - }, - }; + if (!original_action_address) + original_action_address = await fakeActionHash(); + if (!original_entry_address) original_entry_address = await fakeEntryHash(); + if (!author) author = await fakeAgentPubKey(); + return { + type: ActionType.Update, + author, + timestamp: Date.now() * 1000, + action_seq: 10, + prev_action: await fakeActionHash(), + original_entry_address, + original_action_address, + entry_hash: hash(entry, HashType.ENTRY), + entry_type: { + App: { + entry_index: 0, + visibility: 'Public', + zome_index: 0, + }, + }, + }; } export async function fakeRecord( - action: Action, - entry?: Entry | undefined + action: Action, + entry?: Entry | undefined, ): Promise { - let recordEntry: RecordEntry = { - NotApplicable: null, - }; - if (entry) { - recordEntry = { - Present: entry, - }; - } + let recordEntry: RecordEntry = { + NotApplicable: null, + }; + if (entry) { + recordEntry = { + Present: entry, + }; + } - return { - entry: recordEntry, - signed_action: { - hashed: { - content: action, - hash: hash(action, HashType.ACTION), - }, - signature: await randomByteArray(256), - }, - }; + return { + entry: recordEntry, + signed_action: { + hashed: { + content: action, + hash: hash(action, HashType.ACTION), + }, + signature: await randomByteArray(256), + }, + }; } export async function fakeCreateLinkAction( - base_address?: ActionHash, - target_address?: ActionHash, - link_type: number = 0, - tag: any = undefined + base_address?: ActionHash, + target_address?: ActionHash, + link_type: number = 0, + tag: any = undefined, ): Promise { - if (base_address) base_address = await fakeActionHash(); - if (target_address) target_address = await fakeActionHash(); + if (base_address) base_address = await fakeActionHash(); + if (target_address) target_address = await fakeActionHash(); - return { - type: ActionType.CreateLink, - author: await fakeAgentPubKey(), - timestamp: Date.now() * 1000, - action_seq: 10, - prev_action: await fakeActionHash(), - base_address, - target_address, - link_type, - tag, - zome_index: 0, - weight: { - bucket_id: 0, - units: 1, - }, - }; + return { + type: ActionType.CreateLink, + author: await fakeAgentPubKey(), + timestamp: Date.now() * 1000, + action_seq: 10, + prev_action: await fakeActionHash(), + base_address, + target_address, + link_type, + tag, + zome_index: 0, + weight: { + bucket_id: 0, + units: 1, + }, + }; } export async function fakeDeleteLinkAction( - link_add_address?: ActionHash + link_add_address?: ActionHash, ): Promise { - if (link_add_address) link_add_address = await fakeActionHash(); + if (link_add_address) link_add_address = await fakeActionHash(); - return { - type: ActionType.DeleteLink, - author: await fakeAgentPubKey(), - timestamp: Date.now() * 1000, - action_seq: 10, - prev_action: await fakeActionHash(), - base_address: link_add_address, - link_add_address, - }; + return { + type: ActionType.DeleteLink, + author: await fakeAgentPubKey(), + timestamp: Date.now() * 1000, + action_seq: 10, + prev_action: await fakeActionHash(), + base_address: link_add_address, + link_add_address, + }; } diff --git a/packages/utils/src/zome-mock.ts b/packages/utils/src/zome-mock.ts index c7cfae0..8a20af2 100644 --- a/packages/utils/src/zome-mock.ts +++ b/packages/utils/src/zome-mock.ts @@ -1,111 +1,111 @@ import { - AgentPubKey, - AppClient, - AppEvents, - AppNetworkInfoRequest, - AppCreateCloneCellRequest, - AppInfo, - AppSignalCb, - CallZomeRequest, - CellId, - ClonedCell, - decodeHashFromBase64, - DisableCloneCellRequest, - DisableCloneCellResponse, - EnableCloneCellRequest, - EnableCloneCellResponse, - NetworkInfoResponse, -} from "@holochain/client"; -import Emittery, { UnsubscribeFunction } from "emittery"; + AgentPubKey, + AppClient, + AppCreateCloneCellRequest, + AppEvents, + AppInfo, + AppNetworkInfoRequest, + AppSignalCb, + CallZomeRequest, + CellId, + ClonedCell, + DisableCloneCellRequest, + DisableCloneCellResponse, + EnableCloneCellRequest, + EnableCloneCellResponse, + NetworkInfoResponse, + decodeHashFromBase64, +} from '@holochain/client'; +import Emittery, { UnsubscribeFunction } from 'emittery'; const sleep = (ms: number) => - new Promise((r) => { - setTimeout(() => r(null), ms); - }); + new Promise(r => { + setTimeout(() => r(null), ms); + }); export class ZomeMock implements AppClient { - emitter = new Emittery(); + emitter = new Emittery(); - constructor( - protected roleName: string, - protected zomeName: string, - public myPubKey: AgentPubKey = decodeHashFromBase64( - "uhCAk13OZ84d5HFum5PZYcl61kHHMfL2EJ4yNbHwSp4vn6QeOdFii" - ), - protected latency: number = 500 - ) {} + constructor( + protected roleName: string, + protected zomeName: string, + public myPubKey: AgentPubKey = decodeHashFromBase64( + 'uhCAk13OZ84d5HFum5PZYcl61kHHMfL2EJ4yNbHwSp4vn6QeOdFii', + ), + protected latency: number = 500, + ) {} - get cellId(): CellId { - return [ - decodeHashFromBase64("uhCAk6oBoqygFqkDreZ0V0bH4R9cTN1OkcEG78OLxVptLWOI"), - this.myPubKey, - ]; - } + get cellId(): CellId { + return [ + decodeHashFromBase64('uhCAk6oBoqygFqkDreZ0V0bH4R9cTN1OkcEG78OLxVptLWOI'), + this.myPubKey, + ]; + } - async appInfo(): Promise { - return { - agent_pub_key: this.myPubKey, - installed_app_id: "test-app", - status: { running: null }, - cell_info: { - [this.roleName]: [ - { - provisioned: { - cell_id: this.cellId, - name: this.roleName, - dna_modifiers: { - network_seed: "", - origin_time: Date.now(), - properties: undefined, - quantum_time: { - secs: Date.now() / 1000, - nanos: 0, - }, - }, - }, - }, - ], - }, - }; - } + async appInfo(): Promise { + return { + agent_pub_key: this.myPubKey, + installed_app_id: 'test-app', + status: 'running', + cell_info: { + [this.roleName]: [ + { + provisioned: { + cell_id: this.cellId, + name: this.roleName, + dna_modifiers: { + network_seed: '', + origin_time: Date.now(), + properties: undefined, + quantum_time: { + secs: Date.now() / 1000, + nanos: 0, + }, + }, + }, + }, + ], + }, + }; + } - createCloneCell(_args: AppCreateCloneCellRequest): Promise { - throw new Error("Method not implemented."); - } + createCloneCell(_args: AppCreateCloneCellRequest): Promise { + throw new Error('Method not implemented.'); + } - enableCloneCell( - _args: EnableCloneCellRequest - ): Promise { - throw new Error("Method not implemented"); - } + enableCloneCell( + _args: EnableCloneCellRequest, + ): Promise { + throw new Error('Method not implemented'); + } - disableCloneCell( - _args: DisableCloneCellRequest - ): Promise { - throw new Error("Method not implemented"); - } + disableCloneCell( + _args: DisableCloneCellRequest, + ): Promise { + throw new Error('Method not implemented'); + } - networkInfo(_args: AppNetworkInfoRequest): Promise { - throw new Error("Method not implemented"); - } + networkInfo(_args: AppNetworkInfoRequest): Promise { + throw new Error('Method not implemented'); + } - async callZome(req: CallZomeRequest): Promise { - await sleep(this.latency); - return (this as any)[req.fn_name](req.payload); - } + async callZome(req: CallZomeRequest): Promise { + await sleep(this.latency); + return (this as any)[req.fn_name](req.payload); + } - on( - eventName: Name | readonly Name[], - listener: AppSignalCb - ): UnsubscribeFunction { - return this.emitter.on(eventName, listener); - } + on( + eventName: Name | readonly Name[], + listener: AppSignalCb, + ): UnsubscribeFunction { + return this.emitter.on(eventName, listener); + } - protected emitSignal(payload: any) { - this.emitter.emit("signal", { - cell_id: this.cellId, - zome_name: this.zomeName, - payload, - }); - } + protected emitSignal(payload: any) { + this.emitter.emit('signal', { + cell_id: this.cellId, + zome_name: this.zomeName, + payload, + }); + } }