@7365admin1/core 3.118.0 → 3.120.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@7365admin1/core",
3
3
  "license": "MIT",
4
- "version": "3.118.0",
4
+ "version": "3.120.0",
5
5
  "author": "7365admin1",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -0,0 +1,169 @@
1
+ import assert from "node:assert/strict";
2
+ import { createHash } from "node:crypto";
3
+ import http from "node:http";
4
+ import { once } from "node:events";
5
+ import { after, before, test } from "node:test";
6
+
7
+ import {
8
+ OUNITECH_TIMESTAMP_FORMAT,
9
+ ROBOT_CLOUD_PATHS,
10
+ ROBOT_CLOUD_TIMESTAMP_FORMATS,
11
+ robotCloudBody,
12
+ robotCloudTimestamp,
13
+ } from "./.build/utils/robot-cloud.util.mjs";
14
+ import { useRobotFleetService } from "./.build/services/robot-fleet.service.mjs";
15
+
16
+ // Fake credentials only - nothing here reaches Ounitech.
17
+ const APPID = "probe-appid";
18
+ const SECRET = "probe-secret-value";
19
+ // 09:30:05.123 in Singapore = 01:30:05.123 UTC. A local-time bug would print 09.
20
+ const CLOCK = new Date("2026-09-26T09:30:05.123+08:00");
21
+ const sha1 = (s) => createHash("sha1").update(s).digest("hex");
22
+ const PATH = ROBOT_CLOUD_PATHS.robotList;
23
+
24
+ const EXPECTED = {
25
+ yyyyMMddHHmmss: "20260926013005",
26
+ "unix-s": "1790386205",
27
+ "unix-ms": "1790386205123",
28
+ "yyyy-MM-dd HH:mm:ss": "2026-09-26 01:30:05",
29
+ iso8601: "2026-09-26T01:30:05Z",
30
+ "unix-s-number": "1790386205",
31
+ "unix-ms-number": "1790386205123",
32
+ };
33
+
34
+ test("default format is unchanged until the probe proves another", () => {
35
+ assert.equal(OUNITECH_TIMESTAMP_FORMAT, "yyyyMMddHHmmss");
36
+ assert.equal(robotCloudTimestamp(CLOCK), EXPECTED.yyyyMMddHHmmss);
37
+ assert.deepEqual([...ROBOT_CLOUD_TIMESTAMP_FORMATS].sort(), Object.keys(EXPECTED).sort());
38
+ });
39
+
40
+ for (const [format, text] of Object.entries(EXPECTED)) {
41
+ test(`format ${format}: UTC+0 text and a sign recomputed over that exact text`, () => {
42
+ assert.equal(robotCloudTimestamp(CLOCK, format), text);
43
+ const body = robotCloudBody(APPID, SECRET, PATH, {}, CLOCK, format);
44
+ assert.equal(body.sign, sha1(`appid=${APPID},timestamp=${text},url=${PATH},secret=${SECRET}`));
45
+ assert.equal(body.timestamp, format.endsWith("-number") ? Number(text) : text);
46
+ assert.equal(JSON.stringify(body).includes(SECRET), false);
47
+ });
48
+ }
49
+
50
+ /* -------------------------------------------------------------- the probe */
51
+
52
+ // A mock Ounitech that accepts only one format, checking the sign server-side.
53
+ let server;
54
+ let base;
55
+ const calls = [];
56
+ const matches = {
57
+ "unix-ms": (t) => typeof t === "string" && /^\d{13}$/.test(t),
58
+ none: () => false,
59
+ };
60
+
61
+ before(async () => {
62
+ server = http.createServer((req, res) => {
63
+ let raw = "";
64
+ req.on("data", (c) => (raw += c));
65
+ req.on("end", () => {
66
+ const [, mode, ...rest] = req.url.split("/");
67
+ const path = "/" + rest.join("/");
68
+ const body = JSON.parse(raw);
69
+ calls.push({ mode, path, body });
70
+ const signOk = body.sign === sha1(`appid=${body.appid},timestamp=${body.timestamp},url=${path},secret=${SECRET}`);
71
+ res.writeHead(200, { "content-type": "application/json" });
72
+ res.end(JSON.stringify(signOk && matches[mode](body.timestamp) ? { robotList: [{ robotSn: "SN-1" }] } : { ok: false, msg: "bad timestamp" }));
73
+ });
74
+ });
75
+ server.listen(0, "127.0.0.1");
76
+ await once(server, "listening");
77
+ base = `http://127.0.0.1:${server.address().port}`;
78
+ });
79
+ after(() => server.close());
80
+
81
+ const robotRow = (id) => ({ _id: id, name: "HERO", brand: "ounitech", robotSn: "", appKey: APPID, appToken: SECRET, metadata: {} });
82
+ const fleetFor = (mode) => useRobotFleetService({ cloudOptions: { baseUrl: `${base}/${mode}`, timeoutMs: 1000 } });
83
+
84
+ test("probe: tries formats in order on the robot list only, stops at the first accepted", async () => {
85
+ calls.length = 0;
86
+ const r = await fleetFor("unix-ms").probeTimestampFormat(robotRow("a1"));
87
+ assert.equal(r.accepted, "unix-ms");
88
+ assert.deepEqual(r.attempts.map((a) => [a.format, a.ok]), [["yyyyMMddHHmmss", false], ["unix-s", false], ["unix-ms", true]]);
89
+ assert.equal(r.attempts[0].vendorMessage, "bad timestamp");
90
+ assert.ok(calls.every((c) => c.path === PATH), "only the robot list is called");
91
+ assert.equal(calls.length, 3);
92
+ const text = JSON.stringify(r);
93
+ assert.equal(text.includes(SECRET), false);
94
+ assert.equal(text.includes(APPID), false);
95
+ assert.equal(/"sign"|[0-9a-f]{40}/.test(text), false, "no sign field or SHA-1 value");
96
+ });
97
+
98
+ test("probe: none accepted says so; a second run within a minute is refused with 429", async () => {
99
+ const fleet = fleetFor("none");
100
+ const r = await fleet.probeTimestampFormat(robotRow("a2"));
101
+ assert.equal(r.accepted, null);
102
+ assert.equal(r.attempts.length, ROBOT_CLOUD_TIMESTAMP_FORMATS.length);
103
+ assert.match(r.summary, /accepted none/);
104
+ await assert.rejects(fleet.probeTimestampFormat(robotRow("a2")), (e) => e.statusCode === 429);
105
+ });
106
+
107
+ test("probe: refuses non-Ounitech robots and robots with no credentials, without a vendor call", async () => {
108
+ calls.length = 0;
109
+ await assert.rejects(fleetFor("none").probeTimestampFormat({ ...robotRow("a3"), brand: "padbot" }), (e) => e.statusCode === 400);
110
+ await assert.rejects(fleetFor("none").probeTimestampFormat({ ...robotRow("a4"), appKey: "", appToken: "" }), (e) => e.statusCode === 400);
111
+ assert.equal(calls.length, 0);
112
+ });
113
+
114
+ /* ------------------------------------------------------- the offset probe */
115
+
116
+ // A mock Ounitech whose clock runs 30 s ahead and which accepts yyyyMMddHHmmss
117
+ // only at its own clock + 8 h (within 2 s), checking the sign server-side.
118
+ let offServer;
119
+ let offBase;
120
+ const offCalls = [];
121
+ const SKEW_MS = 30_000;
122
+ const parseCompact = (t) =>
123
+ Date.UTC(+t.slice(0, 4), +t.slice(4, 6) - 1, +t.slice(6, 8), +t.slice(8, 10), +t.slice(10, 12), +t.slice(12, 14));
124
+
125
+ before(async () => {
126
+ offServer = http.createServer((req, res) => {
127
+ let raw = "";
128
+ req.on("data", (c) => (raw += c));
129
+ req.on("end", () => {
130
+ const [, mode, ...rest] = req.url.split("/");
131
+ const path = "/" + rest.join("/");
132
+ const body = JSON.parse(raw);
133
+ offCalls.push({ path, body });
134
+ const vendorNow = Date.now() + SKEW_MS;
135
+ const signOk = body.sign === sha1(`appid=${body.appid},timestamp=${body.timestamp},url=${path},secret=${SECRET}`);
136
+ const want = mode === "plus8" ? vendorNow + 8 * 3600_000 : NaN;
137
+ const ok = signOk && /^\d{14}$/.test(body.timestamp) && Math.abs(parseCompact(body.timestamp) - want) < 2000;
138
+ res.writeHead(200, { "content-type": "application/json", date: new Date(vendorNow).toUTCString() });
139
+ res.end(JSON.stringify(ok ? { robotList: [] } : { ok: false, msg: "bad timestamp" }));
140
+ });
141
+ });
142
+ offServer.listen(0, "127.0.0.1");
143
+ await once(offServer, "listening");
144
+ offBase = `http://127.0.0.1:${offServer.address().port}`;
145
+ });
146
+ after(() => offServer.close());
147
+
148
+ const offFleet = (mode) => useRobotFleetService({ cloudOptions: { baseUrl: `${offBase}/${mode}`, timeoutMs: 1000 } });
149
+
150
+ test("offset probe: learns the vendor clock, tries offsets in order, stops at the first accepted (+8 h)", async () => {
151
+ offCalls.length = 0;
152
+ const r = await offFleet("plus8").probeTimestampOffset(robotRow("o1"));
153
+ assert.equal(r.acceptedOffsetMinutes, 480);
154
+ assert.deepEqual(r.attempts.map((a) => a.offsetMinutes), [0, 120, 60, 480]);
155
+ assert.ok(Math.abs(r.vendorSkewSeconds - 30) <= 1);
156
+ assert.ok(offCalls.every((c) => c.path === PATH), "only the robot list is called");
157
+ assert.ok(r.attempts.every((a) => /^\d{14}$/.test(a.sent)));
158
+ const text = JSON.stringify(r);
159
+ assert.equal(text.includes(SECRET) || text.includes(APPID), false);
160
+ assert.equal(/"sign"|[0-9a-f]{40}/.test(text), false);
161
+ });
162
+
163
+ test("offset probe: none accepted tries all 8; its own once-a-minute limit, separate from the format probe", async () => {
164
+ const fleet = offFleet("none");
165
+ const r = await fleet.probeTimestampOffset(robotRow("o2"));
166
+ assert.equal(r.acceptedOffsetMinutes, null);
167
+ assert.equal(r.attempts.length, 8);
168
+ await assert.rejects(fleet.probeTimestampOffset(robotRow("o2")), (e) => e.statusCode === 429);
169
+ });