@7365admin1/core 3.90.0 → 3.91.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,284 @@
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+
4
+ import { createDemoAdapter, demoPatrolPoint, derivedDemoRobot, planDemoRobots, DEMO_ADD_MAX } from "./.build/services/demo.adapter.mjs";
5
+ import { normalisePatrolPlan } from "./.build/utils/robot-adapter.util.mjs";
6
+ import { parseRobotCommand } from "./.build/utils/robot-cloud.util.mjs";
7
+ import { networkMatch, siteSummaryPipeline } from "./.build/utils/robot-network.util.mjs";
8
+
9
+ /* --------------------------------------------------------------------------
10
+ * HERO phase c: the patrol route contract (owner decision 5), the demo patrol
11
+ * robots behind it, the commands the command bar sends, and the site fleet's
12
+ * scoping filters.
13
+ * ----------------------------------------------------------------------- */
14
+
15
+ const at = (ms) => createDemoAdapter({ now: () => ms });
16
+
17
+ const goodRoute = {
18
+ id: "r1",
19
+ name: "Route A · Lobby loop",
20
+ floorNo: 1,
21
+ checkpoints: [
22
+ { name: "Lobby", x: 10, y: 10 },
23
+ { name: "Lift lobby", x: 40, y: 10 },
24
+ ],
25
+ currentCheckpoint: 1,
26
+ };
27
+
28
+ test("patrol contract: a clean answer passes through unchanged", () => {
29
+ const plan = normalisePatrolPlan({
30
+ routes: [goodRoute],
31
+ schedule: [{ routeId: "r1", days: [1, 2, 3], startTime: "20:00", endTime: "06:00", rounds: 6 }],
32
+ });
33
+ assert.deepEqual(plan.routes, [goodRoute]);
34
+ assert.deepEqual(plan.schedule, [{ routeId: "r1", days: [1, 2, 3], startTime: "20:00", endTime: "06:00", rounds: 6 }]);
35
+ });
36
+
37
+ test("patrol contract: anything that is not a plan is an empty plan", () => {
38
+ for (const raw of [null, undefined, 1, "x", [], { routes: "no" }, { routes: [null, 1, "x"] }]) {
39
+ assert.deepEqual(normalisePatrolPlan(raw), { routes: [], schedule: [] });
40
+ }
41
+ });
42
+
43
+ test("patrol contract: checkpoints need a name and a position; positions clamp to the plan", () => {
44
+ const plan = normalisePatrolPlan({
45
+ routes: [{
46
+ ...goodRoute,
47
+ checkpoints: [
48
+ { name: "Lobby", x: -5, y: 120 },
49
+ { name: "", x: 1, y: 1 },
50
+ { name: "No y", x: 1 },
51
+ { name: "NaN", x: Number.NaN, y: 1 },
52
+ { name: "Gate", x: 50, y: 50 },
53
+ ],
54
+ currentCheckpoint: 0,
55
+ }],
56
+ });
57
+ assert.deepEqual(plan.routes[0].checkpoints, [{ name: "Lobby", x: 0, y: 100 }, { name: "Gate", x: 50, y: 50 }]);
58
+ });
59
+
60
+ test("patrol contract: a route needs an id, a name, two checkpoints and a unique id", () => {
61
+ const plan = normalisePatrolPlan({
62
+ routes: [
63
+ { ...goodRoute, id: "" },
64
+ { ...goodRoute, name: " " },
65
+ { ...goodRoute, id: "one", checkpoints: [goodRoute.checkpoints[0]] },
66
+ goodRoute,
67
+ { ...goodRoute, name: "Duplicate id" },
68
+ ],
69
+ });
70
+ assert.deepEqual(plan.routes.map((r) => r.name), ["Route A · Lobby loop"]);
71
+ });
72
+
73
+ test("patrol contract: the current checkpoint must point at one that survived", () => {
74
+ const pick = (currentCheckpoint) => normalisePatrolPlan({ routes: [{ ...goodRoute, currentCheckpoint }] }).routes[0].currentCheckpoint;
75
+ assert.equal(pick(0), 0);
76
+ assert.equal(pick(1), 1);
77
+ assert.equal(pick(2), null);
78
+ assert.equal(pick(-1), null);
79
+ assert.equal(pick(0.5), null);
80
+ assert.equal(pick("1"), null);
81
+ assert.equal(pick(undefined), null);
82
+ assert.equal(normalisePatrolPlan({ routes: [{ ...goodRoute, floorNo: "L1" }] }).routes[0].floorNo, null);
83
+ });
84
+
85
+ test("patrol contract: a schedule row needs its route, a day, a HH:MM window and a round", () => {
86
+ const row = { routeId: "r1", days: [7, 1, 1, 9, 0], startTime: "20:00", endTime: "06:00", rounds: 6 };
87
+ const rows = (schedule) => normalisePatrolPlan({ routes: [goodRoute], schedule }).schedule;
88
+ assert.deepEqual(rows([row]), [{ ...row, days: [1, 7] }], "days: de-duplicated, sorted, only 1-7");
89
+ assert.deepEqual(rows([{ ...row, routeId: "missing" }]), []);
90
+ assert.deepEqual(rows([{ ...row, days: [8] }]), []);
91
+ assert.deepEqual(rows([{ ...row, startTime: "24:00" }]), []);
92
+ assert.deepEqual(rows([{ ...row, endTime: "6:00" }]), []);
93
+ assert.deepEqual(rows([{ ...row, rounds: 0 }]), []);
94
+ assert.deepEqual(rows([{ ...row, rounds: 1.5 }]), []);
95
+ assert.equal(rows([{ ...row, rounds: 500 }])[0].rounds, 99);
96
+ });
97
+
98
+ test("serial-derived demo robots: DEMO-C-### cleans, DEMO-S-### patrols, everything else follows from the number", () => {
99
+ const c = derivedDemoRobot("DEMO-C-007");
100
+ assert.equal(c.name, "Demo Cleaner 007");
101
+ assert.equal(c.route, undefined);
102
+ assert.ok(["Demo S1", "Demo X2", "Demo CX-1"].includes(c.model));
103
+ const s = derivedDemoRobot("DEMO-S-003");
104
+ assert.equal(s.name, "Demo Patrol 003");
105
+ assert.equal(s.model, "Demo SP-1");
106
+ assert.equal(s.route, 0, "route A");
107
+ assert.deepEqual(derivedDemoRobot("DEMO-S-003"), s, "deterministic");
108
+ for (const bad of ["DEMO-X-001", "DEMO-C-", "DEMO-C-1234567", "demo-c-001", "DEMO-0001", ""]) {
109
+ assert.equal(derivedDemoRobot(bad), null, bad);
110
+ }
111
+ });
112
+
113
+ test("the originals are untouched: DEMO-0001/0002/0003 listed as before; derived robots are not listed", async () => {
114
+ const robots = await at(0).listRobots();
115
+ assert.deepEqual(robots.map((r) => r.robotSn), ["DEMO-0001", "DEMO-0002", "DEMO-0003"]);
116
+ assert.equal(robots[0].mode, "IDLE");
117
+ });
118
+
119
+ test("a derived fleet looks alive: working, charging, paused, idle; ~1 in 8 faults, ~1 in 10 offline", async () => {
120
+ const t = 5 * 60_000;
121
+ const fleet = await Promise.all(Array.from({ length: 40 }, (_, i) => at(t).getRobot(`DEMO-C-${String(i + 1).padStart(3, "0")}`)));
122
+ const modes = new Set(fleet.map((r) => r.mode));
123
+ for (const m of ["WORKING", "CHARGING_CYCLE", "PAUSED", "MAINTENANCE", "UNAVAILABLE"]) assert.ok(modes.has(m), `no ${m}`);
124
+ assert.ok(modes.has("IDLE") || modes.has("CHARGED"));
125
+ const faulted = fleet.filter((r) => r.fault);
126
+ const offline = fleet.filter((r) => !r.online);
127
+ // n % 8 === 5 fault (5, 13, 21, 29, 37) - but 37 is also offline, and offline wins.
128
+ assert.equal(faulted.length, 4, "n % 8 === 5, less the offline one");
129
+ assert.equal(offline.length, 4, "n % 10 === 7");
130
+ for (const r of faulted) {
131
+ assert.match(r.fault.message, /simulated/);
132
+ assert.equal(r.fault.since, t - (t % 86_400_000), "one occurrence per robot per day");
133
+ assert.equal(r.mode, "MAINTENANCE");
134
+ }
135
+ for (const r of offline) {
136
+ assert.deepEqual([r.mode, r.position, r.planPosition, r.batteryPercent, r.fault], ["UNAVAILABLE", null, null, null, null]);
137
+ }
138
+ // Only a working robot moves.
139
+ const still = fleet.find((r) => r.online && r.mode === "PAUSED");
140
+ const later = await at(t + 30_000).getRobot(still.robotSn);
141
+ assert.deepEqual(later.position, still.position, "a paused robot holds still");
142
+ const moving = fleet.find((r) => r.online && r.mode === "WORKING");
143
+ assert.notDeepEqual((await at(t + 30_000).getRobot(moving.robotSn)).position, moving.position);
144
+ });
145
+
146
+ test("derived security robots patrol their route and report it; the plan position is on the route", async () => {
147
+ const sn = "DEMO-S-003";
148
+ const robot = await at(5_000).getRobot(sn);
149
+ assert.equal(robot.mode, "WORKING");
150
+ assert.equal(robot.statusName, "PATROL_AUTO");
151
+ const expect = demoPatrolPoint(0, derivedDemoRobot(sn).phase, 5_000);
152
+ assert.ok(Math.abs(robot.planPosition.x - expect.x) < 0.01 && Math.abs(robot.planPosition.y - expect.y) < 0.01);
153
+ const plan = normalisePatrolPlan(await at(5_000).getPatrolRoutes(sn));
154
+ assert.equal(plan.routes[0].name, "Route A · Lobby loop");
155
+ assert.equal(plan.routes[0].currentCheckpoint, expect.next);
156
+ assert.equal(plan.routes[0].floorNo, derivedDemoRobot(sn).floorNo);
157
+ assert.deepEqual(plan.schedule, [{ routeId: "route-a", days: [1, 2, 3, 4, 5, 6, 7], startTime: "20:00", endTime: "06:00", rounds: 6 }]);
158
+ assert.equal((await at(0).getPatrolRoutes("DEMO-S-005")).routes[0].name, "Route C · Facilities deck");
159
+ assert.deepEqual(await at(0).getPatrolRoutes("DEMO-C-003"), { routes: [], schedule: [] });
160
+ });
161
+
162
+ test("every demo robot reports a plan position inside the plan", async () => {
163
+ const sns = ["DEMO-0001", "DEMO-0002", "DEMO-0003", "DEMO-C-001", "DEMO-C-004", "DEMO-S-001", "DEMO-S-002"];
164
+ for (const ms of [0, 17_000, 45_000, 90_000]) {
165
+ for (const sn of sns) {
166
+ const r = await at(ms).getRobot(sn);
167
+ assert.ok(r.planPosition, sn);
168
+ assert.ok(r.planPosition.x >= 0 && r.planPosition.x <= 100, `${sn} x ${r.planPosition.x}`);
169
+ assert.ok(r.planPosition.y >= 0 && r.planPosition.y <= 100, `${sn} y ${r.planPosition.y}`);
170
+ }
171
+ }
172
+ });
173
+
174
+ test("a demo patrol walks checkpoint to checkpoint", () => {
175
+ // At phase 0 it stands on checkpoint 1 heading for checkpoint 2.
176
+ const start = demoPatrolPoint(0, 0, 0);
177
+ assert.deepEqual([start.x, start.y, start.next], [14, 16, 1]);
178
+ const seen = new Set();
179
+ for (let ms = 0; ms < 120_000; ms += 5_000) seen.add(demoPatrolPoint(0, 0, ms).next);
180
+ assert.deepEqual([...seen].sort(), [0, 1, 2, 3], "every checkpoint is the target at some point in a lap");
181
+ });
182
+
183
+ test("getPatrolRoutes: nothing for a cleaner or an unknown serial", async () => {
184
+ assert.deepEqual(await at(0).getPatrolRoutes("DEMO-0001"), { routes: [], schedule: [] });
185
+ assert.deepEqual(await at(0).getPatrolRoutes("NOPE"), { routes: [], schedule: [] });
186
+ });
187
+
188
+ test("pause holds a patrol robot; cancel parks it with no current checkpoint", async () => {
189
+ const sn = "DEMO-S-001"; // route B, patrolling by default
190
+ const demo = at(2_000_000);
191
+ await demo.sendCommand(sn, { kind: "pause" });
192
+ assert.equal((await demo.getRobot(sn)).mode, "PAUSED");
193
+ assert.equal((await demo.getRobot(sn)).statusName, "PAUSED");
194
+ await demo.sendCommand(sn, { kind: "cancel" });
195
+ const parked = await demo.getRobot(sn);
196
+ assert.equal(parked.mode, "IDLE");
197
+ assert.deepEqual([parked.planPosition.x, parked.planPosition.y].map(Math.round), [62, 14], "waits at its first checkpoint");
198
+ assert.equal((await demo.getPatrolRoutes(sn)).routes[0].currentCheckpoint, null);
199
+ await demo.sendCommand(sn, { kind: "resume" });
200
+ assert.equal((await demo.getRobot(sn)).mode, "WORKING");
201
+ });
202
+
203
+ test("Add demo robots plans serials after the highest used, named as demos, at most 20 of each", () => {
204
+ const plan = planDemoRobots({ cleaning: 2, security: 1 }, { C: 7, S: 0 });
205
+ assert.deepEqual(plan, [
206
+ { type: "cleaning", robotSn: "DEMO-C-008", name: "Demo Cleaner 008" },
207
+ { type: "cleaning", robotSn: "DEMO-C-009", name: "Demo Cleaner 009" },
208
+ { type: "security", robotSn: "DEMO-S-001", name: "Demo Patrol 001" },
209
+ ]);
210
+ assert.equal(planDemoRobots({ cleaning: 99, security: 99 }, { C: 0, S: 0 }).length, 2 * DEMO_ADD_MAX);
211
+ assert.deepEqual(planDemoRobots({ cleaning: 0, security: -3 }, { C: 0, S: 0 }), []);
212
+ assert.equal(planDemoRobots({ cleaning: 1, security: 0 }, { C: 1234, S: 0 })[0].robotSn, "DEMO-C-1235");
213
+ });
214
+
215
+ test("the demo plans cover floors 1-3, each drawn differently", async () => {
216
+ const maps = await at(0).getMaps();
217
+ assert.deepEqual(maps.map((m) => m.floorNo), [1, 2, 3]);
218
+ const urls = (await at(0).getMapImages("DEMO-C-001", ["demo/L1.png", "demo/L2.png", "demo/L3.png"])).map((f) => f.url);
219
+ assert.equal(new Set(urls).size, 3);
220
+ });
221
+
222
+ test("commands: pause / resume / cancel take no fields", () => {
223
+ for (const kind of ["pause", "resume", "cancel"]) {
224
+ assert.deepEqual(parseRobotCommand({ kind }), { ok: true, command: { kind } });
225
+ for (const extra of [{ x: 1 }, { cleanTimes: 1 }, { areas: ["a"] }, { floorNo: 1 }]) {
226
+ assert.equal(parseRobotCommand({ kind, ...extra }).ok, false, `${kind} + ${Object.keys(extra)}`);
227
+ }
228
+ }
229
+ });
230
+
231
+ test("commands: navigate needs x and y, theta optional; relocalize needs all four", () => {
232
+ assert.deepEqual(parseRobotCommand({ kind: "navigate", x: 1.5, y: 2 }), { ok: true, command: { kind: "navigate", x: 1.5, y: 2 } });
233
+ assert.deepEqual(parseRobotCommand({ kind: "navigate", x: 1, y: 2, theta: 0.5 }).command.theta, 0.5);
234
+ assert.equal(parseRobotCommand({ kind: "navigate", x: 1 }).ok, false);
235
+ assert.equal(parseRobotCommand({ kind: "navigate", x: "1", y: 2 }).ok, false);
236
+ assert.equal(parseRobotCommand({ kind: "navigate", x: 1, y: 2, floorNo: 1 }).ok, false);
237
+ assert.equal(parseRobotCommand({ kind: "navigate", x: 1, y: 2, theta: "n" }).ok, false);
238
+ assert.deepEqual(parseRobotCommand({ kind: "relocalize", floorNo: 2, x: 1, y: 2, theta: 0 }), {
239
+ ok: true,
240
+ command: { kind: "relocalize", floorNo: 2, x: 1, y: 2, theta: 0 },
241
+ });
242
+ assert.equal(parseRobotCommand({ kind: "relocalize", x: 1, y: 2, theta: 0 }).ok, false);
243
+ assert.equal(parseRobotCommand({ kind: "relocalize", floorNo: 1.5, x: 1, y: 2, theta: 0 }).ok, false);
244
+ assert.equal(parseRobotCommand({ kind: "full", x: 1 }).ok, false, "a clean takes no pose");
245
+ });
246
+
247
+ test("site scoping: types narrows the match; an empty list matches nothing", () => {
248
+ const cleaning = networkMatch({ types: ["cleaning"] });
249
+ assert.deepEqual(cleaning.$and, [{ type: { $in: ["cleaning", null, ""] } }], "legacy rows are cleaning");
250
+ assert.deepEqual(networkMatch({ types: ["security"] }).$and, [{ type: { $in: ["security"] } }]);
251
+ assert.deepEqual(networkMatch({ types: [] }).$and, [{ type: { $in: [] } }]);
252
+ assert.equal(networkMatch({}).$and, undefined, "no types = no narrowing");
253
+ });
254
+
255
+ test("site summary pipeline: estate + class match, fault join, then one count group", () => {
256
+ const p = siteSummaryPipeline({ org: "5f0000000000000000000001", site: "5f0000000000000000000002", types: ["security"] });
257
+ assert.equal(String(p[0].$match["metadata.siteId"]), "5f0000000000000000000002");
258
+ assert.deepEqual(p[0].$match.$and, [{ type: { $in: ["security"] } }]);
259
+ const group = p[p.length - 1].$group;
260
+ assert.deepEqual(Object.keys(group).sort(), ["_id", "faults", "ids", "offline", "total"]);
261
+ });
262
+
263
+ test("demo tracking streams poses with a plan position, and stops when told", async () => {
264
+ const { startDemoTracking } = await import("./.build/services/demo.adapter.mjs");
265
+ const events = [];
266
+ const handle = startDemoTracking(createDemoAdapter(), "DEMO-S-003", (e) => events.push(e), 20);
267
+ await new Promise((r) => setTimeout(r, 90));
268
+ handle.stop();
269
+ const seen = events.length;
270
+ await new Promise((r) => setTimeout(r, 60));
271
+ assert.ok(seen >= 2, `only ${seen} events`);
272
+ assert.equal(events.length, seen, "no event after stop");
273
+ const [first] = events;
274
+ assert.equal(first.type, "pose");
275
+ assert.equal(typeof first.pose.x, "number");
276
+ assert.equal(first.pose.mode, "WORKING");
277
+ assert.ok(first.planPosition.x >= 0 && first.planPosition.x <= 100);
278
+
279
+ const lost = [];
280
+ const h2 = startDemoTracking(createDemoAdapter(), "NOPE", (e) => lost.push(e), 20);
281
+ await new Promise((r) => setTimeout(r, 30));
282
+ h2.stop();
283
+ assert.equal(lost[0].type, "lost");
284
+ });