Skip to content

test(slm): add unit tests for _ensure_local_node startup self-registration (#3225)#3453

Closed
mrveiss wants to merge 1 commit intomainfrom
fix/issue-3225
Closed

test(slm): add unit tests for _ensure_local_node startup self-registration (#3225)#3453
mrveiss wants to merge 1 commit intomainfrom
fix/issue-3225

Conversation

@mrveiss
Copy link
Copy Markdown
Owner

@mrveiss mrveiss commented Apr 4, 2026

Summary

The _ensure_local_node() implementation (lifespan self-registration on startup) was already present on main but had no test coverage. This PR adds 7 unit tests covering the critical startup path:

  • Fresh registration — inserts Node + 4 NodeRole rows when DB is empty
  • Idempotency — no writes when node exists with current IP
  • Stale-IP healing — updates ip_address when SLM_EXTERNAL_URL has changed
  • Missing-env fallback — uses UDP probe or 127.0.0.1 when env var is absent
  • Sentinel ID — node_id and ansible_name are both "00-SLM-Manager"
  • NodeRole metadata — all rows have assignment_type="auto" and status="active"
  • Port-in-URL parsing — IP extraction correctly strips port from https://host:8443

Closes #3225

Test plan

  • pytest autobot-slm-backend/tests/test_ensure_local_node.py -v — 7/7 pass
  • Fresh install: SLM manager appears in node inventory on first backend startup
  • Reinstall with changed IP: node IP is updated on next backend restart

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 4, 2026

AutoBot Phase Validation Results

System Maturity: 0.0%

Phase Status:

Recommendations:

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 4, 2026

✅ SSOT Configuration Compliance: Passing

🎉 No hardcoded values detected that have SSOT config equivalents!

@mrveiss
Copy link
Copy Markdown
Owner Author

mrveiss commented Apr 4, 2026

Code review

Found 1 issue:

  1. models.database is not in the stub dict but _ensure_local_node() imports it inside the function body at call time — after the patch.dict(sys.modules, stubs) context has already exited. Every test will raise ModuleNotFoundError in a CI environment where the real module is not installed.

https://github.com/mrveiss/AutoBot-AI/blob/94b8e5472cee67cf46bab12b77b527e9c8765c11/autobot-slm-backend/tests/test_ensure_local_node.py#L74-L95

Fix: wrap each await self._fn() call in a patch.dict(sys.modules, {"models.database": MagicMock(), "sqlalchemy": MagicMock(), ...}) context, or move the models.database import to module level in main.py so the module-load-time stub intercepts it.

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

@mrveiss
Copy link
Copy Markdown
Owner Author

mrveiss commented Apr 5, 2026

Superseded by PR #3485, which corrects the sys.modules stub and co-locates the test at autobot-slm-backend/main_ensure_local_node_test.py per the project's co-location rule.

@mrveiss mrveiss closed this Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(slm): SLM manager node never self-registers in DB on fresh install

1 participant