update server
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
const assert = require("node:assert/strict");
|
||||
const crypto = require("node:crypto");
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const { after, before, test } = require("node:test");
|
||||
|
||||
const { signLicense } = require("../license-manager");
|
||||
|
||||
let privateKeyPath;
|
||||
let publicKey;
|
||||
let temporaryDirectory;
|
||||
|
||||
before(async () => {
|
||||
temporaryDirectory = await fs.promises.mkdtemp(path.join(os.tmpdir(), "reinloop-license-"));
|
||||
const pair = crypto.generateKeyPairSync("rsa", { modulusLength: 2048 });
|
||||
privateKeyPath = path.join(temporaryDirectory, "license_private.pem");
|
||||
publicKey = pair.publicKey;
|
||||
await fs.promises.writeFile(privateKeyPath, pair.privateKey.export({
|
||||
type: "pkcs8",
|
||||
format: "pem"
|
||||
}));
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await fs.promises.rm(temporaryDirectory, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function validPayload(overrides = {}) {
|
||||
return {
|
||||
customer: "示例公司",
|
||||
company_id: "company-1",
|
||||
production_line_id: "line-1",
|
||||
device_id: "sample-co/line-1",
|
||||
issued: "2026-07-25 12:00",
|
||||
expiry: "2027-07-25 12:00",
|
||||
features: "*",
|
||||
...overrides
|
||||
};
|
||||
}
|
||||
|
||||
test("signLicense creates a Python-compatible RSA-PSS license", () => {
|
||||
const signed = signLicense(validPayload(), privateKeyPath);
|
||||
const [payloadBase64, signatureBase64] = signed.content.split("|");
|
||||
const verified = crypto.verify("sha256", Buffer.from(payloadBase64), {
|
||||
key: publicKey,
|
||||
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
||||
saltLength: crypto.constants.RSA_PSS_SALTLEN_AUTO
|
||||
}, Buffer.from(signatureBase64, "base64"));
|
||||
|
||||
assert.equal(verified, true);
|
||||
assert.deepEqual(JSON.parse(Buffer.from(payloadBase64, "base64").toString("utf8")), signed.payload);
|
||||
assert.match(signed.payload.license_id, /^[0-9a-f-]{36}$/);
|
||||
});
|
||||
|
||||
test("signLicense rejects invalid identity and date ranges", () => {
|
||||
assert.throws(() => signLicense(validPayload({ device_id: "line-only" }), privateKeyPath), /device_id/);
|
||||
assert.throws(() => signLicense(validPayload({ company_id: "" }), privateKeyPath), /company_id/);
|
||||
assert.throws(() => signLicense(validPayload({ expiry: "2026-07-24 12:00" }), privateKeyPath), /到期时间/);
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
const assert = require("node:assert/strict");
|
||||
const { test } = require("node:test");
|
||||
|
||||
const { buildChartConfiguration, normalizeJsonSeries } = require("../plot-json");
|
||||
|
||||
test("travel stability data plots pressure against motor distance", () => {
|
||||
const normalized = normalizeJsonSeries({
|
||||
stable_pressures: [
|
||||
{ distance: 1000, pressure: 12.3 },
|
||||
{ distance: 900, pressure: 15.6 },
|
||||
{ distance: 800, pressure: 18.1 }
|
||||
]
|
||||
});
|
||||
|
||||
assert.deepEqual(normalized.labels, [1000, 900, 800]);
|
||||
assert.deepEqual(normalized.series, [{
|
||||
label: "pressure",
|
||||
data: [
|
||||
{ x: 1000, y: 12.3 },
|
||||
{ x: 900, y: 15.6 },
|
||||
{ x: 800, y: 18.1 }
|
||||
]
|
||||
}]);
|
||||
assert.equal(normalized.chartKind, "travel-stability");
|
||||
assert.equal(normalized.xLabel, "行程");
|
||||
assert.equal(normalized.yLabel, "稳态压力 (kPa)");
|
||||
|
||||
const chart = buildChartConfiguration(normalized, "travel.json");
|
||||
assert.equal(chart.type, "scatter");
|
||||
assert.equal(chart.options.scales.x.type, "linear");
|
||||
assert.equal(chart.options.scales.x.min, 0);
|
||||
assert.equal(chart.options.scales.x.max, 1000);
|
||||
assert.equal(chart.options.plugins.travelPointLabels, true);
|
||||
assert.equal(chart.data.datasets[0].pointRadius, 4);
|
||||
});
|
||||
Reference in New Issue
Block a user