using ClaudeDo.Worker.Config;
using Xunit;

namespace ClaudeDo.Worker.Tests.Config;

/// SaveClaudeBin/SaveOnlineInbox are read-modify-write on a file the user also hand-edits, so the
/// one thing that must hold is: every other key survives.
public sealed class WorkerConfigSaveTests : IDisposable
{
    private readonly string _path = Path.Combine(
        Path.GetTempPath(), $"cd_workercfg_{Guid.NewGuid():N}.json");

    public void Dispose() { try { File.Delete(_path); } catch { } }

    [Fact]
    public void SaveClaudeBin_keeps_every_other_key()
    {
        File.WriteAllText(_path, """
            {
              "claude_bin": "claude",
              "signalr_port": 12345,
              "external_mcp_api_key": "secret",
              "online_inbox": { "enabled": true }
            }
            """);

        var cfg = WorkerConfig.Load(_path);
        cfg.ClaudeBin = @"C:\tools\claude.cmd";
        cfg.SaveClaudeBin(_path);

        var reloaded = WorkerConfig.Load(_path);
        Assert.Equal(@"C:\tools\claude.cmd", reloaded.ClaudeBin);
        Assert.Equal(12345, reloaded.SignalRPort);
        Assert.Equal("secret", reloaded.ExternalMcpApiKey);
        Assert.True(reloaded.OnlineInbox.Enabled);
    }

    [Fact]
    public void SaveClaudeBin_creates_the_file_when_missing()
    {
        var cfg = new WorkerConfig { ClaudeBin = "claude-next" };
        cfg.SaveClaudeBin(_path);

        Assert.Equal("claude-next", WorkerConfig.Load(_path).ClaudeBin);
    }
}
