First commit
This commit is contained in:
commit
9b0fe9b1b7
126 changed files with 31043 additions and 0 deletions
BIN
tests/__pycache__/test_cubiomes.cpython-312-pytest-9.1.1.pyc
Normal file
BIN
tests/__pycache__/test_cubiomes.cpython-312-pytest-9.1.1.pyc
Normal file
Binary file not shown.
BIN
tests/__pycache__/test_search.cpython-312-pytest-9.1.1.pyc
Normal file
BIN
tests/__pycache__/test_search.cpython-312-pytest-9.1.1.pyc
Normal file
Binary file not shown.
309
tests/test_cubiomes.py
Normal file
309
tests/test_cubiomes.py
Normal file
|
|
@ -0,0 +1,309 @@
|
|||
"""Tests for the cubiomes Python bindings."""
|
||||
|
||||
import array
|
||||
import pytest
|
||||
from cubiomes import (
|
||||
Generator, BiomeFilter, StrongholdIterator, StructureVariant,
|
||||
MCVersion, Dimension, BiomeID, StructureType,
|
||||
get_structure_config, get_structure_pos, is_viable_structure_pos,
|
||||
is_viable_feature_biome, get_variant, get_mineshafts,
|
||||
iter_strongholds, init_first_stronghold,
|
||||
setup_biome_filter, is_slime_chunk, get_shadow, move_structure,
|
||||
chunk_generate_rnd, get_population_seed,
|
||||
biome_exists, is_overworld_biome, get_dimension, get_mutated,
|
||||
are_similar, is_mesa, is_shallow_ocean, is_deep_ocean,
|
||||
is_oceanic, is_snowy,
|
||||
)
|
||||
|
||||
SEED = 123456789
|
||||
MC = MCVersion.MC_1_21
|
||||
|
||||
|
||||
# ─── Generator ───────────────────────────────────────────────────────────────
|
||||
|
||||
class TestGenerator:
|
||||
def test_create(self):
|
||||
g = Generator(MC)
|
||||
assert g.mc == MC
|
||||
|
||||
def test_apply_seed(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
assert g.seed == SEED
|
||||
assert g.dim == Dimension.DIM_OVERWORLD
|
||||
|
||||
def test_large_biomes_flag(self):
|
||||
from cubiomes import LARGE_BIOMES
|
||||
g = Generator(MC, LARGE_BIOMES)
|
||||
assert g.flags & LARGE_BIOMES
|
||||
|
||||
def test_get_biome_at_scale4(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
biome = g.get_biome_at(0, 0, 0, scale=4)
|
||||
assert biome == BiomeID.river
|
||||
|
||||
def test_get_biome_at_scale1(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
biome = g.get_biome_at(0, 63, 0, scale=1)
|
||||
assert biome >= 0
|
||||
|
||||
def test_gen_biomes_returns_array(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
result = g.gen_biomes(-64, -64, 32, 32, scale=4)
|
||||
assert isinstance(result, array.array)
|
||||
assert result.typecode == 'i'
|
||||
assert len(result) == 32 * 32
|
||||
|
||||
def test_gen_biomes_3d(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
result = g.gen_biomes(0, 0, 8, 8, scale=4, y=0, sy=4)
|
||||
assert len(result) == 8 * 4 * 8
|
||||
|
||||
def test_gen_biomes_consistent_with_get_biome_at(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
x, z = 0, 0
|
||||
result = g.gen_biomes(x, z, 4, 4, scale=4)
|
||||
# result[z_i*sx + x_i] — top-left corner
|
||||
assert result[0] == g.get_biome_at(x, 0, z, scale=4)
|
||||
|
||||
def test_repr(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
r = repr(g)
|
||||
assert "Generator" in r
|
||||
assert str(SEED) in r
|
||||
|
||||
def test_estimate_spawn(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
x, z = g.estimate_spawn()
|
||||
assert isinstance(x, int)
|
||||
assert isinstance(z, int)
|
||||
|
||||
def test_nether_dimension(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_NETHER, SEED)
|
||||
assert g.dim == Dimension.DIM_NETHER
|
||||
biome = g.get_biome_at(0, 0, 0, scale=4)
|
||||
assert biome >= 0 # nether biome
|
||||
|
||||
def test_memory_cleanup(self):
|
||||
# Create many generators — should not leak memory
|
||||
for _ in range(100):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
g.get_biome_at(0, 0, 0)
|
||||
|
||||
|
||||
# ─── Structure finding ────────────────────────────────────────────────────────
|
||||
|
||||
class TestStructures:
|
||||
def test_get_structure_config_village(self):
|
||||
cfg = get_structure_config(StructureType.Village, MC)
|
||||
assert cfg is not None
|
||||
assert cfg["dim"] == 0
|
||||
assert cfg["region_size"] > 0
|
||||
|
||||
def test_get_structure_config_nonexistent(self):
|
||||
# Feature doesn't exist in 1.21
|
||||
cfg = get_structure_config(StructureType.Feature, MC)
|
||||
assert cfg is None
|
||||
|
||||
def test_get_structure_pos_returns_tuple(self):
|
||||
pos = get_structure_pos(StructureType.Village, MC, SEED, 0, 0)
|
||||
assert pos is None or (isinstance(pos, tuple) and len(pos) == 2)
|
||||
|
||||
def test_get_structure_pos_village(self):
|
||||
# We know this seed has a village in this region from manual testing
|
||||
pos = get_structure_pos(StructureType.Village, MC, SEED, -1, -1)
|
||||
assert pos is not None
|
||||
x, z = pos
|
||||
assert isinstance(x, int) and isinstance(z, int)
|
||||
|
||||
def test_is_viable_structure_pos(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
pos = get_structure_pos(StructureType.Village, MC, SEED, -1, -1)
|
||||
assert pos is not None
|
||||
result = is_viable_structure_pos(StructureType.Village, g, pos[0], pos[1])
|
||||
assert isinstance(result, bool)
|
||||
|
||||
def test_is_viable_feature_biome(self):
|
||||
assert is_viable_feature_biome(MC, StructureType.Desert_Pyramid,
|
||||
BiomeID.desert)
|
||||
assert not is_viable_feature_biome(MC, StructureType.Desert_Pyramid,
|
||||
BiomeID.plains)
|
||||
|
||||
def test_get_variant_village(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
pos = get_structure_pos(StructureType.Village, MC, SEED, -1, -1)
|
||||
if pos:
|
||||
biome = g.get_biome_at(pos[0] >> 2, 0, pos[1] >> 2, scale=4)
|
||||
sv = get_variant(StructureType.Village, MC, SEED, pos[0], pos[1], biome)
|
||||
if sv is not None:
|
||||
assert isinstance(sv.rotation, int)
|
||||
assert 0 <= sv.rotation <= 3
|
||||
|
||||
def test_get_mineshafts(self):
|
||||
mines = get_mineshafts(MC, SEED, -10, -10, 20, 20)
|
||||
assert isinstance(mines, list)
|
||||
for x, z in mines:
|
||||
assert isinstance(x, int) and isinstance(z, int)
|
||||
|
||||
|
||||
# ─── Strongholds ─────────────────────────────────────────────────────────────
|
||||
|
||||
class TestStrongholds:
|
||||
def test_init_first_stronghold(self):
|
||||
x, z = init_first_stronghold(MC, SEED)
|
||||
assert isinstance(x, int) and isinstance(z, int)
|
||||
|
||||
def test_iter_strongholds_yields_tuples(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
positions = []
|
||||
for i, pos in enumerate(iter_strongholds(g, SEED)):
|
||||
positions.append(pos)
|
||||
if i >= 4:
|
||||
break
|
||||
assert len(positions) == 5
|
||||
for x, z in positions:
|
||||
assert isinstance(x, int) and isinstance(z, int)
|
||||
|
||||
def test_iter_strongholds_known_positions(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
first = next(iter(iter_strongholds(g, SEED)))
|
||||
assert first == (-1116, -1596)
|
||||
|
||||
def test_stronghold_iterator_index(self):
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
it = iter_strongholds(g, SEED)
|
||||
next(it)
|
||||
# The iterator pre-fetches the next position after each yield,
|
||||
# so index==2 after the first next() (stronghold 0 yielded, 1 pre-fetched)
|
||||
assert it.index == 2
|
||||
|
||||
|
||||
# ─── BiomeFilter ─────────────────────────────────────────────────────────────
|
||||
|
||||
class TestBiomeFilter:
|
||||
def test_create_required(self):
|
||||
bf = setup_biome_filter(MC, required=[BiomeID.plains, BiomeID.forest])
|
||||
assert isinstance(bf, BiomeFilter)
|
||||
|
||||
def test_create_excluded(self):
|
||||
bf = setup_biome_filter(MC, excluded=[BiomeID.ocean])
|
||||
assert isinstance(bf, BiomeFilter)
|
||||
|
||||
def test_check_for_biomes_returns_int(self):
|
||||
g = Generator(MC)
|
||||
bf = setup_biome_filter(MC, required=[BiomeID.plains])
|
||||
result = g.check_for_biomes(
|
||||
-256, -256, 128, 128, scale=4,
|
||||
dim=Dimension.DIM_OVERWORLD, seed=SEED,
|
||||
biome_filter=bf
|
||||
)
|
||||
assert result in (0, 1, 2)
|
||||
|
||||
|
||||
# ─── Biome helpers ────────────────────────────────────────────────────────────
|
||||
|
||||
class TestBiomeHelpers:
|
||||
def test_biome_exists_true(self):
|
||||
assert biome_exists(MC, BiomeID.plains)
|
||||
|
||||
def test_biome_exists_false(self):
|
||||
# cherry_grove doesn't exist in 1.18
|
||||
assert not biome_exists(MCVersion.MC_1_18, BiomeID.cherry_grove)
|
||||
|
||||
def test_is_overworld_biome(self):
|
||||
assert is_overworld_biome(MC, BiomeID.plains)
|
||||
assert not is_overworld_biome(MC, BiomeID.nether_wastes)
|
||||
|
||||
def test_get_dimension(self):
|
||||
assert get_dimension(BiomeID.plains) == 0
|
||||
assert get_dimension(BiomeID.nether_wastes) == -1
|
||||
assert get_dimension(BiomeID.the_end) == 1
|
||||
|
||||
def test_get_mutated(self):
|
||||
m = get_mutated(MCVersion.MC_1_17, BiomeID.plains)
|
||||
assert m == BiomeID.sunflower_plains or m == -1
|
||||
|
||||
def test_are_similar(self):
|
||||
assert are_similar(MC, BiomeID.forest, BiomeID.birch_forest)
|
||||
|
||||
def test_is_mesa(self):
|
||||
assert is_mesa(BiomeID.badlands)
|
||||
assert not is_mesa(BiomeID.plains)
|
||||
|
||||
def test_ocean_checks(self):
|
||||
assert is_shallow_ocean(BiomeID.ocean)
|
||||
assert is_deep_ocean(BiomeID.deep_ocean)
|
||||
assert is_oceanic(BiomeID.ocean)
|
||||
assert is_oceanic(BiomeID.deep_ocean)
|
||||
assert not is_oceanic(BiomeID.plains)
|
||||
|
||||
def test_is_snowy(self):
|
||||
assert is_snowy(BiomeID.snowy_tundra)
|
||||
assert not is_snowy(BiomeID.desert)
|
||||
|
||||
|
||||
# ─── Seed utilities ───────────────────────────────────────────────────────────
|
||||
|
||||
class TestSeedUtils:
|
||||
def test_is_slime_chunk(self):
|
||||
# Known slime chunk from manual test
|
||||
assert is_slime_chunk(SEED, 0, 1)
|
||||
# Known non-slime chunk
|
||||
assert not is_slime_chunk(SEED, 0, 0)
|
||||
|
||||
def test_get_shadow(self):
|
||||
shadow = get_shadow(SEED)
|
||||
assert isinstance(shadow, int)
|
||||
# Shadow is deterministic
|
||||
assert get_shadow(SEED) == shadow
|
||||
# Shadow of shadow is original
|
||||
assert get_shadow(shadow) == SEED
|
||||
|
||||
def test_move_structure(self):
|
||||
result = move_structure(SEED, 1, 0)
|
||||
assert isinstance(result, int)
|
||||
assert result != SEED
|
||||
|
||||
def test_chunk_generate_rnd(self):
|
||||
rnd = chunk_generate_rnd(SEED, 0, 0)
|
||||
assert isinstance(rnd, int)
|
||||
assert rnd != 0
|
||||
|
||||
def test_get_population_seed(self):
|
||||
pop = get_population_seed(MC, SEED, 0, 0)
|
||||
assert isinstance(pop, int)
|
||||
|
||||
|
||||
# ─── Enums ───────────────────────────────────────────────────────────────────
|
||||
|
||||
class TestEnums:
|
||||
def test_mc_version_newest(self):
|
||||
assert MCVersion.MC_NEWEST == MCVersion.MC_26_3
|
||||
assert int(MCVersion.MC_NEWEST) == 35
|
||||
|
||||
def test_biome_id_from_int(self):
|
||||
assert BiomeID(0) == BiomeID.ocean
|
||||
assert BiomeID(1) == BiomeID.plains
|
||||
|
||||
def test_structure_type_values(self):
|
||||
assert StructureType.Village == 5
|
||||
assert StructureType.Stronghold == 27
|
||||
|
||||
def test_dimension_values(self):
|
||||
assert Dimension.DIM_NETHER == -1
|
||||
assert Dimension.DIM_OVERWORLD == 0
|
||||
assert Dimension.DIM_END == 1
|
||||
577
tests/test_search.py
Normal file
577
tests/test_search.py
Normal file
|
|
@ -0,0 +1,577 @@
|
|||
"""Tests for cubiomes.search module — basic and advanced conditions."""
|
||||
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from cubiomes import MCVersion, BiomeID, StructureType, Dimension
|
||||
from cubiomes.search import (
|
||||
# condition types
|
||||
BiomeCondition, StructureCondition,
|
||||
SpawnCondition, StrongholdCondition, ProximityCondition,
|
||||
QuadStructureCondition, BiomeDensityCondition,
|
||||
SlimeDensityCondition, AnyOfBiomes, CustomCondition,
|
||||
# main classes
|
||||
SearchCriteria, SeedResult, SeedSearch,
|
||||
# helpers
|
||||
_mc_name, _resolve_mc, _resolve_biome,
|
||||
)
|
||||
|
||||
MC = MCVersion.MC_1_21
|
||||
SEED = 123456789
|
||||
|
||||
|
||||
# ─── Helpers ─────────────────────────────────────────────────────────────────
|
||||
|
||||
class TestHelpers:
|
||||
def test_mc_name(self):
|
||||
assert _mc_name(MCVersion.MC_1_21) == "1.21.11"
|
||||
assert _mc_name(MCVersion.MC_1_18) == "1.18.2"
|
||||
assert _mc_name(MCVersion.MC_B1_7) == "B1.7"
|
||||
|
||||
def test_resolve_mc(self):
|
||||
assert _resolve_mc("1.21") == MCVersion.MC_1_21
|
||||
|
||||
def test_resolve_biome_by_name(self):
|
||||
assert _resolve_biome("plains") == BiomeID.plains
|
||||
|
||||
def test_resolve_biome_by_int(self):
|
||||
assert _resolve_biome("1") == 1
|
||||
|
||||
def test_resolve_biome_unknown(self):
|
||||
import argparse
|
||||
with pytest.raises(argparse.ArgumentTypeError):
|
||||
_resolve_biome("__not_a_biome__")
|
||||
|
||||
|
||||
# ─── SeedResult ───────────────────────────────────────────────────────────────
|
||||
|
||||
class TestSeedResult:
|
||||
def test_as_dict(self):
|
||||
r = SeedResult(seed=42, mc=MC,
|
||||
biome_positions={"plains": (10, 20)},
|
||||
structure_positions={"Village": (100, 200)},
|
||||
spawn=(64, -48), spawn_biome=BiomeID.forest,
|
||||
extra={"slime_chunk_count": 7})
|
||||
d = r.as_dict()
|
||||
assert d["seed"] == 42
|
||||
assert d["mc_name"] == "1.21.11"
|
||||
assert d["extra"]["slime_chunk_count"] == 7
|
||||
|
||||
def test_str_contains_quad(self):
|
||||
r = SeedResult(seed=42, mc=MC,
|
||||
quad={"name": "Swamp_Hut", "center": (100, 200),
|
||||
"positions": [(90,190),(110,190),(90,210),(110,210)],
|
||||
"max_r": 15.0})
|
||||
s = str(r)
|
||||
assert "quad" in s
|
||||
assert "Swamp_Hut" in s
|
||||
|
||||
def test_str_contains_extra(self):
|
||||
r = SeedResult(seed=42, mc=MC, extra={"density_badlands": 0.42})
|
||||
assert "0.42" in str(r)
|
||||
|
||||
|
||||
# ─── Basic conditions ─────────────────────────────────────────────────────────
|
||||
|
||||
class TestBasicConditions:
|
||||
def test_biome_match(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
biomes=[BiomeCondition(BiomeID.river, radius=500)],
|
||||
)
|
||||
result = SeedSearch(criteria).check_seed(SEED)
|
||||
assert result is not None
|
||||
assert "river" in result.biome_positions
|
||||
|
||||
def test_structure_match(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
structures=[StructureCondition(StructureType.Village, radius=2000)],
|
||||
)
|
||||
result = SeedSearch(criteria).check_seed(SEED)
|
||||
assert result is not None
|
||||
assert "Village" in result.structure_positions
|
||||
|
||||
def test_slime_chunk_pass(self):
|
||||
criteria = SearchCriteria(mc=MC, slime_chunk=(0, 1))
|
||||
assert SeedSearch(criteria).check_seed(SEED) is not None
|
||||
|
||||
def test_slime_chunk_fail(self):
|
||||
criteria = SearchCriteria(mc=MC, slime_chunk=(0, 0))
|
||||
assert SeedSearch(criteria).check_seed(SEED) is None
|
||||
|
||||
def test_legacy_spawn_biome(self):
|
||||
# Old-style spawn_biome field still works
|
||||
sb = SeedSearch(SearchCriteria(mc=MC)).check_seed(SEED, show_spawn=True)
|
||||
assert sb is not None
|
||||
biome_at_spawn = sb.spawn_biome
|
||||
criteria = SearchCriteria(mc=MC, spawn_biome=biome_at_spawn)
|
||||
assert SeedSearch(criteria).check_seed(SEED) is not None
|
||||
|
||||
|
||||
# ─── Advanced conditions ──────────────────────────────────────────────────────
|
||||
|
||||
class TestSpawnCondition:
|
||||
def test_spawn_condition_biome(self):
|
||||
# First, find the actual spawn biome
|
||||
from cubiomes import Generator, Dimension
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
sp = g.estimate_spawn()
|
||||
sb = g.get_biome_at(sp[0] >> 2, 0, sp[1] >> 2, scale=4)
|
||||
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[SpawnCondition(biome_id=sb)],
|
||||
)
|
||||
result = SeedSearch(criteria).check_seed(SEED)
|
||||
assert result is not None
|
||||
assert result.spawn_biome == sb
|
||||
|
||||
def test_spawn_condition_max_distance_pass(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[SpawnCondition(max_distance=10000)],
|
||||
)
|
||||
assert SeedSearch(criteria).check_seed(SEED) is not None
|
||||
|
||||
def test_spawn_condition_max_distance_fail(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[SpawnCondition(max_distance=1)],
|
||||
)
|
||||
# Spawn is at (64, -48) — definitely further than 1 block
|
||||
assert SeedSearch(criteria).check_seed(SEED) is None
|
||||
|
||||
def test_spawn_condition_stores_spawn(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[SpawnCondition(max_distance=10000)],
|
||||
)
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
assert r.spawn is not None
|
||||
assert isinstance(r.spawn[0], int)
|
||||
|
||||
|
||||
class TestStrongholdCondition:
|
||||
def test_stronghold_within_large_radius(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[StrongholdCondition(count=1, max_distance=5000)],
|
||||
)
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
assert len(r.strongholds) == 1
|
||||
|
||||
def test_stronghold_too_far(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[StrongholdCondition(count=1, max_distance=10)],
|
||||
)
|
||||
# First stronghold is at ~1947 blocks
|
||||
assert SeedSearch(criteria).check_seed(SEED) is None
|
||||
|
||||
def test_stronghold_multiple_count(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[StrongholdCondition(count=3, max_distance=5000)],
|
||||
)
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
assert len(r.strongholds) == 3
|
||||
|
||||
def test_strongholds_in_result(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[StrongholdCondition(count=2, max_distance=5000)],
|
||||
)
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
for sh in r.strongholds:
|
||||
assert isinstance(sh[0], int) and isinstance(sh[1], int)
|
||||
|
||||
|
||||
class TestProximityCondition:
|
||||
def test_proximity_loose(self):
|
||||
# Village and Monument within 5000 blocks of each other should be easy
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[ProximityCondition(
|
||||
struct_a=StructureType.Village,
|
||||
struct_b=StructureType.Monument,
|
||||
max_dist=5000, search_radius=4000,
|
||||
)],
|
||||
)
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
assert "Village" in r.structure_positions
|
||||
assert "Monument" in r.structure_positions
|
||||
|
||||
def test_proximity_impossible(self):
|
||||
# Village within 1 block of Monument — impossible
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[ProximityCondition(
|
||||
struct_a=StructureType.Village,
|
||||
struct_b=StructureType.Monument,
|
||||
max_dist=1, search_radius=4000,
|
||||
)],
|
||||
)
|
||||
assert SeedSearch(criteria).check_seed(SEED) is None
|
||||
|
||||
def test_proximity_distance_recorded(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[ProximityCondition(
|
||||
StructureType.Village, StructureType.Monument,
|
||||
max_dist=5000, search_radius=4000,
|
||||
)],
|
||||
)
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
if r is not None:
|
||||
import math
|
||||
va = r.structure_positions.get("Village")
|
||||
mo = r.structure_positions.get("Monument")
|
||||
if va and mo:
|
||||
d = math.sqrt((va[0]-mo[0])**2 + (va[1]-mo[1])**2)
|
||||
assert d <= 5000
|
||||
|
||||
|
||||
class TestQuadStructureCondition:
|
||||
def test_quad_no_result_for_tight_limit(self):
|
||||
# Strict quad (max_dist=1) should fail for this seed
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[QuadStructureCondition(
|
||||
StructureType.Swamp_Hut, max_dist=1, max_regions=2,
|
||||
)],
|
||||
)
|
||||
assert SeedSearch(criteria).check_seed(SEED) is None
|
||||
|
||||
def test_quad_result_structure(self):
|
||||
# Loose quad (max_dist=9999) should find something in a range search
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[QuadStructureCondition(
|
||||
StructureType.Village, max_dist=9999, max_regions=2,
|
||||
)],
|
||||
)
|
||||
results = list(SeedSearch(criteria).run(seed_range=(0, 200), count=2))
|
||||
for r in results:
|
||||
assert r.quad is not None
|
||||
assert r.quad["name"] == "Village"
|
||||
assert len(r.quad["positions"]) == 4
|
||||
assert "center" in r.quad
|
||||
assert "max_r" in r.quad
|
||||
|
||||
def test_quad_positions_in_structure_positions(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[QuadStructureCondition(
|
||||
StructureType.Village, max_dist=9999, max_regions=2,
|
||||
)],
|
||||
)
|
||||
results = list(SeedSearch(criteria).run(seed_range=(0, 50), count=1))
|
||||
if results:
|
||||
r = results[0]
|
||||
assert any("Village_quad_" in k for k in r.structure_positions)
|
||||
|
||||
|
||||
class TestBiomeDensityCondition:
|
||||
def test_density_always_passes_with_zero(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[BiomeDensityCondition(
|
||||
biome_id=BiomeID.ocean, min_fraction=0.0,
|
||||
x=-128, z=-128, sx=256, sz=256,
|
||||
)],
|
||||
)
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
|
||||
def test_density_fails_impossible(self):
|
||||
# 100% cherry grove in 512×512 — impossible
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[BiomeDensityCondition(
|
||||
biome_id=BiomeID.cherry_grove, min_fraction=1.0,
|
||||
x=-128, z=-128, sx=64, sz=64,
|
||||
)],
|
||||
)
|
||||
assert SeedSearch(criteria).check_seed(SEED) is None
|
||||
|
||||
def test_density_stored_in_extra(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[BiomeDensityCondition(
|
||||
biome_id=BiomeID.plains, min_fraction=0.0,
|
||||
x=-64, z=-64, sx=128, sz=128,
|
||||
)],
|
||||
)
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
assert any("density_plains" in k for k in r.extra)
|
||||
|
||||
|
||||
class TestSlimeDensityCondition:
|
||||
def test_slime_density_zero_always_passes(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[SlimeDensityCondition(min_count=0, cx=-4, cz=-4, cw=9, ch=9)],
|
||||
)
|
||||
assert SeedSearch(criteria).check_seed(SEED) is not None
|
||||
|
||||
def test_slime_density_too_high_fails(self):
|
||||
# Require all 81 slime chunks — impossible
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[SlimeDensityCondition(min_count=81, cx=-4, cz=-4, cw=9, ch=9)],
|
||||
)
|
||||
assert SeedSearch(criteria).check_seed(SEED) is None
|
||||
|
||||
def test_slime_density_count_in_extra(self):
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[SlimeDensityCondition(min_count=0, cx=-4, cz=-4, cw=9, ch=9)],
|
||||
)
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
assert "slime_chunk_count" in r.extra
|
||||
assert isinstance(r.extra["slime_chunk_count"], int)
|
||||
|
||||
|
||||
class TestAnyOfBiomes:
|
||||
def test_any_biomes_one_match(self):
|
||||
# river is at (0,0) for this seed
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[AnyOfBiomes(
|
||||
[BiomeID.river, BiomeID.mushroom_fields], radius=500,
|
||||
)],
|
||||
)
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
assert any("any_of(" in k for k in r.biome_positions)
|
||||
|
||||
def test_any_biomes_none_match(self):
|
||||
# Impossible list, tiny radius
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[AnyOfBiomes([BiomeID.mushroom_fields], radius=1)],
|
||||
)
|
||||
assert SeedSearch(criteria).check_seed(SEED) is None
|
||||
|
||||
def test_any_biomes_or_semantics(self):
|
||||
# Both options are valid — should return on first found
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
conditions=[AnyOfBiomes([BiomeID.river, BiomeID.ocean], radius=2000)],
|
||||
)
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
|
||||
|
||||
class TestCustomCondition:
|
||||
def test_custom_pass(self):
|
||||
def always_true(seed, mc):
|
||||
return True
|
||||
|
||||
criteria = SearchCriteria(mc=MC, conditions=[CustomCondition(always_true)])
|
||||
assert SeedSearch(criteria).check_seed(SEED) is not None
|
||||
|
||||
def test_custom_fail(self):
|
||||
def always_false(seed, mc):
|
||||
return False
|
||||
|
||||
criteria = SearchCriteria(mc=MC, conditions=[CustomCondition(always_false)])
|
||||
assert SeedSearch(criteria).check_seed(SEED) is None
|
||||
|
||||
def test_custom_returns_dict(self):
|
||||
def returns_data(seed, mc):
|
||||
return {"custom_key": 42, "seed_doubled": seed * 2}
|
||||
|
||||
criteria = SearchCriteria(mc=MC, conditions=[CustomCondition(returns_data)])
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
assert r.extra["custom_key"] == 42
|
||||
assert r.extra["seed_doubled"] == SEED * 2
|
||||
|
||||
def test_custom_with_generator(self):
|
||||
from cubiomes import Generator, Dimension
|
||||
|
||||
def check_village_close(seed, mc):
|
||||
from cubiomes import get_structure_pos, is_viable_structure_pos, StructureType
|
||||
g = Generator(mc)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, seed)
|
||||
for rx in range(-2, 3):
|
||||
for rz in range(-2, 3):
|
||||
pos = get_structure_pos(StructureType.Village, mc, seed, rx, rz)
|
||||
if pos and is_viable_structure_pos(StructureType.Village, g, *pos):
|
||||
if pos[0]**2 + pos[1]**2 <= 500**2:
|
||||
return {"closest_village": pos}
|
||||
return False
|
||||
|
||||
criteria = SearchCriteria(mc=MC, conditions=[CustomCondition(check_village_close)])
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
assert "closest_village" in r.extra
|
||||
|
||||
def test_custom_combined_with_native(self):
|
||||
def extra_check(seed, mc):
|
||||
return seed % 2 == 1
|
||||
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
structures=[StructureCondition(StructureType.Village, radius=2000)],
|
||||
conditions=[CustomCondition(extra_check)],
|
||||
)
|
||||
# SEED=123456789 is odd, so custom passes; Village exists so native passes
|
||||
r = SeedSearch(criteria).check_seed(SEED)
|
||||
assert r is not None
|
||||
|
||||
|
||||
# ─── SearchCriteria combined ──────────────────────────────────────────────────
|
||||
|
||||
class TestCombinedCriteria:
|
||||
def test_all_conditions_must_pass(self):
|
||||
# Village nearby AND impossible density — should fail
|
||||
criteria = SearchCriteria(
|
||||
mc=MC,
|
||||
structures=[StructureCondition(StructureType.Village, radius=2000)],
|
||||
conditions=[BiomeDensityCondition(
|
||||
BiomeID.mushroom_fields, min_fraction=1.0,
|
||||
x=-64, z=-64, sx=128, sz=128,
|
||||
)],
|
||||
)
|
||||
assert SeedSearch(criteria).check_seed(SEED) is None
|
||||
|
||||
def test_empty_criteria_always_passes(self):
|
||||
criteria = SearchCriteria(mc=MC)
|
||||
assert SeedSearch(criteria).check_seed(SEED) is not None
|
||||
|
||||
def test_run_respects_count(self):
|
||||
criteria = SearchCriteria(mc=MC)
|
||||
results = list(SeedSearch(criteria).run(seed_range=(0, 1000), count=3))
|
||||
assert len(results) == 3
|
||||
|
||||
|
||||
# ─── CLI ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
class TestCLI:
|
||||
def _run(self, *args):
|
||||
return subprocess.run(
|
||||
[sys.executable, "-m", "cubiomes.search"] + list(args),
|
||||
capture_output=True, text=True,
|
||||
)
|
||||
|
||||
def test_help(self):
|
||||
r = self._run("--help")
|
||||
assert r.returncode == 0
|
||||
assert "quad" in r.stdout.lower()
|
||||
assert "proximity" in r.stdout.lower()
|
||||
|
||||
def test_info_mode(self):
|
||||
r = self._run("--seed", str(SEED), "--info")
|
||||
assert r.returncode == 0
|
||||
assert str(SEED) in r.stdout
|
||||
assert "strongholds" in r.stdout.lower()
|
||||
|
||||
def test_single_seed_match(self):
|
||||
r = self._run("--seed", str(SEED),
|
||||
"--structure", "Village", "--struct-radius", "500")
|
||||
assert r.returncode == 0
|
||||
assert "Village" in r.stdout
|
||||
|
||||
def test_single_seed_no_match(self):
|
||||
r = self._run("--seed", str(SEED),
|
||||
"--structure", "Village", "--struct-radius", "1")
|
||||
assert r.returncode == 1
|
||||
|
||||
def test_range_json(self):
|
||||
r = self._run("--seeds", "0:200",
|
||||
"--structure", "Village", "--struct-radius", "300",
|
||||
"--count", "2", "--format", "json", "--no-progress")
|
||||
assert r.returncode == 0
|
||||
data = json.loads(r.stdout)
|
||||
assert isinstance(data, list)
|
||||
|
||||
def test_range_csv(self):
|
||||
r = self._run("--seeds", "0:200",
|
||||
"--structure", "Village", "--struct-radius", "300",
|
||||
"--count", "2", "--format", "csv", "--no-progress")
|
||||
assert r.returncode == 0
|
||||
lines = r.stdout.strip().split("\n")
|
||||
assert lines[0].startswith("seed,mc,")
|
||||
|
||||
def test_any_biome_flag(self):
|
||||
r = self._run("--seed", str(SEED),
|
||||
"--any-biome", "river", "ocean", "--biome-radius", "500")
|
||||
assert r.returncode == 0
|
||||
|
||||
def test_spawn_biome_flag(self):
|
||||
# Get spawn biome first
|
||||
from cubiomes import Generator, Dimension
|
||||
g = Generator(MC)
|
||||
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
|
||||
sp = g.estimate_spawn()
|
||||
sb = g.get_biome_at(sp[0] >> 2, 0, sp[1] >> 2, scale=4)
|
||||
bname = BiomeID(sb).name
|
||||
|
||||
r = self._run("--seed", str(SEED), "--spawn-biome", bname)
|
||||
assert r.returncode == 0
|
||||
|
||||
def test_spawn_max_dist_pass(self):
|
||||
r = self._run("--seed", str(SEED), "--spawn-max-dist", "10000")
|
||||
assert r.returncode == 0
|
||||
|
||||
def test_spawn_max_dist_fail(self):
|
||||
r = self._run("--seed", str(SEED), "--spawn-max-dist", "1")
|
||||
assert r.returncode == 1
|
||||
|
||||
def test_stronghold_radius_flag(self):
|
||||
r = self._run("--seed", str(SEED),
|
||||
"--stronghold-radius", "5000", "--stronghold-count", "1")
|
||||
assert r.returncode == 0
|
||||
|
||||
def test_proximity_flag(self):
|
||||
r = self._run("--seed", str(SEED),
|
||||
"--proximity", "Village", "Monument", "5000",
|
||||
"--no-progress")
|
||||
assert r.returncode == 0
|
||||
assert "Village" in r.stdout
|
||||
assert "Monument" in r.stdout
|
||||
|
||||
def test_proximity_fail(self):
|
||||
r = self._run("--seed", str(SEED),
|
||||
"--proximity", "Village", "Monument", "1",
|
||||
"--no-progress")
|
||||
assert r.returncode == 1
|
||||
|
||||
def test_biome_density_flag(self):
|
||||
r = self._run("--seed", str(SEED),
|
||||
"--biome-density", "river", "0.0",
|
||||
"--no-progress")
|
||||
assert r.returncode == 0
|
||||
|
||||
def test_min_slime_chunks_flag(self):
|
||||
r = self._run("--seed", str(SEED), "--min-slime-chunks", "1")
|
||||
assert r.returncode == 0
|
||||
|
||||
def test_min_slime_chunks_impossible(self):
|
||||
r = self._run("--seed", str(SEED), "--min-slime-chunks", "81")
|
||||
assert r.returncode == 1
|
||||
|
||||
def test_quad_flag_range(self):
|
||||
# Loose quad should find results quickly
|
||||
r = self._run("--seeds", "0:50",
|
||||
"--quad", "Village", "--quad-dist", "9999",
|
||||
"--quad-max-regions", "2",
|
||||
"--count", "1", "--no-progress")
|
||||
assert r.returncode == 0
|
||||
|
||||
def test_unknown_biome(self):
|
||||
r = self._run("--biome", "__no_such_biome__", "--seeds", "0:10")
|
||||
assert r.returncode != 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue