Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

32.2 KB
Raw Blame
// Copyright 2026 The OpenSandbox Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Net;
using System.Text;
using System.Text.Json;
using FluentAssertions;
using OpenSandbox.Adapters;
using OpenSandbox.Core;
using OpenSandbox.Internal;
using OpenSandbox.Models;
using OpenSandbox.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;

namespace OpenSandbox.Tests;

public class CommandsAdapterTests
{
    [Theory]
    [InlineData(false, false)]
    [InlineData(true, false)]
    [InlineData(false, true)]
    public async Task RunStreamAsync_ShouldPreserveOptionalSuccessExitCode(bool explicitExitCode, bool background)
    {
        var eventJson = explicitExitCode
            ? "{\"type\":\"execution_complete\",\"exit_code\":0}"
            : "{\"type\":\"execution_complete\"}";
        var handler = new StubHttpMessageHandler((_, _) =>
            Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent($"data: {eventJson}\n\n", Encoding.UTF8, "text/event-stream")
            }));
        IExecdCommands commands = CreateAdapter(handler);
        var events = new List<ServerStreamEvent>();

        await foreach (var streamEvent in commands.RunStreamAsync("exit 0", new RunCommandOptions { Background = background }))
        {
            events.Add(streamEvent);
        }

        var complete = events.Should().ContainSingle().Which;
        complete.Type.Should().Be(ServerStreamEventTypes.ExecutionComplete);
        complete.ExitCode.Should().Be(explicitExitCode ? 0 : (int?)null);
    }

    [Theory]
    [InlineData(false)]
    [InlineData(true)]
    public async Task BackgroundCommand_StopsAtCompleteAndDisposesStream(bool argv)
    {
        using var stream = new CommandEventStream("data: {\"type\":\"execution_complete\"}\n\n", failAtEnd: true);
        var adapter = CreateAdapter(new StubHttpMessageHandler((_, _) =>
            Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(stream) })));
        var options = new RunCommandOptions { Background = true };
        using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(2));
        var execution = argv
            ? await adapter.RunAsync(new[] { "sleep", "30" }, options, cancellationToken: timeout.Token)
            : await adapter.RunAsync("sleep 30", options, cancellationToken: timeout.Token);
        execution.Complete.Should().NotBeNull();
        execution.ExitCode.Should().BeNull();
        stream.Disposed.Should().BeTrue();
    }

    [Theory]
    [InlineData(false, false)]
    [InlineData(false, true)]
    [InlineData(true, false)]
    [InlineData(true, true)]
    public async Task ForegroundCommand_DrainsAndDisposesStream(bool failure, bool lateOutput)
    {
        var terminal = failure
            ? "data: {\"type\":\"error\",\"error\":{\"ename\":\"CommandExecError\",\"evalue\":\"7\"}}\n\n"
            : "data: {\"type\":\"execution_complete\"}\n\n";
        var output = "data: {\"type\":\"stdout\",\"text\":\"tail\"}\n\ndata: {\"type\":\"stderr\",\"text\":\"error-tail\"}\n\n";
        using var stream = new CommandEventStream(lateOutput ? terminal + output : output + terminal);
        var adapter = CreateAdapter(new StubHttpMessageHandler((_, _) =>
            Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(stream) })));
        var execution = await adapter.RunAsync("echo test");
        execution.Logs.Stdout.Should().ContainSingle().Which.Text.Should().Be("tail");
        execution.Logs.Stderr.Should().ContainSingle().Which.Text.Should().Be("error-tail");
        execution.ExitCode.Should().Be(failure ? 7 : 0);
        stream.Exhausted.Should().BeTrue();
        stream.Disposed.Should().BeTrue();
    }

    private sealed class CommandEventStream(string text, bool failAtEnd = false)
        : MemoryStream(Encoding.UTF8.GetBytes(text))
    {
        public bool Disposed { get; private set; }
        public bool Exhausted { get; private set; }

        public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
        {
            if (Position == Length)
            {
                if (failAtEnd) throw new IOException("Stream interrupted after terminal event");
                Exhausted = true;
            }
            return base.ReadAsync(buffer[..Math.Min(buffer.Length, 7)], cancellationToken);
        }

        protected override void Dispose(bool disposing)
        {
            Disposed = true;
            base.Dispose(disposing);
        }
    }

    [Fact]
    public async Task NativeArgv_ShouldRejectInvalidInputsBeforeSending()
    {
        var requests = 0;
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            requests++;
            throw new InvalidOperationException("Unexpected request");
        });
        IExecdCommands commands = CreateAdapter(handler);
        IReadOnlyList<string>[] invalid = [null!, [], [""], ["tool", null!], ["tool", "\0"]];
        foreach (var argv in invalid)
        {
            await Assert.ThrowsAsync<InvalidArgumentException>(() => commands.RunAsync(argv));
            Assert.Throws<InvalidArgumentException>(() => commands.RunStreamAsync(argv));
        }
        requests.Should().Be(0);
    }

    private static string? s_capturedSetEnvBody;

    private static StubHttpMessageHandler SetEnvCaptureHandler()
    {
        s_capturedSetEnvBody = null;
        return new StubHttpMessageHandler(async (request, _) =>
        {
            s_capturedSetEnvBody = await request.Content!.ReadAsStringAsync();
            return new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(new CommandEventStream(
                    "data: {\"type\":\"execution_complete\",\"execution_time\":1,\"timestamp\":1}\n\n"))
            };
        });
    }

    private static string CapturedSetEnvCommand => JsonDocument.Parse(s_capturedSetEnvBody!).RootElement
        .GetProperty("command").GetString()!;

    [Fact]
    public async Task SetEnvAsync_ShouldAppendEnvEntryViaSandboxEnvFile()
    {
        var adapter = CreateAdapter(SetEnvCaptureHandler());

        await adapter.SetEnvAsync("MY_TOKEN", "value");

        // Hand-written golden literal (not derived from the implementation).
        CapturedSetEnvCommand.Should().Be(
            "if [ -z \"${EXECD_ENVS:-}\" ]; then printf '%s\\n' " +
            "'EXECD_ENVS is not set; cannot persist environment variable MY_TOKEN' >&2; exit 1; fi\n" +
            "mkdir -p \"$(dirname \"$EXECD_ENVS\")\"\n" +
            "printf '%s\\n' 'MY_TOKEN='\\''value'\\''' >> \"$EXECD_ENVS\"");
    }

    public static TheoryData<string, string, string> SetEnvRoundTripCases => new()
    {
        { "MY_TOKEN", "value", "MY_TOKEN='value'" },
        { "MY_VAR", "line1\nline2 $HOME \\path", "MY_VAR='line1\nline2 $HOME \\path'" },
        { "KV", "a=b=c", "KV='a=b=c'" },
        { "EMPTY", "", "EMPTY=''" },
        { "GREETING", "it's fine \"quoted\"\ttab", "GREETING=\"it's fine \\\"quoted\\\"\\ttab\"" },
        { "PATHY", "it's\nC:\\path", "PATHY=\"it's\\nC:\\\\path\"" },
    };

    [Theory]
    [MemberData(nameof(SetEnvRoundTripCases))]
    public async Task SetEnvAsync_ShouldAppendWellFormedLinesRoundTrippedThroughSh(
        string key, string value, string expectedLine)
    {
        var adapter = CreateAdapter(SetEnvCaptureHandler());

        await adapter.SetEnvAsync(key, value);

        if (!File.Exists("/bin/sh"))
        {
            return; // POSIX round-trip test
        }
        var envFile = Path.Combine(Path.GetTempPath(), $"opensandbox-setenv-{Guid.NewGuid():N}.env");
        try
        {
            var startInfo = new System.Diagnostics.ProcessStartInfo("/bin/sh")
            {
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
            };
            startInfo.ArgumentList.Add("-c");
            startInfo.ArgumentList.Add(CapturedSetEnvCommand);
            startInfo.EnvironmentVariables["EXECD_ENVS"] = envFile;
            var process = System.Diagnostics.Process.Start(startInfo)!;
            process.WaitForExit(10_000).Should().BeTrue();
            process.ExitCode.Should().Be(0, string.Concat(process.StandardError.ReadToEnd(), process.StandardOutput.ReadToEnd()));
            File.ReadAllText(envFile).Should().Be(expectedLine + "\n");
        }
        finally
        {
            File.Delete(envFile);
        }
    }

    [Theory]
    [InlineData("")]
    [InlineData("1ABC")]
    [InlineData("MY-TOKEN")]
    [InlineData("MY TOKEN")]
    [InlineData("A=B")]
    [InlineData("A.B")]
    [InlineData("A\n")]
    public async Task SetEnvAsync_ShouldRejectInvalidKeysBeforeSending(string key)
    {
        var requests = 0;
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            requests++;
            throw new InvalidOperationException("Unexpected request");
        });
        IExecdCommands commands = CreateAdapter(handler);

        await Assert.ThrowsAsync<InvalidArgumentException>(() => commands.SetEnvAsync(key, "value"));
        requests.Should().Be(0);
    }

    [Fact]
    public async Task SetEnvAsync_ShouldRejectNullKeyAndValue()
    {
        IExecdCommands commands = CreateAdapter(new StubHttpMessageHandler((_, _) =>
            throw new InvalidOperationException("Unexpected request")));

        (await Assert.ThrowsAsync<InvalidArgumentException>(() => commands.SetEnvAsync(null!, "value")))
            .Message.Should().Contain("key cannot be null");
        (await Assert.ThrowsAsync<InvalidArgumentException>(() => commands.SetEnvAsync("MY_TOKEN", null!)))
            .Message.Should().Contain("value cannot be null");
    }

    [Fact]
    public async Task SetEnvAsync_ShouldRejectNulBytesInValue()
    {
        var requests = 0;
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            requests++;
            throw new InvalidOperationException("Unexpected request");
        });
        IExecdCommands commands = CreateAdapter(handler);

        await Assert.ThrowsAsync<InvalidArgumentException>(() => commands.SetEnvAsync("MY_TOKEN", "a\0b"));
        requests.Should().Be(0);
    }

    [Fact]
    public async Task SetEnvAsync_ShouldThrowWithStderrWhenAppendFails()
    {
        var sse =
            "data: {\"type\":\"init\",\"text\":\"cmd-1\"}\n\n" +
            "data: {\"type\":\"stderr\",\"text\":\"EXECD_ENVS is not set; cannot persist environment variable MY_TOKEN\"}\n\n" +
            "data: {\"type\":\"error\",\"error\":{\"ename\":\"CommandExecError\",\"evalue\":\"1\"}}\n\n";
        var adapter = CreateAdapter(new StubHttpMessageHandler((_, _) =>
            Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(new CommandEventStream(sse))
            })));

        var ex = await Assert.ThrowsAsync<SandboxException>(() => adapter.SetEnvAsync("MY_TOKEN", "value"));
        ex.Message.Should().Contain("commands.SetEnvAsync failed for 'MY_TOKEN'");
        ex.Message.Should().Contain("EXECD_ENVS is not set");
    }

    [Fact]
    public async Task SetEnvAsync_ShouldFallBackToErrorValueWhenStderrIsEmpty()
    {
        var sse = "data: {\"type\":\"error\",\"error\":{\"ename\":\"CommandExecError\",\"evalue\":\"7\"}}\n\n";
        var adapter = CreateAdapter(new StubHttpMessageHandler((_, _) =>
            Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(new CommandEventStream(sse))
            })));

        var ex = await Assert.ThrowsAsync<SandboxException>(() => adapter.SetEnvAsync("MY_TOKEN", "value"));
        ex.Message.Should().Contain("commands.SetEnvAsync failed for 'MY_TOKEN': 7");
    }

    [Fact]
    public async Task SetEnvAsync_ShouldTreatDroppedStreamWithoutCompletionAsFailure()
    {
        // Stream ends after init only: no execution_complete and no error
        // event, so the append was never confirmed and must not report success.
        var sse = "data: {\"type\":\"init\",\"text\":\"cmd-1\"}\n\n";
        var adapter = CreateAdapter(new StubHttpMessageHandler((_, _) =>
            Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StreamContent(new CommandEventStream(sse))
            })));

        (await Assert.ThrowsAsync<SandboxException>(() => adapter.SetEnvAsync("MY_TOKEN", "value")))
            .Message.Should().Contain("commands.SetEnvAsync failed for 'MY_TOKEN'");
    }

    [Theory]
    [InlineData(false)]
    [InlineData(true)]
    public async Task RunArgv_ShouldPreserveArgumentsAndOptions(bool streaming)
    {
        string[] argv = ["tool", "", "a b", "$HOME", "x'y", "中文"];
        var handler = new StubHttpMessageHandler(async (request, cancellationToken) =>
        {
            using var body = JsonDocument.Parse(await request.Content!.ReadAsStringAsync());
            body.RootElement.TryGetProperty("command", out _).Should().BeFalse();
            body.RootElement.GetProperty("argv").EnumerateArray().Select(x => x.GetString()).Should().Equal(argv);
            body.RootElement.GetProperty("cwd").GetString().Should().Be("$DIR");
            return new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("data: {\"type\":\"execution_complete\"}\n\n", Encoding.UTF8, "text/event-stream")
            };
        });
        IExecdCommands commands = CreateAdapter(handler);
        var options = new RunCommandOptions { WorkingDirectory = "$DIR" };
        if (streaming)
        {
            await foreach (var _ in commands.RunStreamAsync(argv, options)) { }
        }
        else
        {
            await commands.RunAsync(argv, options);
        }
    }

    [Fact]
    public async Task GetCommandStatusAsync_ShouldParseStatusResponse()
    {
        var httpHandler = new StubHttpMessageHandler((request, _) =>
        {
            var body = "{\"id\":\"cmd-1\",\"content\":\"sleep 1\",\"running\":true,\"exit_code\":null}";
            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(body, Encoding.UTF8, "application/json")
            });
        });
        var adapter = CreateAdapter(httpHandler);

        var status = await adapter.GetCommandStatusAsync("cmd-1");

        status.Id.Should().Be("cmd-1");
        status.Content.Should().Be("sleep 1");
        status.Running.Should().BeTrue();
        status.ExitCode.Should().BeNull();
        httpHandler.RequestUris.Should().Contain(uri => uri.EndsWith("/command/status/cmd-1"));
    }

    [Fact]
    public async Task GetBackgroundCommandLogsAsync_ShouldParseCursorHeader()
    {
        var httpHandler = new StubHttpMessageHandler((request, _) =>
        {
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("line1\nline2\n", Encoding.UTF8, "text/plain")
            };
            response.Headers.Add("EXECD-COMMANDS-TAIL-CURSOR", "42");
            return Task.FromResult(response);
        });
        var adapter = CreateAdapter(httpHandler);

        var logs = await adapter.GetBackgroundCommandLogsAsync("cmd-2", cursor: 10);

        logs.Content.Should().Contain("line1");
        logs.Cursor.Should().Be(42);
        httpHandler.RequestUris.Should().Contain(uri => uri.Contains("/command/cmd-2/logs?cursor=10"));
    }

    [Fact]
    public async Task GetBackgroundCommandLogsAsync_ShouldReturnNullCursorWhenHeaderMissing()
    {
        var httpHandler = new StubHttpMessageHandler((request, _) =>
        {
            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("only-content", Encoding.UTF8, "text/plain")
            });
        });
        var adapter = CreateAdapter(httpHandler);

        var logs = await adapter.GetBackgroundCommandLogsAsync("cmd-3");

        logs.Content.Should().Be("only-content");
        logs.Cursor.Should().BeNull();
    }

    [Fact]
    public async Task RunStreamAsync_ShouldSendTimeoutInMilliseconds()
    {
        var handler = new StubHttpMessageHandler(async (request, _) =>
        {
            request.Content.Should().NotBeNull();
            var body = await request.Content!.ReadAsStringAsync().ConfigureAwait(false);
            using var doc = JsonDocument.Parse(body);
            doc.RootElement.GetProperty("timeout").GetInt64().Should().Be(2000);

            return new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("data: {\"type\":\"init\",\"text\":\"cmd-1\"}\n\n", Encoding.UTF8, "text/event-stream")
            };
        });
        var adapter = CreateAdapter(handler);

        var options = new RunCommandOptions
        {
            TimeoutSeconds = 2
        };

        await foreach (var _ in adapter.RunStreamAsync("sleep 1", options))
        {
            // Drain events.
        }
    }

    [Fact]
    public async Task RunStreamAsync_ShouldSendUidGidAndEnvs()
    {
        var handler = new StubHttpMessageHandler(async (request, _) =>
        {
            request.Content.Should().NotBeNull();
            var body = await request.Content!.ReadAsStringAsync().ConfigureAwait(false);
            using var doc = JsonDocument.Parse(body);
            doc.RootElement.GetProperty("uid").GetInt32().Should().Be(1000);
            doc.RootElement.GetProperty("gid").GetInt32().Should().Be(1000);
            var envs = doc.RootElement.GetProperty("envs");
            envs.GetProperty("APP_ENV").GetString().Should().Be("test");
            envs.GetProperty("LOG_LEVEL").GetString().Should().Be("debug");

            return new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("data: {\"type\":\"init\",\"text\":\"cmd-1\"}\n\n", Encoding.UTF8, "text/event-stream")
            };
        });
        var adapter = CreateAdapter(handler);

        var options = new RunCommandOptions
        {
            Uid = 1000,
            Gid = 1000,
            Envs = new Dictionary<string, string>
            {
                ["APP_ENV"] = "test",
                ["LOG_LEVEL"] = "debug"
            }
        };

        await foreach (var _ in adapter.RunStreamAsync("id", options))
        {
            // Drain events.
        }
    }

    [Fact]
    public async Task RunStreamAsync_ShouldRejectGidWithoutUid()
    {
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            throw new InvalidOperationException("HTTP should not be called when options are invalid.");
        });
        var adapter = CreateAdapter(handler);

        var options = new RunCommandOptions
        {
            Gid = 1000
        };

        var act = async () =>
        {
            await foreach (var _ in adapter.RunStreamAsync("id", options))
            {
                // Drain events.
            }
        };

        await act.Should().ThrowAsync<InvalidArgumentException>()
            .WithMessage("*uid is required when gid is provided*");
    }

    [Fact]
    public async Task RunAsync_ShouldPopulateCompleteAndExitCodeForSuccessfulForegroundCommands()
    {
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            const string sse = """
data: {"type":"init","text":"cmd-1","timestamp":1}

data: {"type":"stdout","text":"hi","timestamp":2}

data: {"type":"execution_complete","timestamp":3,"execution_time":4}

""";
            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(sse, Encoding.UTF8, "text/event-stream")
            });
        });
        var adapter = CreateAdapter(handler);

        var execution = await adapter.RunAsync("echo hi");

        execution.Id.Should().Be("cmd-1");
        execution.Logs.Stdout.Should().ContainSingle().Which.Text.Should().Be("hi");
        execution.Complete.Should().NotBeNull();
        execution.Complete!.ExecutionTimeMs.Should().Be(4);
        execution.ExitCode.Should().Be(0);
    }

    [Fact]
    public async Task RunAsync_ShouldInferNonZeroExitCodeFromFinalErrorState()
    {
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            const string sse = """
data: {"type":"init","text":"cmd-2","timestamp":1}

data: {"type":"error","error":{"ename":"CommandExecError","evalue":"7","traceback":["exit status 7"]},"timestamp":2}

""";
            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(sse, Encoding.UTF8, "text/event-stream")
            });
        });
        var adapter = CreateAdapter(handler);

        var execution = await adapter.RunAsync("exit 7");

        execution.Id.Should().Be("cmd-2");
        execution.Error.Should().NotBeNull();
        execution.Error!.Value.Should().Be("7");
        execution.Complete.Should().BeNull();
        execution.ExitCode.Should().Be(7);
    }

    [Fact]
    public async Task RunAsync_ShouldKeepExitCodeNullWhenErrorValueIsEmpty()
    {
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            const string sse = """
data: {"type":"init","text":"cmd-3","timestamp":1}

data: {"type":"execution_complete","timestamp":2,"execution_time":4}

data: {"type":"error","error":{"ename":"CommandExecError","evalue":"","traceback":["failed"]},"timestamp":3}

""";
            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(sse, Encoding.UTF8, "text/event-stream")
            });
        });
        var adapter = CreateAdapter(handler);

        var execution = await adapter.RunAsync("bad command");

        execution.Id.Should().Be("cmd-3");
        execution.Error.Should().NotBeNull();
        execution.Error!.Value.Should().BeEmpty();
        execution.Complete.Should().NotBeNull();
        execution.ExitCode.Should().BeNull();
    }

    // --- Bash session API integration tests ---

    [Fact]
    public async Task CreateSessionAsync_ShouldReturnSessionId_WhenWorkingDirectoryProvided()
    {
        var handler = new StubHttpMessageHandler(async (request, _) =>
        {
            request.Method.Should().Be(HttpMethod.Post);
            request.RequestUri!.ToString().Should().Contain("/session");
            request.Content.Should().NotBeNull();
            var body = await request.Content!.ReadAsStringAsync().ConfigureAwait(false);
            using var doc = JsonDocument.Parse(body);
            doc.RootElement.GetProperty("cwd").GetString().Should().Be("/tmp");

            return new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("{\"session_id\":\"sess-abc123\"}", Encoding.UTF8, "application/json")
            };
        });
        var adapter = CreateAdapter(handler);

        var sessionId = await adapter.CreateSessionAsync(new CreateSessionOptions { WorkingDirectory = "/tmp" });

        sessionId.Should().Be("sess-abc123");
        handler.RequestUris.Should().Contain(uri => uri.EndsWith("/session"));
    }

    [Fact]
    public async Task CreateSessionAsync_ShouldReturnSessionId_WhenNoOptions()
    {
        var handler = new StubHttpMessageHandler((request, _) =>
        {
            request.Method.Should().Be(HttpMethod.Post);
            request.RequestUri!.ToString().Should().Contain("/session");

            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("{\"session_id\":\"sess-default\"}", Encoding.UTF8, "application/json")
            });
        });
        var adapter = CreateAdapter(handler);

        var sessionId = await adapter.CreateSessionAsync();

        sessionId.Should().Be("sess-default");
    }

    [Fact]
    public async Task CreateSessionAsync_ShouldThrow_WhenResponseHasEmptySessionId()
    {
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("{\"session_id\":\"\"}", Encoding.UTF8, "application/json")
            });
        });
        var adapter = CreateAdapter(handler);

        var act = () => adapter.CreateSessionAsync();

        await act.Should().ThrowAsync<SandboxApiException>()
            .WithMessage("*empty session_id*");
    }

    [Fact]
    public async Task RunInSessionAsync_ShouldSendCommandAndOptions()
    {
        var handler = new StubHttpMessageHandler(async (request, _) =>
        {
            request.Method.Should().Be(HttpMethod.Post);
            request.RequestUri!.ToString().Should().Contain("/session/sess-1/run");
            request.Content.Should().NotBeNull();
            var body = await request.Content!.ReadAsStringAsync().ConfigureAwait(false);
            using var doc = JsonDocument.Parse(body);
            doc.RootElement.GetProperty("command").GetString().Should().Be("pwd");
            doc.RootElement.GetProperty("cwd").GetString().Should().Be("/var");
            doc.RootElement.GetProperty("timeout").GetInt64().Should().Be(5000);

            var sse = "data: {\"type\":\"stdout\",\"text\":\"/var\"}\ndata: {\"type\":\"execution_complete\"}\n";
            return new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(sse, Encoding.UTF8, "text/event-stream")
            };
        });
        var adapter = CreateAdapter(handler);

        var run = await adapter.RunInSessionAsync(
            "sess-1",
            "pwd",
            new RunInSessionOptions { WorkingDirectory = "/var", TimeoutSeconds = 5 });

        run.Should().NotBeNull();
        run.Logs.Stdout.Should().ContainSingle(m => m.Text == "/var");
        run.ExitCode.Should().Be(0);
        handler.RequestUris.Should().Contain(uri => uri.Contains("/session/sess-1/run"));
    }

    [Fact]
    public async Task RunInSessionAsync_ShouldInferNonZeroExitCodeFromFinalErrorState()
    {
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            const string sse = """
data: {"type":"init","text":"sess-cmd-2","timestamp":1}

data: {"type":"error","error":{"ename":"CommandExecError","evalue":"7","traceback":["exit status 7"]},"timestamp":2}

""";
            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(sse, Encoding.UTF8, "text/event-stream")
            });
        });
        var adapter = CreateAdapter(handler);

        var execution = await adapter.RunInSessionAsync("sess-2", "exit 7");

        execution.Id.Should().Be("sess-cmd-2");
        execution.Error.Should().NotBeNull();
        execution.Error!.Value.Should().Be("7");
        execution.Complete.Should().BeNull();
        execution.ExitCode.Should().Be(7);
    }

    [Fact]
    public async Task RunInSessionAsync_ShouldThrow_WhenSessionIdEmpty()
    {
        var adapter = CreateAdapter(new StubHttpMessageHandler((_, _) => throw new InvalidOperationException("Should not be called")));

        var act = () => adapter.RunInSessionAsync("", "echo hi");

        await act.Should().ThrowAsync<InvalidArgumentException>()
            .WithMessage("*sessionId*");
    }

    [Fact]
    public async Task RunInSessionAsync_ShouldThrow_WhenCommandEmpty()
    {
        var adapter = CreateAdapter(new StubHttpMessageHandler((_, _) => throw new InvalidOperationException("Should not be called")));

        var act = () => adapter.RunInSessionAsync("sess-1", "  ");

        await act.Should().ThrowAsync<InvalidArgumentException>()
            .WithMessage("*command*");
    }

    [Fact]
    public async Task DeleteSessionAsync_ShouldCallDeleteEndpoint()
    {
        var handler = new StubHttpMessageHandler((request, _) =>
        {
            request.Method.Should().Be(HttpMethod.Delete);
            request.RequestUri!.ToString().Should().Contain("/session/sess-to-delete");

            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK));
        });
        var adapter = CreateAdapter(handler);

        await adapter.DeleteSessionAsync("sess-to-delete");

        handler.RequestUris.Should().Contain(uri => uri.EndsWith("/session/sess-to-delete"));
    }

    [Fact]
    public async Task DeleteSessionAsync_ShouldThrow_WhenSessionIdEmpty()
    {
        var adapter = CreateAdapter(new StubHttpMessageHandler((_, _) => throw new InvalidOperationException("Should not be called")));

        var act = () => adapter.DeleteSessionAsync("  ");

        await act.Should().ThrowAsync<InvalidArgumentException>()
            .WithMessage("*sessionId*");
    }

    [Fact]
    public async Task GetCommandStatusAsync_ShouldIncludeBodyInMessage_WhenBodyUnparseable()
    {
        var body = "{\"error\":\"cursor must be positive\"}";
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent(body, Encoding.UTF8, "application/json")
            });
        });
        var adapter = CreateAdapter(handler);

        var act = () => adapter.GetCommandStatusAsync("exec-1");

        var ex = await act.Should().ThrowAsync<SandboxApiException>();
        ex.Which.Message.Should().Contain(body);
    }

    [Fact]
    public async Task GetCommandStatusAsync_ShouldIncludeBodyInMessage_WhenBodyIsPlainText()
    {
        var body = "cursor must be positive";
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent(body, Encoding.UTF8, "text/plain")
            });
        });
        var adapter = CreateAdapter(handler);

        var act = () => adapter.GetCommandStatusAsync("exec-1");

        var ex = await act.Should().ThrowAsync<SandboxApiException>();
        ex.Which.Message.Should().Contain(body);
        ex.Which.RawBody.Should().Be(body);
    }

    [Fact]
    public async Task GetBackgroundCommandLogsAsync_ShouldIncludeBodyInMessage_WhenBodyUnparseable()
    {
        var body = "quota exceeded for sandbox";
        var handler = new StubHttpMessageHandler((_, _) =>
        {
            return Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent(body, Encoding.UTF8, "text/plain")
            });
        });
        var adapter = CreateAdapter(handler);

        var act = () => adapter.GetBackgroundCommandLogsAsync("exec-1");

        var ex = await act.Should().ThrowAsync<SandboxApiException>();
        ex.Which.Message.Should().Contain(body);
    }

    private static CommandsAdapter CreateAdapter(HttpMessageHandler httpHandler)
    {
        var baseUrl = "http://execd.local";
        var headers = new Dictionary<string, string> { ["X-Test"] = "true" };
        var client = new HttpClientWrapper(new HttpClient(httpHandler), baseUrl, headers);
        var sseHttpClient = new HttpClient(httpHandler);
        var logger = NullLoggerFactory.Instance.CreateLogger("CommandsAdapterTests");
        return new CommandsAdapter(client, sseHttpClient, baseUrl, headers, logger);
    }

    private sealed class StubHttpMessageHandler : HttpMessageHandler
    {
        private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _handler;

        public StubHttpMessageHandler(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handler)
        {
            _handler = handler;
        }

        public List<string> RequestUris { get; } = new();

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            RequestUris.Add(request.RequestUri?.ToString() ?? string.Empty);
            return await _handler(request, cancellationToken).ConfigureAwait(false);
        }
    }
}