@7365admin1/core 3.88.0 → 3.90.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.
- package/CHANGELOG.md +14 -0
- package/dist/index.d.ts +474 -174
- package/dist/index.js +6406 -5418
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6290 -5310
- package/dist/index.mjs.map +1 -1
- package/logs/.6b77785a74c362881f6fb7e95a7530280abbf9ed-audit.json +5 -0
- package/logs/.ca6dad43b7d35307d69543a741eb24d3ce997028-audit.json +5 -0
- package/package.json +2 -2
- package/test/e2e/harness.mjs +14 -0
- package/test/e2e/robot-faults.e2e.test.mjs +307 -0
- package/test/e2e/robot-network.e2e.test.mjs +272 -0
- package/test/robot-adapter.test.mjs +9 -0
- package/test/robot-fault.test.mjs +80 -0
- package/test/robot-network.test.mjs +153 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
// End-to-end proof of the Seven365 console's all-robots view (owner, 2026-09-23:
|
|
2
|
+
// "Seven365 Super Admin can see ALL the robots deployed to ALL its clients'
|
|
3
|
+
// sites"), and that Super Admin holds every robot right at ANY client's site.
|
|
4
|
+
//
|
|
5
|
+
// Who may list: the super-admin MEMBERSHIP (a `members` row of type "admin"
|
|
6
|
+
// whose role is a live role of type "admin") with the cross-client grant -
|
|
7
|
+
// never an email, never a role NAME. Case 3 is an ordinary organisation role
|
|
8
|
+
// merely NAMED "Super Admin", and it is refused.
|
|
9
|
+
//
|
|
10
|
+
// Everything runs against the throwaway harness (in-process MongoDB, loopback
|
|
11
|
+
// Redis and mail sink). Robots are the DEMO brand, which reaches no network, so
|
|
12
|
+
// no vendor cloud is contacted. Credential values are obvious fakes and are only
|
|
13
|
+
// asserted ABSENT.
|
|
14
|
+
//
|
|
15
|
+
// Run with: yarn test:e2e
|
|
16
|
+
|
|
17
|
+
import { after, before, describe, it } from "node:test";
|
|
18
|
+
import assert from "node:assert/strict";
|
|
19
|
+
import { ObjectId } from "mongodb";
|
|
20
|
+
|
|
21
|
+
import { startHarness } from "./harness.mjs";
|
|
22
|
+
|
|
23
|
+
const PASSWORD = "Robot-Passw0rd!";
|
|
24
|
+
const STAFF = "net-staff@e2e.example.com"; // Seven365 super admin (seeded shape: permissions [])
|
|
25
|
+
const STAR = "net-star@e2e.example.com"; // Seven365 staff, role holds "*"
|
|
26
|
+
const PARTIAL = "net-partial@e2e.example.com"; // Seven365 staff ticked for promo codes only
|
|
27
|
+
const MANAGER = "net-manager@e2e.example.com"; // client A's manager, role holds "*"
|
|
28
|
+
const NAMED = "net-named@e2e.example.com"; // an org role merely NAMED "Super Admin"
|
|
29
|
+
|
|
30
|
+
describe("robot network view - Seven365 super admin only", { concurrency: 1 }, () => {
|
|
31
|
+
let h;
|
|
32
|
+
const id = {};
|
|
33
|
+
const sid = {};
|
|
34
|
+
|
|
35
|
+
before(async () => {
|
|
36
|
+
h = await startHarness();
|
|
37
|
+
Object.assign(id, await seed(h));
|
|
38
|
+
for (const [k, email] of Object.entries({ STAFF, STAR, PARTIAL, MANAGER, NAMED })) {
|
|
39
|
+
sid[k] = await h.login(email, PASSWORD);
|
|
40
|
+
}
|
|
41
|
+
}, { timeout: 300000 });
|
|
42
|
+
|
|
43
|
+
after(async () => {
|
|
44
|
+
if (h) await h.stop();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const list = (qs = "", who = "STAFF") => h.api(`/robots/network${qs}`, { sid: sid[who] });
|
|
48
|
+
|
|
49
|
+
it("1. no session is a 401 on both routes", async () => {
|
|
50
|
+
assert.equal((await h.api("/robots/network")).status, 401);
|
|
51
|
+
assert.equal((await h.api("/robots/network/summary")).status, 401);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("2. a client's own manager (role '*') is refused - it is not a platform membership", async () => {
|
|
55
|
+
assert.equal((await list("", "MANAGER")).status, 401);
|
|
56
|
+
assert.equal((await h.api("/robots/network/summary", { sid: sid.MANAGER })).status, 401);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("3. an organisation role merely NAMED 'Super Admin' is refused", async () => {
|
|
60
|
+
assert.equal((await list("", "NAMED")).status, 401);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("4. staff without the cross-client grant are refused", async () => {
|
|
64
|
+
assert.equal((await list("", "PARTIAL")).status, 401);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("5. super admin sees every client's robots, named, without credentials or removed ones", async () => {
|
|
68
|
+
const res = await list("?limit=50");
|
|
69
|
+
assert.equal(res.status, 200, JSON.stringify(res.body));
|
|
70
|
+
const names = res.body.items.map((r) => r.name).sort();
|
|
71
|
+
assert.deepEqual(names, ["A demo 1", "A legacy", "B guard", "B scrubber"]);
|
|
72
|
+
assert.equal(res.body.total, 4);
|
|
73
|
+
assert.equal(names.includes("A removed"), false);
|
|
74
|
+
|
|
75
|
+
const legacy = res.body.items.find((r) => r.name === "A legacy");
|
|
76
|
+
assert.equal(legacy.client.name, "Net Client A");
|
|
77
|
+
assert.equal(legacy.site.name, "Net Site A");
|
|
78
|
+
// written before `type` / `brand` existed: shown as the model's defaults
|
|
79
|
+
assert.equal(legacy.type, "cleaning");
|
|
80
|
+
assert.equal(legacy.brand, "ounitech");
|
|
81
|
+
assert.equal(res.body.items.find((r) => r.name === "A demo 1").demo, true);
|
|
82
|
+
assert.equal(legacy.demo, false);
|
|
83
|
+
|
|
84
|
+
const raw = JSON.stringify(res.body);
|
|
85
|
+
assert.equal(raw.includes("appKey") || raw.includes("appToken") || raw.includes("e2e-fake"), false);
|
|
86
|
+
// default order: client, then site
|
|
87
|
+
assert.deepEqual(res.body.items.map((r) => r.client.name), [
|
|
88
|
+
"Net Client A", "Net Client A", "Net Client B", "Net Client B",
|
|
89
|
+
]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("6. a staff role holding '*' sees the same", async () => {
|
|
93
|
+
const res = await list("?limit=50", "STAR");
|
|
94
|
+
assert.equal(res.status, 200);
|
|
95
|
+
assert.equal(res.body.total, 4);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("7. filters: client, site, class (incl. legacy rows), brand, search by client name", async () => {
|
|
99
|
+
const byOrg = await list(`?org=${id.orgB}`);
|
|
100
|
+
assert.deepEqual(byOrg.body.items.map((r) => r.name).sort(), ["B guard", "B scrubber"]);
|
|
101
|
+
|
|
102
|
+
const bySite = await list(`?site=${id.siteA}`);
|
|
103
|
+
assert.equal(bySite.body.total, 2);
|
|
104
|
+
|
|
105
|
+
const cleaning = await list("?type=cleaning&limit=50");
|
|
106
|
+
assert.deepEqual(cleaning.body.items.map((r) => r.name).sort(), ["A demo 1", "A legacy", "B scrubber"]);
|
|
107
|
+
const security = await list("?type=security");
|
|
108
|
+
assert.deepEqual(security.body.items.map((r) => r.name), ["B guard"]);
|
|
109
|
+
|
|
110
|
+
const ounitech = await list("?brand=ounitech&limit=50");
|
|
111
|
+
assert.deepEqual(ounitech.body.items.map((r) => r.name).sort(), ["A legacy", "B guard", "B scrubber"]);
|
|
112
|
+
|
|
113
|
+
const search = await list("?search=client%20b");
|
|
114
|
+
assert.equal(search.body.total, 2);
|
|
115
|
+
const pattern = await list("?search=.*");
|
|
116
|
+
assert.equal(pattern.body.total, 0, "a search is text, not a regex");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("8. sort and pagination", async () => {
|
|
120
|
+
const page1 = await list("?sort=name&order=desc&limit=3&page=1");
|
|
121
|
+
assert.deepEqual(page1.body.items.map((r) => r.name), ["B scrubber", "B guard", "A legacy"]);
|
|
122
|
+
assert.equal(page1.body.pages, 2);
|
|
123
|
+
const page2 = await list("?sort=name&order=desc&limit=3&page=2");
|
|
124
|
+
assert.deepEqual(page2.body.items.map((r) => r.name), ["A demo 1"]);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("9. bad input is a 400 for staff", async () => {
|
|
128
|
+
assert.equal((await list("?type=drone")).status, 400);
|
|
129
|
+
assert.equal((await list("?org=nope")).status, 400);
|
|
130
|
+
assert.equal((await list("?limit=1000")).status, 400);
|
|
131
|
+
assert.equal((await list("?sort=appKey")).status, 400);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("10. the summary counts stored data; faults come from robot-faults", async () => {
|
|
135
|
+
const res = await h.api("/robots/network/summary", { sid: sid.STAFF });
|
|
136
|
+
assert.equal(res.status, 200);
|
|
137
|
+
assert.equal(res.body.total, 4);
|
|
138
|
+
assert.equal(res.body.sites, 2);
|
|
139
|
+
assert.deepEqual(res.body.byType, [{ key: "cleaning", count: 3 }, { key: "security", count: 1 }]);
|
|
140
|
+
assert.equal(res.body.demo, 1);
|
|
141
|
+
assert.deepEqual(
|
|
142
|
+
res.body.byClient.map((c) => [c.name, c.count, c.sites]),
|
|
143
|
+
[["Net Client A", 2, 1], ["Net Client B", 2, 1]],
|
|
144
|
+
);
|
|
145
|
+
// counted from robot-faults (none recorded in this suite)
|
|
146
|
+
assert.equal(res.body.faults.live, true);
|
|
147
|
+
assert.equal(res.body.faults.count, 0);
|
|
148
|
+
assert.equal(res.body.offline.count, 0);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// ---- Super Admin holds every robot right at ANY client's site ----------
|
|
152
|
+
|
|
153
|
+
it("11. super admin reads live state and commands a robot at a client it is not a member of", async () => {
|
|
154
|
+
const state = await h.api(`/robots/${id.demoA}/state`, { sid: sid.STAFF });
|
|
155
|
+
assert.equal(state.status, 200, JSON.stringify(state.body));
|
|
156
|
+
const cmd = await h.api(`/robots/${id.demoA}/command`, {
|
|
157
|
+
method: "POST", sid: sid.STAFF, body: { kind: "return" },
|
|
158
|
+
});
|
|
159
|
+
assert.equal(cmd.status, 202, JSON.stringify(cmd.body));
|
|
160
|
+
// and a client's manager still cannot reach another client's robot
|
|
161
|
+
assert.equal((await h.api(`/robots/${id.scrubberB}/state`, { sid: sid.MANAGER })).status, 404);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("12. super admin saves a building map and asks for the lift at another client's site", async () => {
|
|
165
|
+
const map = await h.api("/robots/building-maps", {
|
|
166
|
+
method: "POST",
|
|
167
|
+
sid: sid.STAFF,
|
|
168
|
+
body: {
|
|
169
|
+
robotId: String(id.demoA),
|
|
170
|
+
name: "Net tower",
|
|
171
|
+
levels: [{ floorNo: 1, elevationM: 0 }, { floorNo: 2 }],
|
|
172
|
+
lifts: [{ name: "Lift A", x: 1, y: 1, floors: [1, 2] }],
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
assert.equal(map.status, 201, JSON.stringify(map.body));
|
|
176
|
+
const mapId = map.body.id ?? map.body._id ?? map.body.map?._id;
|
|
177
|
+
assert.ok(mapId, JSON.stringify(map.body));
|
|
178
|
+
|
|
179
|
+
const lift = await h.api(`/robots/building-maps/${mapId}/lifts/Lift%20A/sessions`, {
|
|
180
|
+
method: "POST", sid: sid.STAFF, body: { robotId: String(id.demoA), fromFloor: 1, toFloor: 2 },
|
|
181
|
+
});
|
|
182
|
+
assert.equal(lift.status, 201, JSON.stringify(lift.body));
|
|
183
|
+
const queue = await h.api(`/robots/building-maps/${mapId}/lifts`, { sid: sid.STAFF });
|
|
184
|
+
assert.equal(queue.status, 200);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("13. super admin edits and removes a robot at another client's site; it leaves the network list", async () => {
|
|
188
|
+
const edit = await h.api(`/robots/${id.scrubberB}`, {
|
|
189
|
+
method: "PATCH", sid: sid.STAFF, body: { name: "B scrubber (renamed)" },
|
|
190
|
+
});
|
|
191
|
+
assert.equal(edit.status, 200);
|
|
192
|
+
const remove = await h.api(`/robots/${id.scrubberB}`, { method: "DELETE", sid: sid.STAFF });
|
|
193
|
+
assert.equal(remove.status, 200);
|
|
194
|
+
const res = await list("?limit=50");
|
|
195
|
+
assert.equal(res.body.items.some((r) => r.name.startsWith("B scrubber")), false);
|
|
196
|
+
assert.equal(res.body.total, 3);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
async function seed(h) {
|
|
201
|
+
const now = new Date().toISOString();
|
|
202
|
+
const orgA = new ObjectId();
|
|
203
|
+
const orgB = new ObjectId();
|
|
204
|
+
const siteA = new ObjectId();
|
|
205
|
+
const siteB = new ObjectId();
|
|
206
|
+
const staffRole = new ObjectId();
|
|
207
|
+
const starRole = new ObjectId();
|
|
208
|
+
const partialRole = new ObjectId();
|
|
209
|
+
const managerRole = new ObjectId();
|
|
210
|
+
const namedRole = new ObjectId();
|
|
211
|
+
|
|
212
|
+
await h.db.collection("organizations").insertMany([
|
|
213
|
+
{ _id: orgA, name: "Net Client A", email: "neta@e2e.example.com", type: "org", nature: "property_management_agency", status: "active", createdAt: now },
|
|
214
|
+
{ _id: orgB, name: "Net Client B", email: "netb@e2e.example.com", type: "org", nature: "property_management_agency", status: "active", createdAt: now },
|
|
215
|
+
]);
|
|
216
|
+
await h.db.collection("sites").insertMany([
|
|
217
|
+
{ _id: siteA, name: "Net Site A", org: orgA, orgId: orgA, status: "active" },
|
|
218
|
+
{ _id: siteB, name: "Net Site B", org: orgB, orgId: orgB, status: "active" },
|
|
219
|
+
]);
|
|
220
|
+
await h.db.collection("roles").insertMany([
|
|
221
|
+
{ _id: staffRole, name: "Super Admin", type: "admin", default: true, permissions: [], status: "active" },
|
|
222
|
+
{ _id: starRole, name: "Ops", type: "admin", permissions: ["*"], status: "active" },
|
|
223
|
+
{ _id: partialRole, name: "Promo desk", type: "admin", permissions: ["promo-codes:see-all-promo-codes"], status: "active" },
|
|
224
|
+
{ _id: managerRole, name: "Manager A", org: orgA, type: "organization", permissions: ["*"], status: "active" },
|
|
225
|
+
{ _id: namedRole, name: "Super Admin", org: orgA, type: "organization", permissions: ["*"], status: "active" },
|
|
226
|
+
]);
|
|
227
|
+
|
|
228
|
+
const emails = [STAFF, STAR, PARTIAL, MANAGER, NAMED];
|
|
229
|
+
const users = await h.db.collection("users").insertMany(
|
|
230
|
+
emails.map((email) => ({ email, name: email.split("@")[0], status: "active", createdAt: now })),
|
|
231
|
+
);
|
|
232
|
+
const hashed = await h.hashPassword(PASSWORD);
|
|
233
|
+
await h.db.collection("users").updateMany({ email: { $in: emails } }, { $set: { password: hashed } });
|
|
234
|
+
const [staff, star, partial, manager, named] = [0, 1, 2, 3, 4].map((i) => users.insertedIds[i]);
|
|
235
|
+
|
|
236
|
+
await h.db.collection("members").insertMany([
|
|
237
|
+
{ user: staff, type: "admin", role: staffRole, status: "active" },
|
|
238
|
+
{ user: star, type: "admin", role: starRole, status: "active" },
|
|
239
|
+
{ user: partial, type: "admin", role: partialRole, status: "active" },
|
|
240
|
+
{ user: manager, org: orgA, type: "organization", role: managerRole, status: "active" },
|
|
241
|
+
{ user: named, org: orgA, type: "organization", role: namedRole, status: "active" },
|
|
242
|
+
]);
|
|
243
|
+
|
|
244
|
+
const robot = (name, org, site, extra = {}) => ({
|
|
245
|
+
name,
|
|
246
|
+
appKey: "e2e-fake-key",
|
|
247
|
+
appToken: "e2e-fake-token",
|
|
248
|
+
createdBy: manager,
|
|
249
|
+
createdByName: "net-manager",
|
|
250
|
+
robotSn: "",
|
|
251
|
+
metadata: { orgId: org, siteId: site, administratorId: "" },
|
|
252
|
+
status: "",
|
|
253
|
+
createdAt: new Date(),
|
|
254
|
+
deletedAt: "",
|
|
255
|
+
...extra,
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
const res = await h.db.collection("robots").insertMany([
|
|
259
|
+
// written before `type` / `brand` existed, and before `deletedAt` was always set
|
|
260
|
+
(() => { const r = robot("A legacy", orgA, siteA); delete r.deletedAt; return r; })(),
|
|
261
|
+
robot("A demo 1", orgA, siteA, { type: "cleaning", brand: "demo", robotSn: "DEMO-0001" }),
|
|
262
|
+
robot("A removed", orgA, siteA, { type: "cleaning", brand: "ounitech", deletedAt: new Date() }),
|
|
263
|
+
robot("B scrubber", orgB, siteB, { type: "cleaning", brand: "ounitech" }),
|
|
264
|
+
robot("B guard", orgB, siteB, { type: "security", brand: "ounitech" }),
|
|
265
|
+
]);
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
orgA, orgB, siteA, siteB,
|
|
269
|
+
demoA: res.insertedIds[1],
|
|
270
|
+
scrubberB: res.insertedIds[3],
|
|
271
|
+
};
|
|
272
|
+
}
|
|
@@ -237,3 +237,12 @@ test("re-registering a brand replaces it rather than duplicating", () => {
|
|
|
237
237
|
assert.equal(registeredRobotBrands().length, 1);
|
|
238
238
|
assert.equal(robotBrandFor("brand-a").displayName, "Two");
|
|
239
239
|
});
|
|
240
|
+
|
|
241
|
+
test("Ounitech manifest: only proven commands are offered; pause/resume/cancel are recorded as pending", async () => {
|
|
242
|
+
const { OUNITECH_CAPABILITIES, OUNITECH_PENDING_COMMANDS } = await import("./.build/services/ounitech.adapter.mjs");
|
|
243
|
+
const offered = ["full", "area", "spot", "return", "pause", "resume", "cancel", "navigate", "relocalize"]
|
|
244
|
+
.filter((k) => commandSupported(OUNITECH_CAPABILITIES, k));
|
|
245
|
+
assert.deepEqual(offered, ["full", "area", "return"]);
|
|
246
|
+
assert.deepEqual(OUNITECH_PENDING_COMMANDS, ["pause", "resume", "cancel"].map((kind) => ({ kind, reason: "awaiting Ounitech confirmation" })));
|
|
247
|
+
assert.ok(OUNITECH_PENDING_COMMANDS.every((p) => !commandSupported(OUNITECH_CAPABILITIES, p.kind)), "pending is never offered");
|
|
248
|
+
});
|
|
@@ -3,9 +3,15 @@ import { test } from "node:test";
|
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
claimFaultOnce,
|
|
6
|
+
OFFLINE_FAULT_CODE,
|
|
7
|
+
planFaultChanges,
|
|
6
8
|
robotFaultKey,
|
|
9
|
+
robotFaultSweepConfig,
|
|
10
|
+
runBounded,
|
|
11
|
+
withTimeout,
|
|
7
12
|
robotFaultNotifyEnabled,
|
|
8
13
|
robotFaultText,
|
|
14
|
+
robotConnectionText,
|
|
9
15
|
} from "./.build/utils/robot-fault.util.mjs";
|
|
10
16
|
|
|
11
17
|
test("the same fault occurrence is claimed once per process", async () => {
|
|
@@ -37,3 +43,77 @@ test("the text names the robot and the fault", () => {
|
|
|
37
43
|
assert.match(body, /Cleaner 02/);
|
|
38
44
|
assert.match(body, /Side brush jammed/);
|
|
39
45
|
});
|
|
46
|
+
|
|
47
|
+
/* ------------------------------------------- the stored record (phase b) */
|
|
48
|
+
|
|
49
|
+
const rec = (code, since = 1000, _id = code) => ({ _id, code, since: new Date(since) });
|
|
50
|
+
|
|
51
|
+
test("plan: a new fault opens; the same fault on every read changes nothing", () => {
|
|
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: [] });
|
|
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: [] });
|
|
57
|
+
});
|
|
58
|
+
|
|
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"] });
|
|
61
|
+
const again = planFaultChanges([rec("JAM", 1000)], { ok: true, online: true, fault: { code: "JAM", message: "m", since: 90000 } });
|
|
62
|
+
assert.deepEqual(again.clear, ["JAM"]);
|
|
63
|
+
assert.equal(again.open[0].code, "JAM");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("plan: offline / not reporting is an OFFLINE record; a failed read cannot clear robot faults", () => {
|
|
67
|
+
const off = planFaultChanges([], { ok: true, online: false, fault: null });
|
|
68
|
+
assert.deepEqual(off.open, [{ code: OFFLINE_FAULT_CODE, message: "Not reporting" }]);
|
|
69
|
+
const failed = planFaultChanges([rec("JAM")], { ok: false, error: "Robot X did not answer after 10000 ms" });
|
|
70
|
+
assert.deepEqual(failed.clear, [], "JAM is kept - we could not see it");
|
|
71
|
+
assert.equal(failed.open[0].code, OFFLINE_FAULT_CODE);
|
|
72
|
+
assert.match(failed.open[0].message, /^Not reporting: Robot X did not answer/);
|
|
73
|
+
// back online: OFFLINE clears, the robot's own fault is judged on the fresh read
|
|
74
|
+
const back = planFaultChanges([rec(OFFLINE_FAULT_CODE), rec("JAM")], { ok: true, online: true, fault: null });
|
|
75
|
+
assert.deepEqual(back.clear.sort(), ["JAM", OFFLINE_FAULT_CODE].sort());
|
|
76
|
+
// still offline: nothing new
|
|
77
|
+
assert.deepEqual(planFaultChanges([rec(OFFLINE_FAULT_CODE)], { ok: true, online: false }), { open: [], clear: [] });
|
|
78
|
+
// going offline does NOT clear the robot's own fault (it would re-notify on return)
|
|
79
|
+
const wentOff = planFaultChanges([rec("JAM")], { ok: true, online: false, fault: null });
|
|
80
|
+
assert.deepEqual(wentOff.clear, []);
|
|
81
|
+
assert.deepEqual(wentOff.open.map((f) => f.code), [OFFLINE_FAULT_CODE]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("sweep config: on by default every 60 s; off with no deploy; values clamped", () => {
|
|
85
|
+
const d = robotFaultSweepConfig({});
|
|
86
|
+
assert.deepEqual(d, { enabled: true, seconds: 60, cron: "0 */1 * * * *", concurrency: 4, timeoutMs: 10000, offlineAlertMinutes: 10 });
|
|
87
|
+
assert.equal(robotFaultSweepConfig({ ROBOT_FAULT_SWEEP: " Off " }).enabled, false);
|
|
88
|
+
assert.equal(robotFaultSweepConfig({ ROBOT_FAULT_SWEEP_SECONDS: "30" }).cron, "*/30 * * * * *");
|
|
89
|
+
assert.equal(robotFaultSweepConfig({ ROBOT_FAULT_SWEEP_SECONDS: "300" }).cron, "0 */5 * * * *");
|
|
90
|
+
// steps that divide evenly: 45 -> 30 s, 90 -> 60 s, 25 min -> 20 min
|
|
91
|
+
assert.deepEqual([45, 90, 1500].map((v) => robotFaultSweepConfig({ ROBOT_FAULT_SWEEP_SECONDS: String(v) }).seconds), [30, 60, 1200]);
|
|
92
|
+
assert.equal(robotFaultSweepConfig({ ROBOT_FAULT_SWEEP_SECONDS: "1" }).seconds, 10);
|
|
93
|
+
assert.equal(robotFaultSweepConfig({ ROBOT_FAULT_SWEEP_SECONDS: "abc" }).seconds, 60);
|
|
94
|
+
assert.equal(robotFaultSweepConfig({ ROBOT_FAULT_SWEEP_CONCURRENCY: "99" }).concurrency, 16);
|
|
95
|
+
assert.equal(robotFaultSweepConfig({ ROBOT_FAULT_SWEEP_TIMEOUT_MS: "5" }).timeoutMs, 1000);
|
|
96
|
+
assert.equal(robotFaultSweepConfig({ ROBOT_OFFLINE_ALERT_MINUTES: "15" }).offlineAlertMinutes, 15);
|
|
97
|
+
assert.equal(robotFaultSweepConfig({ ROBOT_OFFLINE_ALERT_MINUTES: "0" }).offlineAlertMinutes, 1);
|
|
98
|
+
assert.equal(robotFaultSweepConfig({ ROBOT_OFFLINE_ALERT_MINUTES: "x" }).offlineAlertMinutes, 10);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("offline / back-online alert text names the robot and the threshold", () => {
|
|
102
|
+
assert.deepEqual(robotConnectionText("Cleaner 02", false, 10), { title: "Robot offline", body: "Cleaner 02 has not reported for 10 minutes." });
|
|
103
|
+
assert.deepEqual(robotConnectionText("", true, 10), { title: "Robot back online", body: "A robot is reporting again." });
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("bounded: never more than the limit in flight, one failure does not stop the rest; a hang times out", async () => {
|
|
107
|
+
let inFlight = 0, peak = 0, done = 0;
|
|
108
|
+
await runBounded([1, 2, 3, 4, 5, 6, 7], 3, async (n) => {
|
|
109
|
+
inFlight++; peak = Math.max(peak, inFlight);
|
|
110
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
111
|
+
inFlight--;
|
|
112
|
+
if (n === 2) throw new Error("boom");
|
|
113
|
+
done++;
|
|
114
|
+
});
|
|
115
|
+
assert.equal(peak, 3);
|
|
116
|
+
assert.equal(done, 6);
|
|
117
|
+
await assert.rejects(withTimeout(new Promise(() => {}), 20, "slow"), /slow after 20 ms/);
|
|
118
|
+
assert.equal(await withTimeout(Promise.resolve(7), 20), 7);
|
|
119
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
NETWORK_PROJECTION,
|
|
6
|
+
escapeRegex,
|
|
7
|
+
networkListPipeline,
|
|
8
|
+
networkMatch,
|
|
9
|
+
networkSummaryPipeline,
|
|
10
|
+
networkFaultsPipeline,
|
|
11
|
+
networkFaultJoin,
|
|
12
|
+
shapeFaultCounts,
|
|
13
|
+
shapeNetworkSummary,
|
|
14
|
+
} from "./.build/utils/robot-network.util.mjs";
|
|
15
|
+
|
|
16
|
+
/* --------------------------------------------------------------------------
|
|
17
|
+
* The Seven365 console's all-robots list (owner, 2026-09-23). The pipeline is
|
|
18
|
+
* where it can go wrong quietly: a removed robot listed, a credential
|
|
19
|
+
* projected, a filter on the default class that loses every row written before
|
|
20
|
+
* the field existed, or a search string treated as a pattern.
|
|
21
|
+
* ------------------------------------------------------------------------ */
|
|
22
|
+
|
|
23
|
+
const ORG = "64b7f0c2a1b2c3d4e5f60718";
|
|
24
|
+
const SITE = "64b7f0c2a1b2c3d4e5f60719";
|
|
25
|
+
|
|
26
|
+
test("a removed robot never lists: the soft-delete test is always first", () => {
|
|
27
|
+
const [first] = networkListPipeline({});
|
|
28
|
+
assert.deepEqual(first.$match.deletedAt, { $in: ["", null] });
|
|
29
|
+
const [summaryFirst] = networkSummaryPipeline();
|
|
30
|
+
assert.deepEqual(summaryFirst.$match.deletedAt, { $in: ["", null] });
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("the credentials can never leave: the projection is an allow-list without them", () => {
|
|
34
|
+
assert.equal("appKey" in NETWORK_PROJECTION, false);
|
|
35
|
+
assert.equal("appToken" in NETWORK_PROJECTION, false);
|
|
36
|
+
const facet = networkListPipeline({}).at(-1).$facet.items;
|
|
37
|
+
assert.deepEqual(facet.at(-1).$project, NETWORK_PROJECTION);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("client and site filters are ObjectIds on the indexed metadata fields", () => {
|
|
41
|
+
const m = networkMatch({ org: ORG, site: SITE });
|
|
42
|
+
assert.equal(String(m["metadata.orgId"]), ORG);
|
|
43
|
+
assert.equal(String(m["metadata.siteId"]), SITE);
|
|
44
|
+
assert.equal(typeof m["metadata.orgId"], "object");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("filtering on the DEFAULT class/brand still finds rows written before the field existed", () => {
|
|
48
|
+
assert.deepEqual(networkMatch({ type: "cleaning" }).type, { $in: ["cleaning", null, ""] });
|
|
49
|
+
assert.deepEqual(networkMatch({ brand: "ounitech" }).brand, { $in: ["ounitech", null, ""] });
|
|
50
|
+
// A non-default value is matched exactly.
|
|
51
|
+
assert.equal(networkMatch({ type: "security" }).type, "security");
|
|
52
|
+
assert.equal(networkMatch({ brand: "demo" }).brand, "demo");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("a search string is data, not a pattern", () => {
|
|
56
|
+
assert.equal(escapeRegex("a.b*(c)"), String.raw`a\.b\*\(c\)`);
|
|
57
|
+
const search = networkListPipeline({ search: "Tower (1)" }).find((s) => s.$match?.$or);
|
|
58
|
+
assert.equal(search.$match.$or[0].name.$regex, String.raw`Tower \(1\)`);
|
|
59
|
+
// and the escaped pattern matches the literal text only
|
|
60
|
+
assert.ok(new RegExp(escapeRegex("a.b*(c)")).test("xa.b*(c)y"));
|
|
61
|
+
assert.equal(new RegExp(escapeRegex("a.b")).test("axb"), false);
|
|
62
|
+
// searches the joined client and site names too
|
|
63
|
+
const fields = search.$match.$or.map((c) => Object.keys(c)[0]);
|
|
64
|
+
assert.deepEqual(fields, ["name", "robotSn", "client.name", "site.name"]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("paging is bounded and the default order groups by client then site", () => {
|
|
68
|
+
const p = networkListPipeline({ page: 3, limit: 500 });
|
|
69
|
+
const facet = p.at(-1).$facet.items;
|
|
70
|
+
assert.equal(facet[1].$limit, 100, "limit capped at 100");
|
|
71
|
+
assert.equal(facet[0].$skip, 200);
|
|
72
|
+
const sort = p.find((s) => s.$sort).$sort;
|
|
73
|
+
assert.deepEqual(Object.keys(sort), ["client.name", "site.name", "name", "_id"]);
|
|
74
|
+
const bySite = networkListPipeline({ sort: "site", order: "desc" }).find((s) => s.$sort).$sort;
|
|
75
|
+
assert.deepEqual(bySite, { "site.name": -1, _id: 1 });
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("the stored-data summary carries no fault count of its own (counted from robot-faults)", () => {
|
|
79
|
+
const s = shapeNetworkSummary({
|
|
80
|
+
total: [{ n: 5 }],
|
|
81
|
+
sites: [{ n: 2 }],
|
|
82
|
+
byType: [{ _id: "cleaning", count: 4 }, { _id: "security", count: 1 }],
|
|
83
|
+
byBrand: [{ _id: "demo", count: 3 }, { _id: "ounitech", count: 2 }],
|
|
84
|
+
byClient: [{ _id: ORG, name: "Client A", count: 5, sites: 2 }],
|
|
85
|
+
});
|
|
86
|
+
assert.equal(s.total, 5);
|
|
87
|
+
assert.equal(s.sites, 2);
|
|
88
|
+
assert.deepEqual(s.byType, [{ key: "cleaning", count: 4 }, { key: "security", count: 1 }]);
|
|
89
|
+
assert.equal(s.demo, 3);
|
|
90
|
+
assert.equal(s.byClient[0].name, "Client A");
|
|
91
|
+
assert.equal(s.faults, undefined);
|
|
92
|
+
// empty collection: zeros, not a crash
|
|
93
|
+
const empty = shapeNetworkSummary(undefined);
|
|
94
|
+
assert.equal(empty.total, 0);
|
|
95
|
+
assert.deepEqual(empty.byType.map((t) => t.count), [0, 0]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/* ------------------------------------------- robot-faults (phase b) */
|
|
99
|
+
|
|
100
|
+
test("list: every row carries its open fault / offline record; filters are server-side", () => {
|
|
101
|
+
const plain = networkListPipeline({});
|
|
102
|
+
assert.ok(plain.some((st) => st.$lookup?.from === "robot-faults"), "fault join present");
|
|
103
|
+
assert.equal(plain.some((st) => st.$match?.offline === true || st.$match?.fault), false);
|
|
104
|
+
const faults = networkListPipeline({ faultsOnly: true });
|
|
105
|
+
assert.ok(faults.some((st) => st.$match?.fault?.$ne === null));
|
|
106
|
+
const offline = networkListPipeline({ offlineOnly: true });
|
|
107
|
+
assert.ok(offline.some((st) => st.$match?.offline === true));
|
|
108
|
+
// the join reads OPEN records only, and paging comes after the filter
|
|
109
|
+
const join = networkFaultJoin()[0].$lookup;
|
|
110
|
+
assert.deepEqual(join.pipeline[0], { $match: { clearedAt: null } });
|
|
111
|
+
const iFacet = faults.findIndex((st) => st.$facet);
|
|
112
|
+
const iFilter = faults.findIndex((st) => st.$match?.fault);
|
|
113
|
+
assert.ok(iFilter < iFacet);
|
|
114
|
+
assert.equal(NETWORK_PROJECTION.fault, 1);
|
|
115
|
+
assert.equal(NETWORK_PROJECTION.offline, 1);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("list: the status filter narrows to the snapshot's ids (invalid ids dropped, empty list = nothing)", () => {
|
|
119
|
+
assert.equal(networkMatch({ ids: [ORG, "nope"] })._id.$in.length, 1);
|
|
120
|
+
assert.deepEqual(networkMatch({ ids: [] })._id, { $in: [] });
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("faults list: open only, faults before offline, longest-standing first, removed robots dropped", () => {
|
|
124
|
+
const p = networkFaultsPipeline({ org: ORG, code: "E1", type: "security", page: 2, limit: 10 });
|
|
125
|
+
assert.equal(p[0].$match.clearedAt, null);
|
|
126
|
+
assert.equal(String(p[0].$match.orgId), ORG);
|
|
127
|
+
assert.equal(p[0].$match.code, "E1");
|
|
128
|
+
assert.deepEqual(p[1].$lookup.pipeline[0], { $match: { deletedAt: { $in: ["", null] } } });
|
|
129
|
+
assert.ok(p.some((st) => st.$match?.["_r.type"] === "security"));
|
|
130
|
+
assert.deepEqual(p.find((st) => st.$sort).$sort, { _rank: 1, since: 1, _id: 1 });
|
|
131
|
+
const items = p.find((st) => st.$facet).$facet.items;
|
|
132
|
+
assert.deepEqual(items.slice(0, 2), [{ $skip: 10 }, { $limit: 10 }]);
|
|
133
|
+
const raw = JSON.stringify(items);
|
|
134
|
+
assert.equal(raw.includes("appKey") || raw.includes("appToken"), false);
|
|
135
|
+
assert.deepEqual(networkFaultsPipeline({ limit: 1000 }).find((st) => st.$facet).$facet.items[1], { $limit: 100 });
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("fault counts: robots per class, faults and offline apart, zeros when none", () => {
|
|
139
|
+
const c = shapeFaultCounts({
|
|
140
|
+
byType: [
|
|
141
|
+
{ _id: { offline: false, type: "cleaning" }, count: 3 },
|
|
142
|
+
{ _id: { offline: false, type: "security" }, count: 1 },
|
|
143
|
+
{ _id: { offline: true, type: "cleaning" }, count: 2 },
|
|
144
|
+
],
|
|
145
|
+
bySite: [{ _id: ORG, count: 4 }],
|
|
146
|
+
});
|
|
147
|
+
assert.deepEqual(c.faults, {
|
|
148
|
+
live: true, count: 4, byType: [{ key: "cleaning", count: 3 }, { key: "security", count: 1 }], bySite: [{ key: ORG, count: 4 }],
|
|
149
|
+
});
|
|
150
|
+
assert.deepEqual(c.offline, { count: 2, byType: [{ key: "cleaning", count: 2 }, { key: "security", count: 0 }] });
|
|
151
|
+
assert.equal(shapeFaultCounts().faults.count, 0);
|
|
152
|
+
assert.deepEqual(shapeFaultCounts(undefined).faults.bySite, []);
|
|
153
|
+
});
|