148 lines
4.0 KiB
JavaScript
148 lines
4.0 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import { buildSettingsUpdatePayload, normalizeSettingsPositiveInteger } from "../../src/lib/settings.js";
|
|
|
|
test("does not resend stored secrets when secret inputs are blank", () => {
|
|
const payload = buildSettingsUpdatePayload({
|
|
id: "settings-1",
|
|
k8s: {
|
|
endpoint: "https://cluster.example.com",
|
|
namespace: "droboticflow",
|
|
kubeconfig: "",
|
|
hasKubeconfig: true,
|
|
kubeconfigPreview: "apiV****ig==",
|
|
},
|
|
worker: {
|
|
pollIntervalMs: 2000,
|
|
maxConcurrentRuns: 4,
|
|
},
|
|
ai: {
|
|
provider: "openai-compatible",
|
|
baseURL: "https://ai.example/v1",
|
|
model: "model-x",
|
|
apiKey: "",
|
|
hasAPIKey: true,
|
|
apiKeyPreview: "sk-t****1234",
|
|
},
|
|
defaults: {
|
|
cpuRequest: "500m",
|
|
cpuLimit: "1000m",
|
|
memoryRequest: "1Gi",
|
|
memoryLimit: "2Gi",
|
|
},
|
|
timezone: "Asia/Shanghai",
|
|
});
|
|
|
|
assert.equal("kubeconfig" in payload.k8s, false);
|
|
assert.equal("kubeconfigPreview" in payload.k8s, false);
|
|
assert.equal("apiKey" in payload.ai, false);
|
|
assert.equal("apiKeyPreview" in payload.ai, false);
|
|
});
|
|
|
|
test("sends new secrets when the user provides replacement values", () => {
|
|
const payload = buildSettingsUpdatePayload({
|
|
id: "settings-1",
|
|
k8s: {
|
|
endpoint: "https://cluster.example.com",
|
|
namespace: "droboticflow",
|
|
kubeconfig: " apiVersion: v1\nkind: Config ",
|
|
hasKubeconfig: true,
|
|
},
|
|
worker: {
|
|
pollIntervalMs: 2000,
|
|
maxConcurrentRuns: 4,
|
|
},
|
|
ai: {
|
|
provider: "openai-compatible",
|
|
baseURL: "https://ai.example/v1",
|
|
model: "model-x",
|
|
apiKey: " new-key ",
|
|
hasAPIKey: true,
|
|
},
|
|
defaults: {
|
|
cpuRequest: "500m",
|
|
cpuLimit: "1000m",
|
|
memoryRequest: "1Gi",
|
|
memoryLimit: "2Gi",
|
|
},
|
|
timezone: "Asia/Shanghai",
|
|
});
|
|
|
|
assert.equal(payload.k8s.kubeconfig, "apiVersion: v1\nkind: Config");
|
|
assert.equal(payload.ai.apiKey, "new-key");
|
|
});
|
|
|
|
test("normalizes settings positive integer values", () => {
|
|
assert.equal(normalizeSettingsPositiveInteger(" 2000 ", "worker.pollIntervalMs"), 2000);
|
|
assert.equal(normalizeSettingsPositiveInteger(4, "worker.maxConcurrentRuns"), 4);
|
|
});
|
|
|
|
test("rejects lossy settings positive integer values", () => {
|
|
for (const value of ["", "0", "01", "1.5", "2000ms", Number.MAX_SAFE_INTEGER + 1]) {
|
|
assert.throws(
|
|
() => normalizeSettingsPositiveInteger(value, "worker.pollIntervalMs"),
|
|
/worker\.pollIntervalMs must be a (safe )?positive integer/,
|
|
);
|
|
}
|
|
});
|
|
|
|
test("normalizes worker numbers when building settings update payload", () => {
|
|
const payload = buildSettingsUpdatePayload({
|
|
id: "settings-1",
|
|
k8s: {
|
|
endpoint: "https://cluster.example.com",
|
|
namespace: "droboticflow",
|
|
},
|
|
worker: {
|
|
pollIntervalMs: " 2000 ",
|
|
maxConcurrentRuns: "4",
|
|
},
|
|
ai: {
|
|
provider: "openai-compatible",
|
|
baseURL: "https://ai.example/v1",
|
|
model: "model-x",
|
|
},
|
|
defaults: {
|
|
cpuRequest: "500m",
|
|
cpuLimit: "1000m",
|
|
memoryRequest: "1Gi",
|
|
memoryLimit: "2Gi",
|
|
},
|
|
timezone: "Asia/Shanghai",
|
|
});
|
|
|
|
assert.equal(payload.worker.pollIntervalMs, 2000);
|
|
assert.equal(payload.worker.maxConcurrentRuns, 4);
|
|
});
|
|
|
|
test("rejects invalid worker numbers when building settings update payload", () => {
|
|
assert.throws(
|
|
() =>
|
|
buildSettingsUpdatePayload({
|
|
id: "settings-1",
|
|
k8s: {
|
|
endpoint: "https://cluster.example.com",
|
|
namespace: "droboticflow",
|
|
},
|
|
worker: {
|
|
pollIntervalMs: "2s",
|
|
maxConcurrentRuns: 4,
|
|
},
|
|
ai: {
|
|
provider: "openai-compatible",
|
|
baseURL: "https://ai.example/v1",
|
|
model: "model-x",
|
|
},
|
|
defaults: {
|
|
cpuRequest: "500m",
|
|
cpuLimit: "1000m",
|
|
memoryRequest: "1Gi",
|
|
memoryLimit: "2Gi",
|
|
},
|
|
timezone: "Asia/Shanghai",
|
|
}),
|
|
/worker\.pollIntervalMs must be a positive integer/,
|
|
);
|
|
});
|