@7365admin1/core 3.94.0 → 3.95.0

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,297 @@
1
+ import assert from "node:assert/strict";
2
+ import { readFileSync } from "node:fs";
3
+ import { test } from "node:test";
4
+
5
+ import {
6
+ OPEN_RMF_BRAND,
7
+ OPEN_RMF_CAPABILITIES,
8
+ OPEN_RMF_DISPLAY_NAME,
9
+ buildingFromRmf,
10
+ createOpenRmfAdapter,
11
+ createOpenRmfClient,
12
+ floorNoOf,
13
+ openRmfConfig,
14
+ openRmfDoorDriver,
15
+ openRmfLiftDriver,
16
+ registerOpenRmfBrand,
17
+ } from "./.build/services/open-rmf.adapter.mjs";
18
+ import { DOOR_MODE, isDoorPassable } from "./.build/utils/door-contract.util.mjs";
19
+ import { robotTier, commandSupported } from "./.build/utils/robot-adapter.util.mjs";
20
+
21
+ /* --------------------------------------------------------------------------
22
+ * Fixtures were recorded from a real Open-RMF server (rmf_demos "hotel",
23
+ * rmf-web api-server jazzy-nightly, 2026-09-24) while a patrol, two clean tasks
24
+ * and a cross-floor task ran - see Project-Notes 2026-09-24-OPEN-RMF-trial.md.
25
+ * ----------------------------------------------------------------------- */
26
+
27
+ const fx = (n) => JSON.parse(readFileSync(new URL(`./fixtures/open-rmf/${n}.json`, import.meta.url), "utf8"));
28
+ const CONFIG = { baseUrl: "http://rmf.test:8000", token: "t0k" };
29
+
30
+ /** A fetch that answers from the fixtures and records every call. */
31
+ function fakeRmf(overrides = {}) {
32
+ const calls = [];
33
+ const routes = {
34
+ "GET /fleets": () => fx("fleets"),
35
+ "GET /building_map": () => fx("building_map"),
36
+ "GET /doors/L1_door2/state": () => fx("door_state"),
37
+ "GET /lifts/Lift2/state": () => fx("lift_state"),
38
+ "POST /tasks/robot_task": () => fx("robot_task_response"),
39
+ "POST /tasks/interrupt_task": () => ({ success: true, token: "tok-1" }),
40
+ "POST /tasks/resume_task": () => ({ success: true }),
41
+ "POST /tasks/cancel_task": () => ({ success: true }),
42
+ "POST /doors/L1_door2/request": () => null,
43
+ "POST /lifts/Lift2/request": () => null,
44
+ ...overrides,
45
+ };
46
+ const fetch = async (url, init) => {
47
+ const path = url.slice(CONFIG.baseUrl.length);
48
+ calls.push({ method: init.method, path, headers: init.headers, body: init.body ? JSON.parse(init.body) : undefined });
49
+ const key = `${init.method} ${path}`;
50
+ const hit = routes[key] ?? (/^GET \/tasks\/[^/]+\/state$/.test(key) ? () => fx("task_state_patrol") : null);
51
+ if (!hit) return { ok: false, status: 404, text: async () => '{"detail":"Not Found"}' };
52
+ const body = hit();
53
+ if (body && body.__status) return { ok: false, status: body.__status, text: async () => "" };
54
+ return { ok: true, status: 200, text: async () => (body === null ? "" : JSON.stringify(body)) };
55
+ };
56
+ return { fetch, calls };
57
+ }
58
+
59
+ // Every test gets its own base URL: the client shares a short cache per server.
60
+ let n = 0;
61
+ const adapterWith = (overrides) => {
62
+ const rmf = fakeRmf(overrides);
63
+ const config = { ...CONFIG, baseUrl: `http://rmf${++n}.test:8000` };
64
+ const fetch = (url, init) => rmf.fetch(CONFIG.baseUrl + url.slice(config.baseUrl.length), init);
65
+ return { adapter: createOpenRmfAdapter({ config, fetch }), calls: rmf.calls, config, fetch };
66
+ };
67
+
68
+ test("config: no URL means no Open-RMF, and the brand stays unregistered (production is dormant)", () => {
69
+ assert.equal(openRmfConfig({}), null);
70
+ assert.equal(openRmfConfig({ OPEN_RMF_API_URL: "not a url" }), null);
71
+ assert.deepEqual(openRmfConfig({ OPEN_RMF_API_URL: " http://10.0.0.5:8000/ ", OPEN_RMF_TOKEN: " x " }), {
72
+ baseUrl: "http://10.0.0.5:8000",
73
+ token: "x",
74
+ });
75
+ assert.equal(registerOpenRmfBrand({}), false);
76
+ });
77
+
78
+ test("fails closed: an unconfigured client calls nothing and says why", async () => {
79
+ let called = false;
80
+ const client = createOpenRmfClient(null, { fetch: async () => ((called = true), { ok: true, status: 200, text: async () => "[]" }) });
81
+ await assert.rejects(client.fleets(), /not configured/);
82
+ assert.equal(called, false);
83
+ });
84
+
85
+ test("bounded: a server that never answers times out with a sentence", async () => {
86
+ const hang = (_url, init) =>
87
+ new Promise((_, reject) => init.signal.addEventListener("abort", () => reject(Object.assign(new Error("t"), { name: "TimeoutError" }))));
88
+ const client = createOpenRmfClient({ ...CONFIG, baseUrl: "http://hang.test" }, { fetch: hang, timeoutMs: 50 });
89
+ // AbortSignal.timeout's timer does not hold the event loop open (a real socket does), so the test holds it.
90
+ const hold = setTimeout(() => {}, 5000);
91
+ try {
92
+ await assert.rejects(client.fleets(), /did not answer \(timed out\)/);
93
+ } finally {
94
+ clearTimeout(hold);
95
+ }
96
+ });
97
+
98
+ test("an HTTP error is refused by name, and the bearer is sent", async () => {
99
+ const { adapter, calls } = adapterWith({ "GET /fleets": () => ({ __status: 401 }) });
100
+ await assert.rejects(adapter.listRobots(), /refused GET \/fleets \(HTTP 401\)/);
101
+ assert.equal(calls[0].headers.Authorization, "Bearer t0k");
102
+ });
103
+
104
+ test("capabilities are what the api-server offers: Traffic Light, no point navigation", () => {
105
+ assert.equal(robotTier(OPEN_RMF_CAPABILITIES), "traffic-light");
106
+ assert.equal(commandSupported(OPEN_RMF_CAPABILITIES, "navigate"), false);
107
+ assert.equal(commandSupported(OPEN_RMF_CAPABILITIES, "spot"), false);
108
+ for (const k of ["full", "area", "return", "pause", "resume", "cancel"]) assert.equal(commandSupported(OPEN_RMF_CAPABILITIES, k), true, k);
109
+ });
110
+
111
+ test("robots: every fleet's robots, keyed fleet/robot, with battery, floor and a plan share", async () => {
112
+ const { adapter } = adapterWith();
113
+ const robots = await adapter.listRobots();
114
+ assert.deepEqual(robots.map((r) => r.robotSn).sort(), [
115
+ "cleanerBotA/cleanerBotA_1",
116
+ "cleanerBotA/cleanerBotA_2",
117
+ "deliveryRobot/deliveryBot_1",
118
+ "tinyRobot/tinyBot_1",
119
+ ]);
120
+ for (const r of robots) {
121
+ assert.equal(r.online, true);
122
+ assert.ok(r.batteryPercent >= 0 && r.batteryPercent <= 100);
123
+ assert.ok([1, 2, 3].includes(r.floorNo) && r.floorKnown);
124
+ assert.ok(r.planPosition.x >= 0 && r.planPosition.x <= 100 && r.planPosition.y >= 0 && r.planPosition.y <= 100);
125
+ assert.ok(["IDLE", "WORKING", "CHARGING_CYCLE", "CHARGED", "PAUSED"].includes(r.mode), r.mode);
126
+ }
127
+ assert.equal(await adapter.getRobot("tinyRobot/nobody"), null);
128
+ assert.equal(await adapter.getRobot("no-slash"), null);
129
+ });
130
+
131
+ test("floors: L/B/G names number like our maps", () => {
132
+ assert.equal(floorNoOf("L1"), 1);
133
+ assert.equal(floorNoOf("B2"), -2);
134
+ assert.equal(floorNoOf("G"), 0);
135
+ assert.equal(floorNoOf("roof"), null);
136
+ });
137
+
138
+ test("commands: a cleaner's clean-all is a composed clean of its graph's zones", async () => {
139
+ const { adapter, calls } = adapterWith();
140
+ await adapter.sendCommand("cleanerBotA/cleanerBotA_1", { kind: "full" });
141
+ const body = calls.find((c) => c.path === "/tasks/robot_task").body;
142
+ assert.equal(body.type, "robot_task_request");
143
+ assert.equal(body.fleet, "cleanerBotA");
144
+ assert.equal(body.robot, "cleanerBotA_1");
145
+ assert.equal(body.request.category, "compose");
146
+ assert.equal(body.request.unix_millis_earliest_start_time, undefined, "no wall-clock start on a sim-clock server");
147
+ const zones = body.request.description.phases[0].activity.description.activities
148
+ .filter((a) => a.category === "perform_action")
149
+ .map((a) => a.description.description.zone)
150
+ .sort();
151
+ assert.deepEqual(zones, ["clean_lobby", "clean_restaurant", "clean_waiting_area"]);
152
+ });
153
+
154
+ test("commands: area clean refuses a zone that is not on the robot's graph", async () => {
155
+ const { adapter } = adapterWith();
156
+ await assert.rejects(adapter.sendCommand("cleanerBotA/cleanerBotA_1", { kind: "area", areas: ["kitchen"] }), /Unknown cleaning zones: kitchen/);
157
+ });
158
+
159
+ test("commands: a patrol robot's 'full' patrols its level's named places; 'area' may cross floors", async () => {
160
+ const { adapter, calls } = adapterWith();
161
+ await adapter.sendCommand("tinyRobot/tinyBot_1", { kind: "full" });
162
+ await adapter.sendCommand("deliveryRobot/deliveryBot_1", { kind: "area", areas: ["L2_room1"] });
163
+ const [a, b] = calls.filter((c) => c.path === "/tasks/robot_task").map((c) => c.body.request);
164
+ assert.equal(a.category, "patrol");
165
+ assert.deepEqual([...a.description.places].sort(), ["lobby", "restaurant", "shop"]);
166
+ assert.deepEqual(b.description, { places: ["L2_room1"], rounds: 1 });
167
+ });
168
+
169
+ test("commands: return goes to the charger; pause/resume/cancel act on the current task", async () => {
170
+ const { adapter, calls } = adapterWith();
171
+ await adapter.sendCommand("tinyRobot/tinyBot_1", { kind: "return" });
172
+ const ret = calls.find((c) => c.path === "/tasks/robot_task").body.request;
173
+ assert.equal(ret.description.phases[0].activity.description.activities[0].description, "tinybot_charger");
174
+
175
+ const fleets = fx("fleets");
176
+ const working = fleets.flatMap((f) => Object.entries(f.robots).map(([r, s]) => ({ sn: `${f.name}/${r}`, s }))).find((x) => x.s.task_id);
177
+ await adapter.sendCommand(working.sn, { kind: "pause" });
178
+ // The recorded task was never interrupted, so there is nothing to resume.
179
+ await assert.rejects(adapter.sendCommand(working.sn, { kind: "resume" }), /not paused/);
180
+ const paused = adapterWith({
181
+ "GET /fleets": () => fx("fleets"),
182
+ [`GET /tasks/${working.s.task_id}/state`]: () => ({ ...fx("task_state_patrol"), status: "standby", interruptions: { "tok-1": { labels: [] }, "tok-0": { resumed_by: { labels: [] } } } }),
183
+ });
184
+ assert.equal((await paused.adapter.getRobot(working.sn)).mode, "PAUSED");
185
+ await paused.adapter.sendCommand(working.sn, { kind: "resume" });
186
+ assert.deepEqual(paused.calls.find((c) => c.path === "/tasks/resume_task").body, {
187
+ type: "resume_task_request",
188
+ for_task: working.s.task_id,
189
+ for_tokens: ["tok-1"],
190
+ labels: ["iservice365"],
191
+ });
192
+ await adapter.sendCommand(working.sn, { kind: "cancel" });
193
+ assert.deepEqual(calls.find((c) => c.path === "/tasks/interrupt_task").body, {
194
+ type: "interrupt_task_request",
195
+ task_id: working.s.task_id,
196
+ labels: ["iservice365"],
197
+ });
198
+ assert.equal(calls.find((c) => c.path === "/tasks/cancel_task").body.task_id, working.s.task_id);
199
+
200
+ const idle = fleets.flatMap((f) => Object.entries(f.robots).map(([r, s]) => ({ sn: `${f.name}/${r}`, s }))).find((x) => !x.s.task_id);
201
+ if (idle) await assert.rejects(adapter.sendCommand(idle.sn, { kind: "pause" }), /no task to pause/);
202
+ await assert.rejects(adapter.sendCommand("tinyRobot/tinyBot_1", { kind: "navigate", x: 1, y: 1 }), /do not take "navigate"/);
203
+ });
204
+
205
+ test("a refused task is an error, not a silent success", async () => {
206
+ const { adapter } = adapterWith({ "POST /tasks/robot_task": () => ({ success: false, errors: [{ code: 1 }] }) });
207
+ await assert.rejects(adapter.sendCommand("tinyRobot/tinyBot_1", { kind: "full" }), /did not accept/);
208
+ });
209
+
210
+ test("patrol routes come from the task phases, with the active one current", async () => {
211
+ const { adapter } = adapterWith();
212
+ const plan = await adapter.getPatrolRoutes("tinyRobot/tinyBot_1");
213
+ const fleets = fx("fleets");
214
+ if (!fleets.find((f) => f.name === "tinyRobot").robots.tinyBot_1.task_id) return; // recorded idle: nothing to show
215
+ assert.equal(plan.routes.length, 1);
216
+ assert.deepEqual(plan.routes[0].checkpoints.map((c) => c.name), ["lobby", "restaurant"]);
217
+ assert.equal(plan.routes[0].currentCheckpoint, 1);
218
+ assert.equal(plan.routes[0].floorNo, 1);
219
+ });
220
+
221
+ test("building: RMF levels/doors/lifts in our map shape, nothing stored", () => {
222
+ const b = buildingFromRmf(fx("building_map"));
223
+ assert.deepEqual(b.levels.map((l) => [l.floorNo, l.name, l.elevationM]), [
224
+ [1, "L1", 0],
225
+ [2, "L2", 8],
226
+ [3, "L3", 16],
227
+ ]);
228
+ assert.equal(b.doors.length, 19);
229
+ assert.ok(b.doors.every((d) => ["hinged", "sliding"].includes(d.type) && [1, 2, 3].includes(d.floorNo)));
230
+ assert.deepEqual(b.lifts.map((l) => [l.name, l.floors]), [
231
+ ["Lift1", [1, 2, 3]],
232
+ ["Lift2", [1, 2, 3]],
233
+ ]);
234
+ });
235
+
236
+ test("door driver: RMF DoorState -> our contract; only open/close may be requested", async () => {
237
+ const { config, fetch, calls } = adapterWith();
238
+ const client = createOpenRmfClient(config, { fetch });
239
+ const door = openRmfDoorDriver(client, "L1_door2");
240
+ const s = await door.read();
241
+ assert.deepEqual(s, { doorName: "L1_door2", doorTime: 152090, currentMode: DOOR_MODE.OPEN });
242
+ assert.equal(isDoorPassable(s), true);
243
+ assert.equal(isDoorPassable({ ...s, currentMode: DOOR_MODE.MOVING }), false);
244
+ await door.request({ doorName: "L1_door2", requesterId: "x", requestTime: 0, requestedMode: DOOR_MODE.CLOSED });
245
+ assert.deepEqual(calls.at(-1).body, { mode: 0 });
246
+ await assert.rejects(door.request({ doorName: "L1_door2", requesterId: "x", requestTime: 0, requestedMode: DOOR_MODE.MOVING }), /open or close/);
247
+ assert.equal(await openRmfDoorDriver(client, "nope").read(), null);
248
+ });
249
+
250
+ test("lift driver: RMF LiftState -> our contract; release ends our session too", async () => {
251
+ const { config, fetch, calls } = adapterWith();
252
+ const client = createOpenRmfClient(config, { fetch });
253
+ const lift = openRmfLiftDriver(client, "Lift2");
254
+ const s = await lift.read();
255
+ assert.equal(s.liftName, "Lift2");
256
+ assert.deepEqual(s.availableFloors, ["L3", "L2", "L1"]);
257
+ assert.equal(s.currentFloor, "L2");
258
+ assert.equal(s.currentMode, 2);
259
+ await lift.request({ liftName: "Lift2", sessionId: "journey-1", requestType: 1, destinationFloor: "L3", doorState: 2 });
260
+ await lift.refresh("journey-1");
261
+ await lift.release("journey-1");
262
+ const sent = calls.filter((c) => c.path === "/lifts/Lift2/request").map((c) => c.body);
263
+ assert.deepEqual(sent[0], { request_type: 1, door_mode: 2, destination: "L3", additional_session_ids: ["journey-1"] });
264
+ assert.deepEqual(sent[1], { request_type: 1, door_mode: 2, destination: "L3", additional_session_ids: ["journey-1"] });
265
+ assert.deepEqual(sent[2], { request_type: 0, door_mode: 0, destination: "", additional_session_ids: ["journey-1"] });
266
+ await lift.refresh("journey-1"); // after release: nothing sent
267
+ assert.equal(calls.filter((c) => c.path === "/lifts/Lift2/request").length, 3);
268
+ });
269
+
270
+ test("client-facing name: the brand never names the upstream project", () => {
271
+ assert.equal(OPEN_RMF_BRAND, "fleet-core");
272
+ assert.equal(OPEN_RMF_DISPLAY_NAME, "HERO Fleet Core");
273
+ });
274
+
275
+ test("every request carries what the mirrored rmf_api_msgs schema requires", async () => {
276
+ const schema = (n) => fx(`schemas/${n}`);
277
+ const required = (n) => schema(n).required ?? schema(n).require ?? [];
278
+ const { adapter, calls } = adapterWith({
279
+ [`GET /tasks/${fx("fleets")[0].robots.cleanerBotA_2.task_id}/state`]: () => ({ ...fx("task_state_patrol"), interruptions: { t: {} } }),
280
+ });
281
+ await adapter.sendCommand("tinyRobot/tinyBot_1", { kind: "full" });
282
+ await adapter.sendCommand("cleanerBotA/cleanerBotA_2", { kind: "pause" });
283
+ await adapter.sendCommand("cleanerBotA/cleanerBotA_2", { kind: "resume" });
284
+ await adapter.sendCommand("cleanerBotA/cleanerBotA_2", { kind: "cancel" });
285
+ const body = (path) => calls.find((c) => c.path === path).body;
286
+ const cases = [
287
+ ["robot_task_request", body("/tasks/robot_task")],
288
+ ["task_request", body("/tasks/robot_task").request],
289
+ ["interrupt_task_request", body("/tasks/interrupt_task")],
290
+ ["resume_task_request", body("/tasks/resume_task")],
291
+ ["cancel_task_request", body("/tasks/cancel_task")],
292
+ ];
293
+ for (const [name, b] of cases) for (const k of required(name)) assert.ok(k in b, `${name} needs ${k}`);
294
+ // The recorded states carry what the adapter reads.
295
+ for (const f of fx("fleets")) for (const s of Object.values(f.robots)) for (const k of required("location_2D")) assert.ok(k in s.location);
296
+ assert.deepEqual(schema("robot_state").properties.status.enum, ["uninitialized", "offline", "shutdown", "idle", "charging", "working", "error"]);
297
+ });