Files
api/scripts/test-gateway.test.mjs
T
Jeppe Bundgaard 7d450e285e Remove outdated edge gateway object classes, add new agent implementation
Transitioned from obsolete gateway object classes (`edge_gateway_shell_action_jobs_o`, `edge_gateway_shell_events_o`, `edge_gateway_shell_sessions_o`, `edge_gateway_update_jobs_o`) to the new agent implementation (`edge-gateway-agent/agent.php`).
2026-04-21 14:13:17 +02:00

92 lines
3.1 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
DEFAULT_CONTAINER_NAME,
DEFAULT_CONTAINER_API_URL,
DEFAULT_CONTAINER_BROKER_URL,
DEFAULT_HEARTBEAT_INTERVAL_SECONDS,
buildGatewayConfig,
parseArgs,
resolveComposeNetworkName,
resolveComposeProjectName,
resolveConfigDirectory,
shouldClaimGateway,
} from "./test-gateway.mjs";
test("resolveComposeProjectName prefers COMPOSE_PROJECT_NAME", () => {
assert.equal(
resolveComposeProjectName("C:/Users/test/backend-php", { COMPOSE_PROJECT_NAME: "custom-stack" }),
"custom-stack"
);
});
test("resolveComposeProjectName falls back to backend directory name", () => {
assert.equal(resolveComposeProjectName("C:/Users/test/backend-php", {}), "backend-php");
});
test("resolveComposeNetworkName derives the default compose network", () => {
assert.equal(
resolveComposeNetworkName("C:/Users/test/backend-php", { COMPOSE_PROJECT_NAME: "custom-stack" }),
"custom-stack_default"
);
});
test("resolveConfigDirectory uses the default temp folder when none is provided", () => {
assert.equal(
resolveConfigDirectory("C:/Users/test/backend-php"),
"C:\\Users\\test\\backend-php\\.tmp\\test-gateway"
);
});
test("shouldClaimGateway requires a token when no credentials are present", () => {
assert.equal(shouldClaimGateway({}, ""), true);
assert.equal(shouldClaimGateway({ gatewayId: 12, agentToken: "secret" }, ""), false);
assert.equal(shouldClaimGateway({ gatewayId: 12, agentToken: "secret" }, "fresh-token"), true);
});
test("parseArgs accepts help without an explicit action", () => {
const options = parseArgs(["--help"]);
assert.equal(options.action, "start");
assert.equal(options.help, true);
});
test("buildGatewayConfig applies defaults for a dockerized gateway", () => {
const config = buildGatewayConfig({});
assert.equal(config.apiUrl, DEFAULT_CONTAINER_API_URL);
assert.equal(config.brokerUrl, DEFAULT_CONTAINER_BROKER_URL);
assert.equal(config.hostname, DEFAULT_CONTAINER_NAME);
assert.equal(config.restartMode, "spawn");
assert.equal(config.heartbeatIntervalSeconds, DEFAULT_HEARTBEAT_INTERVAL_SECONDS);
assert.equal(config.installDir, "/opt/truckwash-edge-agent");
assert.equal(config.agentPath, "/opt/truckwash-edge-agent/agent.php");
assert.equal(config.serviceUnitPath, "/opt/truckwash-edge-agent/truckwash-edge-agent.service");
assert.equal(config.serviceName, DEFAULT_CONTAINER_NAME);
});
test("buildGatewayConfig folds claim response into the generated config", () => {
const config = buildGatewayConfig({
existingConfig: {
installedVersion: "0.9.0",
targetVersion: "0.9.1",
},
hostname: "gw-docker-1",
claim: {
gateway: { id: 44 },
agent_token: "agent-secret",
release_channel: "stable",
},
heartbeatIntervalSeconds: 7,
});
assert.equal(config.gatewayId, 44);
assert.equal(config.agentToken, "agent-secret");
assert.equal(config.releaseChannel, "stable");
assert.equal(config.hostname, "gw-docker-1");
assert.equal(config.installedVersion, "0.9.0");
assert.equal(config.targetVersion, "0.9.1");
assert.equal(config.heartbeatIntervalSeconds, 7);
});