Files
Little-God1983-ComfyUI-AI2G…/tests/test_prompt_batch_core.py
T
Chris dc914202e6 refactor: extract prompt batch parse/select into comfy-free core
Move _parse_prompts/_select/DEFAULT_PROMPTS_JSON out of nodes/prompt_batch.py
(which imports comfy_api) into a pure nodes/prompt_batch_core.py so the parsing
logic can be unit tested and reused by the upcoming Civitai metadata tracer
without a ComfyUI install. prompt_batch.py now imports the three names;
behavior is unchanged (select_prompt keeps all three of the original _select's
fallback logger.warning calls, so logging is identical too).

Also fix pytest collection: the repo root doubles as the ComfyUI extension
package (its own __init__.py does relative imports), which made pytest wrap
the repo root in a Package collector and fail importing it. Add
--confcutdir=tests/testpaths in pyproject.toml so `pytest tests/...` (and bare
`pytest`) work without needing a ComfyUI install.
2026-07-12 11:17:06 +02:00

35 lines
1.2 KiB
Python

import json
import pytest
from nodes.prompt_batch_core import parse_prompts, select_prompt, DEFAULT_PROMPTS_JSON
def test_parse_object_list():
raw = json.dumps([{"positive": "fox", "negative": "blur"}, {"positive": "city"}])
assert parse_prompts(raw) == [("fox", "blur"), ("city", "")]
def test_parse_string_shorthand_and_wrapper():
assert parse_prompts(json.dumps(["lonely fox"])) == [("lonely fox", "")]
assert parse_prompts(json.dumps({"prompts": [{"positive": "x"}]})) == [("x", "")]
def test_parse_errors():
with pytest.raises(ValueError):
parse_prompts("")
with pytest.raises(ValueError):
parse_prompts("{not json")
with pytest.raises(ValueError):
parse_prompts(json.dumps([{"positive": " "}])) # empty positive
def test_select_clamps():
prompts = [("a", ""), ("b", "n")]
assert select_prompt(prompts, 1) == ("b", "n", 1)
assert select_prompt(prompts, 9) == ("b", "n", 1) # overshoot clamps to last
assert select_prompt(prompts, -3) == ("a", "", 0) # negative clamps to 0
assert select_prompt(prompts, None) == ("a", "", 0) # non-int tolerated
def test_default_json_is_valid():
assert len(parse_prompts(DEFAULT_PROMPTS_JSON)) >= 1