Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/improve error handling #121

Merged
merged 8 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digicatapult/dscp-process-management",
"version": "1.7.50",
"version": "1.8.0",
"description": "DSCP Process Management Flow",
"main": "./lib/index.js",
"bin": {
Expand Down
21 changes: 13 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { loadProcesses, disableProcess, listTransforming } from './lib/process/i
import { getAll } from './lib/process/api.js'
import cliVersion from './version.js'

const unwrap = <T, E>(res: Process.Result<T, E>): T => {
if (res.type === 'ok') return res.result
else throw res.error
}

const { log, dir } = console
const program = new Command()
const { red: r, blue: b } = {
Expand Down Expand Up @@ -42,24 +47,23 @@ program
program
.command('list')
.description('A command for listing all active process flows')
.option('--verbose', 'Returns all information about the transation, default - false')
.option('-v, --verbose', 'Returns all information about the transation, default - false')
.option('-h, --host <host>', 'substrate blockchain host address or FQDM, default - "localhost"', 'localhost')
.option('-p, --port <port>', 'specify host port number if it is not a default, default - 9944', '9944')
.option('--raw', 'print processes with hex values and extra keys such as "createdAtHash"')
.option('--active', 'returns only active process flows')
.option('--disabled', 'returns only disabled process flows')
.action(async (options: Process.CLIOptions) => {

if (options.print)
log(`
retrieving all process flows from a chain...
options: ${b(JSON.stringify(options))}
`)
try {
const res: Process.RawPayload[] = await getAll(mapOptions(options))
let processes: Process.RawPayload[]

listTransforming(res, processes, options)

const transformed = listTransforming(res, options)
dir(transformed, { depth: null })

process.exit(0)
} catch (err) {
Expand All @@ -86,8 +90,9 @@ program
`)
const { dryRun, verbose } = options
try {
const res: Process.Response = await loadProcesses({ data, dryRun, options: mapOptions(options), verbose })
dir(res, { depth: null })
const loadResult = await loadProcesses({ data, dryRun, options: mapOptions(options), verbose })
dir(loadResult.message, { depth: null })
dir(unwrap(loadResult), { depth: null })
process.exit(0)
} catch (err) {
log(err)
Expand All @@ -108,7 +113,7 @@ program
if (options.print) log(`attempting to disable:\nID:${b(id)}\nVersion:${b(version)}`)
try {
const { dryRun } = options
const res: Process.Result = await disableProcess(id, parseInt(version), dryRun, mapOptions(options))
const res = unwrap(await disableProcess(id, parseInt(version), dryRun, mapOptions(options)))
dir(res, { depth: null })

process.exit(0)
Expand Down
115 changes: 72 additions & 43 deletions src/lib/process/_tests_/unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,145 +1,174 @@
import { expect } from 'chai'

import { HexError } from '../../types/error.js'
import { utf8ToHex } from '../hex.js'
import { listTransforming } from '../index.js'
import { utf8ToHex, hexToUtf8 } from '../hex.js'
import { listTransforming, handleVerbose } from '../index.js'

import sample from '../../../../tests/fixtures/processes.js'

const defaultPolkadot: Process.CLIOptions = { port: '9044', user: 'alice', host: 'localhost' }

describe('utf8ToHex', () => {
it('converts a utf8 string to hexadecimal', () => {
expect(utf8ToHex('test123', 10)).to.equal('0x74657374313233')
expect(utf8ToHex('test123')).to.equal('0x74657374313233')
})
})

it('throws for string over given max length', () => {
expect(() => utf8ToHex('test123', 1)).to.throw(HexError)
describe('hexToUtf8', () => {
it('converts a prefixed string to hex', () => {
expect(hexToUtf8('0x74657374313233')).to.equal('test123')
})
})

describe('listTranforming', () => {
it('returns all transformed processes without a program (--verbose=false)', async () => {
let processes: Process.RawPayload[]
const enriched: Process.RawPayload = {
...sample[0],
id: '123',
name: '123',
status: 'Enabled',
createdAtHash: 'abc',
initialU8aLength: '32',
}
const res = await listTransforming([enriched], processes, { ...defaultPolkadot, verbose: false })

const res = listTransforming([enriched], { ...defaultPolkadot, verbose: false })

expect(res[0]).to.deep.contain({
id: '123',
name: '123',
status: 'Enabled',
version: 1,
})
})

it('returns all transformed processes with a program (--verbose=true)', async () => {
let processes: Process.RawPayload[]
const enriched: Process.RawPayload = {
...sample[0],
id: '123',
name: '123',
status: 'Disabled',
createdAtHash: 'abc',
initialU8aLength: '32',
}
const res = await listTransforming([enriched], processes, { ...defaultPolkadot, verbose: true})

const res = listTransforming([enriched], { ...defaultPolkadot, verbose: true })

expect(res[0]).to.deep.contain({
id: '123',
name: '123',
status: 'Disabled',
version: 1,
})
})

it('returns all options active', async () => {
let processes: Process.RawPayload[]
const enriched: Process.RawPayload = {
...sample[0],
id: '123',
name: '123',
mattdean-digicatapult marked this conversation as resolved.
Show resolved Hide resolved
status: 'Enabled',
createdAtHash: 'abc',
initialU8aLength: '32',
}
const res = await listTransforming([enriched], processes, { ...defaultPolkadot, active: true})

const res = listTransforming([enriched], { ...defaultPolkadot, active: true })

expect(res[0]).to.deep.contain({
id: '123',
name: '123',
status: 'Enabled',
version: 1,
})
})

it('returns all options disabled but with active true', async () => {
let processes: Process.RawPayload[]
const enriched: Process.RawPayload = {
...sample[0],
id: '123',
name: '123',
status: 'Disabled',
createdAtHash: 'abc',
initialU8aLength: '32',
}

const res = await listTransforming([enriched], processes, { ...defaultPolkadot, active: true})
const res = listTransforming([enriched], { ...defaultPolkadot, active: true })

expect(res[0]).to.equal(undefined)
})

it('returns all options disabled', async () => {
let processes: Process.RawPayload[]
const enriched: Process.RawPayload = {
...sample[0],
id: '123',
name: '123',
status: 'Disabled',
createdAtHash: 'abc',
initialU8aLength: '32',
}
const res = await listTransforming([enriched], processes, { ...defaultPolkadot})

const res = listTransforming([enriched], { ...defaultPolkadot })

expect(res[0]).to.deep.contain({
id: '123',
name: '123',
status: 'Disabled',
version: 1,
})
})

it('returns all options active with status enabled', async () => {
let processes: Process.RawPayload[]
const enriched: Process.RawPayload = {
...sample[0],
id: '123',
name: '123',
status: 'Disabled',
createdAtHash: 'abc',
initialU8aLength: '32',
}
const res = await listTransforming([enriched], processes, { ...defaultPolkadot, active: false})

const res = listTransforming([enriched], { ...defaultPolkadot, active: false })

expect(res[0]).to.deep.contain({
id: '123',
name: '123',
status: 'Disabled',
version: 1,
})
})
})

/* TODO will update with other due to this being covered in integration test suites
describe('createProcessTransaction', () => {
describe('if POSIX is invalid', () => {
it('throws invalid program error', () => {

it('returns additional properties with raw', async () => {
const enriched: Process.RawPayload = {
...sample[0],
name: '123',
status: 'Disabled',
createdAtHash: 'abc',
initialU8aLength: '32',
}

const res = listTransforming([enriched], { ...defaultPolkadot, active: false, raw: true })

expect(res[0]).to.deep.contain({
name: '0x313233',
status: 'Disabled',
version: 1,
createdAtHash: 'abc',
initialU8aLength: '32',
})
})
})

describe('handleVerbose', function () {
it('should remove program when verbose == false', function () {
const result = handleVerbose(
{
name: 'test',
status: 'Enabled',
version: 1,
program: [],
},
false
)
expect(result).to.deep.equal({ name: 'test', status: 'Enabled', version: 1 })
})

it('successfully creates process transaction', () => {

it('should include program when verbose == false', function () {
const result = handleVerbose(
{
name: 'test',
status: 'Enabled',
version: 1,
program: [],
},
true
)
expect(result).to.deep.equal({ name: 'test', status: 'Enabled', version: 1, program: [] })
})
})
*/
Loading