Skip to content

Commit

Permalink
feat: show spells separately in showSubnet [NET-635] (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
justprosh authored Nov 16, 2023
1 parent 2901420 commit d481934
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 24 deletions.
15 changes: 10 additions & 5 deletions src/lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ data WorkerServices:
host_id: string
worker_id: ?string
services: ?[]string
spells: ?[]string
func showSubnet() -> []WorkerServices:
deals <- Deals.get()
Expand All @@ -491,15 +492,19 @@ func showSubnet() -> []WorkerServices:
-- get list of all services on this worker
srvs <- Srv.list()
-- gather aliases
aliases: *string
-- gather spells and services aliases
spells_aliases: *string
services_aliases: *string
for s <- srvs:
if s.aliases.length != 0:
aliases <<- s.aliases[0]
if s.service_type == "spell":
spells_aliases <<- s.aliases[0]
if s.service_type == "service":
services_aliases <<- s.aliases[0]
services <<- WorkerServices(host_id=w.host_id, worker_id=w.worker_id, services=?[aliases])
services <<- WorkerServices(host_id=w.host_id, worker_id=w.worker_id, services=?[services_aliases], spells=?[spells_aliases])
else:
services <<- WorkerServices(host_id=w.host_id, worker_id=nil, services=nil)
services <<- WorkerServices(host_id=w.host_id, worker_id=nil, services=nil, spells=nil)
<- services
`;
Expand Down
2 changes: 1 addition & 1 deletion src/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"chain": "fluencelabs/chain-rpc:0.2.20",
"rust-toolchain": "nightly-2023-08-27-x86_64",
"npm": {
"@fluencelabs/aqua-lib": "0.8.0",
"@fluencelabs/aqua-lib": "0.8.1",
"@fluencelabs/spell": "0.5.31",
"@fluencelabs/registry": "0.8.7"
},
Expand Down
79 changes: 61 additions & 18 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { readFile, writeFile, mkdir } from "node:fs/promises";
import { join, relative, resolve } from "node:path";

import { CLIError } from "@oclif/core/lib/errors/index.js";
import type { JSONSchemaType } from "ajv";
import Ajv from "ajv";

import {
setCommandObjAndIsInteractive,
Expand Down Expand Up @@ -46,7 +48,6 @@ import {
getAquaMainPath,
getSpellsDir,
} from "../src/lib/paths.js";
import { hasKey } from "../src/lib/typeHelpers.js";

import {
fluence,
Expand Down Expand Up @@ -542,26 +543,37 @@ describe("integration tests", () => {

const parsedShowSubnetResult = JSON.parse(showSubnetResult);

function isWorkerService(unknown: unknown) {
return (
hasKey("services", unknown) &&
Array.isArray(unknown.services) &&
unknown.services.every((i) => {
return typeof i === "string";
}) &&
hasKey("worker_id", unknown) &&
unknown.worker_id !== null &&
hasKey("host_id", unknown) &&
typeof unknown.host_id === "string"
if (!validateWorkerServices(parsedShowSubnetResult)) {
throw new Error(
`result of running showSubnet aqua function is expected to be an array of WorkerServices, but it is: ${showSubnetResult}`,
);
}

assert(
Array.isArray(parsedShowSubnetResult) &&
parsedShowSubnetResult.every((unknown) => {
return isWorkerService(unknown);
}),
`result of running showSubnet aqua function is expected to be an array of WorkerServices, but it is: ${showSubnetResult}`,
parsedShowSubnetResult
.sort((a, b) => {
if (a.host_id < b.host_id) {
return -1;
}

if (a.host_id > b.host_id) {
return 1;
}

return 0;
})
.forEach((w) => {
return w.spells.sort();
});

expect(parsedShowSubnetResult).toEqual(
peerIds.map((host_id, i) => {
return {
host_id,
services: [MY_SERVICE_NAME],
spells: [NEW_SPELL_NAME, "worker-spell"],
worker_id: parsedShowSubnetResult[i]?.worker_id,
};
}),
);

const logs = await fluence({ args: ["deal", "logs"], cwd });
Expand Down Expand Up @@ -659,3 +671,34 @@ function assertLogsAreValid(logs: string) {
throw new Error(`Failed to get deal logs:\n\n${logs}`);
}
}

type WorkerServices = {
host_id: string;
services: string[];
spells: string[];
worker_id: string;
}[];

const workerServiceSchema: JSONSchemaType<WorkerServices> = {
type: "array",
items: {
type: "object",
properties: {
host_id: { type: "string" },
services: {
type: "array",
items: { type: "string" },
},
spells: {
type: "array",
items: { type: "string" },
},
worker_id: { type: "string" },
},
required: ["host_id", "services", "spells", "worker_id"],
},
};

const validateWorkerServices = new Ajv.default({
code: { esm: true },
}).compile(workerServiceSchema);

0 comments on commit d481934

Please sign in to comment.