Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

3.0 KB
Raw Blame
from __future__ import annotations

import importlib.util
import json
from pathlib import Path

# Absolute, like `test_check_pins.py` and `test_new_architecture_script.py`: a path relative to
# the process cwd only resolves when pytest is started from the repository root, and the four
# tests below fail with a FileNotFoundError from anywhere else.
REPO_ROOT = Path(__file__).resolve().parent.parent


def _load_module(module_path: Path, module_name: str):
    spec = importlib.util.spec_from_file_location(module_name, module_path)
    assert spec is not None
    assert spec.loader is not None
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def test_docs_json_export_bundle_structure():
    module = _load_module(REPO_ROOT / "scripts" / "generate_docs_json.py", "generate_docs_json")

    bundle = module.build_docs_bundle()

    assert set(bundle.keys()) == {"invocation_context", "settings"}


def test_docs_json_export_includes_images_interface_and_host_setting():
    module = _load_module(REPO_ROOT / "scripts" / "generate_docs_json.py", "generate_docs_json")

    bundle = module.build_docs_bundle()

    interface_names = {interface["name"] for interface in bundle["invocation_context"]["interfaces"]}
    assert "ImagesInterface" in interface_names

    setting_names = {setting["name"] for setting in bundle["settings"]["settings"]}
    assert "host" in setting_names
    assert "schema_version" not in setting_names


def test_docs_json_export_includes_rendering_metadata():
    module = _load_module(REPO_ROOT / "scripts" / "generate_docs_json.py", "generate_docs_json")

    bundle = module.build_docs_bundle()

    images_interface = next(
        interface for interface in bundle["invocation_context"]["interfaces"] if interface["name"] == "ImagesInterface"
    )
    save_method = next(method for method in images_interface["methods"] if method["name"] == "save")
    host_setting = next(setting for setting in bundle["settings"]["settings"] if setting["name"] == "host")

    assert save_method["description"]
    assert "parameters" in save_method
    assert save_method["parameters"]

    assert host_setting["env_var"] == "INVOKEAI_HOST"
    assert host_setting["category"] == "WEB"
    assert "validation" in host_setting
    assert host_setting["validation"] == {}


def test_docs_json_export_writes_expected_files(tmp_path: Path):
    module = _load_module(REPO_ROOT / "scripts" / "generate_docs_json.py", "generate_docs_json")

    bundle = module.build_docs_bundle()
    module.write_docs_bundle(bundle, tmp_path)

    invocation_context_path = tmp_path / "invocation-context.json"
    settings_path = tmp_path / "settings.json"

    assert sorted(path.name for path in tmp_path.iterdir()) == [
        "invocation-context.json",
        "settings.json",
    ]

    invocation_context_payload = json.loads(invocation_context_path.read_text())
    settings_payload = json.loads(settings_path.read_text())

    assert invocation_context_payload == bundle["invocation_context"]
    assert settings_payload == bundle["settings"]