Skip to content

Commit d48b7e4

Browse files
committed
Added test for initialize and get compute envs.
1 parent a73a418 commit d48b7e4

2 files changed

Lines changed: 95 additions & 28 deletions

File tree

src/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export class Commands {
420420
return;
421421
}
422422

423-
console.log(`initialize compute details including Escrow payment for compute jobs: ${JSON.stringify(providerInitializeComputeJob)}`)
423+
console.log(`initialize compute details: ${JSON.stringify(providerInitializeComputeJob)}`)
424424

425425
}
426426

test/computeFlow.test.ts

Lines changed: 94 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ import { expect } from "chai";
22
import { exec } from "child_process";
33
import path from "path";
44
import fs from "fs";
5+
import util from "util";
6+
import { homedir } from 'os'
57

68
import { dirname } from 'path'
79
import { fileURLToPath } from 'url'
810

11+
const execPromise = util.promisify(exec);
12+
913
const __filename = fileURLToPath(import.meta.url)
1014
const __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(/did:op:[a-f0-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(/Exiting compute environments:\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(/initialize compute details:\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

Comments
 (0)