Files
electric-cherry/tests/unit/rcon.test.ts
T
joshandClaude Opus 4.7 57746eef19 Use status for the RCON probe (Plutonium doesn't wrap-recognize getstatus)
The hex dump surfaced Plutonium's actual reply:
  ff ff ff ff 70 72 69 6e 74 0a  ....print.
  55 6e 6b 6e 6f 77 6e 20 63 6f  Unknown co
  6d 6d 61 6e 64 20 22 67 65 74  mmand "get
  73 74 61 74 75 73 22 0a        status".

Auth was fine — Plutonium just doesn't recognize `getstatus` as an rcon
subcommand. In Q3-derived engines, `getstatus` is an anonymous query
(no rcon wrapper); we were sending `rcon <password> getstatus`, which
made Plutonium treat it as an unknown subcommand.

Switch rconStatusResult to use `status`, same command rconGetPlayerCount
has been using successfully all along. The response is a `print` body
with a `key: value` header (`map: mp_kino\n...`) followed by the
scoreboard, not the `\info\` line the statusResponse dialect uses.

New parseStatusHeader extracts key:value dvars from the header, stopping
at the `--- ---` separator (or the first player row) so guids and names
can't leak into the metadata. rconStatusResult now merges header values
with the statusResponse info line so both dialects produce map/gametype
in the RconStatus shape.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-09-12 11:19:26 -04:00

181 lines
6.3 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
parseInfoLine,
parsePlayerCount,
parsePlayerRoster,
parseStatusHeader,
formatRawReply
} from '../../src/lib/server/integrations/rcon.js';
describe('parsePlayerCount', () => {
it('counts rows after --- --- separator (status dialect)', () => {
const body = [
'print',
'map: mp_kino',
'num score ping guid name',
'--- ----- ---- -------- ----',
' 0 12 45 abc123def456 joe',
' 1 0 72 xyz789abc123 jill',
' 2 999 30 111222333444 kade'
].join('\n');
expect(parsePlayerCount(body)).toBe(3);
});
it('returns 0 when the scoreboard has no rows', () => {
const body = [
'print',
'map: mp_ascension',
'num score ping guid name',
'--- ----- ---- -------- ----',
''
].join('\n');
expect(parsePlayerCount(body)).toBe(0);
});
it('counts non-blank lines after the info line (statusResponse dialect)', () => {
const body = [
'statusResponse',
'\\mapname\\mp_kino\\g_gametype\\zom\\sv_maxclients\\4',
'10 45 "joe"',
'0 72 "jill"'
].join('\n');
expect(parsePlayerCount(body)).toBe(2);
});
it('returns null on malformed payloads with no separator or info line', () => {
expect(parsePlayerCount('nonsense body\nno structure here')).toBeNull();
});
});
describe('parseInfoLine', () => {
it('splits \\key\\value pairs from an info string', () => {
const body = [
'statusResponse',
'\\mapname\\mp_kino\\g_gametype\\zom\\sv_maxclients\\4'
].join('\n');
const info = parseInfoLine(body);
expect(info.mapname).toBe('mp_kino');
expect(info.g_gametype).toBe('zom');
expect(info.sv_maxclients).toBe('4');
});
it('returns empty object when no info line is present', () => {
expect(parseInfoLine('nothing here')).toEqual({});
});
});
describe('parsePlayerRoster', () => {
it('extracts num/score/ping/guid/name from print dialect', () => {
const body = [
'print',
'map: mp_kino',
'num score ping guid name',
'--- ----- ---- -------- ----',
' 0 12 45 aaaaaaaabbbbbbbbccccccccdddddddd joe',
' 1 0 72 11112222333344445555666677778888 jill h.',
' 2 999 128 abcdef0123456789abcdef0123456789 Toaster'
].join('\n');
const roster = parsePlayerRoster(body);
expect(roster).toHaveLength(3);
expect(roster[0]).toEqual({
num: 0,
score: 12,
ping: 45,
guid: 'aaaaaaaabbbbbbbbccccccccdddddddd',
name: 'joe'
});
// Names with spaces stay intact
expect(roster[1].name).toBe('jill h.');
expect(roster[2].ping).toBe(128);
});
it('extracts score/ping/quoted-name from statusResponse dialect', () => {
const body = [
'statusResponse',
'\\mapname\\mp_kino\\g_gametype\\zclassic',
'10 45 "joe"',
'0 72 "jill h."'
].join('\n');
const roster = parsePlayerRoster(body);
expect(roster).toHaveLength(2);
expect(roster[0]).toEqual({
num: null,
score: 10,
ping: 45,
guid: null,
name: 'joe'
});
expect(roster[1].name).toBe('jill h.');
});
it('returns empty array when no rows are present', () => {
const empty = [
'print',
'map: mp_ascension',
'num score ping guid name',
'--- ----- ---- -------- ----'
].join('\n');
expect(parsePlayerRoster(empty)).toEqual([]);
expect(parsePlayerRoster('nonsense')).toEqual([]);
});
});
describe('parseStatusHeader', () => {
it('extracts key:value header lines from a Plutonium status body', () => {
const body = [
'print',
'map: mp_kino',
'num score ping guid name',
'--- ----- ---- -------- ----',
' 0 12 45 abc123def456 joe'
].join('\n');
const h = parseStatusHeader(body);
expect(h.map).toBe('mp_kino');
// Header keys stop at the scoreboard — guids/names must not leak in.
expect(h.abc123def456).toBeUndefined();
});
it('handles builds that include gametype or other dvars in the header', () => {
const body = [
'print',
'map: mp_ascension',
'gametype: zclassic',
'sv_maxclients: 4',
'--- --- ---',
''
].join('\n');
const h = parseStatusHeader(body);
expect(h.map).toBe('mp_ascension');
expect(h.gametype).toBe('zclassic');
expect(h.sv_maxclients).toBe('4');
});
it('returns empty object for a statusResponse (no colon header)', () => {
const body = [
'statusResponse',
'\\mapname\\mp_kino\\g_gametype\\zclassic',
'10 45 "joe"'
].join('\n');
expect(parseStatusHeader(body)).toEqual({});
});
});
describe('formatRawReply', () => {
it('renders a small buffer as hex + printable ASCII with an offset', () => {
const buf = Buffer.from([0xff, 0xff, 0xff, 0xff, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x0a]);
const out = formatRawReply(buf);
expect(out).toContain('0000');
expect(out).toContain('ff ff ff ff 70 72 69 6e 74 0a');
// Non-printable prefix bytes render as `.`; the printable tail spells "print".
expect(out).toContain('....print.');
});
it('splits into 16-byte rows and truncates past the limit', () => {
const buf = Buffer.alloc(600, 0x41); // "A" repeated
const out = formatRawReply(buf);
const lines = out.split('\n');
expect(lines.length).toBeGreaterThan(1);
expect(lines[lines.length - 1]).toMatch(/more bytes/);
});
});