feat: skip SRV lookup for IP addresses and localhost

Performing DNS SRV lookups for hosts that are already direct IP
addresses or special-case hostnames like 'localhost' is
unnecessary.

In addition, several related test suite improvements
have been included:

- A bug in the Bedrock mock packet creation was fixed where the magic
  GUID was being written to an incorrect buffer offset.
- The DNS mocking strategy in the Java tests has been refactored for
  better accuracy by mocking the `dns.promises.Resolver` class.
- An error test for the Bedrock pinger was corrected to properly
  handle a promise rejection instead of a synchronous throw.
This commit is contained in:
2025-06-25 01:46:53 +03:00
parent 1e6b5b3973
commit 371fb9daa7
3 changed files with 69 additions and 35 deletions

View File

@ -1,18 +1,25 @@
import net from "node:net";
import dns from "node:dns/promises";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { pingJava } from "../lib/java.js";
import * as varint from "../lib/varint.js";
const mockResolveSrv = vi.fn();
vi.mock("node:net");
vi.mock("node:dns/promises");
vi.mock("node:dns/promises", () => ({
Resolver: vi.fn().mockImplementation(() => ({
resolveSrv: mockResolveSrv,
})),
}));
describe("pingJava", () => {
let mockSocket;
beforeEach(() => {
// Simulate no SRV record found.
dns.resolveSrv.mockResolvedValue([]);
// Reset mocks before each test.
mockResolveSrv.mockClear();
// Simulate no SRV record found by default.
mockResolveSrv.mockResolvedValue([]);
const mockHandlers = {};
mockSocket = {
@ -44,7 +51,11 @@ describe("pingJava", () => {
// Allow the async SRV lookup to complete
await vi.runAllTicks();
expect(dns.resolveSrv).toHaveBeenCalledWith(`_minecraft._tcp.${host}`);
expect(net.createConnection).toHaveBeenCalledWith({
host: host,
port: options.port,
});
expect(net.createConnection).toHaveBeenCalledWith({
host,
port: options.port,