linuxcnc-grpc 0.6.0-beta.1

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.
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Integration tests for LinuxCNC gRPC TypeScript client.
3
+ *
4
+ * These tests connect to a Python mock server for cross-language validation.
5
+ */
6
+ export {};
@@ -0,0 +1,292 @@
1
+ "use strict";
2
+ /**
3
+ * Integration tests for LinuxCNC gRPC TypeScript client.
4
+ *
5
+ * These tests connect to a Python mock server for cross-language validation.
6
+ */
7
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8
+ if (k2 === undefined) k2 = k;
9
+ var desc = Object.getOwnPropertyDescriptor(m, k);
10
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11
+ desc = { enumerable: true, get: function() { return m[k]; } };
12
+ }
13
+ Object.defineProperty(o, k2, desc);
14
+ }) : (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ o[k2] = m[k];
17
+ }));
18
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
19
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
20
+ }) : function(o, v) {
21
+ o["default"] = v;
22
+ });
23
+ var __importStar = (this && this.__importStar) || (function () {
24
+ var ownKeys = function(o) {
25
+ ownKeys = Object.getOwnPropertyNames || function (o) {
26
+ var ar = [];
27
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
28
+ return ar;
29
+ };
30
+ return ownKeys(o);
31
+ };
32
+ return function (mod) {
33
+ if (mod && mod.__esModule) return mod;
34
+ var result = {};
35
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
36
+ __setModuleDefault(result, mod);
37
+ return result;
38
+ };
39
+ })();
40
+ Object.defineProperty(exports, "__esModule", { value: true });
41
+ const child_process_1 = require("child_process");
42
+ const path = __importStar(require("path"));
43
+ const fs = __importStar(require("fs"));
44
+ const grpc = __importStar(require("@grpc/grpc-js"));
45
+ const vitest_1 = require("vitest");
46
+ const linuxcnc_1 = require("../linuxcnc");
47
+ const hal_1 = require("../hal");
48
+ // Load fixtures
49
+ const fixturesPath = path.join(__dirname, '../../../../tests/fixtures');
50
+ const linuxcncFixture = JSON.parse(fs.readFileSync(path.join(fixturesPath, 'linuxcnc_status.json'), 'utf-8'));
51
+ const halFixture = JSON.parse(fs.readFileSync(path.join(fixturesPath, 'hal_status.json'), 'utf-8'));
52
+ // Test configuration
53
+ const MOCK_SERVER_PORT = 50097;
54
+ const MOCK_SERVER_ADDRESS = `localhost:${MOCK_SERVER_PORT}`;
55
+ const MOCK_SERVER_SCRIPT = path.join(__dirname, '../../../../tests/mock_server.py');
56
+ let mockServer;
57
+ let linuxcncClient;
58
+ let halClient;
59
+ // Helper to promisify gRPC client calls
60
+ function promisify(fn) {
61
+ return (req) => new Promise((resolve, reject) => {
62
+ fn(req, (err, res) => {
63
+ if (err)
64
+ reject(err);
65
+ else
66
+ resolve(res);
67
+ });
68
+ });
69
+ }
70
+ (0, vitest_1.beforeAll)(async () => {
71
+ // Start mock server
72
+ mockServer = (0, child_process_1.spawn)('python3', [MOCK_SERVER_SCRIPT, '--port', String(MOCK_SERVER_PORT)], {
73
+ stdio: ['pipe', 'pipe', 'pipe'],
74
+ });
75
+ // Wait for ready signal
76
+ await new Promise((resolve, reject) => {
77
+ const timeout = setTimeout(() => reject(new Error('Mock server startup timeout')), 10000);
78
+ mockServer.stdout?.on('data', (data) => {
79
+ if (data.toString().includes('READY:')) {
80
+ clearTimeout(timeout);
81
+ resolve();
82
+ }
83
+ });
84
+ mockServer.on('error', (err) => {
85
+ clearTimeout(timeout);
86
+ reject(err);
87
+ });
88
+ });
89
+ // Create clients
90
+ const credentials = grpc.credentials.createInsecure();
91
+ linuxcncClient = new linuxcnc_1.LinuxCNCServiceClient(MOCK_SERVER_ADDRESS, credentials);
92
+ halClient = new hal_1.HalServiceClient(MOCK_SERVER_ADDRESS, credentials);
93
+ });
94
+ (0, vitest_1.afterAll)(() => {
95
+ // Close clients
96
+ linuxcncClient?.close();
97
+ halClient?.close();
98
+ // Kill mock server
99
+ if (mockServer) {
100
+ mockServer.kill();
101
+ }
102
+ });
103
+ // =============================================================================
104
+ // LinuxCNC Service Tests
105
+ // =============================================================================
106
+ (0, vitest_1.describe)('LinuxCNCService', () => {
107
+ (0, vitest_1.it)('GetStatus returns expected mock data', async () => {
108
+ const getStatus = promisify(linuxcncClient.getStatus.bind(linuxcncClient));
109
+ const status = await getStatus(linuxcnc_1.GetStatusRequest.create());
110
+ // Verify task status
111
+ (0, vitest_1.expect)(status.task?.taskMode).toBe(linuxcnc_1.TaskMode.MODE_MANUAL);
112
+ (0, vitest_1.expect)(status.task?.taskState).toBe(linuxcnc_1.TaskState.STATE_ON);
113
+ (0, vitest_1.expect)(status.task?.execState).toBe(linuxcnc_1.ExecState.EXEC_DONE);
114
+ (0, vitest_1.expect)(status.task?.echoSerialNumber).toBe(linuxcncFixture.task.echo_serial_number);
115
+ // Verify trajectory
116
+ (0, vitest_1.expect)(status.trajectory?.joints).toBe(linuxcncFixture.trajectory.joints);
117
+ (0, vitest_1.expect)(status.trajectory?.enabled).toBe(linuxcncFixture.trajectory.enabled);
118
+ // Verify position
119
+ (0, vitest_1.expect)(status.position?.actualPosition?.x).toBe(linuxcncFixture.position.actual_position.x);
120
+ (0, vitest_1.expect)(status.position?.actualPosition?.y).toBe(linuxcncFixture.position.actual_position.y);
121
+ (0, vitest_1.expect)(status.position?.actualPosition?.z).toBe(linuxcncFixture.position.actual_position.z);
122
+ // Verify joints
123
+ (0, vitest_1.expect)(status.joints).toHaveLength(3);
124
+ status.joints?.forEach((joint, i) => {
125
+ (0, vitest_1.expect)(joint.homed).toBe(linuxcncFixture.joints[i].homed);
126
+ });
127
+ // Verify tool
128
+ (0, vitest_1.expect)(status.tool?.toolInSpindle).toBe(linuxcncFixture.tool.tool_in_spindle);
129
+ });
130
+ (0, vitest_1.it)('SendCommand with state command succeeds', async () => {
131
+ const sendCommand = promisify(linuxcncClient.sendCommand.bind(linuxcncClient));
132
+ const command = linuxcnc_1.LinuxCNCCommand.create({
133
+ serial: 100,
134
+ state: { state: linuxcnc_1.TaskState.STATE_ON },
135
+ });
136
+ const response = await sendCommand(command);
137
+ (0, vitest_1.expect)(response.serial).toBe(100);
138
+ (0, vitest_1.expect)(response.status).toBe(linuxcnc_1.RcsStatus.RCS_DONE);
139
+ });
140
+ (0, vitest_1.it)('SendCommand with MDI command succeeds', async () => {
141
+ const sendCommand = promisify(linuxcncClient.sendCommand.bind(linuxcncClient));
142
+ const command = linuxcnc_1.LinuxCNCCommand.create({
143
+ serial: 101,
144
+ mdi: { command: 'G0 X10 Y10' },
145
+ });
146
+ const response = await sendCommand(command);
147
+ (0, vitest_1.expect)(response.serial).toBe(101);
148
+ (0, vitest_1.expect)(response.status).toBe(linuxcnc_1.RcsStatus.RCS_DONE);
149
+ });
150
+ (0, vitest_1.it)('SendCommand with jog command succeeds', async () => {
151
+ const sendCommand = promisify(linuxcncClient.sendCommand.bind(linuxcncClient));
152
+ const command = linuxcnc_1.LinuxCNCCommand.create({
153
+ serial: 102,
154
+ jog: {
155
+ type: linuxcnc_1.JogType.JOG_CONTINUOUS,
156
+ isJoint: false,
157
+ index: 0,
158
+ velocity: 10.0,
159
+ },
160
+ });
161
+ const response = await sendCommand(command);
162
+ (0, vitest_1.expect)(response.serial).toBe(102);
163
+ (0, vitest_1.expect)(response.status).toBe(linuxcnc_1.RcsStatus.RCS_DONE);
164
+ });
165
+ (0, vitest_1.it)('WaitComplete returns immediately in mock', async () => {
166
+ const waitComplete = promisify(linuxcncClient.waitComplete.bind(linuxcncClient));
167
+ const request = linuxcnc_1.WaitCompleteRequest.create({
168
+ serial: 50,
169
+ timeout: 5.0,
170
+ });
171
+ const response = await waitComplete(request);
172
+ (0, vitest_1.expect)(response.serial).toBe(50);
173
+ (0, vitest_1.expect)(response.status).toBe(linuxcnc_1.RcsStatus.RCS_DONE);
174
+ });
175
+ (0, vitest_1.it)('StreamStatus returns multiple updates', async () => {
176
+ const stream = linuxcncClient.streamStatus(linuxcnc_1.StreamStatusRequest.create({ intervalMs: 50 }));
177
+ const updates = [];
178
+ await new Promise((resolve, reject) => {
179
+ const timeout = setTimeout(() => {
180
+ stream.cancel();
181
+ resolve();
182
+ }, 500);
183
+ stream.on('data', (status) => {
184
+ updates.push(status);
185
+ if (updates.length >= 3) {
186
+ clearTimeout(timeout);
187
+ stream.cancel();
188
+ resolve();
189
+ }
190
+ });
191
+ stream.on('error', (err) => {
192
+ if (err.code !== grpc.status.CANCELLED) {
193
+ clearTimeout(timeout);
194
+ reject(err);
195
+ }
196
+ });
197
+ });
198
+ (0, vitest_1.expect)(updates.length).toBeGreaterThanOrEqual(3);
199
+ updates.forEach((status) => {
200
+ (0, vitest_1.expect)(status.task?.taskMode).toBe(linuxcnc_1.TaskMode.MODE_MANUAL);
201
+ });
202
+ });
203
+ });
204
+ // =============================================================================
205
+ // HAL Service Tests
206
+ // =============================================================================
207
+ (0, vitest_1.describe)('HalService', () => {
208
+ (0, vitest_1.it)('GetSystemStatus returns expected mock data', async () => {
209
+ const getSystemStatus = promisify(halClient.getSystemStatus.bind(halClient));
210
+ const status = await getSystemStatus(hal_1.GetSystemStatusRequest.create());
211
+ // Verify system info
212
+ (0, vitest_1.expect)(status.isSim).toBe(halFixture.is_sim);
213
+ (0, vitest_1.expect)(status.isUserspace).toBe(halFixture.is_userspace);
214
+ (0, vitest_1.expect)(status.kernelVersion).toBe(halFixture.kernel_version);
215
+ // Verify pins
216
+ (0, vitest_1.expect)(status.pins).toHaveLength(halFixture.pins.length);
217
+ // Verify signals
218
+ (0, vitest_1.expect)(status.signals).toHaveLength(halFixture.signals.length);
219
+ // Verify components
220
+ (0, vitest_1.expect)(status.components).toHaveLength(halFixture.components.length);
221
+ });
222
+ (0, vitest_1.it)('QueryPins with wildcard returns all pins', async () => {
223
+ const queryPins = promisify(halClient.queryPins.bind(halClient));
224
+ const response = await queryPins(hal_1.QueryPinsCommand.create({ pattern: '*' }));
225
+ (0, vitest_1.expect)(response.success).toBe(true);
226
+ (0, vitest_1.expect)(response.pins).toHaveLength(3);
227
+ });
228
+ (0, vitest_1.it)('QueryPins with pattern filters correctly', async () => {
229
+ const queryPins = promisify(halClient.queryPins.bind(halClient));
230
+ const response = await queryPins(hal_1.QueryPinsCommand.create({ pattern: 'axis.*' }));
231
+ (0, vitest_1.expect)(response.success).toBe(true);
232
+ (0, vitest_1.expect)(response.pins).toHaveLength(1);
233
+ (0, vitest_1.expect)(response.pins[0].name).toBe('axis.x.pos-cmd');
234
+ });
235
+ (0, vitest_1.it)('QuerySignals returns all signals', async () => {
236
+ const querySignals = promisify(halClient.querySignals.bind(halClient));
237
+ const response = await querySignals(hal_1.QuerySignalsCommand.create({ pattern: '*' }));
238
+ (0, vitest_1.expect)(response.success).toBe(true);
239
+ (0, vitest_1.expect)(response.signals).toHaveLength(2);
240
+ });
241
+ (0, vitest_1.it)('QueryParams returns all params', async () => {
242
+ const queryParams = promisify(halClient.queryParams.bind(halClient));
243
+ const response = await queryParams(hal_1.QueryParamsCommand.create({ pattern: '*' }));
244
+ (0, vitest_1.expect)(response.success).toBe(true);
245
+ (0, vitest_1.expect)(response.params).toHaveLength(3);
246
+ });
247
+ (0, vitest_1.it)('QueryComponents returns all components', async () => {
248
+ const queryComponents = promisify(halClient.queryComponents.bind(halClient));
249
+ const response = await queryComponents(hal_1.QueryComponentsCommand.create({ pattern: '*' }));
250
+ (0, vitest_1.expect)(response.success).toBe(true);
251
+ (0, vitest_1.expect)(response.components).toHaveLength(3);
252
+ const names = response.components.map((c) => c.name);
253
+ (0, vitest_1.expect)(names).toContain('axis');
254
+ (0, vitest_1.expect)(names).toContain('spindle');
255
+ (0, vitest_1.expect)(names).toContain('iocontrol');
256
+ });
257
+ (0, vitest_1.it)('GetValue returns correct value', async () => {
258
+ const getValue = promisify(halClient.getValue.bind(halClient));
259
+ const response = await getValue(hal_1.GetValueCommand.create({ name: 'axis.x.pos-cmd' }));
260
+ (0, vitest_1.expect)(response.success).toBe(true);
261
+ (0, vitest_1.expect)(response.type).toBe(hal_1.HalType.HAL_FLOAT);
262
+ (0, vitest_1.expect)(response.value?.floatValue).toBe(123.456);
263
+ });
264
+ (0, vitest_1.it)('StreamStatus returns multiple updates', async () => {
265
+ const stream = halClient.streamStatus(hal_1.HalStreamStatusRequest.create({ intervalMs: 50 }));
266
+ const updates = [];
267
+ await new Promise((resolve, reject) => {
268
+ const timeout = setTimeout(() => {
269
+ stream.cancel();
270
+ resolve();
271
+ }, 500);
272
+ stream.on('data', (status) => {
273
+ updates.push(status);
274
+ if (updates.length >= 3) {
275
+ clearTimeout(timeout);
276
+ stream.cancel();
277
+ resolve();
278
+ }
279
+ });
280
+ stream.on('error', (err) => {
281
+ if (err.code !== grpc.status.CANCELLED) {
282
+ clearTimeout(timeout);
283
+ reject(err);
284
+ }
285
+ });
286
+ });
287
+ (0, vitest_1.expect)(updates.length).toBeGreaterThanOrEqual(3);
288
+ updates.forEach((status) => {
289
+ (0, vitest_1.expect)(status.isSim).toBe(true);
290
+ });
291
+ });
292
+ });