#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------
# Copyright (c) 2026 Huawei Technologies Co., Ltd.
# This file is part of the MindStudio project.
#
# MindStudio is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#
#    http://license.coscl.org.cn/MulanPSL2
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# -------------------------------------------------------------------------

from __future__ import annotations

from msagent.agents.local_context import (
    build_local_environment_context,
    ensure_local_context_prompt,
)


def test_ensure_local_context_prompt_appends_placeholder_once() -> None:
    prompt = "You are a coding assistant."
    augmented = ensure_local_context_prompt(prompt)

    assert "{local_environment_context}" in augmented
    assert augmented.count("{local_environment_context}") == 1

    augmented_again = ensure_local_context_prompt(augmented)
    assert augmented_again == augmented


def test_build_local_environment_context_includes_project_signals(tmp_path) -> None:
    (tmp_path / "pyproject.toml").write_text("[project]\nname='demo'\n", encoding="utf-8")
    (tmp_path / "uv.lock").write_text("# lock", encoding="utf-8")
    (tmp_path / "README.md").write_text("# Demo\n", encoding="utf-8")
    (tmp_path / "src").mkdir()

    context = build_local_environment_context(tmp_path)

    assert "## Local Runtime Snapshot" in context
    assert "## Project Signals" in context
    assert "Python" in context
    assert "uv" in context
    assert "## Top-Level Workspace Layout" in context