Skip to content

Commit

Permalink
Add tests for ActorAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Le Cam committed Sep 19, 2019
1 parent f84f7cf commit 5362c4b
Show file tree
Hide file tree
Showing 11 changed files with 819 additions and 401 deletions.
565 changes: 334 additions & 231 deletions __tests__/agent.ts

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@
"start": "yarn build && yarn test:all"
},
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/cli": "^7.6.0",
"@babel/core": "^7.6.0",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-transform-runtime": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/preset-typescript": "^7.3.3",
"@babel/plugin-transform-runtime": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"@babel/preset-typescript": "^7.6.0",
"@types/jest": "^24.0.18",
"@typescript-eslint/eslint-plugin": "^2.1.0",
"@typescript-eslint/parser": "^2.1.0",
"@typescript-eslint/eslint-plugin": "^2.3.0",
"@typescript-eslint/parser": "^2.3.0",
"babel-eslint": "^10.0.3",
"babel-jest": "^24.9.0",
"del-cli": "^2.0.0",
"eslint": "^6.3.0",
"del-cli": "^3.0.0",
"eslint": "^6.4.0",
"eslint-config-mainframe": "^4.0.1",
"get-stream": "^5.1.0",
"jest": "^24.9.0",
"jest-junit": "^8.0.0",
"lerna": "^3.16.4",
"prettier": "^1.18.2",
"ts-jest": "^24.0.2",
"typescript": "^3.6.2"
"ts-jest": "^24.1.0",
"typescript": "^3.6.3"
}
}
1 change: 1 addition & 0 deletions packages/agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@erebos/hex": "^0.9.0",
"@erebos/secp256k1": "^0.9.0",
"@erebos/timeline": "^0.9.0",
"fast-deep-equal": "^2.0.1",
"get-stream": "^5.1.0",
"p-queue": "^6.1.1",
"rxjs": "^6.5.3"
Expand Down
49 changes: 43 additions & 6 deletions packages/agent/src/actor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { ACTOR_NAME, ActorData, ProfileData } from '@aegle/core'
import { Sync } from '@aegle/sync'
import { Sync, getPublicAddress } from '@aegle/sync'
import { hexValue } from '@erebos/hex'
import { KeyPair, createKeyPair } from '@erebos/secp256k1'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'

import { ContactAgentData, ContactAgent } from './contact'
import { FileSystemWriter } from './fileSystem'
import { SyncParams } from './types'

const FEED_PARAMS = { entityType: ACTOR_NAME, name: ACTOR_NAME }

const POLL_INTERVAL = 10 * 1000 // 10 secs
export interface ActorReaderParams {
actor: string
sync: Sync
Expand Down Expand Up @@ -72,23 +74,29 @@ export interface ActorAgentData {
fileSystemKeyPair?: KeyPair
}

export interface ActorAgentParams {
export interface ActorAgentParams extends SyncParams {
data: ActorAgentData
sync: Sync
}

export class ActorAgent {
protected autoStart: boolean
protected data: ActorAgentData
protected interval: number

public address: string
public contacts: Record<string, ContactAgent> = {}
public fileSystem: FileSystemWriter
public sync: Sync
public writeActor: (data: ActorData) => Promise<hexValue>

public constructor(params: ActorAgentParams) {
this.autoStart = params.autoStart || false
this.data = params.data
this.interval = params.interval || POLL_INTERVAL
this.sync = params.sync

this.address = getPublicAddress(params.data.actor.keyPair)
this.writeActor = createActorWriter({
keyPair: this.data.actor.keyPair,
sync: this.sync,
Expand All @@ -108,12 +116,35 @@ export class ActorAgent {
fsKeyPair = this.data.fileSystemKeyPair
}
this.fileSystem = new FileSystemWriter({
autoStart: this.autoStart,
interval: this.interval,
keyPair: fsKeyPair,
sync: this.sync,
reader: this.data.actor.keyPair.getPublic('hex'),
})
}

public startAll(): void {
Object.values(this.contacts).forEach(contact => {
contact.startAll()
})
this.fileSystem.start()
}

public stopAll(): void {
Object.values(this.contacts).forEach(contact => {
contact.stopAll()
})
this.fileSystem.stop()
}

public async publishActor(): Promise<void> {
return await this.writeActor({
profile: this.data.actor.profile,
publicKey: this.data.actor.keyPair.getPublic('hex'),
})
}

public async lookupActor(address: string): Promise<ActorData | null> {
return await readActor({ sync: this.sync, actor: address })
}
Expand All @@ -129,6 +160,7 @@ export class ActorAgent {
public async addContact(
address: string,
actor?: ActorData,
params: SyncParams = {},
): Promise<ContactAgent> {
let actorData: ActorData | null = null
if (actor == null) {
Expand All @@ -140,19 +172,24 @@ export class ActorAgent {
throw new Error('Actor not found')
}

const firstContact = {
keyPair: this.data.actor.keyPair,
actorKey: actorData.publicKey,
}
const contact = new ContactAgent({
autoStart: this.autoStart,
interval: this.interval,
...params,
sync: this.sync,
data: {
read: {
actorAddress: address,
actorData: actor,
firstContact,
},
write: {
keyPair: createKeyPair(),
firstContact: {
keyPair: this.data.actor.keyPair,
actorKey: actorData.publicKey,
},
firstContact,
},
},
})
Expand Down
6 changes: 3 additions & 3 deletions packages/agent/src/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { map } from 'rxjs/operators'

import { FileSystemReader, FileSystemWriter } from './fileSystem'
import { InboxesAgent, InboxAgentData, OutboxesAgent } from './messaging'
import { SyncParams } from './types'

const CONTACT_FEED_PARAMS = { entityType: CONTACT_NAME, name: CONTACT_NAME }

Expand Down Expand Up @@ -149,11 +150,9 @@ export interface ContactAgentError {
error: Error
}

export interface ContactAgentParams {
export interface ContactAgentParams extends SyncParams {
sync: Sync
data: ContactAgentData
interval?: number
autoStart?: boolean
}

export class ContactAgent {
Expand Down Expand Up @@ -371,6 +370,7 @@ export class ContactAgent {
writer: read.contactData.fileSystemKey,
keyPair: write.keyPair,
autoStart: this.autoStart,
interval: this.interval,
})
: null
}
Expand Down
26 changes: 17 additions & 9 deletions packages/agent/src/fileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ import { FeedWriteParams, Sync, getFeedWriteParams } from '@aegle/sync'
import { KeyPair } from '@erebos/secp256k1'
// eslint-disable-next-line import/default
import getStream from 'get-stream'
import isEqual from 'fast-deep-equal'
import PQueue from 'p-queue'
import { BehaviorSubject, Subscription, interval } from 'rxjs'
import { flatMap } from 'rxjs/operators'

import { SyncParams } from './types'

const PATH_RE = new RegExp('^(/[^/]+)+$', 'i')

const POLL_INTERVAL = 120 * 1000 // 2 mins
export interface FileUploadParams extends FileMetadata {
encrypt?: boolean
Expand Down Expand Up @@ -55,11 +59,19 @@ export async function uploadFile(
let hash, encryption, size

if (data instanceof Readable) {
if (params.size == null) {
throw new Error(
'The `size` parameter must be provided when calling `uploadFile()` with a Stream',
)
} else {
size = params.size
}
// The `size` option for `uploadFileStream()` is added in Erebos v0.10
if (key === null) {
hash = await sync.bzz.uploadFileStream(data)
hash = await sync.bzz.uploadFileStream(data, { size })
} else {
const { cipher, iv } = await createCipher(CRYPTO_ALGORITHM, key)
hash = await sync.bzz.uploadFileStream(data.pipe(cipher))
hash = await sync.bzz.uploadFileStream(data.pipe(cipher), { size })
encryption = {
algorithm: CRYPTO_ALGORITHM,
authTag: cipher.getAuthTag().toString('base64'),
Expand Down Expand Up @@ -149,11 +161,9 @@ enum FileSystemPushSyncState {
FAILED,
}

export interface FileSystemReaderParams extends FileSystemParams {
export interface FileSystemReaderParams extends FileSystemParams, SyncParams {
writer: string
keyPair?: KeyPair
interval?: number
autoStart?: boolean
}

export class FileSystemReader extends FileSystem {
Expand Down Expand Up @@ -210,7 +220,7 @@ export class FileSystemReader extends FileSystem {
try {
this.error = undefined
const fs = await this.read()
if (fs != null) {
if (fs != null && !isEqual(fs.files, this.files.value)) {
this.files.next(fs.files)
}
this.pullSync.next(FileSystemPullSyncState.DONE)
Expand All @@ -227,12 +237,10 @@ export interface FileSystemChanges {
timestamp: number
}

export interface FileSystemWriterParams extends FileSystemParams {
export interface FileSystemWriterParams extends FileSystemParams, SyncParams {
keyPair: KeyPair
files?: FilesRecord
reader?: string
interval?: number
autoStart?: boolean
}

export class FileSystemWriter extends FileSystem {
Expand Down
Loading

0 comments on commit 5362c4b

Please sign in to comment.