using FluentAssertions; using FreeCode.Tools; using FreeCode.Tests.Unit.Helpers; using Xunit; namespace FreeCode.Tests.Unit.Tools; public sealed class GrepToolTests : IDisposable { private readonly string _tempDirectory = Path.Combine(Path.GetTempPath(), "free-code-tests", Guid.NewGuid().ToString("N")); private readonly GrepTool _sut = new(); public GrepToolTests() { Directory.CreateDirectory(_tempDirectory); File.WriteAllText(Path.Combine(_tempDirectory, "alpha.txt"), "hello\nworld\nhello again"); File.WriteAllText(Path.Combine(_tempDirectory, "beta.txt"), "nothing here"); } [Fact] public async Task ExecuteAsync_FindsMatchingLines() { var result = await _sut.ExecuteAsync(new GrepToolInput("hello", _tempDirectory), TestHelper.CreateContext()); result.IsError.Should().BeFalse(); result.Data.TotalMatches.Should().Be(2); result.Data.Matches.Should().OnlyContain(match => match.Line.Contains("hello", StringComparison.Ordinal)); } [Fact] public async Task ExecuteAsync_WithNoMatches_ReturnsEmpty() { var result = await _sut.ExecuteAsync(new GrepToolInput("absent", _tempDirectory), TestHelper.CreateContext()); result.IsError.Should().BeFalse(); result.Data.TotalMatches.Should().Be(0); result.Data.Matches.Should().BeEmpty(); } public void Dispose() { if (Directory.Exists(_tempDirectory)) { Directory.Delete(_tempDirectory, recursive: true); } } }