Creator App Tests / PR Spam Gate (push) Skipped
Frontend Tests / PR Spam Gate (push) Skipped
NPM Format (website & console) / PR Spam Gate (push) Skipped
Pre-commit Checks / PR Spam Gate (push) Skipped
QwenPaw Creator Plugin Release / check-version (push) Canceled after 0s
QwenPaw Creator Plugin Release / upload-oss (push) Canceled after 0s
Tests / PR Spam Gate (push) Skipped
CodeQL / CodeQL (python) (push) Canceled after 0s
CodeQL / CodeQL (typescript) (push) Canceled after 0s
Creator App Tests / Creator Backend Tests (push) Canceled after 0s
Creator App Tests / Creator UI Build & Tests (push) Canceled after 0s
E2E Smoke Tests / Playwright UI Smoke (push) Canceled after 0s
Frontend Tests / Detect console changes (push) Canceled after 0s
NPM Format (website & console) / website format check (push) Canceled after 0s
NPM Format (website & console) / console format check (push) Canceled after 0s
Pre-commit Checks / Pre-commit (push) Canceled after 0s
QwenPaw Data Plugin Release / check-version (push) Canceled after 0s
Tests / Detect code changes (push) Canceled after 0s
Frontend Tests / Vitest Unit Tests (push) Canceled after 0s
Frontend Tests / Frontend Summary (push) Canceled after 0s
QwenPaw Data Plugin Release / upload-oss (push) Canceled after 0s
Tests / Maintainer Approval (push) Canceled after 0s
Tests / Unit Tests - py3.11 - macos-latest (push) Canceled after 0s
Tests / Unit Tests - py3.11 - ubuntu-latest (push) Canceled after 0s
Tests / Unit Tests - py3.13 - ubuntu-latest (push) Canceled after 0s
Tests / Unit Tests - py3.11 - windows-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - macos-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - ubuntu-latest (push) Canceled after 0s
Tests / Contract Tests - py3.13 - ubuntu-latest (push) Canceled after 0s
Tests / Contract Tests - py3.11 - windows-latest (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - macos-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - fallback (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - ubuntu-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.13 - ubuntu-latest - p2 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p0 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p1 (push) Canceled after 0s
Tests / Integrated Tests - py3.11 - windows-latest - p2 (push) Canceled after 0s
Tests / Coverage Report (push) Canceled after 0s
Tests / Test Summary (push) Canceled after 0s
142 lines
3.6 KiB
Python
142 lines
3.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
# pylint: disable=redefined-outer-name,protected-access
|
|
"""Tests for IterationGate reset behaviour."""
|
|
from __future__ import annotations
|
|
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from qwenpaw.loop.gates.base import StopAction
|
|
from qwenpaw.loop.gates.iteration import IterationGate
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _force_session_id():
|
|
with patch(
|
|
"qwenpaw.loop.gates.loop_gate._session_id",
|
|
return_value="test-session",
|
|
):
|
|
yield
|
|
|
|
|
|
@pytest.fixture()
|
|
def gate():
|
|
g = IterationGate(max_iterations=5)
|
|
g.activate()
|
|
return g
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_check_increments(gate):
|
|
"""check() increments the counter each call."""
|
|
for _ in range(4):
|
|
result = await gate.check({})
|
|
assert result.action == StopAction.BYPASS
|
|
result = await gate.check({})
|
|
assert result.action == StopAction.TERMINATE
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reset_clears_counter(gate):
|
|
"""reset_turn() sets iteration back to 0."""
|
|
for _ in range(3):
|
|
await gate.check({})
|
|
gate.reset_turn()
|
|
state = gate._state()
|
|
assert state is not None
|
|
assert state.iteration == 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reset_preserves_max(gate):
|
|
"""reset_turn() keeps max_iterations unchanged."""
|
|
gate.reset_turn()
|
|
state = gate._state()
|
|
assert state.max_iterations == 5
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reset_allows_full_budget(gate):
|
|
"""After reset the full iteration budget is available."""
|
|
for _ in range(3):
|
|
await gate.check({})
|
|
gate.reset_turn()
|
|
for _ in range(4):
|
|
result = await gate.check({})
|
|
assert result.action == StopAction.BYPASS
|
|
result = await gate.check({})
|
|
assert result.action == StopAction.TERMINATE
|
|
|
|
|
|
def test_reset_when_inactive():
|
|
"""reset_turn() activates a fresh session state."""
|
|
g = IterationGate(max_iterations=5)
|
|
g.reset_turn()
|
|
assert g._state() is not None
|
|
|
|
|
|
@pytest.fixture()
|
|
def gate_pair():
|
|
"""Instance with state in two sessions."""
|
|
g = IterationGate(max_iterations=5)
|
|
with patch(
|
|
"qwenpaw.loop.gates.loop_gate._session_id",
|
|
return_value="session-a",
|
|
):
|
|
g.activate()
|
|
with patch(
|
|
"qwenpaw.loop.gates.loop_gate._session_id",
|
|
return_value="session-b",
|
|
):
|
|
g.activate()
|
|
return g
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_reset_session_isolation(gate_pair):
|
|
"""reset_turn() only affects the current session."""
|
|
g = gate_pair
|
|
with patch(
|
|
"qwenpaw.loop.gates.loop_gate._session_id",
|
|
return_value="session-a",
|
|
):
|
|
for _ in range(3):
|
|
await g.check({})
|
|
|
|
with patch(
|
|
"qwenpaw.loop.gates.loop_gate._session_id",
|
|
return_value="session-b",
|
|
):
|
|
for _ in range(2):
|
|
await g.check({})
|
|
|
|
with patch(
|
|
"qwenpaw.loop.gates.loop_gate._session_id",
|
|
return_value="session-a",
|
|
):
|
|
g.reset_turn()
|
|
assert g._state().iteration == 0
|
|
|
|
with patch(
|
|
"qwenpaw.loop.gates.loop_gate._session_id",
|
|
return_value="session-b",
|
|
):
|
|
assert g._state().iteration == 2
|
|
|
|
|
|
def test_reset_session_removes_only_current_session(gate_pair):
|
|
"""reset_session() removes only the current conversation state."""
|
|
with patch(
|
|
"qwenpaw.loop.gates.loop_gate._session_id",
|
|
return_value="session-a",
|
|
):
|
|
gate_pair.reset_session()
|
|
assert gate_pair._state() is None
|
|
|
|
with patch(
|
|
"qwenpaw.loop.gates.loop_gate._session_id",
|
|
return_value="session-b",
|
|
):
|
|
assert gate_pair._state() is not None
|