The two client fixes for the Atlas frames Jeroen flagged. Region rung no longer requests more planet than exists: cap_extent_to_body() caps the viewport-fit extent at the body's own region grid (the Global canvas extent echo — cols=regions_per_equator, rows=cols/2), matched by gridunit SPACING not rung name, applied before the request/cache key is built; cold-start scroll-before-Global-echo requests uncapped and lets the server clamp (documented). Kills both the side-by-side continent repeat and the past-the-pole stripe smear. Global opener now fit-scales and centers through one shared letterbox mechanism (center_offset/integer_fit_scale/fit_scale, 75%-coverage integer-vs-fractional decision, NEAREST already forced on orbital rungs per D-255's escape hatch) also used for the Region cap's letterbox remainder. Legend column reserved before fitting — pinned to the legend's own width by a cross-constant test, never overlapping the canvas. Also fixes a latent crash: NOTIFICATION_RESIZED fires mid-_ready() before children exist; null guard in the resize path. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
362 lines
16 KiB
GDScript
362 lines
16 KiB
GDScript
## T-1182 tests: step_canvas_transport.gd — the D-255(a) six-rung stepped
|
|
## transport state machine (rung ladder, cursor-anchored step math,
|
|
## viewport-fit extent, world<->canvas-local projection). All pure
|
|
## functions, no scene tree needed.
|
|
class_name TestStepCanvasTransport
|
|
extends GdUnitTestSuite
|
|
|
|
const StepCanvasTransport := preload("res://ui/implant/apps/atlas/step_canvas/step_canvas_transport.gd")
|
|
|
|
|
|
# =============================================================================
|
|
# Rung ladder — index <-> name, scroll clamping
|
|
# =============================================================================
|
|
|
|
|
|
func test_rung_at_index_zero_is_global() -> void:
|
|
assert_str(StepCanvasTransport.rung_at_index(0)).is_equal(StepCanvasTransport.RUNG_GLOBAL)
|
|
|
|
|
|
func test_rung_at_index_five_is_chunk_the_deepest() -> void:
|
|
assert_str(StepCanvasTransport.rung_at_index(5)).is_equal(StepCanvasTransport.RUNG_CHUNK)
|
|
|
|
|
|
func test_rung_at_index_clamps_out_of_range_indices() -> void:
|
|
assert_str(StepCanvasTransport.rung_at_index(-3)).is_equal(StepCanvasTransport.RUNG_GLOBAL)
|
|
assert_str(StepCanvasTransport.rung_at_index(99)).is_equal(StepCanvasTransport.RUNG_CHUNK)
|
|
|
|
|
|
func test_index_for_rung_round_trips_every_ladder_entry() -> void:
|
|
for i in range(StepCanvasTransport.RUNG_LADDER.size()):
|
|
var rung: String = StepCanvasTransport.rung_at_index(i)
|
|
assert_int(StepCanvasTransport.index_for_rung(rung)).is_equal(i)
|
|
|
|
|
|
func test_index_for_rung_unrecognized_returns_negative_one() -> void:
|
|
assert_int(StepCanvasTransport.index_for_rung("Sector")).is_equal(-1)
|
|
|
|
|
|
func test_scroll_step_descends_one_notch_at_a_time() -> void:
|
|
assert_int(StepCanvasTransport.scroll_step(0, 1)).is_equal(1)
|
|
assert_int(StepCanvasTransport.scroll_step(2, 1)).is_equal(3)
|
|
|
|
|
|
func test_scroll_step_ascends_one_notch_at_a_time() -> void:
|
|
assert_int(StepCanvasTransport.scroll_step(3, -1)).is_equal(2)
|
|
|
|
|
|
func test_scroll_step_clamps_at_the_deepest_rung() -> void:
|
|
assert_int(StepCanvasTransport.scroll_step(5, 1)).is_equal(5)
|
|
|
|
|
|
func test_scroll_step_clamps_at_the_global_opener() -> void:
|
|
assert_int(StepCanvasTransport.scroll_step(0, -1)).is_equal(0)
|
|
|
|
|
|
func test_scroll_step_zero_direction_is_a_no_op() -> void:
|
|
assert_int(StepCanvasTransport.scroll_step(2, 0)).is_equal(2)
|
|
|
|
|
|
# =============================================================================
|
|
# D-243 gridunit spacing — pinned against the same metre values scale.rs uses
|
|
# =============================================================================
|
|
|
|
|
|
func test_spacing_for_rung_matches_d243_metre_values() -> void:
|
|
assert_float(StepCanvasTransport.spacing_for_rung("Global")).is_equal_approx(204_800.0, 0.01)
|
|
assert_float(StepCanvasTransport.spacing_for_rung("Region")).is_equal_approx(204_800.0, 0.01)
|
|
assert_float(StepCanvasTransport.spacing_for_rung("District")).is_equal_approx(2_048.0, 0.01)
|
|
assert_float(StepCanvasTransport.spacing_for_rung("Quarter")).is_equal_approx(512.0, 0.01)
|
|
assert_float(StepCanvasTransport.spacing_for_rung("Block")).is_equal_approx(128.0, 0.01)
|
|
assert_float(StepCanvasTransport.spacing_for_rung("Chunk")).is_equal_approx(64.0, 0.01)
|
|
|
|
|
|
# =============================================================================
|
|
# Display ratio — deep/mid 1x1, shallow ~5x5, PRESENTATION only (D-255(a))
|
|
# =============================================================================
|
|
|
|
|
|
func test_display_ratio_deep_rungs_are_one_to_one() -> void:
|
|
for rung in ["District", "Quarter", "Block", "Chunk"]:
|
|
assert_float(StepCanvasTransport.display_ratio_for_rung(rung)).is_equal_approx(1.0, 0.001)
|
|
|
|
|
|
func test_display_ratio_shallow_rungs_use_the_five_x_five_fallback() -> void:
|
|
for rung in ["Global", "Region"]:
|
|
assert_float(StepCanvasTransport.display_ratio_for_rung(rung)).is_equal_approx(5.0, 0.001)
|
|
|
|
|
|
func test_is_orbital_rung_true_only_for_global_and_region() -> void:
|
|
assert_bool(StepCanvasTransport.is_orbital_rung("Global")).is_true()
|
|
assert_bool(StepCanvasTransport.is_orbital_rung("Region")).is_true()
|
|
assert_bool(StepCanvasTransport.is_orbital_rung("District")).is_false()
|
|
assert_bool(StepCanvasTransport.is_orbital_rung("Chunk")).is_false()
|
|
|
|
|
|
# =============================================================================
|
|
# Viewport-fit extent — the client half of "viewport-sized canvas"
|
|
# =============================================================================
|
|
|
|
|
|
func test_viewport_fit_extent_at_deep_ratio_matches_viewport_pixels() -> void:
|
|
# 1x1 ratio -> extent in gridunits == viewport px, 1:1.
|
|
var extent: Vector2i = StepCanvasTransport.viewport_fit_extent(Vector2(800.0, 600.0), "Chunk")
|
|
assert_that(extent).is_equal(Vector2i(800, 600))
|
|
|
|
|
|
func test_viewport_fit_extent_at_shallow_ratio_divides_by_the_display_ratio() -> void:
|
|
var extent: Vector2i = StepCanvasTransport.viewport_fit_extent(Vector2(1000.0, 500.0), "Region")
|
|
assert_that(extent).is_equal(Vector2i(200, 100))
|
|
|
|
|
|
func test_viewport_fit_extent_clamps_to_the_fixed_canvas_max_axis() -> void:
|
|
var extent: Vector2i = StepCanvasTransport.viewport_fit_extent(
|
|
Vector2(20_000.0, 20_000.0), "Chunk"
|
|
)
|
|
assert_int(extent.x).is_equal(StepCanvasTransport.FIXED_CANVAS_MAX_AXIS)
|
|
assert_int(extent.y).is_equal(StepCanvasTransport.FIXED_CANVAS_MAX_AXIS)
|
|
|
|
|
|
func test_viewport_fit_extent_never_produces_a_zero_axis() -> void:
|
|
var extent: Vector2i = StepCanvasTransport.viewport_fit_extent(Vector2(0.0, 0.0), "Chunk")
|
|
assert_int(extent.x).is_greater_equal(1)
|
|
assert_int(extent.y).is_greater_equal(1)
|
|
|
|
|
|
# =============================================================================
|
|
# Gridunit snapping — cache-key stability for repeated "same spot" requests
|
|
# =============================================================================
|
|
|
|
|
|
func test_snap_to_gridunit_snaps_to_the_rungs_own_spacing() -> void:
|
|
var snapped: Vector2i = StepCanvasTransport.snap_to_gridunit(Vector2(2100.0, -1000.0), "District")
|
|
# District spacing = 2048 m: 2100 rounds to 1*2048=2048, -1000 rounds to 0.
|
|
assert_int(snapped.x).is_equal(2048)
|
|
assert_int(snapped.y).is_equal(0)
|
|
|
|
|
|
func test_snap_to_gridunit_is_idempotent_once_already_on_grid() -> void:
|
|
var once: Vector2i = StepCanvasTransport.snap_to_gridunit(Vector2(4096.0, 6144.0), "District")
|
|
var world_again := Vector2(once.x, once.y)
|
|
var twice: Vector2i = StepCanvasTransport.snap_to_gridunit(world_again, "District")
|
|
assert_that(once).is_equal(twice)
|
|
|
|
|
|
# =============================================================================
|
|
# World <-> canvas-local projection — the shared transform both the terrain
|
|
# and annotation layers agree on by construction
|
|
# =============================================================================
|
|
|
|
|
|
func test_world_m_to_canvas_local_centers_the_world_center_on_the_canvas_center() -> void:
|
|
var extent := Vector2i(64, 64)
|
|
var rung := "District"
|
|
var world_center := Vector2(10_000.0, 20_000.0)
|
|
var local: Vector2 = StepCanvasTransport.world_m_to_canvas_local(
|
|
world_center, world_center, rung, extent
|
|
)
|
|
var expected_center: Vector2 = StepCanvasTransport.canvas_footprint_px(rung, extent) * 0.5
|
|
assert_that(local).is_equal_approx(expected_center, Vector2(0.01, 0.01))
|
|
|
|
|
|
## world_m_to_canvas_local() and canvas_local_to_world_m() must be exact
|
|
## inverses of one another — a round trip through both must recover the
|
|
## original world point (within float tolerance). This is the invariant the
|
|
## cursor-anchored scroll step depends on: whatever point the cursor reads
|
|
## as "under it" before a scroll must be the SAME point after re-deriving
|
|
## from the new step's own frame.
|
|
func test_world_to_local_and_back_round_trips() -> void:
|
|
var extent := Vector2i(128, 96)
|
|
var rung := "Quarter"
|
|
var world_center := Vector2(50_000.0, -30_000.0)
|
|
var original_world := Vector2(51_200.0, -29_500.0)
|
|
|
|
var local: Vector2 = StepCanvasTransport.world_m_to_canvas_local(
|
|
original_world, world_center, rung, extent
|
|
)
|
|
var recovered_world: Vector2 = StepCanvasTransport.canvas_local_to_world_m(
|
|
local, world_center, rung, extent
|
|
)
|
|
assert_that(recovered_world).is_equal_approx(original_world, Vector2(0.5, 0.5))
|
|
|
|
|
|
func test_canvas_footprint_px_is_extent_times_display_ratio() -> void:
|
|
var footprint: Vector2 = StepCanvasTransport.canvas_footprint_px("Region", Vector2i(100, 50))
|
|
assert_that(footprint).is_equal(Vector2(500.0, 250.0)) # 5x5 shallow ratio
|
|
|
|
|
|
func test_half_extent_m_is_half_the_cell_count_times_spacing() -> void:
|
|
var half: float = StepCanvasTransport.half_extent_m("District", 64)
|
|
assert_float(half).is_equal_approx(64.0 * 0.5 * 2048.0, 0.01)
|
|
|
|
|
|
# =============================================================================
|
|
# T-1189: extent cap to the body's own region grid — the sideways-repeat /
|
|
# pole-smear fix. The Global echo IS the cap (its canvas already equals the
|
|
# body's region grid, D-255(a): "the Global canvas IS the whole body at
|
|
# region spacing"), so no unit conversion is needed — Region shares Global's
|
|
# gridunit spacing exactly.
|
|
# =============================================================================
|
|
|
|
|
|
func test_cap_extent_to_body_clamps_region_to_the_global_echo() -> void:
|
|
# T-1183 eyeball: 384x216 requested at Region on GJ1c, whose Global echo
|
|
# is 177x88 — the requested extent overruns the body on both axes.
|
|
var capped: Vector2i = StepCanvasTransport.cap_extent_to_body(
|
|
Vector2i(384, 216), "Region", Vector2i(177, 88)
|
|
)
|
|
assert_that(capped).is_equal(Vector2i(177, 88))
|
|
|
|
|
|
func test_cap_extent_to_body_is_a_no_op_when_already_inside_the_grid() -> void:
|
|
var capped: Vector2i = StepCanvasTransport.cap_extent_to_body(
|
|
Vector2i(100, 40), "Region", Vector2i(177, 88)
|
|
)
|
|
assert_that(capped).is_equal(Vector2i(100, 40))
|
|
|
|
|
|
## Shape-generic per the ticket: the guard compares SPACING, not rung name,
|
|
## so it caps ANY rung sharing Global's spacing, not just a hardcoded
|
|
## "Region" check. District's spacing (2048 m) differs from Global's
|
|
## (204,800 m), so it must NEVER be capped by the body-grid cell count —
|
|
## capping cell counts across mismatched spacings would be a unit error.
|
|
func test_cap_extent_to_body_leaves_finer_rungs_uncapped() -> void:
|
|
var capped: Vector2i = StepCanvasTransport.cap_extent_to_body(
|
|
Vector2i(3000, 3000), "District", Vector2i(177, 88)
|
|
)
|
|
assert_that(capped).is_equal(Vector2i(3000, 3000))
|
|
|
|
|
|
## Cold-start fallback (T-1189, StepCanvasViewer's own documented choice):
|
|
## before any Global response has arrived, `_global_body_extent` is ZERO —
|
|
## cap_extent_to_body() must leave the request UNCAPPED on a non-positive
|
|
## axis (server clamps independently) rather than clamping to zero cells.
|
|
func test_cap_extent_to_body_uncapped_when_global_echo_not_yet_available() -> void:
|
|
var capped: Vector2i = StepCanvasTransport.cap_extent_to_body(
|
|
Vector2i(384, 216), "Region", Vector2i.ZERO
|
|
)
|
|
assert_that(capped).is_equal(Vector2i(384, 216))
|
|
|
|
|
|
## A mixed case: one axis of the Global echo has arrived-and-is-real, the
|
|
## other is still ZERO (shouldn't happen in practice since both arrive
|
|
## together, but the function must handle each axis independently rather
|
|
## than assuming both-or-neither).
|
|
func test_cap_extent_to_body_caps_only_the_positive_echo_axis() -> void:
|
|
var capped: Vector2i = StepCanvasTransport.cap_extent_to_body(
|
|
Vector2i(384, 216), "Region", Vector2i(177, 0)
|
|
)
|
|
assert_that(capped).is_equal(Vector2i(177, 216))
|
|
|
|
|
|
# =============================================================================
|
|
# T-1189/T-1192: shared letterbox/centering mechanism
|
|
# =============================================================================
|
|
|
|
|
|
func test_center_offset_centers_a_smaller_canvas_in_a_larger_viewport() -> void:
|
|
var offset: Vector2 = StepCanvasTransport.center_offset(
|
|
Vector2(800.0, 400.0), Vector2(1920.0, 1080.0)
|
|
)
|
|
assert_that(offset).is_equal(Vector2((1920.0 - 800.0) * 0.5, (1080.0 - 400.0) * 0.5))
|
|
|
|
|
|
func test_center_offset_is_zero_when_canvas_exactly_fills_the_viewport() -> void:
|
|
var offset: Vector2 = StepCanvasTransport.center_offset(
|
|
Vector2(1920.0, 1080.0), Vector2(1920.0, 1080.0)
|
|
)
|
|
assert_that(offset).is_equal(Vector2.ZERO)
|
|
|
|
|
|
func test_center_offset_goes_negative_when_the_canvas_overflows_the_viewport() -> void:
|
|
# A canvas bigger than the viewport on an axis crops rather than shrinks
|
|
# (matches every fixed rung's own "canvas can exceed the viewport"
|
|
# precedent) — a negative offset on that axis is the correct, honest
|
|
# result, not clamped to zero.
|
|
var offset: Vector2 = StepCanvasTransport.center_offset(
|
|
Vector2(2000.0, 400.0), Vector2(1920.0, 1080.0)
|
|
)
|
|
assert_float(offset.x).is_less(0.0)
|
|
assert_float(offset.y).is_greater(0.0)
|
|
|
|
|
|
# =============================================================================
|
|
# T-1192: Global integer-fit scale — D-255 texel-exactness
|
|
# =============================================================================
|
|
|
|
|
|
func test_integer_fit_scale_picks_the_largest_multiple_that_fits_both_axes() -> void:
|
|
# GJ1c reference case: 177x88 texels at the 5x5 shallow display ratio =
|
|
# 885x440 px footprint, fit against a full 1920x1080 viewport.
|
|
var scale: int = StepCanvasTransport.integer_fit_scale(
|
|
Vector2(885.0, 440.0), Vector2(1920.0, 1080.0)
|
|
)
|
|
assert_int(scale).is_equal(2)
|
|
|
|
|
|
func test_integer_fit_scale_is_bounded_by_the_tighter_axis() -> void:
|
|
# Wide-but-short viewport: x could fit 4x, y only fits 1x — the smaller
|
|
# wins (never overflow either axis).
|
|
var scale: int = StepCanvasTransport.integer_fit_scale(
|
|
Vector2(100.0, 100.0), Vector2(1000.0, 150.0)
|
|
)
|
|
assert_int(scale).is_equal(1)
|
|
|
|
|
|
func test_integer_fit_scale_never_drops_below_one() -> void:
|
|
# A canvas larger than the viewport still gets scale 1 (draw at native
|
|
# size and let it exceed/crop), never a shrink below native.
|
|
var scale: int = StepCanvasTransport.integer_fit_scale(
|
|
Vector2(3000.0, 3000.0), Vector2(800.0, 600.0)
|
|
)
|
|
assert_int(scale).is_equal(1)
|
|
|
|
|
|
func test_integer_fit_scale_handles_a_zero_canvas_axis_without_dividing_by_zero() -> void:
|
|
var scale: int = StepCanvasTransport.integer_fit_scale(Vector2.ZERO, Vector2(800.0, 600.0))
|
|
assert_int(scale).is_equal(1)
|
|
|
|
|
|
func test_fit_scale_matches_the_integer_fit_when_coverage_is_high() -> void:
|
|
# The GJ1c full-viewport case: integer 2x covers well over the
|
|
# FIT_MIN_COVERAGE_RATIO bar, so fit_scale() must agree with
|
|
# integer_fit_scale() exactly (no fractional fallback).
|
|
var scale: float = StepCanvasTransport.fit_scale(
|
|
Vector2(885.0, 440.0), Vector2(1920.0, 1080.0)
|
|
)
|
|
assert_float(scale).is_equal_approx(2.0, 0.001)
|
|
|
|
|
|
## The GJ1c reference case AFTER the legend column is reserved (T-1192):
|
|
## available area shrinks to 1628x1080, so the 2x integer candidate (1770 px
|
|
## wide) no longer fits — integer_fit_scale() drops to 1x, which only covers
|
|
## ~41% of the tighter available axis, well under FIT_MIN_COVERAGE_RATIO —
|
|
## fit_scale() must fall back to the non-integer uniform fit that fills the
|
|
## tighter (x) axis exactly, not settle for the sparse 1x frame.
|
|
func test_fit_scale_falls_back_to_fractional_fit_on_excessive_letterboxing() -> void:
|
|
var scale: float = StepCanvasTransport.fit_scale(
|
|
Vector2(885.0, 440.0), Vector2(1628.0, 1080.0)
|
|
)
|
|
assert_float(scale).is_greater(1.0)
|
|
assert_float(scale).is_less(2.0)
|
|
# The fractional fit fills the tighter (x) axis exactly.
|
|
assert_float(885.0 * scale).is_equal_approx(1628.0, 0.01)
|
|
|
|
|
|
func test_fit_scale_handles_a_zero_canvas_axis_without_dividing_by_zero() -> void:
|
|
var scale: float = StepCanvasTransport.fit_scale(Vector2.ZERO, Vector2(800.0, 600.0))
|
|
assert_float(scale).is_equal_approx(1.0, 0.001)
|
|
|
|
|
|
# =============================================================================
|
|
# T-1192: shared legend-column reservation constant
|
|
# =============================================================================
|
|
|
|
|
|
func test_legend_column_px_is_positive_and_matches_the_legend_panels_own_sizing() -> void:
|
|
# Pinned against step_canvas_legend.gd's own RESERVED_COLUMN_PX (260 +
|
|
# 16*2 = 292) — the two constants must never drift apart, since the
|
|
# "beside, never over" guarantee depends on both sides agreeing on the
|
|
# SAME reserved width.
|
|
assert_float(StepCanvasTransport.LEGEND_COLUMN_PX).is_equal_approx(292.0, 0.01)
|