<commit_before>﻿namespace Atata.IntegrationTests.Logging;

[TestFixture]
public class FileScreenshotConsumerTests : UITestFixtureBase
{
    internal const string DefaultDateTimeFormat = "yyyyMMddTHHmmss";

    private ScreenshotConsumerAtataContextBuilder<FileScreenshotConsumer> _consumerBuilder;

    private List<string> _directoriesToDelete;

    [SetUp]
    public void SetUp()
    {
        _consumerBuilder = ConfigureBaseAtataContext()
            .ScreenshotConsumers.AddFile();

        _directoriesToDelete = new List<string>();
    }

    [Test]
    public void WithDirectoryPath_Relative()
    {
        _consumerBuilder
            .WithDirectoryPath(@$"TestLogs\{{build-start:{DefaultDateTimeFormat}}}\{{test-name-sanitized}}")
            .Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();

        string directoryPath = Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            "TestLogs",
            AtataContext.BuildStart.Value.ToString(DefaultDateTimeFormat),
            nameof(WithDirectoryPath_Relative));

        _directoriesToDelete.Add(directoryPath);

        FileAssert.Exists(Path.Combine(directoryPath, "01 - Basic Controls page.png"));
    }

    [Test]
    public void WithDirectoryPath_Absolute()
    {
        _consumerBuilder
            .WithDirectoryPath(
                Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,
                    @$"{{build-start:{DefaultDateTimeFormat}}}\{{test-name-sanitized}}"))
            .Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();

        string directoryPath = Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            AtataContext.BuildStart.Value.ToString(DefaultDateTimeFormat),
            nameof(WithDirectoryPath_Absolute));

        _directoriesToDelete.Add(directoryPath);

        FileAssert.Exists(Path.Combine(directoryPath, "01 - Basic Controls page.png"));
    }

    [Test]
    public void WithDirectoryPathBuilder()
    {
        _consumerBuilder
            .WithDirectoryPath(() => $@"TestLogs\{AtataContext.BuildStart.Value.ToString(DefaultDateTimeFormat)}\{AtataContext.Current.TestName}")
            .Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();

        string directoryPath = Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            "TestLogs",
            AtataContext.BuildStart.Value.ToString(DefaultDateTimeFormat),
            nameof(WithDirectoryPathBuilder));

        _directoriesToDelete.Add(directoryPath);

        FileAssert.Exists(Path.Combine(directoryPath, "01 - Basic Controls page.png"));
    }

    [Test]
    public void WithFileName()
    {
        _consumerBuilder
            .WithFileName(@"{screenshot-number:d3} {screenshot-title:* - }{screenshot-pageobjectname}")
            .Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();
        AtataContext.Current.Log.Screenshot("Some title");

        string directoryPath = AtataContext.Current.Artifacts.FullName;

        FileAssert.Exists(Path.Combine(directoryPath, "001 Basic Controls.png"));
        FileAssert.Exists(Path.Combine(directoryPath, "002 Some title - Basic Controls.png"));
    }

    [Test]
    public void SanitizeTitle()
    {
        _consumerBuilder.Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();
        AtataContext.Current.Log.Screenshot("Some\\ /:title");

        string directoryPath = AtataContext.Current.Artifacts.FullName;

        _directoriesToDelete.Add(directoryPath);

        FileAssert.Exists(Path.Combine(directoryPath, "01 - Basic Controls page.png"));
        FileAssert.Exists(Path.Combine(directoryPath, "02 - Basic Controls page - Some title.png"));
    }

    [Test]
    public void FilePath()
    {
        _consumerBuilder
            .WithFilePath(@$"TestLogs\{{build-start:{DefaultDateTimeFormat}}}\Test {{test-name-sanitized}}\{{screenshot-number:d2}}{{screenshot-title: - *}}")
            .Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();
        AtataContext.Current.Log.Screenshot("Some title");

        string directoryPath = Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            "TestLogs",
            AtataContext.BuildStart.Value.ToString(DefaultDateTimeFormat),
            $"Test {nameof(FilePath)}");

        _directoriesToDelete.Add(directoryPath);

        FileAssert.Exists(Path.Combine(directoryPath, "01.png"));
        FileAssert.Exists(Path.Combine(directoryPath, "02 - Some title.png"));
    }

    public override void TearDown()
    {
        base.TearDown();
<commit_msg>Fix `WithDirectoryPath_Relative` and `FilePath` tests of `FileScreenshotConsumerTests`<commit_after>﻿namespace Atata.IntegrationTests.Logging;

[TestFixture]
public class FileScreenshotConsumerTests : UITestFixtureBase
{
    internal const string DefaultDateTimeFormat = "yyyyMMddTHHmmss";

    private ScreenshotConsumerAtataContextBuilder<FileScreenshotConsumer> _consumerBuilder;

    private List<string> _directoriesToDelete;

    [SetUp]
    public void SetUp()
    {
        _consumerBuilder = ConfigureBaseAtataContext()
            .ScreenshotConsumers.AddFile();

        _directoriesToDelete = new List<string>();
    }

    [Test]
    public void WithDirectoryPath_Relative()
    {
        _consumerBuilder
            .WithDirectoryPath(@$"TestLogs/{{build-start:{DefaultDateTimeFormat}}}/{{test-name-sanitized}}")
            .Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();

        string directoryPath = Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            "TestLogs",
            AtataContext.BuildStart.Value.ToString(DefaultDateTimeFormat),
            nameof(WithDirectoryPath_Relative));

        _directoriesToDelete.Add(directoryPath);

        FileAssert.Exists(Path.Combine(directoryPath, "01 - Basic Controls page.png"));
    }

    [Test]
    public void WithDirectoryPath_Absolute()
    {
        _consumerBuilder
            .WithDirectoryPath(
                Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,
                    @$"{{build-start:{DefaultDateTimeFormat}}}\{{test-name-sanitized}}"))
            .Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();

        string directoryPath = Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            AtataContext.BuildStart.Value.ToString(DefaultDateTimeFormat),
            nameof(WithDirectoryPath_Absolute));

        _directoriesToDelete.Add(directoryPath);

        FileAssert.Exists(Path.Combine(directoryPath, "01 - Basic Controls page.png"));
    }

    [Test]
    public void WithDirectoryPathBuilder()
    {
        _consumerBuilder
            .WithDirectoryPath(() => $@"TestLogs\{AtataContext.BuildStart.Value.ToString(DefaultDateTimeFormat)}\{AtataContext.Current.TestName}")
            .Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();

        string directoryPath = Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            "TestLogs",
            AtataContext.BuildStart.Value.ToString(DefaultDateTimeFormat),
            nameof(WithDirectoryPathBuilder));

        _directoriesToDelete.Add(directoryPath);

        FileAssert.Exists(Path.Combine(directoryPath, "01 - Basic Controls page.png"));
    }

    [Test]
    public void WithFileName()
    {
        _consumerBuilder
            .WithFileName(@"{screenshot-number:d3} {screenshot-title:* - }{screenshot-pageobjectname}")
            .Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();
        AtataContext.Current.Log.Screenshot("Some title");

        string directoryPath = AtataContext.Current.Artifacts.FullName;

        FileAssert.Exists(Path.Combine(directoryPath, "001 Basic Controls.png"));
        FileAssert.Exists(Path.Combine(directoryPath, "002 Some title - Basic Controls.png"));
    }

    [Test]
    public void SanitizeTitle()
    {
        _consumerBuilder.Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();
        AtataContext.Current.Log.Screenshot("Some\\ /:title");

        string directoryPath = AtataContext.Current.Artifacts.FullName;

        _directoriesToDelete.Add(directoryPath);

        FileAssert.Exists(Path.Combine(directoryPath, "01 - Basic Controls page.png"));
        FileAssert.Exists(Path.Combine(directoryPath, "02 - Basic Controls page - Some title.png"));
    }

    [Test]
    public void FilePath()
    {
        _consumerBuilder
            .WithFilePath(@$"TestLogs/{{build-start:{DefaultDateTimeFormat}}}/Test {{test-name-sanitized}}/{{screenshot-number:d2}}{{screenshot-title: - *}}")
            .Build();

        Go.To<BasicControlsPage>();

        AtataContext.Current.Log.Screenshot();
        AtataContext.Current.Log.Screenshot("Some title");

        string directoryPath = Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            "TestLogs",
            AtataContext.BuildStart.Value.ToString(DefaultDateTimeFormat),
            $"Test {nameof(FilePath)}");

        _directoriesToDelete.Add(directoryPath);

        FileAssert.Exists(Path.Combine(directoryPath, "01.png"));
        FileAssert.Exists(Path.Combine(directoryPath, "02 - Some title.png"));
    }

    public override void TearDown()
    {
        base.TearDown();

{'fraction_non_alphanumeric': 0.11522973670624677, 'fraction_numerical': 0.004543107898812597, 'mean_word_length': 2.81789515175404, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5563697', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2603, 'n_words': 366}