@7365admin1/core 3.103.0 → 3.106.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,140 @@
1
+ // Notification emails — the pure rules: who may be emailed, the one-click
2
+ // turn-off token, the verification code, and what the email says.
3
+ import { describe, it } from "node:test";
4
+ import assert from "node:assert/strict";
5
+
6
+ import {
7
+ DEFAULT_EMAIL_ALLOWLIST,
8
+ EMAIL_PER_MODULE_PER_HOUR,
9
+ codeMatches,
10
+ emailOpenToEveryone,
11
+ escapeHtml,
12
+ hashCode,
13
+ isEmailAllowed,
14
+ newCode,
15
+ notificationEmail,
16
+ offLink,
17
+ offPage,
18
+ offToken,
19
+ offTokenMatches,
20
+ openLink,
21
+ verificationEmail,
22
+ } from "./.build/utils/notification-email.util.mjs";
23
+
24
+ const U = "65f1a2b3c4d5e6f708192a3b";
25
+ const SECRET = "a".repeat(64);
26
+
27
+ describe("allow-list (staging shares the production mailer)", () => {
28
+ it("fails closed: unset means only Seven365's own domain", () => {
29
+ assert.equal(DEFAULT_EMAIL_ALLOWLIST, "*@seven365.com.sg");
30
+ assert.equal(isEmailAllowed("security@seven365.com.sg", undefined), true);
31
+ assert.equal(isEmailAllowed("Harris@Seven365.com.sg", ""), true);
32
+ assert.equal(isEmailAllowed("someone@gmail.com", undefined), false);
33
+ assert.equal(isEmailAllowed("x@evil-seven365.com.sg", undefined), false);
34
+ assert.equal(isEmailAllowed("x@seven365.com.sg.evil.com", undefined), false);
35
+ });
36
+
37
+ it("an explicit list replaces the default", () => {
38
+ const list = "*@example.org, qa@test.io @other.net";
39
+ assert.equal(isEmailAllowed("a@example.org", list), true);
40
+ assert.equal(isEmailAllowed("qa@test.io", list), true);
41
+ assert.equal(isEmailAllowed("b@test.io", list), false);
42
+ assert.equal(isEmailAllowed("c@other.net", list), true);
43
+ assert.equal(isEmailAllowed("security@seven365.com.sg", list), false);
44
+ });
45
+
46
+ it("only '*' opens it to everyone", () => {
47
+ assert.equal(isEmailAllowed("anyone@gmail.com", "*"), true);
48
+ assert.equal(emailOpenToEveryone("*"), true);
49
+ assert.equal(emailOpenToEveryone(undefined), false);
50
+ assert.equal(emailOpenToEveryone("*@seven365.com.sg"), false);
51
+ });
52
+
53
+ it("garbage is never allowed", () => {
54
+ for (const bad of ["", null, undefined, "no-at-sign", 42]) {
55
+ assert.equal(isEmailAllowed(bad, "*"), false, String(bad));
56
+ }
57
+ });
58
+ });
59
+
60
+ describe("turn-off token (no sign-in, tamper-proof)", () => {
61
+ const t = offToken(U, "feedback", SECRET);
62
+
63
+ it("matches only the same person, module and secret", () => {
64
+ assert.equal(offTokenMatches(U, "feedback", t, SECRET), true);
65
+ assert.equal(offTokenMatches(U, "workOrder", t, SECRET), false, "re-aimed at another module");
66
+ assert.equal(offTokenMatches("65f1a2b3c4d5e6f708192a3c", "feedback", t, SECRET), false, "another person");
67
+ assert.equal(offTokenMatches(U, "feedback", t, "b".repeat(64)), false, "another secret");
68
+ assert.equal(offTokenMatches(U, "feedback", t.slice(0, -1) + (t.endsWith("A") ? "B" : "A"), SECRET), false);
69
+ assert.equal(offTokenMatches(U, "feedback", t, undefined), false, "no secret stored");
70
+ assert.equal(offTokenMatches(U, "feedback", "", SECRET), false);
71
+ });
72
+
73
+ it("the link carries person, module and token", () => {
74
+ const url = new URL(offLink("https://account.example", U, "feedback", SECRET));
75
+ assert.equal(url.pathname, "/api/notification-preferences/email/off");
76
+ assert.equal(url.searchParams.get("u"), U);
77
+ assert.equal(url.searchParams.get("c"), "feedback");
78
+ assert.equal(url.searchParams.get("t"), t);
79
+ });
80
+ });
81
+
82
+ describe("verification code", () => {
83
+ it("is six digits and only its hash, bound to the person, is compared", () => {
84
+ for (let i = 0; i < 50; i++) assert.match(newCode(), /^\d{6}$/);
85
+ const h = hashCode(U, "123456");
86
+ assert.equal(codeMatches(U, "123456", h), true);
87
+ assert.equal(codeMatches(U, " 123456 ", h), true);
88
+ assert.equal(codeMatches(U, "123457", h), false);
89
+ assert.equal(codeMatches("65f1a2b3c4d5e6f708192a3c", "123456", h), false);
90
+ assert.equal(codeMatches(U, "123456", ""), false);
91
+ });
92
+
93
+ it("the email says what to do in plain words", () => {
94
+ const m = verificationEmail("042917");
95
+ assert.match(m.subject, /email code/i);
96
+ assert.match(m.html, /042917/);
97
+ assert.match(m.text, /ignore this email/);
98
+ });
99
+ });
100
+
101
+ describe("the notification email", () => {
102
+ const base = {
103
+ title: "Work order <updated>",
104
+ body: 'Status is now "Done" & closed',
105
+ moduleLabel: "Work Orders",
106
+ openUrl: openLink("https://acc", { kind: "workOrder", id: "abc", site: "s1", org: "o1" }),
107
+ offUrl: "https://acc/api/notification-preferences/email/off?u=1&c=workOrder&t=x",
108
+ settingsUrl: "https://acc/notification-settings",
109
+ };
110
+
111
+ it("has the button, why, one-click turn-off and settings", () => {
112
+ const m = notificationEmail(base);
113
+ assert.equal(m.subject, base.title);
114
+ assert.match(m.html, /Open in iService365/);
115
+ assert.match(m.html, /Why am I getting this\?/);
116
+ assert.match(m.html, /Turn off email for Work Orders/);
117
+ assert.match(m.html, /Notification settings/);
118
+ assert.match(m.text, /notifications\/open\?kind=workOrder&id=abc&site=s1&org=o1/);
119
+ assert.doesNotMatch(m.html, /hold|for up to an hour/);
120
+ });
121
+
122
+ it("escapes what a user typed", () => {
123
+ const m = notificationEmail(base);
124
+ assert.doesNotMatch(m.html, /<updated>/);
125
+ assert.match(m.html, /&lt;updated&gt;/);
126
+ assert.match(m.html, /&quot;Done&quot; &amp; closed/);
127
+ assert.equal(escapeHtml(`<a href="x">'`), "&lt;a href=&quot;x&quot;&gt;&#39;");
128
+ });
129
+
130
+ it("the last one this hour says the rest are held", () => {
131
+ const m = notificationEmail({ ...base, lastThisHour: true });
132
+ assert.match(m.text, new RegExp(`${EMAIL_PER_MODULE_PER_HOUR} Work Orders emails in the last hour`));
133
+ assert.match(m.text, /still arrives in the app/);
134
+ });
135
+
136
+ it("the turn-off page is plain", () => {
137
+ assert.match(offPage(true, "Feedback", "https://acc/notification-settings"), /Email for Feedback is off/);
138
+ assert.match(offPage(false, "Other", "x"), /This link does not work/);
139
+ });
140
+ });
@@ -0,0 +1,220 @@
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+
4
+ import {
5
+ DEFAULT_PLAN_FRAME,
6
+ fromPlan,
7
+ legacyFromRmf,
8
+ mergeLegacyIntoRmf,
9
+ rmfFromFleetCore,
10
+ rmfProblem,
11
+ rmfSummary,
12
+ toBuildingYaml,
13
+ toNavGraphs,
14
+ toPlan,
15
+ } from "./.build/utils/rmf-building.util.mjs";
16
+ import { rmfBuildingSchema, buildingMapBodySchema } from "./.build/models/robot-building-map.model.mjs";
17
+
18
+ // A BuildingMap.msg as the fleet server returns it: two levels, walls, a hinged
19
+ // and a double-sliding door, one nav graph with a charger and a bidirectional +
20
+ // a one-way lane (speed limit, mutex), and a lift with one cabin door.
21
+ const MSG = {
22
+ name: "tower",
23
+ levels: [
24
+ {
25
+ name: "L1",
26
+ elevation: 0,
27
+ doors: [
28
+ { name: "main", v1_x: 4, v1_y: -1, v2_x: 6, v2_y: -1, door_type: 6, motion_range: Math.PI / 2, motion_direction: -1 },
29
+ { name: "slide", v1_x: 10, v1_y: -5, v2_x: 10, v2_y: -7, door_type: 2 },
30
+ ],
31
+ nav_graphs: [
32
+ {
33
+ name: "0",
34
+ vertices: [
35
+ { x: 5, y: 0, name: "entry", params: [{ name: "is_holding_point" }] },
36
+ { x: 5, y: -4, name: "hall", params: [] },
37
+ { x: 12, y: -4, name: "dock", params: [{ name: "is_charger", type: 4, value_bool: true }, { name: "dock_name", type: 1, value_string: "dock1" }] },
38
+ ],
39
+ edges: [
40
+ { v1_idx: 0, v2_idx: 1, edge_type: 0, params: [] },
41
+ { v1_idx: 1, v2_idx: 2, edge_type: 1, params: [{ name: "speed_limit", type: 3, value_float: 0.5 }, { name: "mutex", type: 1, value_string: "corridor" }] },
42
+ ],
43
+ },
44
+ ],
45
+ wall_graph: {
46
+ name: "WallGraph",
47
+ vertices: [{ x: 0, y: 0 }, { x: 20, y: 0 }, { x: 20, y: -10 }, { x: 0, y: -10 }],
48
+ edges: [{ v1_idx: 0, v2_idx: 1 }, { v1_idx: 1, v2_idx: 2 }, { v1_idx: 2, v2_idx: 3 }, { v1_idx: 3, v2_idx: 0 }],
49
+ },
50
+ },
51
+ { name: "L2", elevation: 4, doors: [], nav_graphs: [{ name: "0", vertices: [{ x: 1, y: -1, name: "top" }, { x: 3, y: -1, name: "" }], edges: [{ v1_idx: 0, v2_idx: 1, edge_type: 0 }] }], wall_graph: { vertices: [], edges: [] } },
52
+ ],
53
+ lifts: [
54
+ {
55
+ name: "Lift1",
56
+ levels: ["L1", "L2"],
57
+ ref_x: 15,
58
+ ref_y: -8,
59
+ ref_yaw: 0,
60
+ width: 2,
61
+ depth: 2,
62
+ doors: [{ name: "cabin", v1_x: 16, v1_y: -8.6, v2_x: 16, v2_y: -7.4, door_type: 2 }],
63
+ },
64
+ ],
65
+ };
66
+
67
+ test("import: levels, walls, door types, lanes + params, graphs, lift cabin doors", () => {
68
+ const rmf = rmfFromFleetCore(MSG);
69
+ assert.equal(rmf.source, "fleet-core");
70
+ assert.equal(rmf.levels.length, 2);
71
+ const l1 = rmf.levels[0];
72
+ assert.equal(l1.floorNo, 1);
73
+ assert.equal(l1.walls.length, 4);
74
+ assert.deepEqual(l1.doors.map((d) => d.type), ["double_hinged", "double_sliding"]);
75
+ assert.equal(l1.doors[0].motionDegrees, 90);
76
+ assert.equal(l1.doors[0].motionDirection, -1);
77
+ assert.equal(l1.lanes.length, 2);
78
+ assert.equal(l1.lanes[0].bidirectional, true);
79
+ assert.deepEqual({ b: l1.lanes[1].bidirectional, s: l1.lanes[1].speedLimit, m: l1.lanes[1].mutex }, { b: false, s: 0.5, m: "corridor" });
80
+ const dock = l1.vertices.find((v) => v.name === "dock");
81
+ assert.deepEqual(dock.params, { is_charger: true, dock_name: "dock1" });
82
+ assert.deepEqual(l1.vertices.find((v) => v.name === "entry").params, { is_holding_point: true });
83
+ assert.deepEqual(rmf.graphs, [{ idx: 0, name: "Graph 0", brands: ["fleet-core"] }]);
84
+ const lift = rmf.lifts[0];
85
+ assert.equal(lift.doors.length, 1);
86
+ assert.deepEqual({ x: lift.doors[0].x, y: lift.doors[0].y, w: lift.doors[0].widthM }, { x: 1, y: 0, w: 1.2 });
87
+ assert.deepEqual(lift.levelDoors, [{ level: "L1", doors: ["cabin"] }, { level: "L2", doors: ["cabin"] }]);
88
+ assert.equal(rmfProblem(rmf), null);
89
+ const { error } = rmfBuildingSchema.validate(rmf);
90
+ assert.equal(error, undefined, error?.message);
91
+ });
92
+
93
+ test("plan frame: the import frame is the robots' planShareOf extent; toPlan/fromPlan invert", () => {
94
+ const rmf = rmfFromFleetCore(MSG);
95
+ const f = rmf.levels[0].planFrame;
96
+ // nav vertices x 5..12, doors 4..10 -> 4..12 padded 3; y -7..0 padded 3
97
+ assert.deepEqual(f, { minX: 1, maxX: 15, minY: -10, maxY: 3 });
98
+ const p = { x: 7.5, y: -2.25 };
99
+ assert.deepEqual(fromPlan(f, toPlan(f, p)), p);
100
+ assert.deepEqual(toPlan(DEFAULT_PLAN_FRAME, { x: 10, y: -5 }), { x: 10, y: 5 });
101
+ });
102
+
103
+ test("legacy derived from rmf is valid for every existing reader", () => {
104
+ const legacy = legacyFromRmf(rmfFromFleetCore(MSG));
105
+ assert.deepEqual(legacy.doors.map((d) => d.type), ["hinged", "sliding"]);
106
+ assert.equal(legacy.levels[0].walls.length, 4);
107
+ assert.deepEqual(legacy.lifts[0].floors, [1, 2]);
108
+ const { error } = buildingMapBodySchema.validate({ name: "x", ...legacy });
109
+ assert.equal(error, undefined, error?.message);
110
+ });
111
+
112
+ test("a legacy save folds into rmf: lanes, waypoints, door motion and cabin doors survive", () => {
113
+ const rmf = rmfFromFleetCore(MSG);
114
+ const legacy = legacyFromRmf(rmf);
115
+ // The person moves one wall end, renames nothing, and adds a new door + a new lift.
116
+ legacy.levels[0].walls[0].end = { x: legacy.levels[0].walls[0].end.x - 1, y: legacy.levels[0].walls[0].end.y };
117
+ legacy.doors.push({ name: "new", floorNo: 2, type: "sliding", start: { x: 1, y: 1 }, end: { x: 2, y: 1 } });
118
+ legacy.lifts.push({ name: "Lift2", x: 30, y: 20, widthM: 2, depthM: 2, floors: [1, 2] });
119
+ const merged = mergeLegacyIntoRmf(rmf, legacy);
120
+ assert.equal(rmfProblem(merged), null);
121
+ const l1 = merged.levels[0];
122
+ assert.equal(l1.lanes.length, 2, "lanes kept");
123
+ assert.ok(l1.vertices.find((v) => v.name === "dock")?.params?.is_charger, "waypoint params kept");
124
+ assert.equal(l1.doors.find((d) => d.name === "main").type, "double_hinged", "door kept its RMF type");
125
+ assert.equal(l1.doors.find((d) => d.name === "main").motionDirection, -1);
126
+ assert.equal(merged.levels[1].doors[0].name, "new");
127
+ assert.equal(merged.lifts[0].doors[0].name, "cabin", "cabin door kept");
128
+ assert.equal(merged.lifts[1].doors.length, 1, "a new lift gets a default cabin door");
129
+ assert.deepEqual(merged.lifts[1].levelDoors.map((x) => x.level), ["L1", "L2"]);
130
+ // And it is stable: deriving again gives the same legacy walls.
131
+ const again = legacyFromRmf(merged);
132
+ assert.deepEqual(again.levels[0].walls, legacy.levels[0].walls);
133
+ const { error } = rmfBuildingSchema.validate(merged);
134
+ assert.equal(error, undefined, error?.message);
135
+ });
136
+
137
+ test("a door that changed kind drops its old RMF type; a removed wall's plain vertex is pruned", () => {
138
+ const rmf = rmfFromFleetCore(MSG);
139
+ const legacy = legacyFromRmf(rmf);
140
+ legacy.doors[0].type = "sliding";
141
+ legacy.levels[0].walls = legacy.levels[0].walls.slice(1);
142
+ const merged = mergeLegacyIntoRmf(rmf, legacy);
143
+ assert.equal(merged.levels[0].doors[0].type, "sliding");
144
+ assert.equal(merged.levels[0].walls.length, 3);
145
+ assert.equal(rmfProblem(merged), null);
146
+ });
147
+
148
+ test("a map drawn natively exports: default frame, y flipped up", () => {
149
+ const rmf = mergeLegacyIntoRmf(null, {
150
+ levels: [{ floorNo: 1, walls: [{ start: { x: 0, y: 0 }, end: { x: 10, y: 0 } }], zones: [] }],
151
+ doors: [],
152
+ lifts: [{ name: "L", x: 5, y: 5, widthM: 2, depthM: 2, floors: [1, 2] }],
153
+ });
154
+ assert.deepEqual(rmf.levels[0].planFrame, DEFAULT_PLAN_FRAME);
155
+ assert.deepEqual(rmf.levels[0].vertices.map((v) => [v.x, v.y]), [[0, 0], [10, 0]]);
156
+ assert.deepEqual([rmf.lifts[0].x, rmf.lifts[0].y], [5, -5]);
157
+ assert.equal(rmf.levels.length, 2, "a floor served only by a lift becomes a level");
158
+ });
159
+
160
+ test("references are checked", () => {
161
+ const rmf = rmfFromFleetCore(MSG);
162
+ rmf.levels[0].lanes.push({ v1: "v1", v2: "nope", graphIdx: 0, bidirectional: true });
163
+ assert.match(rmfProblem(rmf), /does not exist/);
164
+ const r2 = rmfFromFleetCore(MSG);
165
+ r2.levels[1].doors.push({ ...r2.levels[0].doors[0], v1: r2.levels[1].vertices[0].id, v2: r2.levels[1].vertices[1].id });
166
+ assert.match(rmfProblem(r2), /unique/);
167
+ const r3 = rmfFromFleetCore(MSG);
168
+ r3.levels[0].lanes[0].graphIdx = 5;
169
+ assert.match(rmfProblem(r3), /route graph/);
170
+ const r4 = rmfFromFleetCore(MSG);
171
+ r4.lifts[0].levelDoors[0].doors = ["ghost"];
172
+ assert.match(rmfProblem(r4), /cabin door/);
173
+ });
174
+
175
+ test("building.yaml export: cartesian metres, [type, value] params, lifts with level_doors", () => {
176
+ const y = JSON.parse(toBuildingYaml("tower", rmfFromFleetCore(MSG)));
177
+ assert.equal(y.coordinate_system, "cartesian_meters");
178
+ const l1 = y.levels.L1;
179
+ const dock = l1.vertices.find((v) => v[3] === "dock");
180
+ assert.deepEqual(dock[4], { is_charger: [4, true], dock_name: [1, "dock1"] });
181
+ assert.equal(l1.walls.length, 4);
182
+ assert.deepEqual(l1.doors[0][2].type, [1, "double_hinged"]);
183
+ assert.deepEqual(l1.lanes[1][2].speed_limit, [3, 0.5]);
184
+ assert.deepEqual(l1.lanes[0][2].graph_idx, [2, 0]);
185
+ assert.deepEqual(y.lifts.Lift1.level_doors, { L1: ["cabin"], L2: ["cabin"] });
186
+ assert.equal(y.lifts.Lift1.doors.cabin.door_type, 2);
187
+ assert.equal(y.lifts.Lift1.lowest_floor, "L1");
188
+ assert.equal(y.lifts.Lift1.highest_floor, "L2");
189
+ });
190
+
191
+ test("nav graph export: bidirectional split, dock/undock, door_name on a crossing lane, lift on a cabin vertex", () => {
192
+ const rmf = rmfFromFleetCore(MSG);
193
+ // A lane through the "main" door (4..6 at y=-1) and a vertex inside Lift1's cabin (15,-8 +-1).
194
+ const l1 = rmf.levels[0];
195
+ l1.vertices.push({ id: "vx1", x: 5, y: 2 }, { id: "vx2", x: 15, y: -8, name: "lift_stop" });
196
+ l1.lanes.push({ v1: "vx1", v2: l1.vertices.find((v) => v.name === "hall").id, graphIdx: 0, bidirectional: false });
197
+ l1.lanes.push({ v1: l1.vertices.find((v) => v.name === "dock").id, v2: "vx2", graphIdx: 0, bidirectional: true });
198
+ const g = JSON.parse(toNavGraphs("tower", rmf)[0]);
199
+ const L1 = g.levels.L1;
200
+ // entry<->hall bidirectional = 2 lanes; hall->dock one way; vx1->hall one way (crosses the door); dock<->vx2 = 2 lanes
201
+ assert.equal(L1.lanes.length, 6);
202
+ const name = (i) => L1.vertices[i][2].name;
203
+ const hallDock = L1.lanes.find((l) => name(l[0]) === "hall" && name(l[1]) === "dock");
204
+ assert.deepEqual(hallDock[2], { speed_limit: 0.5, mutex: "corridor", dock_name: "dock1" });
205
+ const fromDock = L1.lanes.find((l) => name(l[0]) === "dock" && name(l[1]) === "lift_stop");
206
+ assert.equal(fromDock[2].undock_name, "dock1");
207
+ const toDock = L1.lanes.find((l) => name(l[0]) === "lift_stop" && name(l[1]) === "dock");
208
+ assert.equal(toDock[2].dock_name, "dock1");
209
+ const throughDoor = L1.lanes.find((l) => L1.vertices[l[0]][1] === 2);
210
+ assert.equal(throughDoor[2].door_name, "main");
211
+ assert.equal(L1.vertices.find((v) => v[2].name === "lift_stop")[2].lift, "Lift1");
212
+ assert.deepEqual(g.lifts.Lift1, { position: [15, -8, 0], dims: [2, 2] });
213
+ assert.deepEqual(g.doors.main.map, "L1");
214
+ assert.equal(toNavGraphs("tower", { ...rmf, levels: rmf.levels.map((l) => ({ ...l, lanes: [] })) })[0], undefined, "empty graphs are skipped");
215
+ });
216
+
217
+ test("summary counts", () => {
218
+ assert.deepEqual(rmfSummary(rmfFromFleetCore(MSG)), { source: "fleet-core", levels: 2, lanes: 3, waypoints: 4, doors: 2, lifts: 1, graphs: 1 });
219
+ assert.equal(rmfSummary(null), null);
220
+ });
@@ -0,0 +1,110 @@
1
+ import assert from "node:assert/strict";
2
+ import crypto from "node:crypto";
3
+ import { test } from "node:test";
4
+
5
+ import {
6
+ ROBOT_CREDENTIALS_ENV,
7
+ RobotCredentialsError,
8
+ decryptCredential,
9
+ encryptCredential,
10
+ encryptedPairUpdate,
11
+ isEncryptedCredential,
12
+ robotCloudCredentials,
13
+ robotCredentialsKey,
14
+ } from "./.build/utils/robot-credentials.util.mjs";
15
+ import { adapterForRobot, clearRobotBrands, registerRobotBrand } from "./.build/utils/robot-adapter.util.mjs";
16
+
17
+ const HEX = crypto.randomBytes(32).toString("hex");
18
+ const B64 = crypto.randomBytes(32).toString("base64");
19
+
20
+ function withKey(value, fn) {
21
+ const before = process.env[ROBOT_CREDENTIALS_ENV];
22
+ if (value === undefined) delete process.env[ROBOT_CREDENTIALS_ENV];
23
+ else process.env[ROBOT_CREDENTIALS_ENV] = value;
24
+ try {
25
+ return fn();
26
+ } finally {
27
+ if (before === undefined) delete process.env[ROBOT_CREDENTIALS_ENV];
28
+ else process.env[ROBOT_CREDENTIALS_ENV] = before;
29
+ }
30
+ }
31
+
32
+ test("key: 64 hex or base64 of 32 bytes; anything else counts as no key", () => {
33
+ assert.equal(robotCredentialsKey({ [ROBOT_CREDENTIALS_ENV]: HEX }).length, 32);
34
+ assert.equal(robotCredentialsKey({ [ROBOT_CREDENTIALS_ENV]: B64 }).length, 32);
35
+ assert.equal(robotCredentialsKey({ [ROBOT_CREDENTIALS_ENV]: "" }), null);
36
+ assert.equal(robotCredentialsKey({ [ROBOT_CREDENTIALS_ENV]: "short" }), null);
37
+ assert.equal(robotCredentialsKey({ [ROBOT_CREDENTIALS_ENV]: crypto.randomBytes(16).toString("hex") }), null);
38
+ assert.equal(robotCredentialsKey({}), null);
39
+ });
40
+
41
+ test("roundtrip: encrypted value is prefixed, random per write, and decrypts", () => {
42
+ const key = Buffer.from(HEX, "hex");
43
+ const a = encryptCredential("app-key-123", "appKey", key);
44
+ const b = encryptCredential("app-key-123", "appKey", key);
45
+ assert.ok(isEncryptedCredential(a));
46
+ assert.notEqual(a, b, "fresh IV every time");
47
+ assert.equal(a.includes("app-key-123"), false);
48
+ assert.equal(decryptCredential(a, "appKey", key), "app-key-123");
49
+ assert.equal(decryptCredential(b, "appKey", key), "app-key-123");
50
+ assert.equal(encryptCredential(a, "appKey", key), a, "never double-encrypted");
51
+ });
52
+
53
+ test("tamper detection: a changed byte, the wrong field, or the wrong key all refuse", () => {
54
+ const key = Buffer.from(HEX, "hex");
55
+ const enc = encryptCredential("secret-token", "appToken", key);
56
+ const [pre, iv, tag, ct] = enc.split(":");
57
+ const flipped = Buffer.from(ct, "base64url");
58
+ flipped[0] ^= 1;
59
+ assert.throws(() => decryptCredential(`${pre}:${iv}:${tag}:${flipped.toString("base64url")}`, "appToken", key), RobotCredentialsError);
60
+ assert.throws(() => decryptCredential(enc, "appKey", key), RobotCredentialsError, "field is bound as AAD");
61
+ assert.throws(() => decryptCredential(enc, "appToken", crypto.randomBytes(32)), RobotCredentialsError);
62
+ assert.throws(() => decryptCredential("rc1:broken", "appToken", key), RobotCredentialsError);
63
+ });
64
+
65
+ test("missing key: plain passes through both ways; encrypted fails closed by name", () => {
66
+ const key = Buffer.from(HEX, "hex");
67
+ const enc = encryptCredential("k", "appKey", key);
68
+ assert.equal(encryptCredential("plain", "appKey", null), "plain");
69
+ assert.equal(decryptCredential("plain", "appKey", null), "plain");
70
+ assert.throws(() => decryptCredential(enc, "appKey", null), /no key/);
71
+ });
72
+
73
+ test("mixed: plain and encrypted pairs both reach the adapter as plain", () => {
74
+ clearRobotBrands();
75
+ const seen = [];
76
+ registerRobotBrand({
77
+ brand: "t-brand",
78
+ displayName: "T",
79
+ capabilities: {},
80
+ create: (c) => (seen.push(c), { listRobots: async () => [] }),
81
+ });
82
+ withKey(HEX, () => {
83
+ const key = robotCredentialsKey();
84
+ adapterForRobot({ brand: "t-brand", appKey: "plain-k", appToken: "plain-t" });
85
+ adapterForRobot({ brand: "t-brand", appKey: encryptCredential("enc-k", "appKey", key), appToken: encryptCredential("enc-t", "appToken", key) });
86
+ adapterForRobot({ brand: "t-brand", appKey: encryptCredential("half-k", "appKey", key), appToken: "half-t" });
87
+ });
88
+ assert.deepEqual(seen, [
89
+ { appid: "plain-k", secret: "plain-t" },
90
+ { appid: "enc-k", secret: "enc-t" },
91
+ { appid: "half-k", secret: "half-t" },
92
+ ]);
93
+ // Without the key an encrypted robot is refused, never handed an empty or ciphertext credential.
94
+ const enc = withKey(HEX, () => ({ brand: "t-brand", appKey: encryptCredential("x", "appKey"), appToken: encryptCredential("y", "appToken") }));
95
+ withKey(undefined, () => assert.throws(() => adapterForRobot(enc), /no key to read them/));
96
+ assert.equal(seen.length, 3);
97
+ clearRobotBrands();
98
+ });
99
+
100
+ test("encryptedPairUpdate: only plain, non-empty fields; nothing without a key", () => {
101
+ const key = Buffer.from(HEX, "hex");
102
+ assert.equal(encryptedPairUpdate({ appKey: "a", appToken: "b" }, null), null);
103
+ const up = encryptedPairUpdate({ appKey: "a", appToken: "b" }, key);
104
+ assert.ok(isEncryptedCredential(up.appKey) && isEncryptedCredential(up.appToken));
105
+ assert.deepEqual(Object.keys(encryptedPairUpdate({ appKey: up.appKey, appToken: "b" }, key)), ["appToken"]);
106
+ assert.equal(encryptedPairUpdate({ appKey: up.appKey, appToken: up.appToken }, key), null);
107
+ assert.equal(encryptedPairUpdate({ appKey: "", appToken: null }, key), null);
108
+ const creds = withKey(HEX, () => robotCloudCredentials({ appKey: up.appKey, appToken: " b " }));
109
+ assert.deepEqual(creds, { appid: "a", secret: "b" });
110
+ });
@@ -43,8 +43,8 @@ test("the category delivers in-app and push, and is switchable", () => {
43
43
  assert.equal(platform.rendered, true);
44
44
  assert.equal(categorySupportsChannel("platform", "inApp"), true);
45
45
  assert.equal(categorySupportsChannel("platform", "push"), true);
46
- // No mailer recipient for a staff list, so no dead email switch.
47
- assert.equal(categorySupportsChannel("platform", "email"), false);
46
+ // Staff can opt in to email too (2026-09-25): opt-in, verified-only.
47
+ assert.equal(categorySupportsChannel("platform", "email"), true);
48
48
  });
49
49
 
50
50
  test("the stored module tag is one core accepts", () => {