225 lines
7.7 KiB
Python
225 lines
7.7 KiB
Python
import os
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
sys.path.insert(0, os.path.join(HERE, ".."))
|
|
|
|
from chart import Chart, Note, slide_steps # noqa: E402
|
|
from placer import auto_place # noqa: E402
|
|
from validate import validate # noqa: E402
|
|
from zones import TAP_COLS, SLIDE_COLS # noqa: E402
|
|
|
|
BPM = 120.0 # 1 拍 = 500 ms
|
|
|
|
|
|
def make(notes, divisor=4):
|
|
c = Chart("", BPM, 1000, divisor, notes)
|
|
c.sort()
|
|
return c
|
|
|
|
|
|
# ---------- 格線 ----------
|
|
|
|
def test_snap_to_grid():
|
|
c = make([])
|
|
assert c.snap(1000) == 1000
|
|
assert c.snap(1120) == 1125 # 1/4 拍 = 125 ms
|
|
assert c.snap(1060) == 1000
|
|
assert c.snap(1063, divisor=8) == 1062 or c.snap(1063, divisor=8) == 1063
|
|
|
|
|
|
def test_grid_lines_mark_beats_and_bars():
|
|
c = make([])
|
|
lines = c.grid_lines(1000, 3000)
|
|
assert lines[0] == (1000, True, True)
|
|
assert sum(1 for _, beat, _ in lines if beat) == 5 # 1000..3000 每 500ms
|
|
assert sum(1 for _, _, bar in lines if bar) == 2 # 1000、3000
|
|
|
|
|
|
# ---------- 自動配置的區域限制 ----------
|
|
|
|
def dense_chart():
|
|
notes = []
|
|
t = 1000
|
|
for i in range(200):
|
|
notes.append(Note(t, "LRM"[i % 3], "tap"))
|
|
t += 125
|
|
notes.append(Note(t + 500, "L", "hold", t + 1500))
|
|
notes.append(Note(t + 2000, "R", "slide", t + 3000))
|
|
notes.append(Note(t + 4000, "L", "slide", t + 5000))
|
|
return make(notes)
|
|
|
|
|
|
def test_auto_place_respects_zones():
|
|
c = auto_place(dense_chart())
|
|
for n in c.notes:
|
|
if n.type == "slide":
|
|
assert all(x in SLIDE_COLS[n.lane] for x, _ in n.path)
|
|
assert len(n.path) >= 2
|
|
else:
|
|
assert n.pos[0] in TAP_COLS[n.lane]
|
|
assert validate(c) == []
|
|
|
|
|
|
def test_slide_never_crosses_to_other_hand():
|
|
c = auto_place(dense_chart())
|
|
for n in c.notes:
|
|
if n.type == "slide" and n.lane == "L":
|
|
assert max(x for x, _ in n.path) <= 2
|
|
if n.type == "slide" and n.lane == "R":
|
|
assert min(x for x, _ in n.path) >= 2
|
|
|
|
|
|
def test_auto_place_is_deterministic_and_keeps_locked():
|
|
a = auto_place(dense_chart(), seed=7)
|
|
b = dense_chart()
|
|
b.notes[10].pos, b.notes[10].locked = (0, 5) if b.notes[10].lane == "L" else (2, 5) if b.notes[10].lane == "M" else (4, 5), True
|
|
locked = b.notes[10].pos
|
|
auto_place(b, seed=7)
|
|
assert b.notes[10].pos == locked
|
|
assert [n.pos for n in auto_place(dense_chart(), seed=7).notes] == [n.pos for n in a.notes]
|
|
|
|
|
|
# ---------- 檢查 ----------
|
|
|
|
def test_validate_zone_violation_and_overlap():
|
|
c = make([Note(1000, "L", "tap", pos=(4, 0)), # 左手放到右手區
|
|
Note(2000, "R", "tap", pos=(3, 0)),
|
|
Note(2300, "R", "tap", pos=(3, 0))]) # 同格 300ms 內重複
|
|
msgs = [m for _, m in validate(c)]
|
|
assert any("不在 L 軌" in m for m in msgs)
|
|
assert any("重疊" in m for m in msgs)
|
|
|
|
|
|
def test_validate_same_hand_simultaneous():
|
|
c = make([Note(1000, "L", "hold", 2000, pos=(0, 0)),
|
|
Note(1500, "L", "tap", pos=(1, 5))]) # 左手 hold 中又按
|
|
assert any("左手同時" in m for _, m in validate(c))
|
|
|
|
c2 = make([Note(1000, "L", "hold", 2000, pos=(0, 0)),
|
|
Note(1000, "R", "hold", 2000, pos=(4, 0)),
|
|
Note(1500, "M", "tap", pos=(2, 3))]) # 兩手都忙
|
|
assert any("沒有空著的手" in m for _, m in validate(c2))
|
|
|
|
c3 = make([Note(1000, "L", "hold", 2000, pos=(0, 0)),
|
|
Note(1500, "M", "tap", pos=(2, 3))]) # 右手空著 → 合法
|
|
assert validate(c3) == []
|
|
|
|
|
|
# ---------- 匯出 ----------
|
|
|
|
def test_export_sheet_matches_game_format():
|
|
c = make([Note(1000, "L", "tap", pos=(1, 2)),
|
|
Note(2000, "R", "hold", 2500, pos=(3, 1)),
|
|
Note(3000, "L", "slide", 3500, path=[(0, 0), (1, 1), (2, 2)])])
|
|
sheet = c.export_sheet()
|
|
assert sheet[0] == {"type": "tap", "startTime": 1000, "pos": {"x": 1, "y": 2}}
|
|
assert sheet[1] == {"type": "hold", "startTime": 2000, "endTime": 2500, "pos": {"x": 3, "y": 1}}
|
|
s = sheet[2]
|
|
# 遊戲把第 i 格放在 start + i*(end-start)/len:最後一格應落在 3500
|
|
last_hit = s["startTime"] + (s["endTime"] - s["startTime"]) / len(s["path"]) * (len(s["path"]) - 1)
|
|
assert last_hit == pytest.approx(3500)
|
|
|
|
|
|
def test_save_load_roundtrip(tmp_path):
|
|
c = auto_place(dense_chart())
|
|
c.notes[0].locked = True
|
|
p = tmp_path / "x.lanes.json"
|
|
c.save(p)
|
|
d = Chart.load(p)
|
|
assert d.bpm == c.bpm and d.offset == c.offset and len(d.notes) == len(c.notes)
|
|
assert [(n.t, n.lane, n.type, n.pos, n.path, n.locked) for n in d.notes] == \
|
|
[(n.t, n.lane, n.type, n.pos, n.path, n.locked) for n in c.notes]
|
|
|
|
|
|
def test_slide_steps_follow_divisor():
|
|
c = make([])
|
|
assert slide_steps(c, Note(1000, "L", "slide", 1500)) == 5 # 500ms / 125ms + 1
|
|
|
|
|
|
# ---------- osu 匯入(有 osu 檔時才跑)----------
|
|
|
|
OSU_DIR = os.environ.get("RHY_OSU_DIR")
|
|
|
|
|
|
@pytest.mark.skipif(not OSU_DIR, reason="設定 RHY_OSU_DIR 指向解壓的 osu 譜面資料夾")
|
|
def test_import_taiko_keeps_all_objects():
|
|
import glob
|
|
from importers import import_osu
|
|
from osu_parse import parse_osu
|
|
for f in glob.glob(os.path.join(OSU_DIR, "*.osu")):
|
|
c = import_osu(f)
|
|
assert len(c.notes) == len(parse_osu(f))
|
|
auto_place(c)
|
|
assert validate(c) == [], f
|
|
|
|
|
|
# ---------- 編輯器操作(無視窗)----------
|
|
|
|
@pytest.fixture
|
|
def editor():
|
|
os.environ["SDL_VIDEODRIVER"] = "dummy"
|
|
import pygame
|
|
from maker import Editor
|
|
ed = Editor(audio_enabled=False)
|
|
ed.chart = Chart("", BPM, 0, 4, [])
|
|
yield ed
|
|
pygame.quit()
|
|
|
|
|
|
def _click(ed, x, y, button=1, up_y=None, shift=False):
|
|
import pygame
|
|
pygame.key.set_mods(pygame.KMOD_SHIFT if shift else 0)
|
|
ed.handle(pygame.event.Event(pygame.MOUSEBUTTONDOWN, pos=(x, y), button=button))
|
|
if button == 1:
|
|
ed.handle(pygame.event.Event(pygame.MOUSEBUTTONUP, pos=(x, up_y if up_y is not None else y), button=1))
|
|
pygame.key.set_mods(0)
|
|
|
|
|
|
def test_editor_place_tap_hold_slide_and_undo(editor):
|
|
from maker import lane_x, JUDGE_Y, LANE_W
|
|
ed = editor
|
|
ed.time_ms = 0
|
|
# 判定線上方 125ms 處點一下 → L 軌 tap
|
|
y = JUDGE_Y - 125 * ed.zoom
|
|
_click(ed, lane_x("L") + LANE_W // 2, y)
|
|
assert [(n.lane, n.type, n.t) for n in ed.chart.notes] == [("L", "tap", 125)]
|
|
|
|
# R 軌往上拖 500ms → hold
|
|
_click(ed, lane_x("R") + LANE_W // 2, JUDGE_Y - 500 * ed.zoom, up_y=JUDGE_Y - 1000 * ed.zoom)
|
|
hold = [n for n in ed.chart.notes if n.lane == "R"][0]
|
|
assert (hold.type, hold.t, hold.end) == ("hold", 500, 1000)
|
|
|
|
# Shift + 拖曳 → slide
|
|
_click(ed, lane_x("M") + LANE_W // 2, JUDGE_Y - 1500 * ed.zoom, up_y=JUDGE_Y - 2000 * ed.zoom, shift=True)
|
|
assert any(n.type == "slide" and n.lane == "M" for n in ed.chart.notes)
|
|
|
|
# 右鍵刪除 tap,再復原
|
|
_click(ed, lane_x("L") + LANE_W // 2, y, button=3)
|
|
assert not any(n.lane == "L" for n in ed.chart.notes)
|
|
ed.undo()
|
|
assert any(n.lane == "L" for n in ed.chart.notes)
|
|
|
|
|
|
def test_editor_assign_cell_respects_zone(editor):
|
|
from maker import cell_rect
|
|
ed = editor
|
|
note = ed.chart.add(Note(1000, "L", "tap"))
|
|
ed.tab = "pos"
|
|
ed.selected = note
|
|
r = cell_rect(4, 0) # 右手區 → 拒絕
|
|
_click(ed, r.centerx, r.centery)
|
|
assert note.pos is None
|
|
r = cell_rect(1, 3)
|
|
_click(ed, r.centerx, r.centery)
|
|
assert note.pos == (1, 3) and note.locked
|
|
|
|
|
|
def test_editor_recording_snaps(editor):
|
|
ed = editor
|
|
ed.recorded = [("L", 1010), ("R", 1490), ("M", 2130)]
|
|
ed.finish_recording()
|
|
assert [(n.lane, n.t) for n in ed.chart.notes] == [("L", 1000), ("R", 1500), ("M", 2125)]
|