Skip to content

Commit b3aafe8

Browse files
committed
update: clone before modifying
1 parent ac2763f commit b3aafe8

3 files changed

Lines changed: 96 additions & 12 deletions

File tree

dist/js/workflows/create.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
66
exports.createWorkflow = createWorkflow;
7+
var _lodash = _interopRequireDefault(require("lodash"));
78
var _create = require("../subworkflows/create");
89
var _units = require("../units");
910
var _map = require("../units/map");
1011
var _utils = require("../utils");
1112
var _workflow = require("./workflow");
13+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
1214
/**
1315
* @summary Helper for creating Map units for complex workflows
1416
* @param config {Object} map unit configuration
@@ -94,9 +96,12 @@ function createSubworkflowUnit({
9496
const {
9597
[appName]: dataByApp
9698
} = subworkflows;
97-
let {
98-
[unitName]: subworkflowData
99+
const {
100+
[unitName]: originalSubworkflowData
99101
} = dataByApp;
102+
103+
// Clone to avoid modifying shared data
104+
let subworkflowData = _lodash.default.cloneDeep(originalSubworkflowData);
100105
subworkflowData.config = {
101106
...subworkflowData.config,
102107
...config
@@ -107,11 +112,9 @@ function createSubworkflowUnit({
107112
});
108113
if (uniqueFlowchartIds && unitIndex !== undefined) {
109114
subworkflowData.units.forEach(unit => {
110-
if (unit.flowchartId) {
111-
unit.flowchartId = `${unit.flowchartId}-${unitIndex}`;
115+
if (unit.config && unit.config.flowchartId) {
116+
unit.config.flowchartId = `${unit.config.flowchartId}-${unitIndex}`;
112117
}
113-
});
114-
subworkflowData.units.forEach(unit => {
115118
if (unit.next) {
116119
unit.next = `${unit.next}-${unitIndex}`;
117120
}

src/js/workflows/create.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import lodash from "lodash";
2+
13
import { createSubworkflow } from "../subworkflows/create";
24
import { UnitFactory } from "../units";
35
import { defaultMapConfig } from "../units/map";
@@ -49,18 +51,18 @@ function createSubworkflowUnit({ appName, unitData, unitIndex, workflowData, ...
4951
const { name: unitName, unitConfigs, config, uniqueFlowchartIds } = unitData;
5052
const { subworkflows } = workflowData;
5153
const { [appName]: dataByApp } = subworkflows;
52-
let { [unitName]: subworkflowData } = dataByApp;
54+
const { [unitName]: originalSubworkflowData } = dataByApp;
55+
56+
// Clone to avoid modifying shared data
57+
let subworkflowData = lodash.cloneDeep(originalSubworkflowData);
5358
subworkflowData.config = { ...subworkflowData.config, ...config };
5459
if (unitConfigs) subworkflowData = updateUnitConfigs({ subworkflowData, unitConfigs });
5560

5661
if (uniqueFlowchartIds && unitIndex !== undefined) {
5762
subworkflowData.units.forEach((unit) => {
58-
if (unit.flowchartId) {
59-
unit.flowchartId = `${unit.flowchartId}-${unitIndex}`;
63+
if (unit.config && unit.config.flowchartId) {
64+
unit.config.flowchartId = `${unit.config.flowchartId}-${unitIndex}`;
6065
}
61-
});
62-
63-
subworkflowData.units.forEach((unit) => {
6466
if (unit.next) {
6567
unit.next = `${unit.next}-${unitIndex}`;
6668
}

tests/js/workflow.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,82 @@ describe("Workflow UUIDs", () => {
201201
expect(workflow1._id).to.equal(workflow2._id);
202202
});
203203
});
204+
205+
describe("subworkflow uniqueFlowchartIds", () => {
206+
it("should append suffix to flowchart IDs when uniqueFlowchartIds is true", () => {
207+
const workflowData = {
208+
name: "Test Workflow",
209+
units: [
210+
{
211+
name: "average_electrostatic_potential_via_band_structure",
212+
type: "subworkflow",
213+
uniqueFlowchartIds: true,
214+
},
215+
],
216+
};
217+
218+
const workflow = createWorkflow({
219+
appName: "espresso",
220+
workflowData,
221+
workflowSubworkflowMapByApplication,
222+
workflowCls: Workflow,
223+
SubworkflowCls: Subworkflow,
224+
UnitFactoryCls: UnitFactory,
225+
unitBuilders: builders,
226+
});
227+
228+
const subworkflowUnit = workflow.subworkflows[0];
229+
230+
// Check that explicit flowchart IDs have suffix (units at index 2 and 7)
231+
expect(subworkflowUnit.units[2].flowchartId).to.equal(
232+
"pw-bands-calculate-band-gap-right-1",
233+
);
234+
expect(subworkflowUnit.units[7].flowchartId).to.equal(
235+
"average-electrostatic-potential-right-1",
236+
);
237+
});
238+
239+
it("should use different suffixes for multiple subworkflow instances", () => {
240+
const workflowData = {
241+
name: "Test Workflow",
242+
units: [
243+
{
244+
name: "average_electrostatic_potential_via_band_structure",
245+
type: "subworkflow",
246+
uniqueFlowchartIds: true,
247+
},
248+
{
249+
name: "average_electrostatic_potential_via_band_structure",
250+
type: "subworkflow",
251+
uniqueFlowchartIds: true,
252+
},
253+
],
254+
};
255+
256+
const workflow = createWorkflow({
257+
appName: "espresso",
258+
workflowData,
259+
workflowSubworkflowMapByApplication,
260+
workflowCls: Workflow,
261+
SubworkflowCls: Subworkflow,
262+
UnitFactoryCls: UnitFactory,
263+
unitBuilders: builders,
264+
});
265+
266+
// First subworkflow should have -1 suffix
267+
expect(workflow.subworkflows[0].units[2].flowchartId).to.equal(
268+
"pw-bands-calculate-band-gap-right-1",
269+
);
270+
expect(workflow.subworkflows[0].units[7].flowchartId).to.equal(
271+
"average-electrostatic-potential-right-1",
272+
);
273+
274+
// Second subworkflow should have -2 suffix
275+
expect(workflow.subworkflows[1].units[2].flowchartId).to.equal(
276+
"pw-bands-calculate-band-gap-right-2",
277+
);
278+
expect(workflow.subworkflows[1].units[7].flowchartId).to.equal(
279+
"average-electrostatic-potential-right-2",
280+
);
281+
});
282+
});

0 commit comments

Comments
 (0)