1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/test/integration/ocean/Compute.test.ts

221 lines
7.0 KiB
TypeScript

import { assert } from 'chai'
import { config } from '../config'
import {
Ocean,
Account,
DDO,
MetaData,
ComputeJobStatus,
Config,
MetaDataAlgorithm
} from '../../../src'
import { getMetadata } from '../utils'
import { ServiceCompute, ServiceComputePrivacy } from '../../../src/ddo/Service'
const metadataAsset = getMetadata()
const metadataAlgorithm = getMetadata(0, 'algorithm')
const customConfig: Config = {
...config,
// nodeUri: 'https://nile.dev-ocean.com',
// aquariusUri: 'https://aquarius.nile.dev-ocean.com',
// brizoUri: 'http://89.46.156.10:8030',
// secretStoreUri: 'https://secret-store.nile.dev-ocean.com',
// brizoAddress: '0x413c9ba0a05b8a600899b41b0c62dd661e689354',
verbose: true
}
export const rawAlgoMeta = {
rawcode: `console.log('Hello world'!)`,
format: 'docker-image',
version: '0.1',
container: {
entrypoint: 'node $ALGO',
image: 'node',
tag: '10'
}
}
describe('Compute', () => {
let ocean: Ocean
let account: Account
let agreementId: string
let dataset: DDO
let datasetNoRawAlgo: DDO
let datasetWithTrustedAlgo: DDO
let algorithm: DDO
let computeService: ServiceCompute
let jobId: string
before(async () => {
ocean = await Ocean.getInstance(customConfig)
;[account] = await ocean.accounts.list()
})
/* it('should authenticate the consumer account', async () => {
await account.authenticate()
})
*/
it('should publish a dataset with a compute service object', async () => {
const stepsAsset = []
computeService = await ocean.compute.createComputeServiceAttributes(
account,
'1000',
metadataAsset.main.datePublished
)
dataset = await ocean.assets
.create(metadataAsset as MetaData, account, [computeService])
.next((step) => stepsAsset.push(step))
assert.instanceOf(dataset, DDO)
assert.isDefined(
dataset.findServiceByType('compute'),
`DDO compute service doesn't exist`
)
assert.deepEqual(stepsAsset, [0, 1, 2, 3, 4, 5, 6, 7])
})
it('should publish a dataset with a compute service object that does not allow rawAlgo', async () => {
const stepsAsset = []
const origComputePrivacy = {
allowRawAlgorithm: false,
allowNetworkAccess: false,
trustedAlgorithms: []
}
computeService = await ocean.compute.createComputeServiceAttributes(
account,
'1000',
metadataAsset.main.datePublished,
origComputePrivacy as ServiceComputePrivacy
)
datasetNoRawAlgo = await ocean.assets
.create(metadataAsset as MetaData, account, [computeService])
.next((step) => stepsAsset.push(step))
assert.instanceOf(datasetNoRawAlgo, DDO)
assert.isDefined(
dataset.findServiceByType('compute'),
`DDO compute service doesn't exist`
)
assert.deepEqual(stepsAsset, [0, 1, 2, 3, 4, 5, 6, 7])
})
it('should publish a dataset with a compute service object that allows only algo with did:op:1234', async () => {
const stepsAsset = []
const origComputePrivacy = {
allowRawAlgorithm: false,
allowNetworkAccess: false,
trustedAlgorithms: ['did:op:1234']
}
computeService = await ocean.compute.createComputeServiceAttributes(
account,
'1000',
metadataAsset.main.datePublished,
origComputePrivacy as ServiceComputePrivacy
)
datasetWithTrustedAlgo = await ocean.assets
.create(metadataAsset as MetaData, account, [computeService])
.next((step) => stepsAsset.push(step))
assert.instanceOf(datasetWithTrustedAlgo, DDO)
assert.isDefined(
dataset.findServiceByType('compute'),
`DDO compute service doesn't exist`
)
assert.deepEqual(stepsAsset, [0, 1, 2, 3, 4, 5, 6, 7])
})
it('should publish an algorithm', async () => {
const stepsAlgorithm = []
algorithm = await ocean.assets
.create(metadataAlgorithm as MetaData, account)
.next((step) => stepsAlgorithm.push(step))
assert.instanceOf(algorithm, DDO)
assert.deepEqual(stepsAlgorithm, [0, 1, 2, 3, 4, 5, 6, 7])
})
it('should not allow order the compute service with raw algo for dataset that does not allow raw algo', async () => {
const steps = []
agreementId = await ocean.compute
.order(account, datasetNoRawAlgo.id, null, rawAlgoMeta)
.next((step) => steps.push(step))
assert.equal(agreementId, null)
})
it('should not allow order the compute service with did != did:op:1234 for dataset that allows only did:op:1234 as algo', async () => {
const steps = []
agreementId = await ocean.compute
.order(account, datasetWithTrustedAlgo.id, 'did:op:233454', null)
.next((step) => steps.push(step))
assert.equal(agreementId, null)
})
it('should order the compute service of the dataset', async () => {
const steps = []
try {
await account.requestTokens(
parseInt(
(
+computeService.attributes.main.price *
10 ** -(await ocean.keeper.token.decimals())
).toString()
)
)
agreementId = await ocean.compute
.order(account, dataset.id)
.next((step) => steps.push(step))
console.log(agreementId)
assert.isDefined(agreementId)
assert.deepEqual(steps, [0, 1, 2, 3])
} catch {}
})
it('should start a compute job with a published algo', async () => {
const response = await ocean.compute.start(account, agreementId, algorithm.id)
assert.equal(response.status, ComputeJobStatus.Started)
})
it('should start a compute job with a rawcode algo', async () => {
const algoMeta: MetaDataAlgorithm = {
rawcode: `console.log('Hello world!')`,
container: {
entrypoint: 'node $ALGO',
image: 'node',
tag: '10'
}
}
const response = await ocean.compute.start(
account,
agreementId,
undefined,
algoMeta
)
assert.isAtLeast(response.status, ComputeJobStatus.Started)
jobId = response.jobId
})
it('should get status of a compute job', async () => {
const response = await ocean.compute.status(account, agreementId, jobId)
assert.equal(response[0].jobId, jobId)
})
it('should get status of all compute jobs for an address', async () => {
const response = await ocean.compute.status(account, undefined, undefined)
assert.isAbove(response.length, 0)
})
})