Project_Journal-Csharp_back.../tests/test_cli_fragments_hybrid.py
2026-02-23 20:12:10 -06:00

95 lines
3.1 KiB
Python

import io
import sys
import unittest
from contextlib import redirect_stdout
from pathlib import Path
from unittest.mock import patch
PROJECT_ROOT = Path(__file__).resolve().parents[1]
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
from journal.cli import main as cli_main
class CliFragmentsHybridTests(unittest.TestCase):
def test_fragments_list_calls_sidecar(self):
with (
patch.object(sys, "argv", ["journal", "fragments", "list"]),
patch("journal.cli.main.BACKEND_MODE", "csharp-hybrid"),
patch(
"journal.cli.main.call_sidecar_action",
return_value=[{"Id": "1", "Type": "!NOTE", "Description": "desc", "Tags": []}],
) as mock_call,
redirect_stdout(io.StringIO()) as stdout,
):
cli_main.main()
mock_call.assert_called_once_with("fragments.list")
self.assertIn("!NOTE", stdout.getvalue())
def test_fragments_create_calls_sidecar(self):
with (
patch.object(
sys,
"argv",
[
"journal",
"fragments",
"create",
"--type",
"!TRIGGER",
"--description",
"flashback",
"--tag",
"stress",
],
),
patch("journal.cli.main.BACKEND_MODE", "csharp-hybrid"),
patch("journal.cli.main.call_sidecar_action", return_value={"Id": "1"}) as mock_call,
redirect_stdout(io.StringIO()) as stdout,
):
cli_main.main()
mock_call.assert_called_once_with(
"fragments.create",
payload={"type": "!TRIGGER", "description": "flashback", "tags": ["stress"]},
)
self.assertIn("Fragment created.", stdout.getvalue())
def test_fragments_search_calls_sidecar_with_command_fields(self):
with (
patch.object(
sys,
"argv",
["journal", "fragments", "search", "--type", "!NOTE", "--tag", "daily"],
),
patch("journal.cli.main.BACKEND_MODE", "csharp-hybrid"),
patch("journal.cli.main.call_sidecar_action", return_value=[]) as mock_call,
redirect_stdout(io.StringIO()) as stdout,
):
cli_main.main()
mock_call.assert_called_once_with(
"fragments.search",
command_fields={"type": "!NOTE", "tag": "daily"},
)
self.assertIn("No fragments found.", stdout.getvalue())
def test_fragments_command_requires_hybrid(self):
with (
patch.object(sys, "argv", ["journal", "fragments", "list"]),
patch("journal.cli.main.BACKEND_MODE", "python"),
patch("journal.cli.main.call_sidecar_action") as mock_call,
redirect_stdout(io.StringIO()) as stdout,
):
cli_main.main()
mock_call.assert_not_called()
self.assertIn("requires JOURNAL_BACKEND_MODE=csharp-hybrid", stdout.getvalue())
if __name__ == "__main__":
unittest.main()