|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | 2 | import * as ROSLIB from "../../src/RosLib.ts"; |
3 | 3 |
|
4 | | -// Noetic is the only version of ROS 1 we support, so we skip based on distro name |
5 | | -// instead of adding extra plumbing for ROS_VERSION. |
6 | | -describe.skipIf(process.env["ROS_DISTRO"] !== "noetic")( |
7 | | - "ROS 1 Fibonacci Example", |
8 | | - function () { |
9 | | - it( |
10 | | - "Fibonacci", |
11 | | - () => |
12 | | - new Promise<void>((done) => { |
13 | | - const ros = new ROSLIB.Ros({ |
14 | | - url: "ws://localhost:9090", |
15 | | - }); |
16 | | - /* |
17 | | - * The ActionClient |
18 | | - * ---------------- |
19 | | - */ |
20 | | - |
21 | | - const fibonacciClient = new ROSLIB.ActionClient({ |
22 | | - ros: ros, |
23 | | - serverName: "/fibonacci", |
24 | | - actionName: "actionlib_tutorials/FibonacciAction", |
25 | | - }); |
26 | | - |
27 | | - // Create a goal. |
28 | | - const goal = new ROSLIB.Goal({ |
29 | | - actionClient: fibonacciClient, |
30 | | - goalMessage: { |
31 | | - order: 7, |
32 | | - }, |
33 | | - }); |
34 | | - |
35 | | - // Print out their output into the terminal. |
36 | | - const items = [ |
37 | | - { sequence: [0, 1, 1] }, |
38 | | - { sequence: [0, 1, 1, 2] }, |
39 | | - { sequence: [0, 1, 1, 2, 3] }, |
40 | | - { sequence: [0, 1, 1, 2, 3, 5] }, |
41 | | - { sequence: [0, 1, 1, 2, 3, 5, 8] }, |
42 | | - { sequence: [0, 1, 1, 2, 3, 5, 8, 13] }, |
43 | | - { sequence: [0, 1, 1, 2, 3, 5, 8, 13, 21] }, |
44 | | - ]; |
45 | | - goal.on("feedback", function (feedback) { |
46 | | - expect(feedback).to.eql(items.shift()); |
47 | | - }); |
48 | | - goal.on("result", function (result) { |
49 | | - expect(result).to.eql({ sequence: [0, 1, 1, 2, 3, 5, 8, 13, 21] }); |
| 4 | +const fibonacciItems = [ |
| 5 | + { sequence: [0, 1, 1] }, |
| 6 | + { sequence: [0, 1, 1, 2] }, |
| 7 | + { sequence: [0, 1, 1, 2, 3] }, |
| 8 | + { sequence: [0, 1, 1, 2, 3, 5] }, |
| 9 | + { sequence: [0, 1, 1, 2, 3, 5, 8] }, |
| 10 | + { sequence: [0, 1, 1, 2, 3, 5, 8, 13] }, |
| 11 | + { sequence: [0, 1, 1, 2, 3, 5, 8, 13, 21] }, |
| 12 | +]; |
| 13 | + |
| 14 | +describe("Fibonacci Example", function () { |
| 15 | + // Noetic is the only version of ROS 1 we support, so we skip based on distro name |
| 16 | + // instead of adding extra plumbing for ROS_VERSION. |
| 17 | + it.skipIf(process.env["ROS_DISTRO"] !== "noetic")( |
| 18 | + "Fibonacci ROS 1", |
| 19 | + () => |
| 20 | + new Promise<void>((done) => { |
| 21 | + const ros = new ROSLIB.Ros({ |
| 22 | + url: "ws://localhost:9090", |
| 23 | + }); |
| 24 | + /* |
| 25 | + * The ActionClient |
| 26 | + * ---------------- |
| 27 | + */ |
| 28 | + |
| 29 | + const fibonacciClient = new ROSLIB.ActionClient({ |
| 30 | + ros: ros, |
| 31 | + serverName: "/fibonacci", |
| 32 | + actionName: "actionlib_tutorials/FibonacciAction", |
| 33 | + }); |
| 34 | + |
| 35 | + // Create a goal. |
| 36 | + const goal = new ROSLIB.Goal({ |
| 37 | + actionClient: fibonacciClient, |
| 38 | + goalMessage: { |
| 39 | + order: 7, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + goal.on("feedback", function (feedback) { |
| 44 | + expect(feedback).to.eql(fibonacciItems.shift()); |
| 45 | + }); |
| 46 | + goal.on("result", function (result) { |
| 47 | + expect(result).to.eql({ sequence: [0, 1, 1, 2, 3, 5, 8, 13, 21] }); |
| 48 | + done(); |
| 49 | + }); |
| 50 | + |
| 51 | + /* |
| 52 | + * Send the goal to the action server. |
| 53 | + * The timeout is to allow rosbridge to properly subscribe all the |
| 54 | + * Action topics - otherwise, the first feedback message might get lost |
| 55 | + */ |
| 56 | + setTimeout(function () { |
| 57 | + goal.send(); |
| 58 | + }, 100); |
| 59 | + }), |
| 60 | + 8000, |
| 61 | + ); |
| 62 | + |
| 63 | + it.skipIf(process.env["ROS_DISTRO"] !== "humble")( |
| 64 | + "Fibonacci ROS 2", |
| 65 | + () => |
| 66 | + new Promise<void>((done) => { |
| 67 | + const ros = new ROSLIB.Ros({ |
| 68 | + url: "ws://localhost:9090", |
| 69 | + }); |
| 70 | + /* |
| 71 | + * The Action |
| 72 | + * ---------------- |
| 73 | + */ |
| 74 | + |
| 75 | + const fibonacciAction = new ROSLIB.Action({ |
| 76 | + ros, |
| 77 | + name: "/fibonacci", |
| 78 | + actionType: "action_tutorials_interfaces/action/Fibonacci", |
| 79 | + }); |
| 80 | + |
| 81 | + const goal = { order: 8 }; |
| 82 | + |
| 83 | + /* |
| 84 | + * Send the goal to the action server. |
| 85 | + */ |
| 86 | + fibonacciAction.sendGoal( |
| 87 | + goal, |
| 88 | + (result) => { |
| 89 | + expect(result).to.eql({ |
| 90 | + sequence: [0, 1, 1, 2, 3, 5, 8, 13, 21], |
| 91 | + }); |
50 | 92 | done(); |
51 | | - }); |
52 | | - |
53 | | - /* |
54 | | - * Send the goal to the action server. |
55 | | - * The timeout is to allow rosbridge to properly subscribe all the |
56 | | - * Action topics - otherwise, the first feedback message might get lost |
57 | | - */ |
58 | | - setTimeout(function () { |
59 | | - goal.send(); |
60 | | - }, 100); |
61 | | - }), |
62 | | - 8000, |
63 | | - ); |
64 | | - }, |
65 | | -); |
| 93 | + }, |
| 94 | + (feedback) => { |
| 95 | + expect(feedback).to.eql(fibonacciItems.shift()); |
| 96 | + }, |
| 97 | + ); |
| 98 | + }), |
| 99 | + 8000, |
| 100 | + ); |
| 101 | + |
| 102 | + it.skipIf(process.env["ROS_DISTRO"] !== "humble")( |
| 103 | + "Fibonacci ROS 2, cancel all goals", |
| 104 | + async () => { |
| 105 | + const ros = new ROSLIB.Ros(); |
| 106 | + await ros.connect("ws://localhost:9090"); |
| 107 | + |
| 108 | + let resultCalled = false; |
| 109 | + let failedCalled = false; |
| 110 | + |
| 111 | + /* |
| 112 | + * The Action |
| 113 | + * ---------------- |
| 114 | + */ |
| 115 | + const fibonacciAction = new ROSLIB.Action({ |
| 116 | + ros, |
| 117 | + name: "/fibonacci", |
| 118 | + actionType: "action_tutorials_interfaces/action/Fibonacci", |
| 119 | + }); |
| 120 | + |
| 121 | + const goal = { order: 8 }; |
| 122 | + |
| 123 | + /* |
| 124 | + * Send the goal to the action server. |
| 125 | + */ |
| 126 | + fibonacciAction.sendGoal( |
| 127 | + goal, |
| 128 | + // result callback. |
| 129 | + () => { |
| 130 | + resultCalled = true; |
| 131 | + }, |
| 132 | + // feedback callback. |
| 133 | + undefined, |
| 134 | + // failed callback |
| 135 | + () => { |
| 136 | + failedCalled = true; |
| 137 | + }, |
| 138 | + ); |
| 139 | + |
| 140 | + /* |
| 141 | + * Cancel all goals. |
| 142 | + */ |
| 143 | + fibonacciAction.cancelAllGoals(); |
| 144 | + |
| 145 | + setTimeout(() => { |
| 146 | + expect(failedCalled).toBe(true); |
| 147 | + expect(resultCalled).toBe(false); |
| 148 | + }, 500); // wait 500ms to be sure the server handled the cancel request. |
| 149 | + }, |
| 150 | + ); |
| 151 | +}); |
0 commit comments