@7365admin1/core 3.116.0 → 3.117.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@7365admin1/core",
3
3
  "license": "MIT",
4
- "version": "3.116.0",
4
+ "version": "3.117.1",
5
5
  "author": "7365admin1",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -0,0 +1,165 @@
1
+ // PadBot S2 in production (2026-09-26): what the first real robot showed and
2
+ // how the adapter now reads it - routes whose stops cannot be placed on the
3
+ // plan are still listed, schedules are said to live in PadBot's app, a
4
+ // positive Wi-Fi number is a level (not dBm), a missing position is said in
5
+ // words, and staff Diagnose returns PadBot's raw answers without credentials.
6
+
7
+ import assert from "node:assert/strict";
8
+ import { test } from "node:test";
9
+
10
+ import {
11
+ PADBOT_PATHS,
12
+ asList,
13
+ clearPadbotCache,
14
+ createPadbotAdapter,
15
+ networkText,
16
+ padbotPatrolPlan,
17
+ } from "./.build/services/padbot.adapter.mjs";
18
+ import { normalisePatrolPlan } from "./.build/utils/robot-adapter.util.mjs";
19
+
20
+ const CREDS = { appid: "ak-real-id", secret: "sk-real-secret-never-shown" };
21
+ const NOW = Date.parse("2026-09-26T08:00:00.000Z");
22
+ const fresh = new Date(NOW - 5000).toISOString();
23
+
24
+ function fake(routes) {
25
+ const calls = [];
26
+ const fetch = async (url, init) => {
27
+ const path = new URL(url).pathname;
28
+ calls.push({ path, body: JSON.parse(init.body), headers: init.headers });
29
+ const hit = routes[path];
30
+ if (!hit) return { status: 404, text: async () => "" };
31
+ const out = hit(JSON.parse(init.body));
32
+ return { status: 200, text: async () => JSON.stringify(out?.__raw ?? { code: 0, message: "success", data: out ?? null }) };
33
+ };
34
+ return { fetch, calls };
35
+ }
36
+
37
+ test("network: negative = dBm; zero or positive = a signal level (the real S2 reports 4)", () => {
38
+ assert.equal(networkText({ wifi: { signalStrength: -61 } }), " · Wi-Fi -61 dBm");
39
+ assert.equal(networkText({ wifi: { signalStrength: 4 } }), " · Wi-Fi signal 4");
40
+ assert.equal(networkText({ cellular: { signalStrength: 3 } }), " · Mobile signal 3");
41
+ assert.equal(networkText({}), "");
42
+ });
43
+
44
+ test("routes: stops that cannot be placed are listed by name, the route is kept (not dropped)", () => {
45
+ // Map without resolution: no plan frame, so no stop can be placed.
46
+ const maps = [{ mapId: "m1", floor: 3, targetPoints: [{ targetPointId: "t1", name: "Gate" }, { targetPointId: "t2", name: "Lobby" }] }];
47
+ const paths = [{ pathId: "p1", pathName: "Night round", mapId: "m1", isDefault: true, points: [
48
+ { targetPointId: "t2", targetPointName: "Lobby", pointIndex: 1 },
49
+ { targetPointId: "t1", targetPointName: "Gate", pointIndex: 0 },
50
+ ] }];
51
+ const plan = normalisePatrolPlan(padbotPatrolPlan(paths, maps, {}));
52
+ assert.equal(plan.routes.length, 1);
53
+ assert.equal(plan.routes[0].name, "Night round (default)");
54
+ assert.deepEqual(plan.routes[0].stops, ["Gate", "Lobby"]);
55
+ assert.deepEqual(plan.routes[0].checkpoints, [], "nothing drawn at a guessed spot");
56
+ assert.equal(plan.routes[0].floorNo, 3);
57
+ assert.equal(plan.scheduleShared, false, "schedules live in PadBot's app");
58
+ assert.deepEqual(plan.schedule, []);
59
+ });
60
+
61
+ test("routes: pointNames when points are absent; a stop found on the robot's OTHER map is placed", () => {
62
+ const frame = { width: 200, height: 100, resolution: 0.05, originX: 0, originY: 0 };
63
+ const maps = [
64
+ { mapId: "m1", floor: 3, ...frame, targetPoints: [] },
65
+ { mapId: "m2", floor: 4, ...frame, targetPoints: [{ targetPointId: "a", name: "A", worldX: 1, worldY: 1 }, { targetPointId: "b", name: "B", worldX: 5, worldY: 2.5 }] },
66
+ ];
67
+ const byNames = normalisePatrolPlan(padbotPatrolPlan([{ pathId: "p9", pathName: "Loop", mapId: "m1", pointNames: ["A", "B"] }], maps, {}));
68
+ assert.deepEqual(byNames.routes[0].stops, ["A", "B"]);
69
+ assert.equal(byNames.routes[0].checkpoints.length, 2, "found by name on m2 and placed");
70
+ const perPoint = normalisePatrolPlan(padbotPatrolPlan([{ pathId: "p8", pathName: "Cross", mapId: "m1", points: [
71
+ { targetPointId: "a", targetPointName: "A", mapId: "m2", pointIndex: 0 },
72
+ { targetPointId: "b", targetPointName: "B", mapId: "m2", pointIndex: 1 },
73
+ ] }], maps, {}));
74
+ assert.deepEqual(perPoint.routes[0].checkpoints.map((c) => c.name), ["A", "B"]);
75
+ });
76
+
77
+ test("routes: a route with neither placed checkpoints nor stop names is still dropped; demo shape unchanged", () => {
78
+ assert.equal(normalisePatrolPlan({ routes: [{ id: "x", name: "Empty", checkpoints: [] }] }).routes.length, 0);
79
+ const demo = normalisePatrolPlan({ routes: [{ id: "d", name: "Demo", checkpoints: [{ name: "a", x: 1, y: 1 }, { name: "b", x: 2, y: 2 }] }], schedule: [] });
80
+ assert.equal(demo.routes[0].checkpoints.length, 2);
81
+ assert.equal("stops" in demo.routes[0], false);
82
+ assert.equal("scheduleShared" in demo, false);
83
+ });
84
+
85
+ test("list answers: bare array or an {items}/{list}/{records} page", () => {
86
+ assert.deepEqual(asList([1]), [1]);
87
+ assert.deepEqual(asList({ items: [2] }), [2]);
88
+ assert.deepEqual(asList({ list: [3] }), [3]);
89
+ assert.deepEqual(asList({ records: [4] }), [4]);
90
+ assert.deepEqual(asList(null), []);
91
+ });
92
+
93
+ const STATES = [{ serialNumber: "PS8517", states: [
94
+ { stateType: "1000", data: { updatedAt: fresh } },
95
+ { stateType: "1001", data: { batteryPercent: 100, updatedAt: fresh } },
96
+ { stateType: "1007", data: { mapId: "m1", floor: 3 } },
97
+ { stateType: "1107", data: { wifi: { signalStrength: 4 } } },
98
+ ] }];
99
+
100
+ test("getRobot: online with no position says so in words and is not placed", async () => {
101
+ clearPadbotCache();
102
+ const f = fake({
103
+ [PADBOT_PATHS.states]: () => STATES,
104
+ [PADBOT_PATHS.robots]: () => [{ serialNumber: "PS8517", robotModel: "S2" }],
105
+ [PADBOT_PATHS.location]: () => ({ __raw: { code: 1008, message: "Failed to retrieve data", data: null } }),
106
+ [PADBOT_PATHS.alarms]: () => ({ items: [] }),
107
+ });
108
+ const r = await createPadbotAdapter(CREDS, { baseUrl: "https://pb.test", fetch: f.fetch, now: () => NOW }).getRobot("PS8517");
109
+ assert.equal(r.online, true);
110
+ assert.equal(r.statusName, "Standing by · Wi-Fi signal 4 · Position not reported");
111
+ assert.equal(r.position, null);
112
+ assert.equal(r.planPosition, null);
113
+ assert.equal(r.model, "PadBot S2");
114
+ });
115
+
116
+ test("getRobot: x/y in place of worldX/worldY is read; map from the current-map state when the answer omits it", async () => {
117
+ clearPadbotCache();
118
+ const f = fake({
119
+ [PADBOT_PATHS.states]: () => STATES,
120
+ [PADBOT_PATHS.robots]: () => [],
121
+ [PADBOT_PATHS.location]: () => ({ x: 5, y: 2.5, yaw: 0 }),
122
+ [PADBOT_PATHS.maps]: () => ({ items: [{ mapId: "m1", width: 200, height: 100, resolution: 0.05, originX: 0, originY: 0 }] }),
123
+ [PADBOT_PATHS.alarms]: () => ({ items: [] }),
124
+ });
125
+ const r = await createPadbotAdapter(CREDS, { baseUrl: "https://pb.test", fetch: f.fetch, now: () => NOW }).getRobot("PS8517");
126
+ assert.deepEqual(r.position, { x: 5, y: 2.5, theta: 0 });
127
+ assert.deepEqual(r.planPosition, { x: 50, y: 50 });
128
+ assert.equal(r.statusName, "Standing by · Wi-Fi signal 4");
129
+ });
130
+
131
+ test("diagnose: PadBot's raw answers per read (code, message, data); maps summarised; no credential anywhere", async () => {
132
+ clearPadbotCache();
133
+ const f = fake({
134
+ [PADBOT_PATHS.states]: () => STATES,
135
+ [PADBOT_PATHS.location]: () => ({ __raw: { code: 1008, message: "Failed to retrieve data", data: null } }),
136
+ [PADBOT_PATHS.maps]: () => [{ mapId: "m1", mapName: "20260528172106098_5ecd8fa9", floor: 3, imageUrl: "https://img/x.png", targetPoints: [{ targetPointId: "t", name: "Gate", worldX: 1, worldY: 2, targetPointType: 6 }], restrictedAreas: [{}, {}] }],
137
+ [PADBOT_PATHS.patrolRoutes]: () => [{ pathId: "p1", pathName: "Night", mapId: "m1" }],
138
+ [PADBOT_PATHS.alarms]: () => ({ items: [], totalItems: 0 }),
139
+ [PADBOT_PATHS.records]: () => ({ items: [], totalItems: 0 }),
140
+ });
141
+ const steps = await createPadbotAdapter(CREDS, { baseUrl: "https://pb.test", fetch: f.fetch, now: () => NOW }).diagnose("PS8517");
142
+ assert.deepEqual(steps.map((s) => s.step), ["states (4.2)", "location (4.3)", "maps (5.1)", "patrol routes (6.1)", "alarms (6.4)", "patrol records (6.7)"]);
143
+ const loc = steps[1];
144
+ assert.equal(loc.code, 1008);
145
+ assert.equal(loc.message, "Failed to retrieve data");
146
+ assert.deepEqual(loc.body, { serialNumber: "PS8517" });
147
+ const map = steps[2].data[0];
148
+ assert.equal(map.mapName, "20260528172106098_5ecd8fa9");
149
+ assert.equal(map.counts.restrictedAreas, 2);
150
+ assert.equal(map.targetPoints[0].targetPointType, 6);
151
+ assert.equal("imageUrl" in map, false);
152
+ assert.deepEqual(steps[3].data, [{ pathId: "p1", pathName: "Night", mapId: "m1" }]);
153
+ const text = JSON.stringify(steps);
154
+ assert.ok(!text.includes(CREDS.secret) && !text.includes(CREDS.appid), "never a credential");
155
+ assert.ok(!f.calls.some((c) => c.path === "/api/v1/robots/mqtt/credentials"), "never asks for MQTT credentials");
156
+ });
157
+
158
+ test("live video: HD asks PadBot for quality 1, default smooth for quality 2", async () => {
159
+ clearPadbotCache();
160
+ const f = fake({ [PADBOT_PATHS.video]: () => ({ playbackUrl: "https://v/play", expiresAt: "2026-09-26T09:00:00.000Z" }) });
161
+ const a = createPadbotAdapter(CREDS, { baseUrl: "https://pb.test", fetch: f.fetch, now: () => NOW });
162
+ await a.getLiveVideo("PS8517", "hd");
163
+ await a.getLiveVideo("PS8517");
164
+ assert.deepEqual(f.calls.map((c) => c.body.quality), [1, 2]);
165
+ });
@@ -282,13 +282,13 @@ test("alarm dedupe: the same alarm on every read opens once; a new occurrence re
282
282
  assert.equal(first.open.length, 1);
283
283
  const rec = { _id: "r1", code: f.code, since: new Date(f.since) };
284
284
  const again = planFaultChanges([rec], { ok: true, online: true, fault: padbotAlarmFault(alarm) });
285
- assert.deepEqual(again, { open: [], clear: [] });
285
+ assert.deepEqual(again, { open: [], clear: [], refresh: [] });
286
286
  const later = padbotAlarmFault({ alarmType: "3", occurredAt: "2026-07-25T09:04:17.010Z" });
287
287
  const second = planFaultChanges([rec], { ok: true, online: true, fault: later });
288
288
  assert.deepEqual(second.clear, ["r1"]);
289
289
  assert.equal(second.open.length, 1);
290
290
  const resolved = planFaultChanges([rec], { ok: true, online: true, fault: null });
291
- assert.deepEqual(resolved, { open: [], clear: ["r1"] });
291
+ assert.deepEqual(resolved, { open: [], clear: ["r1"], refresh: [] });
292
292
  });
293
293
 
294
294
  /* ------------------------------------------------------ patrol */
@@ -50,14 +50,14 @@ const rec = (code, since = 1000, _id = code) => ({ _id, code, since: new Date(si
50
50
 
51
51
  test("plan: a new fault opens; the same fault on every read changes nothing", () => {
52
52
  const fault = { code: "JAM", message: "Brush jammed", since: 1000 };
53
- assert.deepEqual(planFaultChanges([], { ok: true, online: true, fault }), { open: [fault], clear: [] });
54
- assert.deepEqual(planFaultChanges([rec("JAM")], { ok: true, online: true, fault }), { open: [], clear: [] });
53
+ assert.deepEqual(planFaultChanges([], { ok: true, online: true, fault }), { open: [fault], clear: [], refresh: [] });
54
+ assert.deepEqual(planFaultChanges([rec("JAM")], { ok: true, online: true, fault }), { open: [], clear: [], refresh: [] });
55
55
  // a brand that cannot say when it started: the open record stands
56
- assert.deepEqual(planFaultChanges([rec("JAM")], { ok: true, online: true, fault: { code: "JAM" } }), { open: [], clear: [] });
56
+ assert.deepEqual(planFaultChanges([rec("JAM")], { ok: true, online: true, fault: { code: "JAM" } }), { open: [], clear: [], refresh: [] });
57
57
  });
58
58
 
59
59
  test("plan: a fault that goes away is cleared; coming back later is a new occurrence", () => {
60
- assert.deepEqual(planFaultChanges([rec("JAM")], { ok: true, online: true, fault: null }), { open: [], clear: ["JAM"] });
60
+ assert.deepEqual(planFaultChanges([rec("JAM")], { ok: true, online: true, fault: null }), { open: [], clear: ["JAM"], refresh: [] });
61
61
  const again = planFaultChanges([rec("JAM", 1000)], { ok: true, online: true, fault: { code: "JAM", message: "m", since: 90000 } });
62
62
  assert.deepEqual(again.clear, ["JAM"]);
63
63
  assert.equal(again.open[0].code, "JAM");
@@ -74,7 +74,7 @@ test("plan: offline / not reporting is an OFFLINE record; a failed read cannot c
74
74
  const back = planFaultChanges([rec(OFFLINE_FAULT_CODE), rec("JAM")], { ok: true, online: true, fault: null });
75
75
  assert.deepEqual(back.clear.sort(), ["JAM", OFFLINE_FAULT_CODE].sort());
76
76
  // still offline: nothing new
77
- assert.deepEqual(planFaultChanges([rec(OFFLINE_FAULT_CODE)], { ok: true, online: false }), { open: [], clear: [] });
77
+ assert.deepEqual(planFaultChanges([rec(OFFLINE_FAULT_CODE)], { ok: true, online: false }), { open: [], clear: [], refresh: [] });
78
78
  // going offline does NOT clear the robot's own fault (it would re-notify on return)
79
79
  const wentOff = planFaultChanges([rec("JAM")], { ok: true, online: false, fault: null });
80
80
  assert.deepEqual(wentOff.clear, []);
@@ -117,3 +117,24 @@ test("bounded: never more than the limit in flight, one failure does not stop th
117
117
  await assert.rejects(withTimeout(new Promise(() => {}), 20, "slow"), /slow after 20 ms/);
118
118
  assert.equal(await withTimeout(Promise.resolve(7), 20), 7);
119
119
  });
120
+
121
+ test("plan: a still-open OFFLINE record takes the LATEST failed read's reason, without re-opening", () => {
122
+ // HERO-OTH: the record opened with "ROBOT_CLOUD_BASE_URL is not configured"
123
+ // and kept saying so after the base URL was fixed and the robot failed for a
124
+ // different reason.
125
+ const stale = { ...rec(OFFLINE_FAULT_CODE), message: "Not reporting: Robot cloud: ROBOT_CLOUD_BASE_URL is not configured." };
126
+ const plan = planFaultChanges([stale], { ok: false, error: "Ounitech rejected the robot list (sign-in) (sign error)." });
127
+ assert.deepEqual(plan.open, [], "no second record, so no second alert and the same since");
128
+ assert.deepEqual(plan.clear, []);
129
+ assert.deepEqual(plan.refresh, [
130
+ { id: OFFLINE_FAULT_CODE, message: "Not reporting: Ounitech rejected the robot list (sign-in) (sign error)." },
131
+ ]);
132
+
133
+ // Same reason: nothing to rewrite. A record read without its message: nothing either.
134
+ const same = { ...stale, message: plan.refresh[0].message };
135
+ assert.deepEqual(planFaultChanges([same], { ok: false, error: "Ounitech rejected the robot list (sign-in) (sign error)." }).refresh, []);
136
+ assert.deepEqual(planFaultChanges([rec(OFFLINE_FAULT_CODE)], { ok: false, error: "x" }).refresh, []);
137
+ // A machine fault's message is the robot's own and is never rewritten here.
138
+ const jam = { ...rec("JAM"), message: "old" };
139
+ assert.deepEqual(planFaultChanges([jam], { ok: true, online: true, fault: { code: "JAM", message: "new", since: 1000 } }).refresh, []);
140
+ });
@@ -287,3 +287,37 @@ test("diagnose: an unknown brand is named, not thrown", async () => {
287
287
  const d = await fleetFor("ok").diagnose(robotRow({ brand: "nobody" }));
288
288
  assert.equal(d.failedStep, "brand");
289
289
  });
290
+
291
+ /* ------------------------------------------- base URL with the env unset */
292
+
293
+ import { execFileSync } from "node:child_process";
294
+
295
+ test("with OUNITECH_API_BASE and ROBOT_CLOUD_BASE_URL unset (or blank), the Ounitech host is the default and nothing throws", () => {
296
+ const script = `
297
+ const { ROBOT_CLOUD_BASE_URL } = await import("./test/.build/config.mjs");
298
+ const { useRobotCloudService } = await import("./test/.build/services/robot-cloud.service.mjs");
299
+ useRobotCloudService({ appid: "a", secret: "s" }); // threw "not configured" when the base was empty
300
+ console.log(ROBOT_CLOUD_BASE_URL);`;
301
+ for (const extra of [{}, { OUNITECH_API_BASE: "", ROBOT_CLOUD_BASE_URL: "" }]) {
302
+ const env = { ...process.env, ...extra };
303
+ if (!("OUNITECH_API_BASE" in extra)) {
304
+ delete env.OUNITECH_API_BASE;
305
+ delete env.ROBOT_CLOUD_BASE_URL;
306
+ }
307
+ const out = execFileSync(process.execPath, ["--input-type=module", "-e", script], { env, encoding: "utf8" }).trim();
308
+ assert.equal(out.split("\n").pop(), "https://app.frankfurt.robotcloud.top", JSON.stringify(extra));
309
+ }
310
+ });
311
+
312
+ test("a missing base URL (only reachable by passing an empty one) is a handled 502, never a 500", () => {
313
+ let e;
314
+ try {
315
+ useRobotCloudService(CREDS, { baseUrl: "" });
316
+ } catch (err) {
317
+ e = err;
318
+ }
319
+ assert.ok(e instanceof RobotCloudError);
320
+ const answer = apiAnswer(e);
321
+ assert.equal(answer.status, 502);
322
+ assert.match(answer.body.message, /not configured/);
323
+ });