mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
refactor code and tests to as response to review
This commit is contained in:
parent
3ca247ebf4
commit
a128585123
@ -1,59 +1,44 @@
|
||||
import { IndexationPayload } from '@iota/client/lib/types'
|
||||
|
||||
import { sendDataMessage, getMessage, getAllMessages } from '@/apis/IotaConnector'
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
import { IotaClientSingleton } from '@/apis/IotaConnector'
|
||||
import { CONFIG } from '@/config'
|
||||
|
||||
jest.mock('@/config', () => ({
|
||||
CONFIG: {
|
||||
IOTA: true,
|
||||
IOTA_COMMUNITY_ALIAS: 'GRADIDO: TestHelloWelt1',
|
||||
IOTA_API_URL: 'https://chrysalis-nodes.iota.org',
|
||||
},
|
||||
}))
|
||||
CONFIG.IOTA = true
|
||||
CONFIG.IOTA_COMMUNITY_ALIAS = 'GRADIDO: TestHelloWelt1'
|
||||
CONFIG.IOTA_API_URL = 'https://chrysalis-nodes.iota.org'
|
||||
|
||||
describe('apis/IotaConnector/enabled', () => {
|
||||
describe('Hello World', () => {
|
||||
const now = new Date()
|
||||
let messageId = ''
|
||||
let messageId: string
|
||||
const messageString = 'Hello World - ' + now.toString()
|
||||
const messageHexString = Buffer.from(messageString, 'utf8').toString('hex')
|
||||
const indexHexString = Buffer.from(CONFIG.IOTA_COMMUNITY_ALIAS, 'utf8').toString('hex')
|
||||
const iota = IotaClientSingleton.getInstance()
|
||||
it('sends hello world message to iota tangle', async () => {
|
||||
const iotaMessage = await sendDataMessage(messageString)
|
||||
expect(iotaMessage).not.toBeNull()
|
||||
if (iotaMessage) {
|
||||
const indexationPayload = iotaMessage.message.payload as IndexationPayload
|
||||
expect(indexationPayload.data).toBe(messageHexString)
|
||||
expect(indexationPayload.index).toBe(indexHexString)
|
||||
messageId = iotaMessage.messageId
|
||||
}
|
||||
const iotaMessage = await iota?.sendDataMessage(messageString)
|
||||
expect(iotaMessage).toMatchObject({
|
||||
message: {
|
||||
payload: {
|
||||
data: messageHexString,
|
||||
index: indexHexString,
|
||||
},
|
||||
},
|
||||
messageId: expect.any(String),
|
||||
})
|
||||
messageId =
|
||||
iotaMessage?.messageId ?? '5498130bc3918e1a7143969ce05805502417e3e1bd596d3c44d6a0adeea22710'
|
||||
})
|
||||
it('receives hello world message from iota tangle by message id', async () => {
|
||||
const iotaMessage = await getMessage(messageId)
|
||||
expect(iotaMessage).not.toBeNull()
|
||||
if (iotaMessage) {
|
||||
const indexationPayload = iotaMessage.message.payload as IndexationPayload
|
||||
expect(indexationPayload.data).toBe(messageHexString)
|
||||
expect(indexationPayload.index).toBe(indexHexString)
|
||||
}
|
||||
})
|
||||
it('receives hello world message from iota tangle by searching throw all', async () => {
|
||||
const iotaMessages = await getAllMessages()
|
||||
expect(iotaMessages).not.toBeNull()
|
||||
if (iotaMessages) {
|
||||
let foundMessage = false
|
||||
for (const messageId of iotaMessages) {
|
||||
const iotaMessage = await getMessage(messageId)
|
||||
if (iotaMessage) {
|
||||
const indexationPayload = iotaMessage.message.payload as IndexationPayload
|
||||
if (indexationPayload.data.toString() === messageHexString) {
|
||||
foundMessage = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
expect(foundMessage).toBeTruthy()
|
||||
}
|
||||
const iotaMessage = await iota?.getMessage(messageId)
|
||||
expect(iotaMessage).toMatchObject({
|
||||
message: {
|
||||
payload: {
|
||||
data: messageHexString,
|
||||
index: indexHexString,
|
||||
},
|
||||
},
|
||||
messageId,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -12,8 +12,9 @@ import { backendLogger as logger } from '@/server/logger'
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
||||
class IotaClientSingleton {
|
||||
private static IotaClient: Client
|
||||
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
private static instance: IotaClientSingleton
|
||||
private client: Client
|
||||
/**
|
||||
* The Singleton's constructor should always be private to prevent direct
|
||||
* construction calls with the `new` operator.
|
||||
@ -27,56 +28,40 @@ class IotaClientSingleton {
|
||||
* This implementation let you subclass the Singleton class while keeping
|
||||
* just one instance of each subclass around.
|
||||
*/
|
||||
public static getInstance(): Client | null {
|
||||
public static getInstance(): IotaClientSingleton | null {
|
||||
if (!CONFIG.IOTA || !CONFIG.IOTA_API_URL) {
|
||||
logger.info(`Iota are disabled via config...`)
|
||||
return null
|
||||
}
|
||||
if (!IotaClientSingleton.IotaClient) {
|
||||
IotaClientSingleton.IotaClient = new ClientBuilder().node(CONFIG.IOTA_API_URL).build()
|
||||
if (!IotaClientSingleton.instance) {
|
||||
IotaClientSingleton.instance = new IotaClientSingleton()
|
||||
IotaClientSingleton.instance.client = new ClientBuilder().node(CONFIG.IOTA_API_URL).build()
|
||||
}
|
||||
|
||||
return IotaClientSingleton.IotaClient
|
||||
return IotaClientSingleton.instance
|
||||
}
|
||||
|
||||
/**
|
||||
* send data message onto iota tangle
|
||||
* use CONFIG.IOTA_COMMUNITY_ALIAS for index
|
||||
* @param {string} message - the message as utf based string, will be converted to hex automatically from @iota/client
|
||||
* @return {Promise<MessageWrapper>} the iota message typed
|
||||
*/
|
||||
public sendDataMessage(message: string): Promise<MessageWrapper> {
|
||||
return this.client.message().index(CONFIG.IOTA_COMMUNITY_ALIAS).data(message).submit()
|
||||
}
|
||||
|
||||
/**
|
||||
* receive message for known message id from iota tangle
|
||||
* @param {string} messageId - as hex string
|
||||
* @return {Promise<MessageWrapper>} the iota message typed
|
||||
*/
|
||||
public getMessage(messageId: string): Promise<MessageWrapper> {
|
||||
return this.client.getMessage().data(messageId)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* send data message onto iota tangle
|
||||
* use CONFIG.IOTA_COMMUNITY_ALIAS for index
|
||||
* @param {string} message - the message as utf based string, will be converted to hex automatically from @iota/client
|
||||
* @return {MessageWrapper|null} the iota message as json object with transaction data from iota or null if iota is disabled in config
|
||||
*/
|
||||
export const sendDataMessage = (message: string): Promise<MessageWrapper> | null => {
|
||||
const client = IotaClientSingleton.getInstance()
|
||||
if (!client) {
|
||||
return null
|
||||
}
|
||||
return client.message().index(CONFIG.IOTA_COMMUNITY_ALIAS).data(message).submit()
|
||||
}
|
||||
|
||||
/**
|
||||
* receive message for known message id from iota tangle
|
||||
* @param {string} messageId - as hex string
|
||||
* @return {MessageWrapper|null} the iota message as json object with transaction data from iota or null if iota is disabled in config
|
||||
*/
|
||||
export const getMessage = (messageId: string): Promise<MessageWrapper> | null => {
|
||||
const client = IotaClientSingleton.getInstance()
|
||||
if (!client) {
|
||||
return null
|
||||
}
|
||||
return client.getMessage().data(messageId)
|
||||
}
|
||||
/**
|
||||
* receive all message ids belonging to our topic from iota tangle
|
||||
* @returns array of messageIds
|
||||
*/
|
||||
export const getAllMessages = (): Promise<string[]> | null => {
|
||||
const client = IotaClientSingleton.getInstance()
|
||||
if (!client) {
|
||||
return null
|
||||
}
|
||||
return client.getMessage().index(CONFIG.IOTA_COMMUNITY_ALIAS)
|
||||
}
|
||||
export { IotaClientSingleton }
|
||||
|
||||
/**
|
||||
* example for message:
|
||||
|
||||
@ -1,19 +1,11 @@
|
||||
import { sendDataMessage, getMessage, getAllMessages } from '@/apis/IotaConnector'
|
||||
import { IotaClientSingleton } from '@/apis/IotaConnector'
|
||||
|
||||
jest.mock('@/config', () => ({
|
||||
CONFIG: { IOTA: false },
|
||||
}))
|
||||
|
||||
describe('apis/IotaConnector/disabled', () => {
|
||||
describe('disabled', () => {
|
||||
it('sendDataMessage return null if iota is disabled', () => {
|
||||
expect(sendDataMessage('empty')).toBeNull()
|
||||
})
|
||||
it('getMessage return null if iota is disabled', () => {
|
||||
expect(getMessage('empty')).toBeNull()
|
||||
})
|
||||
it('getAllMessages return null if iota is disabled', () => {
|
||||
expect(getAllMessages()).toBeNull()
|
||||
})
|
||||
it('getInstance return null if iota is disabled', () => {
|
||||
expect(IotaClientSingleton.getInstance()).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user