[feat] change str id type to UUID
This commit is contained in:
parent
e629e34c4d
commit
770af1fe76
11 changed files with 150 additions and 173 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from datetime import UTC, datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Any, TypedDict
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
from docker import DockerClient
|
||||
|
|
@ -12,8 +13,9 @@ from adapter.docker.runtime import DockerSandboxRuntime
|
|||
from domain.error import SandboxError, SandboxStartError
|
||||
from domain.sandbox import SandboxStatus
|
||||
|
||||
CHAT_ID = '123e4567-e89b-12d3-a456-426614174000'
|
||||
CHAT_ID = UUID('123e4567-e89b-12d3-a456-426614174000')
|
||||
NON_CANONICAL_CHAT_ID = '123E4567E89B12D3A456426614174000'
|
||||
SESSION_ID = UUID('00000000-0000-0000-0000-000000000010')
|
||||
|
||||
|
||||
class FakeContainer:
|
||||
|
|
@ -107,26 +109,26 @@ def test_runtime_create_applies_mount_policy_and_labels_with_canonical_chat_id(
|
|||
expires_at = created_at + timedelta(minutes=5)
|
||||
|
||||
session = runtime.create(
|
||||
session_id='session-123',
|
||||
chat_id=NON_CANONICAL_CHAT_ID,
|
||||
session_id=SESSION_ID,
|
||||
chat_id=UUID(NON_CANONICAL_CHAT_ID),
|
||||
created_at=created_at,
|
||||
expires_at=expires_at,
|
||||
)
|
||||
|
||||
assert session.session_id == 'session-123'
|
||||
assert session.session_id == SESSION_ID
|
||||
assert session.chat_id == CHAT_ID
|
||||
assert session.container_id == 'container-123'
|
||||
assert session.status is SandboxStatus.RUNNING
|
||||
assert session.created_at == created_at
|
||||
assert session.expires_at == expires_at
|
||||
assert (tmp_path / 'chats' / CHAT_ID).is_dir()
|
||||
assert (tmp_path / 'chats' / str(CHAT_ID)).is_dir()
|
||||
|
||||
call = containers.run_calls[0]
|
||||
assert call['args'] == ('sandbox:latest',)
|
||||
assert call['kwargs']['detach'] is True
|
||||
assert call['kwargs']['labels'] == {
|
||||
'session_id': 'session-123',
|
||||
'chat_id': CHAT_ID,
|
||||
'session_id': str(SESSION_ID),
|
||||
'chat_id': str(CHAT_ID),
|
||||
'expires_at': expires_at.isoformat(),
|
||||
}
|
||||
|
||||
|
|
@ -134,7 +136,7 @@ def test_runtime_create_applies_mount_policy_and_labels_with_canonical_chat_id(
|
|||
assert [dict(mount) for mount in mounts] == [
|
||||
{
|
||||
'Target': '/workspace/chat',
|
||||
'Source': str((tmp_path / 'chats' / CHAT_ID).resolve(strict=False)),
|
||||
'Source': str((tmp_path / 'chats' / str(CHAT_ID)).resolve(strict=False)),
|
||||
'Type': 'bind',
|
||||
'ReadOnly': False,
|
||||
},
|
||||
|
|
@ -164,14 +166,14 @@ def test_runtime_create_raises_start_error_when_container_id_is_missing(
|
|||
|
||||
with pytest.raises(SandboxStartError) as excinfo:
|
||||
runtime.create(
|
||||
session_id='session-123',
|
||||
session_id=SESSION_ID,
|
||||
chat_id=CHAT_ID,
|
||||
created_at=datetime(2026, 4, 2, 12, 0, tzinfo=UTC),
|
||||
expires_at=datetime(2026, 4, 2, 12, 5, tzinfo=UTC),
|
||||
)
|
||||
|
||||
assert str(excinfo.value) == 'sandbox_start_failed'
|
||||
assert excinfo.value.chat_id == CHAT_ID
|
||||
assert excinfo.value.chat_id == str(CHAT_ID)
|
||||
|
||||
|
||||
def test_runtime_stop_ignores_missing_container(tmp_path: Path) -> None:
|
||||
|
|
@ -195,24 +197,3 @@ def test_runtime_stop_wraps_docker_errors(tmp_path: Path) -> None:
|
|||
runtime.stop('container-123')
|
||||
|
||||
assert str(excinfo.value) == 'sandbox_stop_failed'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('chat_id', ['.', 'a/..', 'x/../y'])
|
||||
def test_runtime_create_rejects_non_uuid_chat_id(tmp_path: Path, chat_id: str) -> None:
|
||||
config = build_config(tmp_path)
|
||||
(tmp_path / 'dependencies').mkdir()
|
||||
(tmp_path / 'lambda-tools').mkdir()
|
||||
containers = FakeContainers()
|
||||
runtime = DockerSandboxRuntime(config, FakeDockerClient(containers))
|
||||
|
||||
with pytest.raises(SandboxStartError) as excinfo:
|
||||
runtime.create(
|
||||
session_id='session-123',
|
||||
chat_id=chat_id,
|
||||
created_at=datetime(2026, 4, 2, 12, 0, tzinfo=UTC),
|
||||
expires_at=datetime(2026, 4, 2, 12, 5, tzinfo=UTC),
|
||||
)
|
||||
|
||||
assert str(excinfo.value) == 'sandbox_start_failed'
|
||||
assert excinfo.value.chat_id == chat_id
|
||||
assert containers.run_calls == []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue