# AICG3D 插件实机加载探针：验证「HEAD 崩溃 / GH 节点未注册 / SelfLift 被 mock」三条结论
import importlib.util, importlib, sys, os, traceback

COMFY = "/home/zyw/ComfyUI"
PROBE = "/tmp/aicg3d-probe"
sys.path.insert(0, COMFY)
sys.path.insert(0, PROBE)
os.chdir(COMFY)


def load(name, path):
    spec = importlib.util.spec_from_file_location(
        name, path, submodule_search_locations=[PROBE]
    )
    m = importlib.util.module_from_spec(spec)
    sys.modules[name] = m
    spec.loader.exec_module(m)
    return m


print("=" * 18, "A: main HEAD 原版 __init__.py（未修复）", "=" * 18, flush=True)
try:
    load("probe_broken", os.path.join(PROBE, "_init_broken.py.orig"))
    print("A 结果：居然加载成功（与预期不符！）", flush=True)
except SyntaxError as e:
    print("A 结果：SyntaxError ->", e, flush=True)
except Exception as e:
    print("A 结果：", type(e).__name__, e, flush=True)

print("=" * 18, "B: 修复 __init__.py 后加载", "=" * 18, flush=True)
try:
    m = load("comfyui_AICG3D", os.path.join(PROBE, "__init__.py"))
    ids = sorted(m.NODE_CLASS_MAPPINGS)
    print("B 结果：注册节点数 =", len(ids), flush=True)
    for i in ids:
        print("    ", i, flush=True)
    gh = [
        x
        for x in (
            "MiniMaxH3IntegrationGH",
            "MiniMaxH3IntegrationAdapterGH",
            "MiniMaxH3DualClockT8GH",
            "MiniMaxH3AVDecodeT8GH",
        )
        if x in m.NODE_CLASS_MAPPINGS
    ]
    print("    Goohai 4 个 GH 节点是否注册：", gh or "一个都没有", flush=True)
    print("    顶层是否暴露 comfy_entrypoint：", hasattr(m, "comfy_entrypoint"), flush=True)
except Exception:
    traceback.print_exc()

print("=" * 18, "C: SelfLift / 采样计划用的是哪套实现", "=" * 18, flush=True)
try:
    import h3easy.nodes as hn

    print("C 结果：h3easy.nodes.SAMPLING_PLAN_TYPE =", hn.SAMPLING_PLAN_TYPE, flush=True)
    print("        h3easy.nodes.SELFLIFT_KIND     =", hn.SELFLIFT_KIND, flush=True)
    try:
        import h3easy.sampling_strategies as ss

        print("        真实模块常量 =", ss.SAMPLING_PLAN_TYPE, "/", ss.SELFLIFT_KIND, flush=True)
        plan = hn.MiniMaxH3SamplingPlan(kind="selflift", cfg=1.0)
        print("        节点造出的 plan 字段 =", getattr(plan, "__dict__", {}), flush=True)
        out = hn.sample_with_sampling_plan(plan, model=None, positive=None, latent=None,
                                          sampler=None, sigmas=None, seed=1,
                                          video_vae=None, callback=None, disable_pbar=True)
        print("        sample_with_sampling_plan 返回 =", out, "  ← None 会让渲染节点输出空 latent", flush=True)
    except Exception:
        traceback.print_exc()
except Exception:
    traceback.print_exc()
print("探针结束", flush=True)
