"""v6k ControlNet track: window / frame mapping / cache keys, processed control video, sampling hook.""" import os import numpy as np import pytest import torch from PIL import Image import folder_paths from h3s import h3s_core as core from h3s import h3s_gen as gen from h3s import h3s_control as control from fakes import tiny_patcher, FakeClip, FakeVAE @pytest.fixture(scope="module") def patcher(): return tiny_patcher() @pytest.fixture() def dirs(tmp_path): out, inp = tmp_path / "out", tmp_path / "in" (inp / "h3labstudio").mkdir(parents=True) out.mkdir() folder_paths.set_output_directory(str(out)) folder_paths.set_input_directory(str(inp)) Image.fromarray((np.random.rand(64, 96, 3) * 255).astype("uint8")).save(inp / "h3labstudio" / "hero.png") return out, inp def _video(inp, name="ctl.mp4", n=72, fps=24, w=96, h=64): from h3s import h3s_media as media fr = torch.zeros(n, h, w, 3) for k in range(n): # a bright bar moving right: frame k has it at column 4k % w x = (4 * k) % (w - 8) fr[k, :, x:x + 8] = torch.tensor([1.0, 0.5, 0.2]) media.write_mp4(str(inp / "h3labstudio" / name), fr, fps=fps) return {"id": "v1", "file": name, "subfolder": "h3labstudio", "category": "video", "frames": n * 24 // fps, "duration": n / fps} def project(ctl=None, **kw): p = {"id": "c1", "settings": {"prompt": "g", "res_mode": "custom", "width": 96, "height": 64, "steps": 2, "cfg": 1.0, "sampler": "euler", "scheduler": "simple", "overlap_blocks": 1}, "assets": [{"id": "a1", "file": "hero.png", "subfolder": "h3labstudio", "category": "character"}, {"id": "v1", "file": "ctl.mp4", "subfolder": "h3labstudio", "category": "video", "frames": 72, "duration": 3.0}], "segments": [{"id": "s1", "frames": 39, "prompt": "one", "refs": ["a1"]}, {"id": "s2", "frames": 39, "prompt": "two", "refs": ["a1"]}]} if ctl is not None: p["control"] = ctl p.update(kw) return p def test_normalize_control_and_window(): p = core.normalize_project(project({"mode": "pose", "strength": 5, "start": 0.2, "end": 0.1, "clips": [{"id": "k", "asset": "v1", "start": 1.0, "in": 0.5, "length": 0}]})) ct = p["control"] assert ct["row"] and ct["on"] and ct["mode"] == "pose" and ct["strength"] == 2.0 and ct["start"] == 0.2 and ct["end"] == 0.2 assert core.control_clip_span(p, ct["clips"][0]) == (1.0, 3.5) # to the file end (3 s − 0.5 s in) it = core.plan(p) w0 = core.control_window(p, it[0]) # segment 1: 0..39/24 s assert w0["rel"] == 1.0 and w0["in"] == 0.5 and w0["asset"] == "v1" # before the clip → its first frame; inside → time-aligned; after it → the last frame held assert core.control_frame_index(w0, 0) == 12 assert core.control_frame_index(w0, 30) == round((0.5 + 30 / 24 - 1.0) * 24) assert core.control_frame_index(w0, 10 ** 4) == round((0.5 + 2.5 - 1 / 24) * 24) # off / strength 0 / unknown asset → no control for bad in ({"on": False, "clips": [{"asset": "v1"}]}, {"strength": 0, "clips": [{"asset": "v1"}]}, {"clips": [{"asset": "zz"}]}): q = core.normalize_project(project(bad)) assert all(core.control_window(q, i) is None for i in core.plan(q)) assert core.normalize_project(project())["control"] == core.normalize_control(None, set()) def test_control_cache_keys(): base = core.segment_keys(core.normalize_project(project())) ctl = {"mode": "depth", "clips": [{"asset": "v1", "start": 0, "length": 1.0}]} k = core.segment_keys(core.normalize_project(project(ctl))) assert k[0] != base[0] # guided segment changes assert core.segment_keys(core.normalize_project(project(dict(ctl, on=False)))) == base # off = old keys for ch in ({"strength": 1.5}, {"mode": "canny"}, {"end": 0.5}, {"params": {"depth_model": "base"}}, {"clips": [{"asset": "v1", "start": 0, "in": 0.25, "length": 1.0}]}): assert core.segment_keys(core.normalize_project(project(dict(ctl, **ch))))[0] != k[0], ch # params of another mode do not matter assert core.segment_keys(core.normalize_project(project(dict(ctl, params={"canny_lo": 0.1}))))[0] == k[0] def test_preprocess_gray_and_canny(): fr = torch.rand(3, 32, 48, 3) g = control.preprocess(fr, "gray") assert g.shape == fr.shape and torch.allclose(g[..., 0], g[..., 2]) assert control.preprocess(fr, "ready") is fr c = control.preprocess(fr, "canny", {"canny_lo": 0.2, "canny_hi": 0.5}) assert c.shape == fr.shape and set(torch.unique(c).tolist()) <= {0.0, 1.0} def test_processed_video_cached_per_mode(dirs): out, inp = dirs a = _video(inp) src = gen.asset_path(a) seen = [] p1 = control.build_processed(src, "gray", {}, progress=lambda d, n: seen.append((d, n))) assert os.path.isfile(p1) and seen[-1][0] == 72 and seen[-1][1] == 72 assert os.path.getmtime(p1) and os.path.dirname(p1).endswith(os.path.join("h3labstudio", "_control")) t = os.path.getmtime(p1) assert control.build_processed(src, "gray", {}) == p1 and os.path.getmtime(p1) == t # cached p2 = control.build_processed(src, "canny", {}) p3 = control.build_processed(src, "canny", {"canny_lo": 0.1}) assert len({p1, p2, p3}) == 3 from h3s import h3s_media as media assert media.video_info(p1)["frames"] == 72 fr = media.load_video_frames(p1, 0, 2, 96, 64) assert np.abs(fr[..., 0] - fr[..., 2]).max() < 0.08 # gray # the timeline job: done at once when cached j = control.start_job(src, "gray", {}) assert j["state"] == "done" and j["ref"]["filename"] == os.path.basename(p1) def _pinfo(name=None, bad=()): return lambda: {"name": name, "path": "/x" if name else None, "v1": bool(name and "2.0" not in name), "fallback": bool(name and "2.0" not in name), "bad": [dict(b) for b in bad]} def test_generation_without_patch_is_blocked_clearly(patcher, dirs, monkeypatch): """v6l: no usable patch + a control clip under a segment → the run stops before sampling with a clear message (naming the incomplete files, which are never loaded) instead of a silent run without control.""" out, inp = dirs _video(inp) monkeypatch.setattr(control, "patch_info", _pinfo(None, [{"file": "minimax_h3_fun_controlnet_union_2.0_pruned_int8_convrot.safetensors", "why": "size"}])) sampled, real = [], gen.sample_pass monkeypatch.setattr(gen, "sample_pass", lambda *a, **k: sampled.append(1) or real(*a, **k)) p = project({"mode": "gray", "clips": [{"asset": "v1", "start": 0, "length": 0.5}]}) with pytest.raises(control.ControlPatchMissing) as e: gen.run_project(patcher, FakeClip(), FakeVAE(), p) assert "ControlNet" in str(e.value) and "union_2.0" in str(e.value) and "Настройки → Модели" in str(e.value) assert not sampled # the track off → no block p["control"]["on"] = False gen.run_project(patcher, FakeClip(), FakeVAE(), p) assert sampled def test_v1_fallback_warns_and_keys_differ(patcher, dirs, monkeypatch): out, inp = dirs _video(inp) import comfy_extras.nodes_minimax_h3 as nm class FakeApply: @staticmethod def execute(model, patch, vae, strength, start, end, control_video=None): return (model,) monkeypatch.setattr(nm, "MiniMaxH3FunControlNetApply", FakeApply) v1 = "minimax_h3_fun_controlnet_union_pruned_int8_convrot.safetensors" monkeypatch.setattr(control, "patch_info", _pinfo(v1, [{"file": "minimax_h3_fun_controlnet_union_2.0_pruned_int8_convrot.safetensors", "why": "part"}])) monkeypatch.setattr(control, "load_patch", lambda: (v1, "PATCH")) warns, logs = [], [] monkeypatch.setattr(gen, "log", lambda m: logs.append(m)) p = project({"mode": "gray", "clips": [{"asset": "v1", "start": 0, "length": 0.5}]}) _, _, _, info = gen.run_project(patcher, FakeClip(), FakeVAE(), p) assert info["segments"][0]["ctl"]["ok"] and info["segments"][0]["ctl"]["patch"] == v1 assert sum("запасная модель v1" in m and "недокачан" in m for m in logs) == 1 # once per run assert info["keys"][0].endswith("1") and len(info["keys"][0]) == len(info["keys"][1]) + 1 def _st(path, tensors=(("w", 16),), cut=0, header=None): import json import struct hdr, off = {}, 0 for k, n in tensors: hdr[k] = {"dtype": "F32", "shape": [n // 4], "data_offsets": [off, off + n]} off += n hb = json.dumps(header if header is not None else hdr).encode() data = struct.pack(" 0 def test_cn_audio_without_sound_is_skipped_with_a_warning(monkeypatch): p = _cn_audio() warns = [] monkeypatch.setitem(gen.CTL_RUN, "warn", warns.append) def no_audio(ref): raise ValueError("в файле нет аудиодорожки: ctl.mp4") monkeypatch.setattr(gen, "load_audio_ref", no_audio) assert gen.audio_sources(p) == {} assert warns and "Звук в H3" in warns[0] # a real audio asset without sound still fails loudly p["audio"]["clips"] = [{"id": "x", "asset": "m1", "lane": 0, "start": 0, "in": 0, "length": 1}] with pytest.raises(ValueError): gen.audio_sources(p)