Add support for cancelling in-progress LLM completion requests via new /v1/completions/cancel endpoint with task tracking. Implement mermaid diagram rendering in the Milkdown editor with a new mermaidPlugin. Update copilotPlugin to properly abort requests with descriptive reasons. Refactor settings panel to handle system theme changes reactively. Add camera capture support for image uploads.
92 lines
3.5 KiB
Python
92 lines
3.5 KiB
Python
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
BACKEND_DIR = Path(__file__).resolve().parents[1]
|
|
if str(BACKEND_DIR) not in sys.path:
|
|
sys.path.insert(0, str(BACKEND_DIR))
|
|
|
|
import prompt # noqa: E402
|
|
|
|
|
|
def test_prompt_builds_system_and_user():
|
|
system_prompt, user_prompt = prompt.build_completion_prompts(
|
|
prefix="The result is ",
|
|
suffix="for this dataset.",
|
|
language_id="markdown",
|
|
)
|
|
|
|
assert "Hard constraints you must follow" in system_prompt
|
|
assert "strict KaTeX-compatible math" in system_prompt
|
|
assert "$...$" in system_prompt
|
|
assert "$$...$$" in system_prompt
|
|
assert "```{language}" in system_prompt
|
|
assert "Mermaid-specific completion rules" in system_prompt
|
|
assert "CURSOR_FENCE_LANGUAGE" in system_prompt
|
|
assert "MERMAID_CONTEXT" in system_prompt
|
|
assert "Output Mermaid statements only." in system_prompt
|
|
assert "CURSOR_IN_FENCED_CODE_BLOCK" in user_prompt
|
|
assert "CURSOR_FENCE_LANGUAGE" in user_prompt
|
|
assert "MERMAID_CONTEXT" in user_prompt
|
|
assert "PREFIX_ENDS_WITH_NEWLINE" in user_prompt
|
|
assert "SUFFIX_STARTS_WITH_NEWLINE" in user_prompt
|
|
|
|
|
|
def test_cursor_in_fence_detection():
|
|
assert prompt._cursor_in_fenced_code_block("") is False
|
|
assert prompt._cursor_in_fenced_code_block("```python\nprint('x')\n") is True
|
|
assert prompt._cursor_in_fenced_code_block("```python\nprint('x')\n```\n") is False
|
|
assert prompt._cursor_in_fenced_code_block("text ```not-a-fence``` tail") is False
|
|
|
|
|
|
def test_active_fence_language_detection():
|
|
assert prompt._active_fence_language("") == "none"
|
|
assert prompt._active_fence_language("```mermaid\nflowchart TD\nA-->B\n") == "mermaid"
|
|
assert prompt._active_fence_language("```python\nprint('x')\n") == "python"
|
|
assert prompt._active_fence_language("```\nline\n") == "unknown"
|
|
assert prompt._active_fence_language("```mermaid\nA-->B\n```\n") == "none"
|
|
|
|
|
|
def test_newline_flags():
|
|
_, user_prompt_a = prompt.build_completion_prompts(
|
|
prefix="Hello",
|
|
suffix="World",
|
|
)
|
|
assert "CURSOR_IN_FENCED_CODE_BLOCK: false" in user_prompt_a
|
|
assert "CURSOR_FENCE_LANGUAGE: none" in user_prompt_a
|
|
assert "MERMAID_CONTEXT: false" in user_prompt_a
|
|
assert "PREFIX_ENDS_WITH_NEWLINE: false" in user_prompt_a
|
|
assert "SUFFIX_STARTS_WITH_NEWLINE: false" in user_prompt_a
|
|
|
|
_, user_prompt_b = prompt.build_completion_prompts(
|
|
prefix="Hello\n",
|
|
suffix="\nWorld",
|
|
)
|
|
assert "CURSOR_FENCE_LANGUAGE: none" in user_prompt_b
|
|
assert "PREFIX_ENDS_WITH_NEWLINE: true" in user_prompt_b
|
|
assert "SUFFIX_STARTS_WITH_NEWLINE: true" in user_prompt_b
|
|
|
|
|
|
def test_mermaid_context_flags():
|
|
_, prompt_in_mermaid = prompt.build_completion_prompts(
|
|
prefix="```mermaid\nflowchart TD\nA --> ",
|
|
suffix="\n```",
|
|
)
|
|
assert "CURSOR_IN_FENCED_CODE_BLOCK: true" in prompt_in_mermaid
|
|
assert "CURSOR_FENCE_LANGUAGE: mermaid" in prompt_in_mermaid
|
|
assert "MERMAID_CONTEXT: true" in prompt_in_mermaid
|
|
|
|
_, prompt_mermaid_keyword = prompt.build_completion_prompts(
|
|
prefix="Please draw a mermaid flowchart for deploy pipeline.",
|
|
suffix="",
|
|
)
|
|
assert "CURSOR_IN_FENCED_CODE_BLOCK: false" in prompt_mermaid_keyword
|
|
assert "CURSOR_FENCE_LANGUAGE: none" in prompt_mermaid_keyword
|
|
assert "MERMAID_CONTEXT: true" in prompt_mermaid_keyword
|
|
|
|
|
|
def test_examples_coverage():
|
|
_, user_prompt = prompt.build_completion_prompts(prefix="", suffix="")
|
|
for ex in range(1, 15):
|
|
assert f"[EX{ex:02d}]" in user_prompt
|