From 926b86b9011456fc4b102344bfab300def35b172 Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Tue, 17 Nov 2026 09:29:58 +0900 Subject: [PATCH] v4: Fix the legacy batch update, the update policy, console messages and settings Six fixes. The CHANGELOG has an entry for each. - Queue the legacy ComfyUI update and answer failed version requests - Check out a branch head by its name with GitPython - Follow the update policy when an update request names no version line - Print the messages of manager_core as they are - Refuse a db_mode or update_policy request the servers cannot use - Keep a setting chosen while the legacy menu is loading it The E2E cases wait for the Registry list, which a fresh environment has not cached, and the Playwright cases no longer depend on the timing of the UI. --- CHANGELOG.md | 64 +++++++++- comfyui_manager/common/enums.py | 3 +- comfyui_manager/common/git_compat.py | 4 + .../data_models/generated_models.py | 8 +- comfyui_manager/glob/manager_core.py | 12 +- comfyui_manager/glob/manager_server.py | 12 +- comfyui_manager/js/comfyui-manager.js | 20 ++- comfyui_manager/legacy/manager_core.py | 12 +- comfyui_manager/legacy/manager_server.py | 33 +++-- openapi.yaml | 14 +- tests/e2e/test_e2e_endpoint.py | 20 ++- tests/e2e/test_e2e_task_operations.py | 10 +- tests/playwright/helpers.ts | 30 +++-- tests/playwright/legacy-ui-install.spec.ts | 9 +- .../playwright/legacy-ui-manager-menu.spec.ts | 59 +++++++++ tests/playwright/legacy-ui-snapshot.spec.ts | 4 +- tests/test_core_print.py | 47 +++++++ tests/test_git_compat.py | 22 ++++ tests/test_legacy_failure_answers.py | 120 ++++++++++++++++++ tests/test_openapi_setting_values.py | 23 ++++ tests/test_setting_values.py | 92 ++++++++++++++ tests/test_update_comfyui_policy.py | 70 ++++++++++ 22 files changed, 631 insertions(+), 57 deletions(-) create mode 100644 tests/test_core_print.py create mode 100644 tests/test_legacy_failure_answers.py create mode 100644 tests/test_openapi_setting_values.py create mode 100644 tests/test_setting_values.py create mode 100644 tests/test_update_comfyui_policy.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 9897252040..b350d2707a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,66 @@ and this project adheres to [Semantic Versioning 2.0.0](https://semver.org/spec/ ## [Unreleased] +### Fixed + +- Restarting ComfyUI from the Manager under comfy-cli no longer prints + `ValueError: I/O operation on closed file` on the console. The log file is + closed before the process exits, and what was logged after that was still + written to it. +- In the legacy UI, Update ComfyUI and Update All with ComfyUI no longer end + with status 500 and nothing queued. The batch route called the handler of + the update route without a request. +- With `update_policy = nightly`, the legacy server updates ComfyUI to the + nightly version. Only `nightly-comfyui` was taken as nightly, and `nightly` + as stable. +- The legacy server answers 400 again when the list of ComfyUI versions cannot + be read or a switch to another version fails. The call that logs the cause + was given an argument it does not take, so the answer was 500 with a + `TypeError` in the log. + +- Update ComfyUI to the latest stable version again when Manager runs git + through GitPython, which it does whenever the `git` command is available. + The update ended with `Failed to checkout 'master' branch` and ComfyUI + stayed at its version: the branch to check out was passed to git as an + object instead of by its name. An update to the nightly version failed the + same way when the checkout was detached and the remote's default branch was + not known locally. + +- On the default server, `POST /v2/manager/queue/update_comfyui` follows + `update_policy` when the request names no version line. Its `stable` + parameter defaulted to true, so the policy was never read and such a request + updated ComfyUI to the stable version also under `nightly-comfyui`. + `nightly` is taken as nightly too. +- On the default server, the same route reads `is_stable`, the name the + ComfyUI frontend sends, when `stable` is not given. It was ignored. +- `openapi.yaml` lists the values the two setting routes accept: `cache` for + `db_mode` and `stable-comfyui` for the update policy were missing. + +- Print console messages with paths, URLs and package names as they are. + Manager printed many of its messages through rich, which reads square + brackets as markup: a package name such as `diffusers[torch]` was shown as + `diffusers`, a text such as `:100:` as an emoji, and a line longer than the + console was broken in the middle of a path or a URL. A text with a closing + tag such as `[/b]` raised `MarkupError` instead of being printed, in the + middle of the operation that reported it. Numbers and quoted strings keep + their colors. + +- Refuse a `db_mode` or an `update_policy` that Manager does not know in the + legacy server. With `--enable-manager-legacy-ui`, the two setting routes + stored whatever value a request carried and wrote it to `config.ini`. They + answer 400 for a value that is not one of the known ones, as the default + server does. +- Answer 400 from the two setting routes of both servers for a JSON body that + is not an object. It ended with status 500. +- Name the security level `normal-` and the database mode `channel` as the + servers do in `common/enums.py`, which had `normal-minus` and no `channel`. + Nothing read these two members. + +- Keep a database mode, update policy or share option that is chosen in the + legacy Manager menu while the menu is still loading the stored value. The + value was saved, but the menu went back to showing the old one when it + arrived. + ## [4.4] - 2026-11-16 ### Changed @@ -210,10 +270,6 @@ and this project adheres to [Semantic Versioning 2.0.0](https://semver.org/spec/ always answered that it was not one, so after `colorama.init()` the colors of every `print()` were removed, and tqdm and rich took a terminal for a pipe (#3259, #3423). -- Restarting ComfyUI from the Manager under comfy-cli no longer prints - `ValueError: I/O operation on closed file` on the console. The log file is - closed before the process exits, and what was logged after that was still - written to it. ## [4.3] - 2026-09-19 diff --git a/comfyui_manager/common/enums.py b/comfyui_manager/common/enums.py index ed60e86b47..feb9559c99 100644 --- a/comfyui_manager/common/enums.py +++ b/comfyui_manager/common/enums.py @@ -9,10 +9,11 @@ class NetworkMode(enum.Enum): class SecurityLevel(enum.Enum): STRONG = "strong" NORMAL = "normal" - NORMAL_MINUS = "normal-minus" + NORMAL_MINUS = "normal-" WEAK = "weak" class DBMode(enum.Enum): LOCAL = "local" CACHE = "cache" + CHANNEL = "channel" REMOTE = "remote" \ No newline at end of file diff --git a/comfyui_manager/common/git_compat.py b/comfyui_manager/common/git_compat.py index cc2c1a7337..42dc2940d6 100644 --- a/comfyui_manager/common/git_compat.py +++ b/comfyui_manager/common/git_compat.py @@ -415,6 +415,10 @@ def create_backup_branch(self, name): self._repo.create_head(name) def checkout(self, ref): + # ref can be a _HeadProxy from get_head_by_name + if isinstance(ref, _HeadProxy): + ref = ref.name + self._repo.git.checkout(ref) def checkout_new_branch(self, branch_name, start_point): diff --git a/comfyui_manager/data_models/generated_models.py b/comfyui_manager/data_models/generated_models.py index 0723027907..e144fbf29c 100644 --- a/comfyui_manager/data_models/generated_models.py +++ b/comfyui_manager/data_models/generated_models.py @@ -224,8 +224,12 @@ class UpdateComfyUIQueryParams(BaseModel): ) ui_id: str = Field(..., description="UI identifier for task tracking") stable: Optional[bool] = Field( - True, - description="Whether to update to stable version (true) or nightly (false)", + None, + description="Whether to update to stable version (true) or nightly (false). When neither this nor `is_stable` is given, the update policy decides.", + ) + is_stable: Optional[bool] = Field( + None, + description="Another name for `stable`, the one the task parameters use. Read when `stable` is not given.", ) diff --git a/comfyui_manager/glob/manager_core.py b/comfyui_manager/glob/manager_core.py index 1f75624f23..d73733810a 100644 --- a/comfyui_manager/glob/manager_core.py +++ b/comfyui_manager/glob/manager_core.py @@ -29,7 +29,9 @@ orig_print = print -from rich import print +import rich +from rich.console import Console +from rich.markup import escape from packaging import version import uuid @@ -46,6 +48,12 @@ from ..common import context +def print(*objects, sep=" ", end="\n", file=None): + # rich's print with its highlighting only, so that a path, a URL or an error text is written as it is + console = rich.get_console() if file is None else Console(file=file) + console.print(*objects, sep=sep, end=end, markup=False, emoji=False, soft_wrap=True) + + try: from importlib.metadata import version as _pkg_version _raw_version = _pkg_version("comfyui-manager") @@ -699,7 +707,7 @@ async def load_nightly(channel, mode): channel_url = normalize_channel(channel) if channel_url: if mode not in ['remote', 'local', 'cache']: - print(f"[bold red]ERROR: Invalid mode is specified `--mode {mode}`[/bold red]", file=sys.stderr) + rich.print(f"[bold red]ERROR: Invalid mode is specified `--mode {escape(mode)}`[/bold red]", file=sys.stderr) return {} # validate channel - only the channel set by the user is allowed. diff --git a/comfyui_manager/glob/manager_server.py b/comfyui_manager/glob/manager_server.py index 61d6ecf9f2..06dd5d422a 100644 --- a/comfyui_manager/glob/manager_server.py +++ b/comfyui_manager/glob/manager_server.py @@ -1860,10 +1860,12 @@ async def update_comfyui(request): ) # Check if stable parameter was provided, otherwise use config - if query_params.stable is None: - is_stable = core.get_config()["update_policy"] != "nightly-comfyui" - else: + if query_params.stable is not None: is_stable = query_params.stable + elif query_params.is_stable is not None: + is_stable = query_params.is_stable + else: + is_stable = core.get_config()["update_policy"] not in ("nightly", "nightly-comfyui") client_id = query_params.client_id ui_id = query_params.ui_id @@ -1993,7 +1995,7 @@ async def set_db_mode_api(request): environment_utils.set_db_mode(data["value"]) core.write_config() return web.Response(status=200) - except (json.JSONDecodeError, KeyError): + except (json.JSONDecodeError, KeyError, TypeError): return web.Response(status=400, text="Invalid request") except ValueError as e: return web.Response(status=400, text=str(e)) @@ -2015,7 +2017,7 @@ async def set_update_policy_api(request): environment_utils.set_update_policy(data["value"]) core.write_config() return web.Response(status=200) - except (json.JSONDecodeError, KeyError): + except (json.JSONDecodeError, KeyError, TypeError): return web.Response(status=400, text="Invalid request") except ValueError as e: return web.Response(status=400, text=str(e)) diff --git a/comfyui_manager/js/comfyui-manager.js b/comfyui_manager/js/comfyui-manager.js index 22be7004fe..da697c4204 100644 --- a/comfyui_manager/js/comfyui-manager.js +++ b/comfyui_manager/js/comfyui-manager.js @@ -232,6 +232,7 @@ var switch_comfyui_button = null; var update_all_button = null; var restart_stop_button = null; var update_policy_combo = null; +var update_policy_chosen = false; let share_option = 'all'; const share_options = [ @@ -644,10 +645,12 @@ async function switchComfyUI() { showVersionSelectorDialog(versions, obj.current, async (selected_version) => { if(selected_version == 'nightly') { + update_policy_chosen = true; update_policy_combo.value = 'nightly-comfyui'; api.fetchApi('/v2/manager/policy/update', { method: 'POST', body: JSON.stringify({value: 'nightly-comfyui'}) }); } else { + update_policy_chosen = true; update_policy_combo.value = 'stable-comfyui'; api.fetchApi('/v2/manager/policy/update', { method: 'POST', body: JSON.stringify({value: 'stable-comfyui'}) }); } @@ -978,11 +981,14 @@ class ManagerMenuDialog extends ComfyDialog { this.datasrc_combo.appendChild($el('option', { value: 'local', text: 'Local' }, [])); this.datasrc_combo.appendChild($el('option', { value: 'remote', text: 'Channel (remote)' }, [])); + // a choice made before the stored value arrives is not to be replaced by it + let db_mode_chosen = false; api.fetchApi('/v2/manager/db_mode') .then(response => response.text()) - .then(data => { this.datasrc_combo.value = data; }); + .then(data => { if(!db_mode_chosen) this.datasrc_combo.value = data; }); this.datasrc_combo.addEventListener('change', function (event) { + db_mode_chosen = true; api.fetchApi('/v2/manager/db_mode', { method: 'POST', body: JSON.stringify({value: event.target.value}) }); }); @@ -1026,14 +1032,18 @@ class ManagerMenuDialog extends ComfyDialog { share_combo.appendChild($el('option', { value: option[0], text: `${option[1]}` }, [])); } + let share_option_chosen = false; api.fetchApi('/v2/manager/share_option') .then(response => response.text()) .then(data => { - share_option = to_share_option(data); - share_combo.value = share_option; + if(!share_option_chosen) { + share_option = to_share_option(data); + share_combo.value = share_option; + } }); share_combo.addEventListener('change', function (event) { + share_option_chosen = true; const value = event.target.value; share_option = value; api.fetchApi(`/v2/manager/share_option?value=${value}`); @@ -1056,10 +1066,12 @@ class ManagerMenuDialog extends ComfyDialog { api.fetchApi('/v2/manager/policy/update') .then(response => response.text()) .then(data => { - update_policy_combo.value = data; + if(!update_policy_chosen) + update_policy_combo.value = data; }); update_policy_combo.addEventListener('change', function (event) { + update_policy_chosen = true; api.fetchApi('/v2/manager/policy/update', { method: 'POST', body: JSON.stringify({value: event.target.value}) }); }); diff --git a/comfyui_manager/legacy/manager_core.py b/comfyui_manager/legacy/manager_core.py index a292a9e6ca..e1ac47dd28 100644 --- a/comfyui_manager/legacy/manager_core.py +++ b/comfyui_manager/legacy/manager_core.py @@ -27,7 +27,9 @@ orig_print = print -from rich import print +import rich +from rich.console import Console +from rich.markup import escape from packaging import version import uuid @@ -44,6 +46,12 @@ from ..common import context +def print(*objects, sep=" ", end="\n", file=None): + # rich's print with its highlighting only, so that a path, a URL or an error text is written as it is + console = rich.get_console() if file is None else Console(file=file) + console.print(*objects, sep=sep, end=end, markup=False, emoji=False, soft_wrap=True) + + try: from importlib.metadata import version as _pkg_version _raw_version = _pkg_version("comfyui-manager") @@ -689,7 +697,7 @@ async def load_nightly(channel, mode): channel_url = normalize_channel(channel) if channel_url: if mode not in ['remote', 'local', 'cache']: - print(f"[bold red]ERROR: Invalid mode is specified `--mode {mode}`[/bold red]", file=sys.stderr) + rich.print(f"[bold red]ERROR: Invalid mode is specified `--mode {escape(mode)}`[/bold red]", file=sys.stderr) return {} # validate channel - only the channel set by the user is allowed. diff --git a/comfyui_manager/legacy/manager_server.py b/comfyui_manager/legacy/manager_server.py index 7332a979be..0b917a261b 100644 --- a/comfyui_manager/legacy/manager_server.py +++ b/comfyui_manager/legacy/manager_server.py @@ -199,10 +199,18 @@ def run_script(self, cmd, cwd='.'): local_db_extension_node_mappings = os.path.join(manager_util.comfyui_manager_path, "extension-node-map.json") +ALLOWED_UPDATE_POLICIES = ('stable', 'stable-comfyui', 'nightly', 'nightly-comfyui') +ALLOWED_DB_MODES = ('cache', 'channel', 'local', 'remote') + + def set_update_policy(mode): + if mode not in ALLOWED_UPDATE_POLICIES: + raise ValueError(f"Invalid update_policy {mode!r}; must be one of {ALLOWED_UPDATE_POLICIES}") core.get_config()['update_policy'] = mode def set_db_mode(mode): + if mode not in ALLOWED_DB_MODES: + raise ValueError(f"Invalid db_mode {mode!r}; must be one of {ALLOWED_DB_MODES}") core.get_config()['db_mode'] = mode def print_comfyui_version(): @@ -802,7 +810,8 @@ async def queue_batch(request): failed.add(x['id']) elif k == 'update_comfyui': - await update_comfyui(None) + if v: + _update_comfyui() elif k == 'disable': for x in v: @@ -1313,7 +1322,7 @@ def unzip_install(files): os.remove(temp_filename) except Exception as e: - logging.error(f"Install(unzip) error: {url} / {e}", file=sys.stderr) + logging.error(f"Install(unzip) error: {url} / {e}") return False logging.info("Installation was successful.") @@ -1655,18 +1664,22 @@ async def update_comfyui(request): rejection = manager_security.reject_simple_form_post(request) if rejection is not None: return rejection - is_stable = core.get_config()['update_policy'] != 'nightly-comfyui' - temp_queue_batch.append(("update-comfyui", ('comfyui', is_stable))) + _update_comfyui() return web.Response(status=200) +def _update_comfyui(): + is_stable = core.get_config()['update_policy'] not in ('nightly', 'nightly-comfyui') + temp_queue_batch.append(("update-comfyui", ('comfyui', is_stable))) + + @routes.get("/v2/comfyui_manager/comfyui_versions") async def comfyui_versions(request): try: res, current, latest = core.get_comfyui_versions() return web.json_response({'versions': res, 'current': current}, status=200, content_type='application/json') except Exception as e: - logging.error(f"ComfyUI update fail: {e}", file=sys.stderr) + logging.error(f"ComfyUI update fail: {e}") return web.Response(status=400) @@ -1691,7 +1704,7 @@ async def comfyui_switch_version(request): except json.JSONDecodeError: return web.Response(status=400, text="Invalid JSON body") except Exception as e: - logging.error(f"ComfyUI update fail: {e}", file=sys.stderr) + logging.error(f"ComfyUI update fail: {e}") return web.Response(status=400) @@ -1780,8 +1793,10 @@ async def set_db_mode_api(request): set_db_mode(data['value']) core.write_config() return web.Response(status=200) - except (json.JSONDecodeError, KeyError): + except (json.JSONDecodeError, KeyError, TypeError): return web.Response(status=400, text='Invalid request') + except ValueError as e: + return web.Response(status=400, text=str(e)) @routes.get("/v2/manager/policy/update") @@ -1800,8 +1815,10 @@ async def set_update_policy_api(request): set_update_policy(data['value']) core.write_config() return web.Response(status=200) - except (json.JSONDecodeError, KeyError): + except (json.JSONDecodeError, KeyError, TypeError): return web.Response(status=400, text='Invalid request') + except ValueError as e: + return web.Response(status=400, text=str(e)) @routes.get("/v2/manager/channel_url_list") diff --git a/openapi.yaml b/openapi.yaml index 7ccea9a795..431c07953f 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -422,8 +422,10 @@ components: description: UI identifier for task tracking stable: type: boolean - default: true - description: Whether to update to stable version (true) or nightly (false) + description: Whether to update to stable version (true) or nightly (false). When neither this nor `is_stable` is given, the update policy decides. + is_stable: + type: boolean + description: Another name for `stable`, the one the task parameters use. Read when `stable` is not given. required: [client_id, ui_id] ComfyUISwitchVersionParams: type: object @@ -1333,7 +1335,7 @@ paths: text/plain: schema: type: string - enum: [channel, local, remote] + enum: [cache, channel, local, remote] post: summary: Set database mode description: Sets the database mode. Payload is a JSON object with a `value` field. @@ -1347,7 +1349,7 @@ paths: properties: value: type: string - enum: [channel, local, remote] + enum: [cache, channel, local, remote] description: New database mode responses: '200': @@ -1365,7 +1367,7 @@ paths: text/plain: schema: type: string - enum: [stable, nightly, nightly-comfyui] + enum: [stable, stable-comfyui, nightly, nightly-comfyui] post: summary: Set update policy description: Sets the update policy. Payload is a JSON object with a `value` field. @@ -1379,7 +1381,7 @@ paths: properties: value: type: string - enum: [stable, nightly, nightly-comfyui] + enum: [stable, stable-comfyui, nightly, nightly-comfyui] description: New update policy responses: '200': diff --git a/tests/e2e/test_e2e_endpoint.py b/tests/e2e/test_e2e_endpoint.py index 554ddcc39f..0fcbba64af 100644 --- a/tests/e2e/test_e2e_endpoint.py +++ b/tests/e2e/test_e2e_endpoint.py @@ -49,6 +49,10 @@ POLL_TIMEOUT = 30 # max seconds to wait for an operation POLL_INTERVAL = 0.5 # seconds between polls +# A fresh environment has no cache of the Registry list. Manager fetches the +# list when the server starts, and an install is refused until it is cached. +REGISTRY_TIMEOUT = 300 + pytestmark = pytest.mark.skipif( not E2E_ROOT or not os.path.isfile(os.path.join(E2E_ROOT, ".e2e_setup_complete")), @@ -143,6 +147,11 @@ def _wait_for(predicate, timeout=POLL_TIMEOUT, interval=POLL_INTERVAL): return False +def _registry_list_is_cached() -> bool: + cache = os.path.join(COMFYUI_PATH, "user", "__manager", "cache") + return os.path.isdir(cache) and any(name.endswith("_nodes.json") for name in os.listdir(cache)) + + # --------------------------------------------------------------------------- # Fixtures # --------------------------------------------------------------------------- @@ -152,9 +161,14 @@ def comfyui(): """Start ComfyUI once for the module, stop after all tests.""" _remove_pack(PACK_DIR_NAME) pid = _start_comfyui() - yield pid - _stop_comfyui() - _remove_pack(PACK_DIR_NAME) + try: + assert _wait_for(_registry_list_is_cached, timeout=REGISTRY_TIMEOUT), ( + f"Manager did not cache the Registry list within {REGISTRY_TIMEOUT}s; an install fails without it" + ) + yield pid + finally: + _stop_comfyui() + _remove_pack(PACK_DIR_NAME) # --------------------------------------------------------------------------- diff --git a/tests/e2e/test_e2e_task_operations.py b/tests/e2e/test_e2e_task_operations.py index c6c3ebf83d..98bd361eae 100644 --- a/tests/e2e/test_e2e_task_operations.py +++ b/tests/e2e/test_e2e_task_operations.py @@ -827,10 +827,10 @@ def test_update_all_queues_tasks(self, comfyui): if isinstance(pkg, dict) and pkg.get("enabled", True): active_packs += 1 - # The handler runs reload + get_custom_nodes synchronously, which can - # be slow (up to ~2 min on cache-cold runs). Use an explicit long - # timeout; a timeout here is a real server-perf regression, NOT - # something to hide as a pass. + # The handler fetches the whole Registry list before it answers. The + # list is fetched 30 nodepacks per page with a pause after each page, + # to keep the load on the Registry low: about three minutes for 6,500 + # nodepacks, and longer as the list grows. resp = requests.post( f"{BASE_URL}/v2/manager/queue/update_all", params={ @@ -838,7 +838,7 @@ def test_update_all_queues_tasks(self, comfyui): "client_id": "e2e-update-all", "ui_id": "e2e-update-all", }, - timeout=180, + timeout=600, ) # Detect expected outcome based on server security configuration. diff --git a/tests/playwright/helpers.ts b/tests/playwright/helpers.ts index aa0b8120f7..6e2f256538 100644 --- a/tests/playwright/helpers.ts +++ b/tests/playwright/helpers.ts @@ -101,18 +101,32 @@ export async function clickMenuButton(page: Page, text: string) { await dialog.locator(`button:has-text("${text}")`).click(); } +/** + * Close the toasts of the ComfyUI frontend. One of them, such as the error + * `getCanvas: canvas is null` that the frontend can raise while several pages + * load at once, can lie over a button of a Manager dialog, and a click on + * that button then never arrives. + */ +export async function closeToasts(page: Page) { + const closeButtons = page.locator('[data-testid="toast"]').getByRole('button', { name: 'Close' }); + // A closed toast leaves the page and the others move up, so each click is + // on the one that is first by then. + for (let left = await closeButtons.count(); left > 0; left--) { + await closeButtons.first().click({ timeout: 2_000 }).catch(() => {}); + await expect(closeButtons).toHaveCount(left - 1, { timeout: 2_000 }).catch(() => {}); + } +} + /** Close the topmost dialog via its X (close) button or Escape. */ export async function closeDialog(page: Page) { - // Try clicking close buttons on visible dialogs. The manager-menu dialog - // (`#cm-manager-dialog`) is a ComfyDialog with `.p-dialog-close-button` (X), - // while sub-dialogs use `.cm-close-btn`. Try both. + // Every Manager dialog has a `.p-dialog-close-button` (X). A sub-dialog lies + // over the manager-menu dialog (`#cm-manager-dialog`), whose X stays visible + // but cannot be clicked then, so the sub-dialogs are tried first. for (const sel of [ - '#cn-manager-dialog button.cm-close-btn', - '#cmm-manager-dialog button.cm-close-btn', - '#snapshot-manager-dialog button.cm-close-btn', - '#cm-manager-dialog button.cm-close-btn', + '#cn-manager-dialog .p-dialog-close-button', + '#cmm-manager-dialog .p-dialog-close-button', + '#snapshot-manager-dialog .p-dialog-close-button', '#cm-manager-dialog .p-dialog-close-button', - '.cm-close-btn', '.p-dialog-close-button', ]) { const btn = page.locator(sel).last(); diff --git a/tests/playwright/legacy-ui-install.spec.ts b/tests/playwright/legacy-ui-install.spec.ts index 75b3d64ac2..a16eec9e16 100644 --- a/tests/playwright/legacy-ui-install.spec.ts +++ b/tests/playwright/legacy-ui-install.spec.ts @@ -141,16 +141,13 @@ test.describe('UI-driven install/uninstall', () => { // Wait for grid to populate before applying filter (avoids race on empty grid) await expect(page.locator('.tg-body .tg-row').first()).toBeVisible({ timeout: 30_000 }); - const initialRowCount = await page.locator('.tg-body .tg-row').count(); - // Filter to Not Installed to make install buttons visible. Wait for - // filtered row count to actually change (DOM state, not wall-clock). + // Filter to Not Installed to make install buttons visible. The grid + // renders only the rows in view, so their number does not tell whether + // the filter was applied; the search below waits for its own result. const filterSelect = page.locator('select.cn-manager-filter').first(); if (await filterSelect.isVisible().catch(() => false)) { await filterSelect.selectOption({ value: 'not-installed' }); - await expect - .poll(async () => page.locator('.tg-body .tg-row').count(), { timeout: 10_000 }) - .not.toBe(initialRowCount); } // Search for the specific test pack. Wait for search to narrow results. diff --git a/tests/playwright/legacy-ui-manager-menu.spec.ts b/tests/playwright/legacy-ui-manager-menu.spec.ts index ef0401b378..d9e25f5473 100644 --- a/tests/playwright/legacy-ui-manager-menu.spec.ts +++ b/tests/playwright/legacy-ui-manager-menu.spec.ts @@ -106,6 +106,65 @@ test.describe('Manager Menu Dialog', () => { } }); + for (const setting of [ + { name: 'DB mode', route: '/v2/manager/db_mode', options: /Cache|Local|Channel/, values: ['local', 'remote'], post: true }, + { name: 'Update Policy', route: '/v2/manager/policy/update', options: /Stable|Nightly/, values: ['nightly-comfyui', 'stable-comfyui'], post: true }, + { name: 'Share', route: '/v2/manager/share_option', options: /YouML|Matrix/, values: ['youml', 'matrix'], post: false }, + ]) { + test(`${setting.name}: a choice made while the setting is still loading is kept`, async ({ page }) => { + // The dialog asks the server for the setting when it is first opened. + // Hold the answer back after the server has given it, choose another + // value, then let the answer through. The value that is chosen is not + // the first option, which the select shows before the answer and which + // a user therefore cannot choose. The share option is saved by a GET + // with `value`, the other two by a POST. + const reads = (request: { method(): string; url(): string }) => + request.method() === 'GET' && request.url().includes(setting.route) && !new URL(request.url()).searchParams.has('value'); + const saves = (request: { method(): string; url(): string }) => request.url().includes(setting.route) && !reads(request); + const save = (value: string) => setting.post + ? page.request.post(setting.route, { data: JSON.stringify({ value }), headers: { 'Content-Type': 'application/json' } }) + : page.request.get(`${setting.route}?value=${value}`); + const stored = await (await page.request.get(setting.route)).text(); + const chosen = setting.values.find((value) => value !== stored)!; + let release!: () => void; + const held = new Promise((resolve) => { release = resolve; }); + let answered!: () => void; + const answer = new Promise((resolve) => { answered = resolve; }); + await page.route(`**${setting.route}*`, async (route) => { + if (!reads(route.request())) { + await route.continue(); + return; + } + const response = await route.fetch(); + answered(); + await held; + await route.fulfill({ response }); + }); + + try { + await openManagerMenu(page); + const combo = page.locator('#cm-manager-dialog').first().locator('select').filter({ hasText: setting.options }).first(); + // The answer that is held back is one the server gave before the choice. + await answer; + const saved = page.waitForResponse((response) => saves(response.request())); + await combo.selectOption(chosen); + await saved; + + const loaded = page.waitForResponse((response) => reads(response.request())); + release(); + await loaded; + await page.waitForTimeout(300); + + await expect(combo).toHaveValue(chosen); + expect(await (await page.request.get(setting.route)).text()).toBe(chosen); + } finally { + release(); + await page.unroute(`**${setting.route}*`); + await save(stored); + } + }); + } + test('Update Policy dropdown persists via UI (close-reopen verification)', async ({ page }) => { // Wave3 WI-U Cluster H target 2: same UI-only pattern as the DB mode test. await openManagerMenu(page); diff --git a/tests/playwright/legacy-ui-snapshot.spec.ts b/tests/playwright/legacy-ui-snapshot.spec.ts index fd8948fddc..9f665b3acd 100644 --- a/tests/playwright/legacy-ui-snapshot.spec.ts +++ b/tests/playwright/legacy-ui-snapshot.spec.ts @@ -6,7 +6,7 @@ */ import { test, expect } from '@playwright/test'; -import { waitForComfyUI, openManagerMenu, clickMenuButton } from './helpers'; +import { waitForComfyUI, openManagerMenu, clickMenuButton, closeToasts } from './helpers'; const SNAPSHOT_ROW_SELECTOR = '#snapshot-manager-dialog tr, #snapshot-manager-dialog li'; @@ -113,6 +113,7 @@ test.describe('Snapshot Manager', () => { page.once('dialog', async (d) => { await d.accept(); }); + await closeToasts(page); await removeBtn.first().click(); // Effect verification: snapshot disappears from backend AND from UI @@ -146,6 +147,7 @@ test.describe('Snapshot Manager', () => { }; }); }); + await closeToasts(page); await page.locator(SNAPSHOT_ROW_SELECTOR, { hasText: name }).first() .locator(`button:has-text("${action}")`).click(); await page.evaluate(() => (window as any).__handled); diff --git a/tests/test_core_print.py b/tests/test_core_print.py new file mode 100644 index 0000000000..aec3051741 --- /dev/null +++ b/tests/test_core_print.py @@ -0,0 +1,47 @@ +"""`print` of the two `manager_core` modules writes its text as it is. + +The cases take what binds the name `print` out of a `manager_core.py`, an +import or a function, and call it. +""" +import ast +import io + +import pytest + +from legacy_html_testutil import PACKAGE + + +@pytest.fixture(params=["glob", "legacy"]) +def printed(request): + source = (PACKAGE / request.param / "manager_core.py").read_text(encoding="utf-8") + nodes = [node for node in ast.parse(source).body + if isinstance(node, (ast.Import, ast.ImportFrom)) and "rich" in ast.unparse(node) + or isinstance(node, ast.FunctionDef) and node.name == "print"] + namespace = {} + exec(compile(ast.Module(body=nodes, type_ignores=[]), "", "exec"), namespace) + + def printed(*objects, **options): + out = io.StringIO() + namespace["print"](*objects, file=out, **options) + return out.getvalue() + + return printed + + +@pytest.mark.parametrize("text", [ + "Install(git-clone) error: https://example.com/a[/b] / failed", + "## ComfyUI-Manager: EXECUTE => ['python', '-m', 'pip', 'install', 'diffusers[torch]']", + "skip black listed pip installation: 'requests[socks]'", + "Uninstall: custom_nodes/pack:100:backup", + "Download: git clone 'https://github.com/" + "a" * 120 + "'", +], ids=["closing tag", "list with an extra", "extra", "emoji code", "longer than 80 columns"]) +def test_text_is_written_as_it_is(printed, text): + assert printed(text) == text + "\n" + + +def test_objects_are_written_as_their_text(printed): + assert printed("Update error:", ValueError("no section [/x]"), 3) == "Update error: no section [/x] 3\n" + + +def test_end_and_sep_are_kept(printed): + assert printed("a", "b", sep="-", end="") == "a-b" diff --git a/tests/test_git_compat.py b/tests/test_git_compat.py index 0d33428902..8dca525e6f 100644 --- a/tests/test_git_compat.py +++ b/tests/test_git_compat.py @@ -578,6 +578,28 @@ def test_checkout_tag(self): self.assertTrue(p2['detached']) self.assertTrue(p2['not_head']) + def test_checkout_head_by_name(self): + # The head of a branch, as get_head_by_name gives it, checks that branch out + head = self.head_sha + snippet = f""" +import tempfile, shutil +dest = tempfile.mkdtemp() +try: + repo = clone_repo(REPO_PATH, os.path.join(dest, 'cloned')) + repo.checkout('v1.0.0') + repo.checkout(repo.get_head_by_name('master')) + result = {{"branch": repo.active_branch_name, "detached": repo.head_is_detached, + "at_head": repo.head_commit_hexsha == {head!r}}} + repo.close() + print(json.dumps(result)) +finally: + shutil.rmtree(dest, ignore_errors=True) +""" + gp, p2 = _run_both(snippet, self.repo_path) + expected = {"branch": "master", "detached": False, "at_head": True} + self.assertEqual(gp, expected) + self.assertEqual(p2, expected) + # === checkout_new_branch === def test_checkout_new_branch(self): diff --git a/tests/test_legacy_failure_answers.py b/tests/test_legacy_failure_answers.py new file mode 100644 index 0000000000..b1225983a5 --- /dev/null +++ b/tests/test_legacy_failure_answers.py @@ -0,0 +1,120 @@ +"""The legacy server answers a failed ComfyUI version request, and a batch queues a ComfyUI update. + +Each case runs the real handlers of `legacy/manager_server.py`; what they call +in `core` is replaced, so nothing is fetched, switched or updated. +""" +import ast +import asyncio +import importlib.util +import json +import logging +import sys +import threading +from types import SimpleNamespace + +import pytest +from aiohttp import web + +from legacy_html_testutil import PACKAGE, load_functions + +SERVER = PACKAGE / "legacy/manager_server.py" +SECURITY = PACKAGE / "common/manager_security.py" +NAMES = {"comfyui_versions", "comfyui_switch_version", "queue_batch", "update_comfyui", "_update_comfyui"} +STABLE = ("update-comfyui", ("comfyui", True)) +NIGHTLY = ("update-comfyui", ("comfyui", False)) +POLICIES = [("stable-comfyui", STABLE), ("stable", STABLE), ("nightly-comfyui", NIGHTLY), ("nightly", NIGHTLY)] + + +def manager_security(): + spec = importlib.util.spec_from_file_location("_manager_security", SECURITY) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def fail(*args): + raise RuntimeError("not a git repository") + + +def server(policy="stable-comfyui", **core): + """The handlers under test and the list they queue to.""" + defined = {node.name for node in ast.parse(SERVER.read_text(encoding="utf-8")).body + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))} + queued = [] + route = lambda path: lambda handler: handler # noqa: E731 + handlers = load_functions(SERVER, NAMES & defined, { + "web": web, "logging": logging, "json": json, "sys": sys, + "routes": SimpleNamespace(get=route, post=route), + "core": SimpleNamespace(get_config=lambda: {"update_policy": policy}, **core), + "manager_security": manager_security(), + "is_allowed_security_level": lambda level: True, "SECURITY_MESSAGE_HIGH_P": "", + "temp_queue_batch": queued, "task_worker_lock": threading.Lock(), + "finalize_temp_queue_batch": lambda batch, failed: None, "_queue_start": lambda: None, + }) + return handlers, queued + + +def request(body=None, content_type="application/json"): + async def read(): + return body + + return SimpleNamespace(json=read, content_type=content_type) + + +def answer(handler, *args): + response = asyncio.run(handler(*args)) + return response.status, response.text + + +def test_version_list_that_cannot_be_read_is_answered_with_400(caplog): + handlers, _ = server(get_comfyui_versions=fail) + assert answer(handlers["comfyui_versions"], request()) == (400, None) + assert "ComfyUI update fail: not a git repository" in caplog.text + + +def test_version_list_is_answered_with_the_versions(): + handlers, _ = server(get_comfyui_versions=lambda: (["nightly", "v0.3.1"], "v0.3.1", "v0.3.1")) + assert answer(handlers["comfyui_versions"], request()) == ( + 200, '{"versions": ["nightly", "v0.3.1"], "current": "v0.3.1"}') + + +def test_version_switch_that_fails_is_answered_with_400(caplog): + handlers, _ = server(switch_comfyui=fail) + assert answer(handlers["comfyui_switch_version"], request({"ver": "v0.3.0"})) == (400, None) + assert "ComfyUI update fail: not a git repository" in caplog.text + + +def test_version_switch_is_passed_on_and_answered_with_200(): + switched = [] + handlers, _ = server(switch_comfyui=switched.append) + assert answer(handlers["comfyui_switch_version"], request({"ver": "v0.3.0"})) == (200, None) + assert switched == ["v0.3.0"] + + +@pytest.mark.parametrize("policy, update", POLICIES) +def test_batch_queues_the_comfyui_update_of_the_policy(policy, update): + handlers, queued = server(policy) + batch = {"batch_id": "b", "update_comfyui": True} + assert answer(handlers["queue_batch"], request(batch)) == (200, '{"failed": []}') + assert queued == [update] + + +@pytest.mark.parametrize("batch", [{"batch_id": "b"}, {"batch_id": "b", "update_comfyui": False}], + ids=["no update_comfyui", "update_comfyui false"]) +def test_batch_that_asks_for_no_comfyui_update_queues_none(batch): + handlers, queued = server() + assert answer(handlers["queue_batch"], request(batch)) == (200, '{"failed": []}') + assert queued == [] + + +@pytest.mark.parametrize("policy, update", POLICIES) +def test_update_route_queues_the_comfyui_update_of_the_policy(policy, update): + handlers, queued = server(policy) + assert answer(handlers["update_comfyui"], request())[0] == 200 + assert queued == [update] + + +def test_update_route_still_refuses_a_form_post(): + handlers, queued = server() + assert answer(handlers["update_comfyui"], request(content_type="application/x-www-form-urlencoded"))[0] == 400 + assert queued == [] diff --git a/tests/test_openapi_setting_values.py b/tests/test_openapi_setting_values.py new file mode 100644 index 0000000000..a06e91fc29 --- /dev/null +++ b/tests/test_openapi_setting_values.py @@ -0,0 +1,23 @@ +"""`openapi.yaml` lists, for `db_mode` and the update policy, the values the routes accept.""" +import ast + +import pytest +import yaml + +from legacy_html_testutil import PACKAGE + +SPEC = yaml.safe_load((PACKAGE.parent / "openapi.yaml").read_text(encoding="utf-8")) +ACCEPTED = {node.targets[0].id: ast.literal_eval(node.value) + for node in ast.parse((PACKAGE / "glob/utils/environment_utils.py").read_text(encoding="utf-8")).body + if isinstance(node, ast.Assign) and getattr(node.targets[0], "id", "").startswith("ALLOWED_")} + + +@pytest.mark.parametrize("route, accepted", [ + ("/v2/manager/db_mode", "ALLOWED_DB_MODES"), + ("/v2/manager/policy/update", "ALLOWED_UPDATE_POLICIES"), +]) +def test_values_of_a_setting_route_are_the_ones_the_route_accepts(route, accepted): + answered = SPEC["paths"][route]["get"]["responses"]["200"]["content"]["text/plain"]["schema"]["enum"] + asked = SPEC["paths"][route]["post"]["requestBody"]["content"]["application/json"]["schema"]["properties"]["value"]["enum"] + + assert sorted(answered) == sorted(asked) == sorted(ACCEPTED[accepted]) diff --git a/tests/test_setting_values.py b/tests/test_setting_values.py new file mode 100644 index 0000000000..73003bc573 --- /dev/null +++ b/tests/test_setting_values.py @@ -0,0 +1,92 @@ +"""Both servers store only a `db_mode` or an `update_policy` that Manager knows. + +Each case runs the real handler of `legacy/manager_server.py` or of +`glob/manager_server.py` with the functions and the lists of values it uses; +`core` holds a configuration in memory and counts how often it is written. +""" +import ast +import asyncio +import importlib.util +import json +import logging +from types import SimpleNamespace + +import pytest +import yaml +from aiohttp import web + +from legacy_html_testutil import PACKAGE + +DB_MODE = ("set_db_mode_api", "db_mode", ["cache", "channel", "local", "remote"]) +UPDATE_POLICY = ("set_update_policy_api", "update_policy", ["stable", "stable-comfyui", "nightly", "nightly-comfyui"]) +SETTINGS = [DB_MODE, UPDATE_POLICY] +HANDLERS = {"set_db_mode_api", "set_update_policy_api"} +SETTERS = {"set_db_mode", "set_update_policy"} +BEFORE = "the value before the request" + + +def take(path, names, namespace): + """Define the functions named, and the lists of known values, of the file in the namespace.""" + nodes = [node for node in ast.parse(path.read_text(encoding="utf-8")).body + if (isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name in names) + or (isinstance(node, ast.Assign) and getattr(node.targets[0], "id", "").startswith("ALLOWED_"))] + exec(compile(ast.Module(nodes, []), str(path), "exec"), namespace) + return namespace + + +@pytest.fixture(params=["legacy", "glob"]) +def post(request): + def post(handler, key, body, allowed=True): + """The status the handler answers, the stored value and how often the configuration was written.""" + config, written = {key: BEFORE}, [] + core = SimpleNamespace(get_config=lambda: config, write_config=lambda: written.append(dict(config))) + namespace = {"web": web, "logging": logging, "json": json, "core": core, "SECURITY_MESSAGE_MIDDLE": "", + "routes": SimpleNamespace(post=lambda path: lambda function: function)} + if request.param == "legacy": + namespace["is_allowed_security_level"] = lambda level: allowed + take(PACKAGE / "legacy/manager_server.py", HANDLERS | SETTERS, namespace) + else: + setters = take(PACKAGE / "glob/utils/environment_utils.py", SETTERS, {"core": core}) + namespace["environment_utils"] = SimpleNamespace(**{name: setters[name] for name in SETTERS}) + namespace["security_utils"] = SimpleNamespace(is_allowed_security_level=lambda level: allowed) + take(PACKAGE / "glob/manager_server.py", HANDLERS, namespace) + + async def read(): + return json.loads(body) + + return asyncio.run(namespace[handler](SimpleNamespace(json=read))).status, config[key], len(written) + + return post + + +@pytest.mark.parametrize("handler, key, value", [(handler, key, value) for handler, key, values in SETTINGS for value in values]) +def test_known_value_is_stored(post, handler, key, value): + assert post(handler, key, json.dumps({"value": value})) == (200, value, 1) + + +@pytest.mark.parametrize("handler, key, values", SETTINGS) +@pytest.mark.parametrize("value", ['"no-such-value"', '""', '"CACHE"', '"STABLE"', "7", "null", '["cache"]', '["stable"]']) +def test_unknown_value_is_refused(post, handler, key, values, value): + assert post(handler, key, '{"value": %s}' % value) == (400, BEFORE, 0) + + +@pytest.mark.parametrize("handler, key, values", SETTINGS) +@pytest.mark.parametrize("body", ["{}", "not json", "[]", '"cache"', "7", "null"]) +def test_request_without_a_value_is_refused(post, handler, key, values, body): + assert post(handler, key, body) == (400, BEFORE, 0) + + +@pytest.mark.parametrize("handler, key, values", SETTINGS) +def test_request_is_refused_by_the_security_level(post, handler, key, values): + assert post(handler, key, json.dumps({"value": values[0]}), allowed=False) == (403, BEFORE, 0) + + +def test_enums_hold_the_values_the_servers_use(): + spec = importlib.util.spec_from_file_location("_enums", PACKAGE / "common/enums.py") + enums = importlib.util.module_from_spec(spec) + spec.loader.exec_module(enums) + known = take(PACKAGE / "glob/utils/environment_utils.py", set(), {}) + openapi = yaml.safe_load((PACKAGE.parent / "openapi.yaml").read_text(encoding="utf-8")) + + assert sorted(mode.value for mode in enums.DBMode) == sorted(known["ALLOWED_DB_MODES"]) + assert sorted(level.value for level in enums.SecurityLevel) == sorted(openapi["components"]["schemas"]["SecurityLevel"]["enum"]) diff --git a/tests/test_update_comfyui_policy.py b/tests/test_update_comfyui_policy.py new file mode 100644 index 0000000000..7cca718dfc --- /dev/null +++ b/tests/test_update_comfyui_policy.py @@ -0,0 +1,70 @@ +"""The update route queues the ComfyUI update that `update_policy` names. + +Each case runs the real `update_comfyui` of `glob/manager_server.py` with the +real request models; `core` and the task queue are replaced, so nothing is +updated. +""" +import asyncio +import importlib.util +import sys +from types import SimpleNamespace + +import pytest +from aiohttp import web +from pydantic import ValidationError + +from legacy_html_testutil import PACKAGE, load_functions + +SERVER = PACKAGE / "glob/manager_server.py" + + +def module(path): + spec = importlib.util.spec_from_file_location("_" + path.stem, path) + loaded = importlib.util.module_from_spec(spec) + sys.modules[spec.name] = loaded # pydantic resolves the models' annotations through it + spec.loader.exec_module(loaded) + return loaded + + +def update(policy, **query): + """The status the route answers, and whether the update it queued is the stable one.""" + models = module(PACKAGE / "data_models/generated_models.py") + queued = [] + route = lambda path: lambda handler: handler # noqa: E731 + handler = load_functions(SERVER, {"update_comfyui"}, { + "web": web, "ValidationError": ValidationError, + "routes": SimpleNamespace(post=route), + "security_utils": module(PACKAGE / "common/manager_security.py"), + "core": SimpleNamespace(get_config=lambda: {"update_policy": policy}), + "UpdateComfyUIQueryParams": models.UpdateComfyUIQueryParams, + "UpdateComfyUIParams": models.UpdateComfyUIParams, + "QueueTaskItem": models.QueueTaskItem, "OperationType": models.OperationType, + "task_queue": SimpleNamespace(put=queued.append), + })["update_comfyui"] + request = SimpleNamespace(content_type="application/json", rel_url=SimpleNamespace(query=query)) + status = asyncio.run(handler(request)).status + return status, [task.params.is_stable for task in queued] + + +WHO = {"client_id": "c", "ui_id": "u"} + + +@pytest.mark.parametrize("policy, stable", [("stable-comfyui", True), ("stable", True), + ("nightly-comfyui", False), ("nightly", False)]) +def test_request_without_stable_follows_the_policy(policy, stable): + assert update(policy, **WHO) == (200, [stable]) + + +@pytest.mark.parametrize("policy", ["stable-comfyui", "nightly-comfyui"]) +@pytest.mark.parametrize("given, stable", [("true", True), ("false", False)]) +@pytest.mark.parametrize("name", ["stable", "is_stable"]) +def test_request_that_names_the_version_line_decides_itself(name, policy, given, stable): + assert update(policy, **{name: given}, **WHO) == (200, [stable]) + + +def test_stable_is_read_before_is_stable(): + assert update("stable-comfyui", stable="false", is_stable="true", **WHO) == (200, [False]) + + +def test_request_without_a_client_is_refused(): + assert update("stable-comfyui", ui_id="u") == (400, [])