mineping/test/varint.test.js
Timofey Gelazoniya c7b99cb6db
feat: implement SRV record lookup and improve ping logic
Add support for DNS SRV record lookups (`_minecraft._tcp`) to
automatically resolve the correct server host and port, falling back
to the provided address if no record is found. This makes server
discovery compliant with standard Minecraft client behavior.

The Java ping implementation has been refactored:
- The `varint` module is rewritten to throw specific error codes and
  its `decodeVarInt` function now returns bytes read, which simplifies
  parsing logic.
- The core ping logic is now promise-based and modular, breaking out
  packet creation and response processing into helper functions.
- The TCP stream handler now robustly processes chunked data by
  catching recoverable decoder errors and waiting for more data,
  preventing crashes on incomplete packets.
- Error handling is improved.
2025-06-19 01:45:27 +03:00

62 lines
2.1 KiB
JavaScript

import { describe, it, expect } from "vitest";
import * as varint from "../lib/varint.js";
describe("varint.js", () => {
it("should encode and decode integers symmetrically (round-trip)", () => {
const testValues = [
0,
1,
127, // Max 1-byte
128, // Min 2-byte
255,
16383, // Max 2-byte
16384, // Min 3-byte
2147483647, // Max signed 32-bit int
-1, // Critical edge case (encodes as max unsigned int)
];
testValues.forEach((value) => {
const encoded = varint.encodeVarInt(value);
const { value: decoded } = varint.decodeVarInt(encoded, 0);
expect(decoded, `Value ${value} failed round-trip`).toBe(value);
});
});
it("should decode an integer from a non-zero offset", () => {
// [255 (invalid varint), 128 (valid varint), 127 (valid varint)]
const buffer = Buffer.from([0xff, 0x80, 0x01, 0x7f]);
const { value: decoded } = varint.decodeVarInt(buffer, 1);
expect(decoded).toBe(128);
});
it("should throw an error for a malformed varint that is too long", () => {
const invalidBuffer = Buffer.from([0x80, 0x80, 0x80, 0x80, 0x80, 0x80]);
expect(() => varint.decodeVarInt(invalidBuffer, 0)).toThrow(
"VarInt is too big or malformed"
);
});
it("should encode 16-bit unsigned shorts in big-endian format", () => {
expect(varint.encodeUShort(0)).toEqual(Buffer.from([0x00, 0x00]));
expect(varint.encodeUShort(256)).toEqual(Buffer.from([0x01, 0x00]));
expect(varint.encodeUShort(65535)).toEqual(Buffer.from([0xff, 0xff]));
});
it("should correctly assemble a Minecraft packet with a length prefix", () => {
const payloadParts = [
varint.encodeVarInt(0), // protocol
varint.encodeString("mc.example.com"), // host
varint.encodeUShort(25565), // port
];
const payload = Buffer.concat(payloadParts);
const finalPacket = varint.concatPackets(payloadParts);
const { value: decodedPacketLength, bytesRead } = varint.decodeVarInt(
finalPacket,
0
);
expect(decodedPacketLength).toBe(payload.length);
const decodedPayload = finalPacket.subarray(bytesRead);
expect(decodedPayload).toEqual(payload);
});
});