@@ -2,10 +2,14 @@ import { expect } from "chai";
22import { exec } from "child_process" ;
33import path from "path" ;
44import fs from "fs" ;
5+ import util from "util" ;
6+ import { homedir } from 'os'
57
68import { dirname } from 'path'
79import { fileURLToPath } from 'url'
810
11+ const execPromise = util . promisify ( exec ) ;
12+
913const __filename = fileURLToPath ( import . meta. url )
1014const __dirname = dirname ( __filename )
1115
@@ -14,9 +18,35 @@ describe("Ocean CLI Publishing", function() {
1418
1519 let computeDatasetDid : string ;
1620 let jsAlgoDid : string ;
21+ let computeEnvId : string ;
22+ let resources : any ;
23+ let providerInitializeResponse : any
1724
1825 const projectRoot = path . resolve ( __dirname , ".." ) ;
1926
27+ const runCommand = async ( command : string ) : Promise < string > => {
28+ console . log ( `\n[CMD]: ${ command } ` ) ;
29+ try {
30+ const { stdout } = await execPromise ( command , { cwd : projectRoot } ) ;
31+ console . log ( `[OUTPUT]:\n${ stdout } ` ) ;
32+ return stdout ;
33+ } catch ( error : any ) {
34+ console . error ( `[ERROR]:\n${ error . stderr || error . message } ` ) ;
35+ throw error ;
36+ }
37+ } ;
38+
39+ const getAddresses = ( ) => {
40+ const data = JSON . parse (
41+ fs . readFileSync (
42+ process . env . ADDRESS_FILE ||
43+ `${ homedir } /.ocean/ocean-contracts/artifacts/address.json` ,
44+ 'utf8'
45+ )
46+ )
47+ return data . development
48+ } ;
49+
2050 it ( "should publish a compute dataset using 'npm run cli publish'" , function ( done ) {
2151 const metadataFile = path . resolve ( projectRoot , "metadata/simpleComputeDataset.json" ) ;
2252 // Ensure the metadata file exists
@@ -31,6 +61,7 @@ describe("Ocean CLI Publishing", function() {
3161 process . env . NODE_URL = "http://127.0.0.1:8001" ;
3262 process . env . ADDRESS_FILE = path . join ( process . env . HOME || "" , ".ocean/ocean-contracts/artifacts/address.json" ) ;
3363
64+
3465 exec ( `npm run cli publish ${ metadataFile } ` , { cwd : projectRoot } , ( error , stdout ) => {
3566 try {
3667 const match = stdout . match ( / d i d : o p : [ a - f 0 - 9 ] { 64 } / ) ;
@@ -84,34 +115,70 @@ describe("Ocean CLI Publishing", function() {
84115 } ) ;
85116 } ) ;
86117
87- it ( "should get compute environments using 'npm run cli getComputeEnvironments'" , function ( done ) {
88- exec ( `npm run cli getComputeEnvironments` , { cwd : projectRoot } , ( error , stdout ) => {
89- expect ( stdout ) . to . contain ( "Exiting compute environments:" ) ;
90- done ( )
91- } ) ;
118+ it ( "should get compute environments using 'npm run cli getComputeEnvironments'" , async function ( ) {
119+ const output = await runCommand ( `npm run cli getComputeEnvironments` ) ;
120+
121+ const jsonMatch = output . match ( / E x i t i n g c o m p u t e e n v i r o n m e n t s : \s * ( [ \s \S ] * ) / ) ;
122+ if ( ! jsonMatch ) {
123+ console . error ( "Raw output:" , output ) ;
124+ throw new Error ( "Could not find compute environments in the output" ) ;
125+ }
126+
127+ let environments ;
128+ try {
129+ environments = eval ( `(${ jsonMatch [ 1 ] } )` ) ;
130+ } catch ( error ) {
131+ console . error ( "Extracted output:" , jsonMatch [ 1 ] ) ;
132+ throw new Error ( "Failed to parse the extracted output:\n" + error ) ;
133+ }
134+
135+ expect ( environments ) . to . be . an ( "array" ) . that . is . not . empty ;
136+
137+ const firstEnv = environments [ 0 ] ;
138+
139+ expect ( firstEnv ) . to . have . property ( "id" ) . that . is . a ( "string" ) ;
140+ expect ( firstEnv ) . to . have . property ( "consumerAddress" ) . that . is . a ( "string" ) ;
141+ expect ( firstEnv ) . to . have . property ( "resources" ) . that . is . an ( "array" ) ;
142+
143+ computeEnvId = firstEnv . id ;
144+ resources = [
145+ {
146+ id : 'cpu' ,
147+ amount : firstEnv . resources [ 0 ] . max - 1
148+ } ,
149+ {
150+ id : 'ram' ,
151+ amount : firstEnv . resources [ 1 ] . max - 1000
152+ } ,
153+ {
154+ id : 'disk' ,
155+ amount : 0
156+ }
157+ ]
158+ console . log ( `Fetched Compute Env ID: ${ computeEnvId } ` ) ;
92159 } ) ;
93160
94- // it("should initialize compute on compute dataset and algorithm", async function(done ) {
95- // this.timeout(10000); // Increase timeout if needed
96-
97- // (async () => {
98- // try {
99- // const { stdout } = await new Promise<{ stdout: string, error: Error | null }>((resolve, reject) => {
100- // exec(`npm run cli initializeCompute ${computeDatasetDid} ${jsAlgoDid} `, { cwd: projectRoot }, (error, stdout) => {
101- // if (error) {
102- // reject(error);
103- // } else {
104- // resolve({ stdout, error: null });
105- // }
106- // });
107- // } );
108-
109- // expect(stdout).to.contain("File downloaded successfully");
110- // done()
111- // } catch (err) {
112- // done(err );
113- // }
114- // })();
115- // });
161+ it ( "should initialize compute on compute dataset and algorithm" , async function ( ) {
162+ const paymentToken = getAddresses ( ) . Ocean
163+ const output = await runCommand ( `npm run cli initializeCompute ${ computeDatasetDid } ${ jsAlgoDid } ${ computeEnvId } 900 ${ paymentToken } ${ JSON . stringify ( resources ) } ` ) ;
164+ const jsonMatch = output . match ( / i n i t i a l i z e c o m p u t e d e t a i l s : \s * ( [ \s \S ] * ) / ) ;
165+ if ( ! jsonMatch ) {
166+ console . error ( "Raw output:" , output ) ;
167+ throw new Error ( "Could not find initialize response in the output" ) ;
168+ }
169+
170+ let providerInitializeComputeJob ;
171+ try {
172+ providerInitializeComputeJob = eval ( `( ${ jsonMatch [ 1 ] } )` ) ;
173+ } catch ( error ) {
174+ console . error ( "Extracted output:" , jsonMatch [ 1 ] ) ;
175+ throw new Error ( "Failed to parse the extracted output:\n" + error ) ;
176+ }
177+ providerInitializeResponse = providerInitializeComputeJob
178+ expect ( providerInitializeResponse ) . to . have . property ( "payment" ) . that . is . an ( "object" ) ;
179+ // expect(providerInitializeResponse).to.have.property("consumerAddress").that.is.a("string" );
180+ // expect(providerInitializeResponse).to.have.property("resources").that.is.an("array");
181+
182+ } ) ;
116183
117184} ) ;
0 commit comments