Files
transcience/tests/unit/test_input_frame.gd
claude 050b8251a7
ci / verify (push) Successful in 48s
Stage 3: inventory, ground loot, and two loot visibilities
Four always-on-screen slots, items as data, and loot tables on enemies and
bosses. Health potions drop rarely from trash and always from the Warden;
the Warden also drops a Warden's Ration, one per living player, which does
nothing at all.

The ration is not filler. Player-instanced loot is a separate code path from
shared loot -- a distinct entity per owner, filtered per peer in the snapshot
encoder -- and the cheapest way to keep that path honest is to have something
in the game that exercises it on every boss kill.

Item actions ride the input frame rather than becoming new client messages.
InputFrame gained BTN_USE, BTN_DROP and a slot byte, which buys the packet-loss
redundancy, the replay guard on last_input_tick, ordering against movement on
the same tick, and a rate limit of one action per tick -- all of which a
separate RPC would have needed bolted back on. The cost is that anything in
the frame which must not repeat has to be edge-triggered, since frames are
resent and a starved server coasts on the last one it holds.

Instanced loot is enforced in NetCodec.encode_snapshot, beside the actor
interest radius: a peer is never told another player's copy exists. Hiding it
client-side would have been the same mistake as relying on fog to hide enemies.

Inventories live on the character and are written to the store on every
transaction, so a crash between "picked it up" and "wrote it down" cannot lose
or duplicate an item. Anything dropped becomes world-shared whatever it was
before, and a potion used at full health is refused rather than spent.

tools/diag_loot.tscn covers drop -> snapshot -> pick up -> persist -> use ->
drop plus both visibilities on the wire, for the same reason diag_progression
exists: bots are poor shots and almost never produce a drop. It asserts each
input frame was actually consumed, after an early version silently dropped its
first press and every later check passed for the wrong reason.

check.sh clean, 266 tests, SMOKE PASS, all three diagnostics green.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-04 21:16:15 +02:00

73 lines
2.6 KiB
GDScript

extends GutTest
## The input packet is the only thing a client can put on the wire, so its
## bounds are a security surface as much as a format.
func _round_trip(f: InputFrame) -> InputFrame:
var b := StreamPeerBuffer.new()
b.big_endian = false
f.write(b)
b.seek(0)
return InputFrame.read(b)
func test_round_trip_preserves_tick_and_buttons() -> void:
var f := InputFrame.make(123456, Vector2(0.5, -0.25), 1.75,
InputFrame.BTN_FIRE | InputFrame.BTN_ESCAPE)
var out := _round_trip(f)
assert_eq(out.tick, 123456)
assert_true(out.pressed(InputFrame.BTN_FIRE))
assert_true(out.pressed(InputFrame.BTN_ESCAPE))
assert_false(out.pressed(InputFrame.BTN_INTERACT))
func test_move_survives_quantisation_within_tolerance() -> void:
var out := _round_trip(InputFrame.make(1, Vector2(0.5, -0.25), 0.0, 0))
assert_almost_eq(out.move.x, 0.5, 0.01)
assert_almost_eq(out.move.y, -0.25, 0.01)
func test_aim_survives_quantisation() -> void:
var out := _round_trip(InputFrame.make(1, Vector2.ZERO, 2.5, 0))
assert_almost_eq(out.aim, 2.5, 0.001)
func test_oversized_move_cannot_be_expressed_on_the_wire() -> void:
var out := _round_trip(InputFrame.make(1, Vector2(50.0, 50.0), 0.0, 0))
assert_lt(out.move.length(), 1.45, "the byte encoding must cap the move vector")
func test_frame_is_exactly_the_declared_size() -> void:
var b := StreamPeerBuffer.new()
InputFrame.make(1, Vector2.ONE, 1.0, 7).write(b)
assert_eq(b.data_array.size(), InputFrame.SIZE)
## The slot is what makes "use item" expressible without a second message type.
func test_round_trip_preserves_the_inventory_slot() -> void:
var out := _round_trip(InputFrame.make(1, Vector2.ZERO, 0.0,
InputFrame.BTN_USE, SimConfig.INVENTORY_SLOTS - 1))
assert_eq(out.slot, SimConfig.INVENTORY_SLOTS - 1)
assert_true(out.pressed(InputFrame.BTN_USE))
assert_false(out.pressed(InputFrame.BTN_DROP))
## Every slot the game has must survive the byte it is sent in. This is the
## check that fails if the slot count ever outgrows the format.
func test_every_slot_index_survives_the_wire() -> void:
for i in SimConfig.INVENTORY_SLOTS:
assert_eq(_round_trip(InputFrame.make(1, Vector2.ZERO, 0.0, 0, i)).slot, i)
assert_lte(SimConfig.INVENTORY_SLOTS, 256,
"the slot rides in one byte")
## Button bits have to stay distinct, or one action would trigger another.
func test_button_bits_do_not_overlap() -> void:
var bits := [InputFrame.BTN_FIRE, InputFrame.BTN_ESCAPE, InputFrame.BTN_INTERACT,
InputFrame.BTN_USE, InputFrame.BTN_DROP]
var seen := 0
for bit in bits:
assert_eq(seen & bit, 0, "bit %d collides with an earlier one" % bit)
assert_lte(bit, 128, "the button field is a single byte")
seen |= bit