Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import { OperatorMetadataService } from "./../../operator-metadata/operator-meta
import { inject, TestBed } from "@angular/core/testing";

import { WorkflowUtilService } from "./workflow-util.service";
import { mockScanSourceSchema } from "../../operator-metadata/mock-operator-metadata.data";
import { mockMultiInputOutputSchema, mockScanSourceSchema } from "../../operator-metadata/mock-operator-metadata.data";
import { commonTestProviders } from "../../../../common/testing/test-utils";
import { OperatorPredicate } from "../../../types/workflow-common.interface";
import { ExecutionMode, Workflow, WorkflowContent } from "../../../../common/type/workflow";

describe("WorkflowUtilService", () => {
let workflowUtilService: WorkflowUtilService;
Expand Down Expand Up @@ -101,4 +103,128 @@ describe("WorkflowUtilService", () => {
// assert all IDs are distinct
expect(idSet.size).toEqual(repeat);
});

it("should rebuild ports and version from the schema when the operator has no ports", () => {
const op: OperatorPredicate = {
operatorID: "op-1",
operatorType: mockMultiInputOutputSchema.operatorType,
operatorVersion: "stale-version",
operatorProperties: {},
inputPorts: [],
outputPorts: [],
showAdvanced: false,
isDisabled: false,
};

const updated = workflowUtilService.updateOperatorVersion(op);

// version is refreshed from the schema
expect(updated.operatorVersion).toEqual(mockMultiInputOutputSchema.operatorVersion);
// ports are regenerated from the schema's port metadata
expect(updated.inputPorts.map(port => port.portID)).toEqual(["input-0", "input-1", "input-2"]);
expect(updated.outputPorts.map(port => port.portID)).toEqual(["output-0", "output-1", "output-2"]);
});

it("should keep the operator's existing ports when they are already present", () => {
const existingInputPorts = [{ portID: "input-existing", displayName: "keep-me" }];
const existingOutputPorts = [{ portID: "output-existing", displayName: "keep-me-too" }];
const op: OperatorPredicate = {
operatorID: "op-2",
operatorType: mockMultiInputOutputSchema.operatorType,
operatorVersion: "stale-version",
operatorProperties: {},
inputPorts: existingInputPorts,
outputPorts: existingOutputPorts,
showAdvanced: false,
isDisabled: false,
};

const updated = workflowUtilService.updateOperatorVersion(op);

// existing ports are preserved untouched, only the version is refreshed
expect(updated.inputPorts).toEqual(existingInputPorts);
expect(updated.outputPorts).toEqual(existingOutputPorts);
expect(updated.operatorVersion).toEqual(mockMultiInputOutputSchema.operatorVersion);
});

it("should throw an error when updating the version of an operator with an unknown type", () => {
const op: OperatorPredicate = {
operatorID: "op-3",
operatorType: "non-exist-operator-type",
operatorVersion: "v1",
operatorProperties: {},
inputPorts: [],
outputPorts: [],
showAdvanced: false,
isDisabled: false,
};

expect(() => workflowUtilService.updateOperatorVersion(op)).toThrowError(new RegExp("doesn't exist"));
});

it("should parse the workflow content string into an object", () => {
const content: WorkflowContent = {
operators: [],
operatorPositions: {},
links: [],
commentBoxes: [],
settings: { dataTransferBatchSize: 400, executionMode: ExecutionMode.PIPELINED },
};
const workflow = {
name: "test-workflow",
description: undefined,
wid: 1,
creationTime: undefined,
lastModifiedTime: undefined,
isPublished: 0,
readonly: false,
content: JSON.stringify(content) as any,
} as unknown as Workflow;

const parsed = WorkflowUtilService.parseWorkflowInfo(workflow);

// the string content is replaced with the parsed object
expect(typeof parsed.content).toEqual("object");
expect(parsed.content).toEqual(content);
});

it("should leave workflow content untouched when it is already an object", () => {
const content: WorkflowContent = {
operators: [],
operatorPositions: {},
links: [],
commentBoxes: [],
settings: { dataTransferBatchSize: 400, executionMode: ExecutionMode.PIPELINED },
};
const workflow = {
name: "test-workflow",
description: undefined,
wid: 1,
creationTime: undefined,
lastModifiedTime: undefined,
isPublished: 0,
readonly: false,
content,
} as unknown as Workflow;

const parsed = WorkflowUtilService.parseWorkflowInfo(workflow);

// an object content is returned as-is
expect(parsed.content).toBe(content);
});

it("should create a fresh comment box at the default position", () => {
const commentBox = workflowUtilService.getNewCommentBox();

expect(commentBox.commentBoxID).toMatch(/^commentBox-/);
expect(commentBox.comments).toEqual([]);
expect(commentBox.commentBoxPosition).toEqual({ x: 500, y: 20 });
});

it("should generate a distinct comment box each time", () => {
const first = workflowUtilService.getNewCommentBox();
const second = workflowUtilService.getNewCommentBox();

expect(first.commentBoxID).not.toEqual(second.commentBoxID);
});
});
Loading