Files
LangBot/tests/unit_tests/persistence/test_dedicated_space_provider.py
T
RockChinQ 7fa9c0e426 fix: 修复独立环境登录 404 与「添加第一个模型」500(4.11 回归) (#2755)
* fix(space): keep dedicated-environment Space login working

A dedicated environment runs the OSS runtime with system.edition=cloud. The
process-level 'LangBot Models' compatibility provider is part of the OSS
single-Workspace contract, but write_space_model_providers() skipped creating it
for every non-community edition. The Space OAuth callback then unconditionally
rewarded the provider row by api key, so provider_service raised
WorkspaceNotFoundError('Provider not found') and the whole callback returned 404
-- after the Account and its Workspace had already been created. Every retry
failed the same way, making dedicated-environment login impossible on 4.11.x.

Gate the compatibility provider on the deployment shape instead of the edition
(genuine multi-tenant Cloud runs in CLOUD_RUNTIME persistence and never reaches
this path), and treat a missing provider row as a model-routing warning rather
than an authentication failure so a data anomaly can never block login.

* fix(agent): bind the first model to the 4.11 runner config

create_llm_model() still wrote the auto-selected model into the pre-4.11
ai['local-agent'] container. On a 4.11 Workspace that key does not exist, so the
assignment raised KeyError and POST /provider/models/llm returned 500 the first
time a user added a model -- and the default pipeline kept primary='', so every
Agent run failed with 'No authorized model for local-agent'.

Delegate to RunnerDefaultConfigService, which resolves the selected runner and
writes the schema-defined model selector into ai.runner_config[<runner id>].

Also default the NOT NULL description column on pipeline and bot creation:
omitting an optional description returned a 500 from the constraint instead of
creating the resource.

* fix(agent): keep seeding the model for pipelines that never migrated

set_pipeline_llm_model_if_empty() returns False when a pipeline has no 4.11
runner selected -- that is exactly the un-migrated shape, which still carries
ai.local-agent. Seed that container, but only when it exists, so the legacy path
keeps its old behaviour without the KeyError.
2026-10-22 23:26:30 +08:00

76 lines
3.0 KiB
Python

"""Dedicated-environment (edition=cloud on OSS persistence) Space provider coverage.
A dedicated environment runs the OSS runtime with ``system.edition = cloud``. The
process-level ``LangBot Models`` compatibility provider is part of the OSS
single-Workspace contract, so gating its creation on ``edition == 'community'``
left dedicated Cores without the row and broke Space OAuth login with
"Provider not found".
"""
from __future__ import annotations
from importlib import import_module
from types import SimpleNamespace
from unittest.mock import AsyncMock, Mock
import pytest
def get_persistence_module():
return import_module('langbot.pkg.persistence.mgr')
def _manager(edition: str, *, multi_workspace_enabled: bool, workspace_uuids: list[str]):
persistence = get_persistence_module()
app = Mock()
app.persistence_mgr = None
app.instance_config = SimpleNamespace(data={'space': {'models_gateway_api_url': 'https://models.example.test/v1'}})
app.deployment = SimpleNamespace(multi_workspace_enabled=multi_workspace_enabled)
app.logger = Mock()
mgr = persistence.PersistenceManager(app)
mgr.execute_async = AsyncMock()
workspace_result = Mock()
workspace_result.scalars.return_value.all.return_value = workspace_uuids
provider_result = Mock()
provider_result.first.return_value = None
# Third call is the INSERT of the compatibility provider.
mgr.execute_async.side_effect = [workspace_result, provider_result, Mock()]
return mgr
class TestWriteSpaceModelProviders:
@pytest.mark.asyncio
async def test_dedicated_cloud_edition_creates_compatibility_provider(self, monkeypatch):
"""edition=cloud on a single Workspace must still own the provider row."""
constants = import_module('langbot.pkg.utils.constants')
monkeypatch.setattr(constants, 'edition', 'cloud')
mgr = _manager('cloud', multi_workspace_enabled=False, workspace_uuids=['workspace-a'])
await mgr.write_space_model_providers()
# One select for the Workspace list, one for the existing provider, one insert.
assert mgr.execute_async.await_count == 3
@pytest.mark.asyncio
async def test_multi_workspace_cloud_never_projects_provider(self, monkeypatch):
"""Real multi-tenant Cloud keeps the provider out of its Workspaces."""
constants = import_module('langbot.pkg.utils.constants')
monkeypatch.setattr(constants, 'edition', 'cloud')
mgr = _manager('cloud', multi_workspace_enabled=True, workspace_uuids=['workspace-a'])
await mgr.write_space_model_providers()
mgr.execute_async.assert_not_awaited()
@pytest.mark.asyncio
async def test_community_edition_still_creates_provider(self, monkeypatch):
constants = import_module('langbot.pkg.utils.constants')
monkeypatch.setattr(constants, 'edition', 'community')
mgr = _manager('community', multi_workspace_enabled=False, workspace_uuids=['workspace-a'])
await mgr.write_space_model_providers()
assert mgr.execute_async.await_count == 3