from deerflow.agents.thread_state import (
    ARTIFACTS_REPLACE_SENTINEL,
    merge_artifacts,
)


def test_merge_artifacts_default_merge_dedup():
    existing = ["/mnt/user-data/outputs/a.md", "/mnt/user-data/outputs/b.md"]
    new = ["/mnt/user-data/outputs/b.md", "/mnt/user-data/outputs/c.md"]

    result = merge_artifacts(existing, new)

    assert result == [
        "/mnt/user-data/outputs/a.md",
        "/mnt/user-data/outputs/b.md",
        "/mnt/user-data/outputs/c.md",
    ]


def test_merge_artifacts_supports_replace_sentinel():
    existing = ["/mnt/user-data/outputs/a.md", "/mnt/user-data/outputs/b.md"]
    new = [
        ARTIFACTS_REPLACE_SENTINEL,
        "/mnt/user-data/outputs/b.md",
        "/mnt/user-data/outputs/c.md",
        "/mnt/user-data/outputs/c.md",
    ]

    result = merge_artifacts(existing, new)

    assert result == [
        "/mnt/user-data/outputs/b.md",
        "/mnt/user-data/outputs/c.md",
    ]


def test_merge_artifacts_always_strips_sentinel_from_existing():
    existing = [
        "/mnt/user-data/outputs/a.md",
        ARTIFACTS_REPLACE_SENTINEL,
        "/mnt/user-data/outputs/b.md",
    ]

    result = merge_artifacts(existing, None)

    assert result == [
        "/mnt/user-data/outputs/a.md",
        "/mnt/user-data/outputs/b.md",
    ]


def test_merge_artifacts_strips_sentinel_from_non_replace_payload():
    existing = ["/mnt/user-data/outputs/a.md"]
    new = [
        "/mnt/user-data/outputs/b.md",
        ARTIFACTS_REPLACE_SENTINEL,
        "/mnt/user-data/outputs/c.md",
    ]

    result = merge_artifacts(existing, new)

    assert result == [
        "/mnt/user-data/outputs/a.md",
        "/mnt/user-data/outputs/b.md",
        "/mnt/user-data/outputs/c.md",
    ]
