Files
snotes/tests/test_actions.py

90 lines
2.8 KiB
Python

from __future__ import annotations
from backend import actions
def test_load_actions(tmp_path, monkeypatch):
f = tmp_path / "snotes.toml"
f.write_text(
'[[actions]]\nname = "task"\n'
'uri = "http://tasky.loc/new?title=#string1#"\n'
'link = "http://tasky.loc/?title=#string1#"\n'
'[[actions]]\nname = "other"\nuri = "http://x.loc/?q=#string1#"\n'
)
monkeypatch.setattr(actions.settings, "actions_file", str(f))
got = actions.load_actions(reload=True)
assert set(got) == {"task", "other"}
assert got["task"]["uri"] == "http://tasky.loc/new?title=#string1#"
assert got["task"]["link"] == "http://tasky.loc/?title=#string1#"
# "other" has no link -> link defaults to uri
assert got["other"]["link"] == "http://x.loc/?q=#string1#"
async def test_process_actions(monkeypatch):
monkeypatch.setattr(
actions,
"load_actions",
lambda: {
"task": {
"uri": "http://tasky.loc/new?title=#string1#",
"link": "http://tasky.loc/?title=#string1#",
}
},
)
called = []
async def fake_perform(uri):
called.append(uri)
return True
monkeypatch.setattr(actions, "_perform", fake_perform)
out = await actions.process_actions("before\n/task Go to the supermarket\nafter")
assert called == ["http://tasky.loc/new?title=Go%20to%20the%20supermarket"]
assert (
"[Go to the supermarket](http://tasky.loc/?title=Go%20to%20the%20supermarket)"
in out
)
async def test_process_actions_colon_separator(monkeypatch):
monkeypatch.setattr(
actions,
"load_actions",
lambda: {"task": {"uri": "http://x/?t=#string1#", "link": "http://x/?t=#string1#"}},
)
monkeypatch.setattr(actions, "_perform", lambda uri: _ok(uri))
out = await actions.process_actions("/task: shop milk")
assert "[shop milk](http://x/?t=shop%20milk)" in out
async def test_process_actions_failure_leaves_line(monkeypatch):
monkeypatch.setattr(
actions,
"load_actions",
lambda: {"task": {"uri": "http://x/?t=#string1#", "link": "http://x/?t=#string1#"}},
)
async def fail(uri):
return False
monkeypatch.setattr(actions, "_perform", fail)
out = await actions.process_actions("/task keep this line")
assert out == "/task keep this line"
async def test_process_actions_unknown_keyword(monkeypatch):
monkeypatch.setattr(
actions,
"load_actions",
lambda: {"task": {"uri": "http://x/?t=#string1#", "link": "http://x/?t=#string1#"}},
)
monkeypatch.setattr(actions, "_perform", lambda uri: _ok(uri))
out = await actions.process_actions("/nope do nothing")
assert out == "/nope do nothing"
async def _ok(uri):
return True