Files
trade-message-center/apps/server/test/websocket-diagnostics.test.ts
T

79 lines
3.2 KiB
TypeScript

// 验证 WebSocket 通用诊断不含旧资料投递契约
import assert from "node:assert/strict";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { join } from "node:path";
import test from "node:test";
import { tmpdir } from "node:os";
import { composeOneTalkDiagnosticLog } from "@trade-message-center/onetalk-contract";
import * as diagnosticFileLog from "../src/websocket/diagnostic-file-log.ts";
import type { OneTalkDiagnostic } from "../src/websocket/diagnostics.ts";
test("puts the WebSocket event and frame type in the diagnostic headline", () => {
const helloLog = composeOneTalkDiagnosticLog("server", {
event: "ws_frame",
frameType: "ws.hello",
});
const closeLog = composeOneTalkDiagnosticLog("server", { event: "ws_close" });
assert.equal(helloLog.headline, "[server] event=ws_frame frameType=ws.hello");
assert.equal(closeLog.headline, "[server] event=ws_close frameType=-");
});
test("appends complete diagnostics to the build-hash log file", () => {
const logsDirectory = mkdtempSync(join(tmpdir(), "onetalk-diagnostic-log-"));
const payload = { binding: "binding-1", content: { text: "full message" } };
try {
const writeDiagnostic = diagnosticFileLog.createOneTalkDiagnosticFileLogger({
logsDirectory,
buildHash: "0123456789abcdef",
});
writeDiagnostic({ event: "ws_frame", frameType: "ws.hello", payload });
writeDiagnostic({ event: "ws_close" });
assert.equal(
readFileSync(join(logsDirectory, "0123456789abcdef.log"), "utf8"),
[
'[server] event=ws_frame frameType=ws.hello {"binding":"binding-1","content":{"text":"full message"}}',
'[server] event=ws_close frameType=- {"event":"ws_close"}',
"",
].join("\n"),
);
} finally {
rmSync(logsDirectory, { recursive: true, force: true });
}
});
test("keeps file diagnostics disabled unless explicitly enabled", () => {
assert.equal(typeof diagnosticFileLog.isOneTalkDiagnosticFileLogEnabled, "function");
assert.equal(diagnosticFileLog.isOneTalkDiagnosticFileLogEnabled(undefined), false);
assert.equal(diagnosticFileLog.isOneTalkDiagnosticFileLogEnabled("false"), false);
assert.equal(diagnosticFileLog.isOneTalkDiagnosticFileLogEnabled("true"), true);
});
test("does not accept removed profile delivery diagnostic fields", () => {
const lifecycleDiagnostic: OneTalkDiagnostic = {
event: "ws_close",
closeCode: 1000,
closeReason: "normal",
};
assert.deepEqual(lifecycleDiagnostic, {
event: "ws_close",
closeCode: 1000,
closeReason: "normal",
});
// @ts-expect-error Hard cutover removed the profile delivery event.
const legacyEvent: OneTalkDiagnostic = { event: "profile_delivery" };
// @ts-expect-error Hard cutover removed profile delivery count diagnostics.
const legacyProfileCount: OneTalkDiagnostic = { event: "ws_frame", profileCount: 1 };
// @ts-expect-error Hard cutover removed profile delivery duration diagnostics.
const legacyDuration: OneTalkDiagnostic = { event: "ws_frame", durationMs: 1 };
void legacyEvent;
void legacyProfileCount;
void legacyDuration;
});