-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathClientConnected.test.ts
More file actions
92 lines (79 loc) · 3.11 KB
/
Copy pathClientConnected.test.ts
File metadata and controls
92 lines (79 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { describe, expect, it,vi } from "vitest";
import { mockDeep } from "vitest-mock-extended";
import { clientConnected } from "./ClientConnected";
import { UDPCommandsServices } from "./UDPCommandServices";
import { logger } from "@tf2qs/telemetry";
vi.mock("@tf2qs/telemetry", async (importActual) => {
const actual = await importActual<typeof import("@tf2qs/telemetry")>();
return {
...actual,
logger: {
...actual.logger,
emit: vi.fn(),
},
};
});
describe("clientConnected command parser", () => {
it("should parse a valid client connected message", () => {
const rawString = '02/05/2026 - 01:42:17: "sonikro<5><[U:1:29162964]><>" connected, address "169.254.249.16:18930"';
const result = clientConnected(rawString);
expect(result).not.toBeNull();
expect(result?.type).toBe("clientConnected");
expect(result?.args).toEqual({ nickname: "sonikro", ipAddress: "169.254.249.16", steamId3: "U:1:29162964" });
});
it("should return null for non-matching string", () => {
const rawString = "invalid log line";
const result = clientConnected(rawString);
expect(result).toBeNull();
});
it("should handle nicknames with special characters", () => {
const rawString = '02/05/2026 - 01:42:17: "player[TAG]<3><[U:1:12345]><>" connected, address "192.168.1.100:27015"';
const result = clientConnected(rawString);
expect(result).not.toBeNull();
expect(result?.args).toEqual({ nickname: "player[TAG]", ipAddress: "192.168.1.100", steamId3: "U:1:12345" });
});
describe("handler", () => {
const rawString = '02/05/2026 - 01:42:17: "sonikro<5><[U:1:29162964]><>" connected, address "169.254.249.16:18930"';
function createTestEnvironment() {
const services = mockDeep<UDPCommandsServices>();
const command = clientConnected(rawString);
const handler = command?.handler;
return { services, command, handler };
}
it("should log client connection information", async () => {
const { services, command, handler } = createTestEnvironment();
if (!command || !handler) throw new Error("Command or handler is undefined");
await handler({
args: command.args,
password: "test-password",
services,
});
expect(logger.emit).toHaveBeenCalledWith({
severityText: "INFO",
body: `Client connected to server`,
attributes: {
nickname: "sonikro",
ipAddress: "169.254.249.16",
steamId3: "U:1:29162964"
}
});
});
it("should save connection history to repository", async () => {
const { services, command, handler } = createTestEnvironment();
if (!command || !handler) throw new Error("Command or handler is undefined");
await handler({
args: command.args,
password: "test-password",
services,
});
expect(services.playerConnectionHistoryRepository.save).toHaveBeenCalledWith({
connectionHistory: {
steamId3: "U:1:29162964",
ipAddress: "169.254.249.16",
nickname: "sonikro",
timestamp: expect.any(Date),
},
});
});
});
});