test: 锁定 Cloud Web 配置失败不监听
This commit is contained in:
@@ -7,7 +7,7 @@ import path from "node:path";
|
||||
import test from "node:test";
|
||||
import { gzipSync } from "node:zlib";
|
||||
|
||||
import { createCloudWebServer, isClientDisconnectError, validateCloudWebRuntimeConfig } from "./cloud-web-runtime.mjs";
|
||||
import { createCloudWebServer, isClientDisconnectError, serveCloudWeb } from "./cloud-web-runtime.mjs";
|
||||
|
||||
test("cloud web classifies Bun client disconnect ECONNRESET as non-fatal", () => {
|
||||
const error = new Error("aborted");
|
||||
@@ -16,7 +16,7 @@ test("cloud web classifies Bun client disconnect ECONNRESET as non-fatal", () =>
|
||||
assert.equal(isClientDisconnectError(new Error("database migration failed")), false);
|
||||
});
|
||||
|
||||
test("cloud web rejects a missing refresh replay capability before listening", async () => {
|
||||
test("serveCloudWeb rejects a missing refresh replay capability without opening health or SPA routes", async () => {
|
||||
const restoreEnv = withEnv({
|
||||
HWLAB_CLOUD_WEB_DISPLAY_TIME_ZONE: "Asia/Shanghai",
|
||||
HWLAB_CLOUD_WEB_DISPLAY_TIME_LOCALE: "zh-CN",
|
||||
@@ -29,8 +29,18 @@ test("cloud web rejects a missing refresh replay capability before listening", a
|
||||
HWLAB_WORKBENCH_RAW_HWLAB_EVENT_WINDOW_MAX_ENTRIES: "200",
|
||||
HWLAB_WORKBENCH_RAW_HWLAB_EVENT_WINDOW_MAX_RETAINED_BYTES: "1048576"
|
||||
});
|
||||
delete process.env.HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_ENABLED;
|
||||
const port = await unusedPort();
|
||||
try {
|
||||
await assert.rejects(validateCloudWebRuntimeConfig(), /HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_ENABLED/u);
|
||||
await assert.rejects(serveCloudWeb({
|
||||
port,
|
||||
serviceId: "hwlab-cloud-web",
|
||||
healthPayload: () => ({ status: "ok" }),
|
||||
sendJson() {
|
||||
assert.fail("missing runtime configuration must fail before serving any response");
|
||||
}
|
||||
}), /HWLAB_WORKBENCH_KAFKA_REFRESH_REPLAY_ENABLED/u);
|
||||
assert.equal(await canConnect(port), false, "serveCloudWeb must not bind before runtime configuration is valid");
|
||||
} finally {
|
||||
restoreEnv();
|
||||
}
|
||||
@@ -1191,6 +1201,25 @@ function listen(server) {
|
||||
});
|
||||
}
|
||||
|
||||
async function unusedPort() {
|
||||
const server = createServer();
|
||||
await listen(server);
|
||||
const port = server.address().port;
|
||||
await close(server);
|
||||
return port;
|
||||
}
|
||||
|
||||
function canConnect(port) {
|
||||
return new Promise((resolve) => {
|
||||
const socket = connect(port, "127.0.0.1");
|
||||
socket.once("connect", () => {
|
||||
socket.destroy();
|
||||
resolve(true);
|
||||
});
|
||||
socket.once("error", () => resolve(false));
|
||||
});
|
||||
}
|
||||
|
||||
function close(server) {
|
||||
return new Promise((resolve, reject) => {
|
||||
server.close((error) => (error ? reject(error) : resolve()));
|
||||
|
||||
Reference in New Issue
Block a user