|
| 1 | +# Copyright 2025-2026 Dimensional Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Guard against import-time regressions in the CLI entrypoint. |
| 16 | +
|
| 17 | +`dimos --help` should never pull in heavy ML/viz libraries. If it does, |
| 18 | +startup time balloons from <2s to >5s, which is a terrible UX. |
| 19 | +""" |
| 20 | + |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +import time |
| 24 | + |
| 25 | +# CI runners are slower — give generous headroom but still catch gross regressions. |
| 26 | +HELP_TIMEOUT_SECONDS = 8 |
| 27 | + |
| 28 | + |
| 29 | +def test_help_does_not_import_heavy_deps() -> None: |
| 30 | + """GlobalConfig import must not drag in matplotlib, torch, or scipy.""" |
| 31 | + result = subprocess.run( |
| 32 | + [ |
| 33 | + sys.executable, |
| 34 | + "-c", |
| 35 | + ( |
| 36 | + "import sys; " |
| 37 | + "from dimos.core.global_config import GlobalConfig; " |
| 38 | + "bad = [m for m in ('matplotlib', 'torch', 'scipy') if m in sys.modules]; " |
| 39 | + "assert not bad, f'Heavy deps imported: {bad}'" |
| 40 | + ), |
| 41 | + ], |
| 42 | + capture_output=True, |
| 43 | + text=True, |
| 44 | + timeout=30, |
| 45 | + ) |
| 46 | + assert result.returncode == 0, f"Heavy deps leaked into GlobalConfig import:\n{result.stderr}" |
| 47 | + |
| 48 | + |
| 49 | +def test_help_startup_time() -> None: |
| 50 | + """`dimos --help` must finish in under {HELP_TIMEOUT_SECONDS}s.""" |
| 51 | + start = time.monotonic() |
| 52 | + result = subprocess.run( |
| 53 | + [sys.executable, "-m", "dimos.robot.cli.dimos", "--help"], |
| 54 | + capture_output=True, |
| 55 | + text=True, |
| 56 | + timeout=HELP_TIMEOUT_SECONDS + 5, # hard kill safety margin |
| 57 | + ) |
| 58 | + elapsed = time.monotonic() - start |
| 59 | + assert result.returncode == 0, f"dimos --help failed:\n{result.stderr}" |
| 60 | + assert elapsed < HELP_TIMEOUT_SECONDS, ( |
| 61 | + f"dimos --help took {elapsed:.1f}s (limit: {HELP_TIMEOUT_SECONDS}s). " |
| 62 | + f"Check for heavy imports in the CLI entrypoint or GlobalConfig." |
| 63 | + ) |
0 commit comments