@skyramp/skyramp 1.3.15 → 1.3.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skyramp/skyramp",
3
- "version": "1.3.15",
3
+ "version": "1.3.16",
4
4
  "description": "module for leveraging skyramp cli functionality",
5
5
  "scripts": {
6
6
  "lint": "eslint 'src/**/*.js' 'src/**/*.ts' --fix",
@@ -0,0 +1,66 @@
1
+ import { Page } from '@playwright/test';
2
+
3
+ export interface AddNodeOptions {
4
+ /** Key to assign to the new node in the GoJS model. */
5
+ key: string;
6
+ /** GoJS node category — used to look up the palette template. */
7
+ category: string;
8
+ /** CSS selector for the div that hosts the target GoJS diagram. */
9
+ diagramSelector: string;
10
+ /** CSS selector for the palette div to copy app-specific properties from. Optional. */
11
+ paletteSelector?: string;
12
+ /** Key of the pre-existing diagram node to use as placement anchor. */
13
+ anchorKey?: string;
14
+ /** X offset from the anchor node's document-space location. */
15
+ anchorOffsetX?: number;
16
+ /** Y offset from the anchor node's document-space location. */
17
+ anchorOffsetY?: number;
18
+ }
19
+
20
+ export interface NodeRef {
21
+ panelSelector: string;
22
+ key: string | number;
23
+ port?: string;
24
+ }
25
+
26
+ export interface PaletteSource {
27
+ panelSelector: string;
28
+ category: string;
29
+ }
30
+
31
+ export interface DocTarget {
32
+ panelSelector: string;
33
+ docX: number;
34
+ docY: number;
35
+ }
36
+
37
+ export interface NodeSource {
38
+ panelSelector: string;
39
+ key: string | number;
40
+ }
41
+
42
+ export declare class GoJSDiagram {
43
+ constructor(page: Page, framePath?: string[]);
44
+
45
+ /**
46
+ * Add a new node to a GoJS diagram at a position relative to an anchor node.
47
+ * App-specific properties are copied from the palette template for the given
48
+ * category, skipping GoJS internals and loc to avoid palette mutation.
49
+ */
50
+ addNode(opts: AddNodeOptions): Promise<void>;
51
+
52
+ /**
53
+ * Create a directed link between two GoJS nodes via the model API.
54
+ */
55
+ linkNodes(source: NodeRef, target: NodeRef): Promise<void>;
56
+
57
+ /**
58
+ * Drag a node from a GoJS Palette into a GoJS Diagram.
59
+ */
60
+ dragFromPalette(source: PaletteSource, target: DocTarget): Promise<void>;
61
+
62
+ /**
63
+ * Move an existing node within a GoJS Diagram.
64
+ */
65
+ moveNode(source: NodeSource, target: DocTarget): Promise<void>;
66
+ }
@@ -0,0 +1,368 @@
1
+ /**
2
+ * Copyright (c) Skyramp Corporation.
3
+ *
4
+ * GoJSDiagram — a Playwright helper for driving GoJS canvases.
5
+ *
6
+ * GoJS renders entirely into a <canvas> element so there are no DOM nodes for
7
+ * individual diagram items. This class handles the three-step coordinate
8
+ * resolution that is required for any reliable interaction:
9
+ *
10
+ * 1. Ask the GoJS model for the viewport-space offset of the element
11
+ * (via go.Diagram.fromDiv().transformDocToView() or findNodeForKey()).
12
+ * 2. Ask Playwright for the page-level bounding box of the <canvas> element
13
+ * (via locator.boundingBox() which correctly traverses the iframe chain).
14
+ * 3. Add the two together → final page coordinate for page.mouse.*.
15
+ *
16
+ * The class is generic: it locates GoJS containers via the standard
17
+ * go.Diagram.fromDiv() API and does NOT rely on application-specific CSS IDs.
18
+ *
19
+ * Usage (TypeScript):
20
+ *
21
+ * import { GoJSDiagram } from '@skyramp/skyramp';
22
+ *
23
+ * const gojs = new GoJSDiagram(page, ['iframe[title="main menu"]', 'iframe[title="iframe process"]']);
24
+ *
25
+ * // Drag a node from a palette to a diagram
26
+ * await gojs.dragFromPalette(
27
+ * { panelSelector: '#myPalette', category: 'Task' },
28
+ * { panelSelector: '#myDiagram', docX: 350, docY: 285 },
29
+ * );
30
+ *
31
+ * // Move an existing node within a diagram
32
+ * await gojs.moveNode(
33
+ * { panelSelector: '#myDiagram', key: '3' },
34
+ * { panelSelector: '#myDiagram', docX: 250, docY: 300 },
35
+ * );
36
+ */
37
+
38
+ /* global document, window */
39
+
40
+ const GOJS_DRAG_STEPS = 50; // Minimum steps for GoJS to register a drag
41
+
42
+ class GoJSDiagram {
43
+ /**
44
+ * @param {import('@playwright/test').Page} page The Playwright Page object.
45
+ * @param {string[]} framePath Ordered list of iframe selectors to reach the
46
+ * frame that contains the GoJS diagram (e.g. ['iframe[title="menu"]',
47
+ * 'iframe[title="process"]']). Empty array means the diagram is in the
48
+ * top-level page.
49
+ */
50
+ constructor(page, framePath = []) {
51
+ this._page = page;
52
+ this._framePath = framePath;
53
+ }
54
+
55
+ /**
56
+ * Returns a FrameLocator scoped to the innermost frame in framePath,
57
+ * or the page itself when framePath is empty.
58
+ *
59
+ * @returns {import('@playwright/test').Page | import('@playwright/test').FrameLocator}
60
+ */
61
+ _frameLocator() {
62
+ let scope = this._page;
63
+ for (const sel of this._framePath) {
64
+ scope = scope.frameLocator(sel);
65
+ }
66
+ return scope;
67
+ }
68
+
69
+ /**
70
+ * Resolve the page-level {x, y} of a point expressed in GoJS document
71
+ * coordinates within the specified panel container.
72
+ *
73
+ * @param {string} panelSelector CSS selector for the div that hosts the GoJS
74
+ * diagram or palette (e.g. '#myPalette', '[data-testid="diagram"]').
75
+ * @param {number} docX X coordinate in GoJS document space.
76
+ * @param {number} docY Y coordinate in GoJS document space.
77
+ * @returns {Promise<{x: number, y: number}>}
78
+ */
79
+ async _resolveDocPoint(panelSelector, docX, docY) {
80
+ const fl = this._frameLocator();
81
+ const canvasLocator = fl.locator(`${panelSelector} canvas`).first();
82
+
83
+ // Page-level bounding box of the canvas element
84
+ const box = await canvasLocator.boundingBox();
85
+ if (!box) {
86
+ throw new Error(`GoJSDiagram: canvas not found for panel "${panelSelector}"`);
87
+ }
88
+
89
+ // Viewport-space point inside the canvas, obtained via GoJS API in the frame
90
+ const vpPt = await this._evaluateInFrame(([sel, dx, dy]) => {
91
+ const container = document.querySelector(sel);
92
+ if (!container) return null;
93
+ const diagram = window.go?.Diagram?.fromDiv?.(container);
94
+ if (!diagram) return null;
95
+ const vp = diagram.transformDocToView(new window.go.Point(dx, dy));
96
+ return { x: vp.x, y: vp.y };
97
+ }, [panelSelector, docX, docY]);
98
+
99
+ if (!vpPt) {
100
+ throw new Error(`GoJSDiagram: could not resolve document point via GoJS for panel "${panelSelector}"`);
101
+ }
102
+
103
+ return { x: box.x + vpPt.x, y: box.y + vpPt.y };
104
+ }
105
+
106
+ /**
107
+ * Resolve the page-level {x, y} of a palette node identified by category.
108
+ * Picks the first node in the palette whose data.category matches.
109
+ *
110
+ * @param {string} panelSelector CSS selector for the palette container div.
111
+ * @param {string} category The GoJS node category to look for.
112
+ * @returns {Promise<{x: number, y: number}>}
113
+ */
114
+ async _resolvePaletteNode(panelSelector, category) {
115
+ const fl = this._frameLocator();
116
+ const canvasLocator = fl.locator(`${panelSelector} canvas`).first();
117
+
118
+ const box = await canvasLocator.boundingBox();
119
+ if (!box) {
120
+ throw new Error(`GoJSDiagram: palette canvas not found for panel "${panelSelector}"`);
121
+ }
122
+
123
+ const vpPt = await this._evaluateInFrame(([sel, cat]) => {
124
+ const container = document.querySelector(sel);
125
+ if (!container) return null;
126
+ const diagram = window.go?.Diagram?.fromDiv?.(container);
127
+ if (!diagram) return null;
128
+ let found = null;
129
+ diagram.nodes.each(n => {
130
+ if (!found && n.data?.category === cat) found = n;
131
+ });
132
+ if (!found) return null;
133
+ const center = found.getDocumentPoint(window.go.Spot.Center);
134
+ const vp = diagram.transformDocToView(center);
135
+ return { x: vp.x, y: vp.y };
136
+ }, [panelSelector, category]);
137
+
138
+ if (!vpPt) {
139
+ throw new Error(`GoJSDiagram: no node with category "${category}" found in palette "${panelSelector}"`);
140
+ }
141
+
142
+ return { x: box.x + vpPt.x, y: box.y + vpPt.y };
143
+ }
144
+
145
+ /**
146
+ * Resolve the page-level {x, y} of a diagram node identified by key.
147
+ *
148
+ * @param {string} panelSelector CSS selector for the diagram container div.
149
+ * @param {string|number} key The GoJS node key.
150
+ * @returns {Promise<{x: number, y: number}>}
151
+ */
152
+ async _resolveDiagramNode(panelSelector, key) {
153
+ const fl = this._frameLocator();
154
+ const canvasLocator = fl.locator(`${panelSelector} canvas`).first();
155
+
156
+ const box = await canvasLocator.boundingBox();
157
+ if (!box) {
158
+ throw new Error(`GoJSDiagram: diagram canvas not found for panel "${panelSelector}"`);
159
+ }
160
+
161
+ const vpPt = await this._evaluateInFrame(([sel, k]) => {
162
+ const container = document.querySelector(sel);
163
+ if (!container) return null;
164
+ const diagram = window.go?.Diagram?.fromDiv?.(container);
165
+ if (!diagram) return null;
166
+ const node = diagram.findNodeForKey(k);
167
+ if (!node) return null;
168
+ const center = node.getDocumentPoint(window.go.Spot.Center);
169
+ const vp = diagram.transformDocToView(center);
170
+ return { x: vp.x, y: vp.y };
171
+ }, [panelSelector, key]);
172
+
173
+ if (!vpPt) {
174
+ throw new Error(`GoJSDiagram: no node with key "${key}" found in diagram "${panelSelector}"`);
175
+ }
176
+
177
+ return { x: box.x + vpPt.x, y: box.y + vpPt.y };
178
+ }
179
+
180
+ /**
181
+ * Execute a function inside the correct frame context.
182
+ * Uses page.evaluate() when there are no iframes, otherwise uses
183
+ * page.frames().find() to reach the correct nested frame.
184
+ *
185
+ * @param {Function} fn Function to evaluate in the frame.
186
+ * @param {...any} args Arguments forwarded to fn.
187
+ * @returns {Promise<any>}
188
+ */
189
+ async _evaluateInFrame(fn, ...args) {
190
+ if (this._framePath.length === 0) {
191
+ return this._page.evaluate(fn, ...args);
192
+ }
193
+
194
+ // Locate the target frame by walking the frame tree
195
+ const targetUrl = await this._frameLocator()
196
+ .locator('html')
197
+ .evaluate(el => el.ownerDocument.defaultView?.location?.href ?? '');
198
+
199
+ const frame = this._page.frames().find(f => f.url() === targetUrl);
200
+ if (!frame) {
201
+ throw new Error(`GoJSDiagram: could not find frame for path [${this._framePath.join(', ')}]`);
202
+ }
203
+
204
+ return frame.evaluate(fn, args[0]);
205
+ }
206
+
207
+ /**
208
+ * Execute the mouse drag sequence that GoJS responds to.
209
+ * Requires ≥50 intermediate mousemove steps.
210
+ *
211
+ * @param {{x: number, y: number}} src Page-level source coordinates.
212
+ * @param {{x: number, y: number}} dest Page-level destination coordinates.
213
+ */
214
+ async _drag(src, dest) {
215
+ // Move to source first (hover to let GoJS know where we start)
216
+ await this._page.mouse.move(src.x, src.y);
217
+ await this._page.waitForTimeout(200);
218
+ await this._page.mouse.down();
219
+ await this._page.waitForTimeout(600);
220
+ await this._page.mouse.move(dest.x, dest.y, { steps: GOJS_DRAG_STEPS });
221
+ await this._page.waitForTimeout(800);
222
+ await this._page.mouse.up();
223
+ await this._page.waitForTimeout(500);
224
+ }
225
+
226
+ /**
227
+ * Drag a node from a GoJS Palette into a GoJS Diagram.
228
+ *
229
+ * @param {{ panelSelector: string, category: string }} source
230
+ * @param {{ panelSelector: string, docX: number, docY: number }} target
231
+ */
232
+ async dragFromPalette(source, target) {
233
+ const src = await this._resolvePaletteNode(source.panelSelector, source.category);
234
+ const dest = await this._resolveDocPoint(target.panelSelector, target.docX, target.docY);
235
+ await this._drag(src, dest);
236
+ }
237
+
238
+ /**
239
+ * Move an existing node within a GoJS Diagram.
240
+ *
241
+ * @param {{ panelSelector: string, key: string|number }} source
242
+ * @param {{ panelSelector: string, docX: number, docY: number }} target
243
+ */
244
+ async moveNode(source, target) {
245
+ const src = await this._resolveDiagramNode(source.panelSelector, source.key);
246
+ const dest = await this._resolveDocPoint(target.panelSelector, target.docX, target.docY);
247
+ await this._drag(src, dest);
248
+ }
249
+
250
+ /**
251
+ * Create a directed link between two GoJS nodes via the model API.
252
+ *
253
+ * This method uses diagram.model.addLinkData() which bypasses the invisible
254
+ * overlay div that intercepts all coordinate-based pointer events on the GoJS
255
+ * canvas. Raw mouse drag approaches (including _drag()) are unreliable for
256
+ * link creation because the port hit areas are small and position-dependent.
257
+ *
258
+ * Usage (TypeScript):
259
+ *
260
+ * await gojs.linkNodes(
261
+ * { panelSelector: '#myDiagram', key: 'TaskA' },
262
+ * { panelSelector: '#myDiagram', key: 'TaskB' },
263
+ * );
264
+ *
265
+ * @param {{ panelSelector: string, key: string|number, port?: string }} source
266
+ * @param {{ panelSelector: string, key: string|number, port?: string }} target
267
+ */
268
+ async linkNodes(source, target) {
269
+ await this._evaluateInFrame(([sel, fromKey, toKey, fromPort, toPort]) => {
270
+ const container = document.querySelector(sel);
271
+ if (!container) return;
272
+ const diagram = window.go?.Diagram?.fromDiv?.(container);
273
+ if (!diagram) return;
274
+ const linkData = { from: fromKey, to: toKey };
275
+ if (fromPort) linkData.fromPort = fromPort;
276
+ if (toPort) linkData.toPort = toPort;
277
+ const txName = `link ${fromKey} -> ${toKey}`;
278
+ diagram.startTransaction(txName);
279
+ diagram.model.addLinkData(linkData);
280
+ diagram.commitTransaction(txName);
281
+ }, [source.panelSelector, source.key, target.key, source.port ?? '', target.port ?? '']);
282
+ }
283
+
284
+ /**
285
+ * Add a new node to a GoJS diagram, placing it at a position relative to an
286
+ * existing anchor node. App-specific properties are copied from the palette
287
+ * template for the given category, skipping GoJS internal fields and the loc
288
+ * property to prevent palette mutation from corrupting subsequent placements.
289
+ *
290
+ * Usage (TypeScript):
291
+ *
292
+ * await _canvas.addNode({
293
+ * key: 'Conditional-0001',
294
+ * category: 'Conditional',
295
+ * diagramSelector: '#divCenter',
296
+ * paletteSelector: '#divLeft',
297
+ * anchorKey: 'InputTask-0001',
298
+ * anchorOffsetX: -9,
299
+ * anchorOffsetY: 137,
300
+ * });
301
+ *
302
+ * @param {{
303
+ * key: string,
304
+ * category: string,
305
+ * diagramSelector: string,
306
+ * paletteSelector?: string,
307
+ * anchorKey?: string,
308
+ * anchorOffsetX?: number,
309
+ * anchorOffsetY?: number,
310
+ * }} opts
311
+ */
312
+ async addNode(opts) {
313
+ const {
314
+ key,
315
+ category,
316
+ diagramSelector,
317
+ paletteSelector,
318
+ anchorKey,
319
+ anchorOffsetX = 0,
320
+ anchorOffsetY = 0,
321
+ } = opts;
322
+
323
+ await this._evaluateInFrame(([diagSel, palSel, nodeKey, nodeCat, anKey, offX, offY]) => {
324
+ const go = window.go;
325
+ if (!go) throw new Error('GoJSDiagram: GoJS not found on window');
326
+ const diagramDiv = document.querySelector(diagSel);
327
+ if (!diagramDiv) throw new Error(`GoJSDiagram: panel not found for "${diagSel}"`);
328
+ const diagram = go.Diagram.fromDiv(diagramDiv);
329
+ if (!diagram) throw new Error(`GoJSDiagram: no GoJS diagram found for "${diagSel}"`);
330
+
331
+ const nodeData = { key: nodeKey, category: nodeCat };
332
+
333
+ // Copy app-specific properties from the palette template, skipping GoJS
334
+ // internals and loc to prevent palette mutation across multiple addNode calls.
335
+ if (palSel) {
336
+ const paletteDiv = document.querySelector(palSel);
337
+ const palette = paletteDiv ? go.Diagram.fromDiv(paletteDiv) : null;
338
+ if (palette) {
339
+ const skip = new Set(['__gohashid', '__gohash', 'loc', 'key']);
340
+ palette.nodes.each((n) => {
341
+ if (n.data?.category === nodeCat) {
342
+ for (const prop in n.data) {
343
+ if (!skip.has(prop)) nodeData[prop] = n.data[prop];
344
+ }
345
+ }
346
+ });
347
+ }
348
+ }
349
+
350
+ const txName = `add ${nodeCat} node`;
351
+ diagram.startTransaction(txName);
352
+ try {
353
+ diagram.model.addNodeData(nodeData);
354
+ const node = diagram.findNodeForKey(nodeKey);
355
+ if (node && anKey) {
356
+ const anchor = diagram.findNodeForKey(anKey);
357
+ if (!anchor) throw new Error(`GoJSDiagram: anchor node "${anKey}" not found`);
358
+ node.location = new go.Point(anchor.location.x + offX, anchor.location.y + offY);
359
+ }
360
+ } finally {
361
+ diagram.commitTransaction(txName);
362
+ }
363
+ }, [diagramSelector, paletteSelector ?? '', key, category, anchorKey ?? '', anchorOffsetX, anchorOffsetY]);
364
+ }
365
+ }
366
+
367
+ module.exports = GoJSDiagram;
368
+
package/src/index.d.ts CHANGED
@@ -19,4 +19,5 @@ export * from "./classes/AsyncTestStatus";
19
19
  export * from "./utils";
20
20
  export * from "./function";
21
21
  export * from "./classes/SmartPlaywright";
22
+ export * from "./classes/GoJSDiagram";
22
23
  export * from "./workspace";
package/src/index.js CHANGED
@@ -20,6 +20,7 @@ const MockV2 = require('./classes/MockV2');
20
20
  const { getValue, getResponseValue, checkSchema, iterate, pushToolEvent, getBaseUrl } = require('./utils');
21
21
  const { checkStatusCode, checkRequestPayload } = require('./function');
22
22
  const { newSkyrampPlaywrightPage, expect } = require('./classes/SmartPlaywright');
23
+ const GoJSDiagram = require('./classes/GoJSDiagram');
23
24
  const {
24
25
  workspaceConfigSchema,
25
26
  serviceSchema,
@@ -60,6 +61,7 @@ module.exports = {
60
61
  pushToolEvent,
61
62
  getBaseUrl,
62
63
  newSkyrampPlaywrightPage,
64
+ GoJSDiagram,
63
65
  expect,
64
66
  workspaceConfigSchema,
65
67
  serviceSchema,