Skip to content

Commit

Permalink
[NayNay] User Program Management::View Programs + Testing
Browse files Browse the repository at this point in the history
- added new service file for view program pure function
- added tests
  • Loading branch information
rh0delta committed Jul 16, 2024
1 parent 236f413 commit e81fe22
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 20 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil

## [UNRELEASED]

### Added
- new: 'src/flows/user-program-management/view.ts' - service file for pure functions of viewing user programs
- new: 'src/flows/user-program-management/helpers/utils.ts' - utility helper file for user program management specific methods
### Fixed

### Changed

### Broke

### Meta/Dev

## [0.0.2] AntMan - 2024-07-12 (entropy-core compatibility: 0.2.0)

### Added
- new: './src/flows/balance/balance.ts' - service file separated out of main flow containing the pure functions to perform balance requests for one or multiple addresses
- new: './tests/balance.test.ts' - new unit tests file for balance pure functions
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@entropyxyz/cli",
"version": "0.0.1",
"version": "0.0.2",
"description": "cli and tui for interacting with the entropy protocol",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/flows/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { entropyFaucet } from './entropyFaucet'
export { checkBalance } from './balance'
export { register } from './register'
export { userPrograms } from './UserPrograms'
export { userPrograms } from './user-program-management'
export { devPrograms } from './DeployPrograms'
export { sign } from './sign'
export { entropyTransfer } from './entropyTransfer'
Expand Down
9 changes: 9 additions & 0 deletions src/flows/user-program-management/helpers/questions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Entropy from "@entropyxyz/sdk";

export const verifyingKeyQuestion = (entropy: Entropy) => [{
type: 'list',
name: 'verifyingKey',
message: 'Select the key to proceeed',
choices: entropy.keyring.accounts.registration.verifyingKeys,
default: entropy.keyring.accounts.registration.verifyingKeys[0]
}]
11 changes: 11 additions & 0 deletions src/flows/user-program-management/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { print } from "src/common/utils"

export function displayPrograms (programs): void {
programs.forEach((program, index) => {
print(
`${index + 1}. Pointer: ${
program.program_pointer
}, Config: ${JSON.stringify(program.program_config)}`
)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import * as util from "@polkadot/util"
import { initializeEntropy } from "../../common/initializeEntropy"
import { getSelectedAccount, print } from "../../common/utils"
import { EntropyLogger } from "src/common/logger";
import { verifyingKeyQuestion } from "./helpers/questions";
import { viewPrograms } from "./view";
import { displayPrograms } from "./helpers/utils";

let verifyingKey: string;

Expand Down Expand Up @@ -35,35 +38,21 @@ export async function userPrograms ({ accounts, selectedAccount: selectedAccount
throw new Error("Keys are undefined")
}

const verifyingKeyQuestion = [{
type: 'list',
name: 'verifyingKey',
message: 'Select the key to proceeed',
choices: entropy.keyring.accounts.registration.verifyingKeys,
default: entropy.keyring.accounts.registration.verifyingKeys[0]
}]

switch (actionChoice.action) {
case "View My Programs": {
try {
if (!verifyingKey && entropy.keyring.accounts.registration.verifyingKeys.length) {
({ verifyingKey } = await inquirer.prompt(verifyingKeyQuestion))
({ verifyingKey } = await inquirer.prompt(verifyingKeyQuestion(entropy)))
} else {
print('You currently have no verifying keys, please register this account to generate the keys')
break
}
const programs = await entropy.programs.get(verifyingKey)
const programs = await viewPrograms(entropy, { verifyingKey })
if (programs.length === 0) {
print("You currently have no programs set.")
} else {
print("Your Programs:")
programs.forEach((program, index) => {
print(
`${index + 1}. Pointer: ${
program.program_pointer
}, Config: ${JSON.stringify(program.program_config)}`
)
})
displayPrograms(programs)
}
} catch (error) {
console.error(error.message)
Expand Down Expand Up @@ -132,7 +121,7 @@ export async function userPrograms ({ accounts, selectedAccount: selectedAccount
case "Remove a Program from My List": {
try {
if (!verifyingKey) {
({ verifyingKey } = await inquirer.prompt(verifyingKeyQuestion))
({ verifyingKey } = await inquirer.prompt(verifyingKeyQuestion(entropy)))
}
const { programPointerToRemove } = await inquirer.prompt([
{
Expand Down
3 changes: 3 additions & 0 deletions src/flows/user-program-management/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ViewProgramsParams {
verifyingKey: string
}
6 changes: 6 additions & 0 deletions src/flows/user-program-management/view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Entropy from "@entropyxyz/sdk";
import { ViewProgramsParams } from "./types";

export async function viewPrograms (entropy: Entropy, { verifyingKey }: ViewProgramsParams): Promise<any[]> {
return entropy.programs.get(verifyingKey)
}
15 changes: 15 additions & 0 deletions tests/user-program-management.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import test from 'tape'
import { viewPrograms } from 'src/flows/user-program-management/view'
import { charlieStashSeed, setupTest } from './testing-utils'

const networkType = 'two-nodes'

test('User Program Management::View Programs', async t => {
const { run, entropy } = await setupTest(t, { seed: charlieStashSeed, networkType })

await run('charlie register', entropy.register())
const programs = await run('get charlie programs', viewPrograms(entropy, { verifyingKey: entropy.programs.verifyingKey }))

t.equal(programs.length, 1, 'charlie has 1 program')
t.end()
})

0 comments on commit e81fe22

Please sign in to comment.