mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 15:49:19 +08:00
fix: 草稿首次落库前先改绑发布助手会话,避免路由重挂新建空会话
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
"""未落库会话 scene_ref 改绑规则。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from application.session_service import SessionService
|
||||
from domain.models import Actor, Session, SessionStatus
|
||||
|
||||
|
||||
class _MemSessions:
|
||||
def __init__(self) -> None:
|
||||
self.rows: dict[str, Session] = {}
|
||||
|
||||
async def create(self, session: Session) -> Session:
|
||||
self.rows[session.thread_id] = session
|
||||
return session
|
||||
|
||||
async def get(self, company_id: int, user_id: str, thread_id: str) -> Session | None:
|
||||
s = self.rows.get(thread_id)
|
||||
if s is None:
|
||||
return None
|
||||
if s.company_id != company_id or s.user_id != user_id:
|
||||
return None
|
||||
return s
|
||||
|
||||
async def list_active(self, *args: Any, **kwargs: Any) -> list[Session]:
|
||||
return []
|
||||
|
||||
async def find_active_by_scene_ref(
|
||||
self,
|
||||
company_id: int,
|
||||
user_id: str,
|
||||
*,
|
||||
scene: str,
|
||||
scene_ref: str,
|
||||
) -> Session | None:
|
||||
for s in self.rows.values():
|
||||
if (
|
||||
s.company_id == company_id
|
||||
and s.user_id == user_id
|
||||
and s.status == SessionStatus.ACTIVE
|
||||
and (s.scene or "general") == scene
|
||||
and s.scene_ref == scene_ref
|
||||
):
|
||||
return s
|
||||
return None
|
||||
|
||||
async def update(self, session: Session) -> Session:
|
||||
self.rows[session.thread_id] = session
|
||||
return session
|
||||
|
||||
|
||||
def _actor() -> Actor:
|
||||
return Actor(company_id=1, user_id="u1")
|
||||
|
||||
|
||||
def _session(*, thread_id: str, scene_ref: str | None) -> Session:
|
||||
now = datetime.utcnow()
|
||||
return Session(
|
||||
thread_id=thread_id,
|
||||
company_id=1,
|
||||
user_id="u1",
|
||||
title="发布草稿(未保存)",
|
||||
status=SessionStatus.ACTIVE,
|
||||
scene="ecom_goods_publish",
|
||||
scene_ref=scene_ref,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
|
||||
def test_rebind_local_to_draft_id():
|
||||
repo = _MemSessions()
|
||||
svc = SessionService(repo)
|
||||
|
||||
async def scenario():
|
||||
local = _session(thread_id="t1", scene_ref="local-abc")
|
||||
await repo.create(local)
|
||||
out = await svc.rebind_scene_ref(
|
||||
_actor(), "t1", scene_ref="42", title="发布草稿 #42"
|
||||
)
|
||||
assert out.scene_ref == "42"
|
||||
assert out.title == "发布草稿 #42"
|
||||
|
||||
asyncio.run(scenario())
|
||||
|
||||
|
||||
def test_rebind_rejects_non_local():
|
||||
repo = _MemSessions()
|
||||
svc = SessionService(repo)
|
||||
|
||||
async def scenario():
|
||||
await repo.create(_session(thread_id="t1", scene_ref="7"))
|
||||
try:
|
||||
await svc.rebind_scene_ref(_actor(), "t1", scene_ref="8")
|
||||
raise AssertionError("expected ValueError")
|
||||
except ValueError as e:
|
||||
assert "未落库" in str(e)
|
||||
|
||||
asyncio.run(scenario())
|
||||
|
||||
|
||||
def test_rebind_rejects_occupied_draft():
|
||||
repo = _MemSessions()
|
||||
svc = SessionService(repo)
|
||||
|
||||
async def scenario():
|
||||
await repo.create(_session(thread_id="t-old", scene_ref="9"))
|
||||
await repo.create(_session(thread_id="t-local", scene_ref="local-x"))
|
||||
try:
|
||||
await svc.rebind_scene_ref(_actor(), "t-local", scene_ref="9")
|
||||
raise AssertionError("expected ValueError")
|
||||
except ValueError as e:
|
||||
assert "已有关联会话" in str(e)
|
||||
|
||||
asyncio.run(scenario())
|
||||
Reference in New Issue
Block a user