frame.simulator 1.0.1 → 1.0.3

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/batch.ts ADDED
@@ -0,0 +1,218 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import { Command } from './command';
4
+ import { TUserAction } from './types';
5
+
6
+ const PORT: number = parseInt(process.env.PORT || '3010', 10);
7
+
8
+ export class Batch {
9
+ private filename: string;
10
+
11
+ constructor(filename: string) {
12
+ const batchFilePath = path.join(process.cwd(), filename);
13
+ this.filename = batchFilePath;
14
+
15
+ // Check if batch.json exists
16
+ if (!fs.existsSync(batchFilePath)) {
17
+ console.error(
18
+ 'Error: batch.json file not found in the current directory',
19
+ );
20
+ process.exit(1);
21
+ }
22
+ }
23
+
24
+ async run(
25
+ handleCommand: (batch: Batch, item: any, index: number) => Promise<void>,
26
+ ) {
27
+ try {
28
+ // Read the batch.json file
29
+ const fileContent = fs.readFileSync(this.filename, 'utf-8');
30
+
31
+ // Parse the JSON content
32
+ let batchData;
33
+ try {
34
+ batchData = JSON.parse(fileContent);
35
+ } catch (parseError) {
36
+ console.error('Error: Invalid JSON in batch.json file');
37
+ console.error(parseError);
38
+ process.exit(1);
39
+ }
40
+ console.log('Processing batch.json...');
41
+ console.log('='.repeat(50));
42
+
43
+ // Check if batchData is an array or object
44
+ if (Array.isArray(batchData)) {
45
+ // If it's an array of objects, process each object sequentially
46
+ for (let index = 0; index < batchData.length; index++) {
47
+ // console.log(`Processing item ${index + 1}/${batchData.length}...`);
48
+ const item = batchData[index];
49
+ if (typeof item === 'object' && item !== null) {
50
+ // Print all keys in the object
51
+ await handleCommand(this, item, index + 1);
52
+ } else {
53
+ console.log(` Not an object: ${item}`);
54
+ }
55
+ console.log('');
56
+ }
57
+ } else if (typeof batchData === 'object' && batchData !== null) {
58
+ // If it's a single object, print its keys
59
+ console.log('Root object keys:');
60
+ const keys = Object.keys(batchData);
61
+ keys.forEach((key) => {
62
+ console.log(` Key: ${key}`);
63
+ });
64
+
65
+ // If any values are also objects, print their keys too
66
+ keys.forEach((key) => {
67
+ const value = batchData[key];
68
+ if (
69
+ typeof value === 'object' &&
70
+ value !== null &&
71
+ !Array.isArray(value)
72
+ ) {
73
+ console.log(`\nKeys in "${key}" object:`);
74
+ Object.keys(value).forEach((subKey) => {
75
+ console.log(` Key: ${subKey}`);
76
+ });
77
+ }
78
+ });
79
+ } else {
80
+ console.log(
81
+ 'Error: batch.json should contain an object or array of objects',
82
+ );
83
+ process.exit(1);
84
+ }
85
+ } catch (error) {
86
+ console.error('Error processing batch.json:');
87
+ console.error(error);
88
+ process.exit(1);
89
+ }
90
+ }
91
+
92
+ async handleDrawFrame(item: any, index: number) {
93
+ const coordinates = item.payload.coordinates;
94
+ console.log(` Command[${index}]: MainDrawFrame`);
95
+ await this.selectButtonSafe('FDOptions.button.draw-frame');
96
+
97
+ const coords: { x: number; y: number }[] = coordinates.points;
98
+ for (const point of coords) {
99
+ console.log(` Drawing point: (${point.x}, ${point.y})`);
100
+ // Small delay between points to simulate drawing
101
+ // await new Promise(resolve => setTimeout(resolve, 500));
102
+ await this.sampleClickCanvasDrawAt('canvas.draw', point.x, point.y); // Example click to trigger drawing (replace with actual coordinates if needed)
103
+ }
104
+ await this.selectButtonSafe('Finish.button.start-transformation'); // Example button click after drawing (replace with actual testId if needed)
105
+ }
106
+
107
+ async handleScaleFrame(item: any, index: number) {
108
+ const length = item.payload.length;
109
+ const height = item.payload.height;
110
+ console.log(` Command[${index}]: MainScaleFrame`);
111
+ console.log(
112
+ ` Simulating scale frame with length: ${length}, height: ${height}`,
113
+ );
114
+ await this.selectInput('ScaleInput.input.length', length.toString());
115
+ await this.selectInput('ScaleInput.input.height', height.toString());
116
+ await this.selectButtonSafe('ShowOptions.cbx.scaledBox');
117
+ await this.selectButtonSafe('ScaleForm.button.apply');
118
+ await this.selectButtonSafe('ShowOptions.cbx.points');
119
+ }
120
+
121
+ async handleDefineTrademark(item: any, index: number) {
122
+ const name = item.payload.name;
123
+ console.log(` Command[${index}]: MainDefineTrademark`);
124
+ console.log(` Simulating trademark definition with name: ${name}`);
125
+ await this.selectInput('TrademarkInput.input.name', name);
126
+ await this.selectButtonSafe('TrademarkForm.button.apply');
127
+ }
128
+
129
+ async selectButtonSafe(testId: string): Promise<void> {
130
+ // Small delay to ensure element is fully ready
131
+ await new Promise((resolve) => setTimeout(resolve, 100));
132
+
133
+ // Now click the button
134
+ await this.selectButton(testId);
135
+ }
136
+
137
+ private async sendRequest(
138
+ command: string,
139
+ data?: TUserAction,
140
+ ): Promise<string> {
141
+ let status = 'blocked';
142
+ try {
143
+ const response = await fetch(`http://localhost:${PORT}/send-command`, {
144
+ method: 'POST',
145
+ headers: {
146
+ 'Content-Type': 'application/json',
147
+ },
148
+ body: JSON.stringify({
149
+ command,
150
+ data,
151
+ }),
152
+ });
153
+ // console.log(`response: ${JSON.stringify(response, null)}`);
154
+ // console.log(`response.status: ${response.status}`);
155
+ if (response.status === 200) {
156
+ status = 'ok';
157
+ }
158
+ return status;
159
+ } catch (error) {
160
+ console.log(`error.status: ${status}`);
161
+ console.error('Error:', error);
162
+ }
163
+ console.log(`return status: ${status}`);
164
+ return status;
165
+ }
166
+
167
+ async selectButton(
168
+ testId: string,
169
+ retries: number = 5,
170
+ delay: number = 1000,
171
+ ): Promise<void> {
172
+ const sleep = (ms: number) =>
173
+ new Promise((resolve) => setTimeout(resolve, ms));
174
+
175
+ for (let attempt = 1; attempt <= retries; attempt++) {
176
+ try {
177
+ if (attempt >= 1) {
178
+ console.log(
179
+ `Attempt ${attempt}/${retries} to click button: ${testId}`,
180
+ );
181
+ }
182
+
183
+ const status = await this.sendRequest(Command.SimulateClick, {
184
+ testId,
185
+ });
186
+
187
+ if (status !== 'ok') {
188
+ console.log('Failed to send button simulation command');
189
+ }
190
+ await sleep(delay);
191
+ return; // Success, exit the retry loop
192
+ } catch (error) {
193
+ console.error('Error in main:', error);
194
+ }
195
+ }
196
+ }
197
+
198
+ async selectInput(testId: string, value: string): Promise<void> {
199
+ const status = await this.sendRequest(Command.SimulateInput, {
200
+ testId,
201
+ value,
202
+ });
203
+ if (status !== 'ok') {
204
+ console.log('Failed to send input simulation command');
205
+ }
206
+ }
207
+
208
+ async sampleClickCanvasDrawAt(testId: string, x: number, y: number) {
209
+ console.log(`sampleClickCanvasDrawAt(${x}, ${y})`);
210
+ await this.sendRequest(Command.SimulateClickOnCanvas, {
211
+ testId,
212
+ x,
213
+ y,
214
+ });
215
+ // Small delay to ensure element is fully ready
216
+ await new Promise((resolve) => setTimeout(resolve, 300));
217
+ }
218
+ }
package/command.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export enum Command {
2
2
  SimulateClick = 'SimulateClick',
3
3
  SimulateClickOnCanvas = 'SimulateClickOnCanvas',
4
- MainScaleFrame = 'MainScaleFrame',
4
+ SimulateInput = 'SimulateInput',
5
+ SimulateCheck = 'SimulateUserCheck',
5
6
  }
package/commandHandler.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Command } from './command';
2
2
  import { Simulator } from './simulator';
3
- import { TUserAction, TClick, TClickOnCanvas, TScaleFrame } from './types';
3
+ import { TUserAction, TClick, TClickOnCanvas, TInput } from './types';
4
4
 
5
5
  export function useCommandHandler() {
6
6
  const handleCommand = async (command: string, data: TUserAction) => {
@@ -8,11 +8,13 @@ export function useCommandHandler() {
8
8
 
9
9
  switch (command) {
10
10
  case Command.SimulateClick:
11
- return simulator.simulateUserClick(data as TClick);
11
+ return simulator.simulateClick(data as TClick);
12
12
  case Command.SimulateClickOnCanvas:
13
13
  return simulator.simulateClickOnCanvas(data as TClickOnCanvas);
14
- case Command.MainScaleFrame:
15
- return simulator.simulateScaleFrame(data as TScaleFrame);
14
+ case Command.SimulateInput:
15
+ return simulator.simulateInput(data as TInput);
16
+ case Command.SimulateCheck:
17
+ return simulator.simulateUserCheck(data as TClick);
16
18
  default:
17
19
  console.warn('Unknown command:', command);
18
20
  console.log(`SimulateUserAction command received with data:`, data);
package/draw.json ADDED
@@ -0,0 +1,25 @@
1
+ [
2
+ {
3
+ "command": "main-draw-frame",
4
+ "payload": {
5
+ "coordinates": {
6
+ "points": [
7
+ { "x": 200, "y": 100 },
8
+ { "x": -100, "y": 100 },
9
+ { "x": -60, "y": 0 },
10
+ { "x": -100, "y": -100 },
11
+ { "x": 200, "y": -100 }
12
+ ],
13
+ "universeCenter": { "x": 300, "y": 200 }
14
+ }
15
+ }
16
+ },
17
+ {
18
+ "command": "main-scale-frame",
19
+ "payload": { "length": 500, "height": 250 }
20
+ },
21
+ {
22
+ "command": "main-define-trademark",
23
+ "payload": { "x": -95, "y": 62 }
24
+ }
25
+ ]
package/index.ts CHANGED
@@ -2,3 +2,4 @@ export { Simulator } from './simulator';
2
2
  export { TClick, TClickOnCanvas, TScaleFrame, TUserAction } from './types';
3
3
  export { Command } from './command';
4
4
  export { useCommandHandler } from './commandHandler';
5
+ export { Batch } from './batch';
package/main.ts ADDED
@@ -0,0 +1,73 @@
1
+ import { Batch } from './batch';
2
+
3
+ async function handleDrawFrame(batch: Batch, item: any, index: number) {
4
+ const coordinates = item.payload.coordinates;
5
+ console.log(` Command[${index}]: MainDrawFrame`);
6
+ await batch.selectButtonSafe('FDOptions.button.draw-frame');
7
+
8
+ const coords: { x: number; y: number }[] = coordinates.points;
9
+ for (const point of coords) {
10
+ console.log(` Drawing point: (${point.x}, ${point.y})`);
11
+ // Small delay between points to simulate drawing
12
+ // await new Promise(resolve => setTimeout(resolve, 500));
13
+ await batch.sampleClickCanvasDrawAt('canvas.draw', point.x, point.y); // Example click to trigger drawing (replace with actual coordinates if needed)
14
+ }
15
+ await batch.selectButtonSafe('Finish.button.start-transformation'); // Example button click after drawing (replace with actual testId if needed)
16
+ }
17
+
18
+ async function handleScaleFrame(batch: Batch, item: any, index: number) {
19
+ const length = item.payload.length;
20
+ const height = item.payload.height;
21
+ console.log(` Command[${index}]: MainScaleFrame`);
22
+ console.log(
23
+ ` Simulating scale frame with length: ${length}, height: ${height}`,
24
+ );
25
+ await batch.selectInput('ScaleInput.input.length', length.toString());
26
+ await batch.selectInput('ScaleInput.input.height', height.toString());
27
+ await batch.selectButtonSafe('ShowOptions.cbx.scaledBox');
28
+ await batch.selectButtonSafe('ScaleForm.button.apply');
29
+ await batch.selectButtonSafe('ShowOptions.cbx.points');
30
+ }
31
+
32
+ async function handleDefineTrademark(batch: Batch, item: any, index: number) {
33
+ const name = item.payload.name;
34
+ console.log(` Command[${index}]: MainDefineTrademark`);
35
+ console.log(` Simulating trademark definition with name: ${name}`);
36
+ await batch.selectInput('TrademarkInput.input.name', name);
37
+ await batch.selectButtonSafe('TrademarkForm.button.apply');
38
+ }
39
+
40
+ async function handleCommand(batch: Batch, item: any, index: number) {
41
+ // console.log(JSON.stringify(item, null, 2));
42
+ if (item.command) {
43
+ switch (item.command) {
44
+ case 'main-draw-frame':
45
+ await handleDrawFrame(batch, item, index);
46
+ break;
47
+ case 'main-scale-frame':
48
+ await handleScaleFrame(batch, item, index);
49
+ break;
50
+ case 'main-define-trademark':
51
+ await handleDefineTrademark(batch, item, index);
52
+ break;
53
+ default:
54
+ throw new Error(`Unknown command: ${item.command}`);
55
+ }
56
+ } else {
57
+ console.log('No command found in this item.');
58
+ }
59
+ }
60
+
61
+ async function main(batchFile: string) {
62
+ try {
63
+ const batch = new Batch(batchFile);
64
+ await batch.run(handleCommand);
65
+ } catch (error) {
66
+ console.error('Error in main:', error);
67
+ }
68
+ }
69
+
70
+ // Accept batch file path as command-line argument
71
+ const batchFileArg = process.argv[2] || 'batch.json';
72
+ console.log(`Starting batch processing with file: ${batchFileArg}`);
73
+ main(batchFileArg);
package/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "frame.simulator",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "A simulator for testing form interactions in a controlled environment.",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
7
7
  "scripts": {
8
8
  "build": "tsc -b",
9
- "command-server": "tsx command-server.ts"
9
+ "command-server": "tsx command-server.ts",
10
+ "draw": "tsx main.ts draw.json"
10
11
  },
11
12
  "peerDependencies": {
12
13
  "frame.constants": "^1.0.5"
13
14
  },
14
15
  "devDependencies": {
16
+ "@types/cors": "^2.8.19",
17
+ "@types/express": "^5.0.6",
15
18
  "@types/node": "^25.3.0",
16
19
  "cors": "^2.8.5",
17
20
  "express": "^5.2.1",
package/simulator.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { green, red, reset } from 'frame.constants';
2
- import { TClick, TClickOnCanvas, TScaleFrame } from './types';
2
+ import { TClick, TClickOnCanvas, TInput } from './types';
3
3
 
4
4
  export class Simulator {
5
5
  constructor() {
@@ -55,7 +55,7 @@ export class Simulator {
55
55
  }
56
56
  }
57
57
 
58
- simulateUserClick(data: TClick) {
58
+ simulateClick(data: TClick) {
59
59
  const { testId } = data;
60
60
  const element = this.findHTMLElement(testId, HTMLButtonElement, 'click');
61
61
  if (element) {
@@ -77,7 +77,8 @@ export class Simulator {
77
77
  }
78
78
  }
79
79
 
80
- simulateUserInput(testId: string, value: string) {
80
+ simulateInput(data: TInput) {
81
+ const { testId, value } = data;
81
82
  const element = this.findHTMLElement(testId, HTMLDivElement, 'input');
82
83
 
83
84
  if (element) {
@@ -94,16 +95,4 @@ export class Simulator {
94
95
  }
95
96
  }
96
97
  }
97
-
98
- simulateScaleFrame(scaleValues: TScaleFrame) {
99
- const { length, height } = scaleValues;
100
- console.log(
101
- `${red}Simulating scale frame with length: ${length}, height: ${height}${reset}`,
102
- );
103
- this.simulateUserInput('ScaleInput.input.length', length.toString());
104
- this.simulateUserInput('ScaleInput.input.height', height.toString());
105
- this.simulateUserCheck({ testId: 'ShowOptions.cbx.scaledBox' });
106
- this.simulateUserClick({ testId: 'ScaleForm.button.apply' });
107
- this.simulateUserCheck({ testId: 'ShowOptions.cbx.points' });
108
- }
109
98
  }
package/types.ts CHANGED
@@ -2,6 +2,11 @@ export type TClick = {
2
2
  testId: string;
3
3
  };
4
4
 
5
+ export type TInput = {
6
+ testId: string;
7
+ value: string;
8
+ };
9
+
5
10
  export type TClickOnCanvas = {
6
11
  x: number;
7
12
  y: number;
@@ -12,4 +17,4 @@ export type TScaleFrame = {
12
17
  height: number;
13
18
  };
14
19
 
15
- export type TUserAction = TClickOnCanvas | TScaleFrame | TClick;
20
+ export type TUserAction = TClickOnCanvas | TScaleFrame | TClick | TInput;