Name Message Date
📄 OpenAsyncShould.cs One-shot 5 days ago
📄 ui/tests/ScanSessionFactoryTests/OpenAsyncShould.cs
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Cfa.Ui.Graph;
using Cfa.Ui.Services;
using Shouldly;
using Xunit;

namespace Cfa.Ui.Tests.ScanSessionFactoryTests;

public sealed class OpenAsyncShould
{
    [Fact]
    public async Task LoadTheCfaSolutionAndReuseTheSessionForSeveralQueries()
    {
        // Arrange
        var directory = new DirectoryInfo(AppContext.BaseDirectory);
        while (directory is not null && !File.Exists(Path.Combine(directory.FullName, "code-flow-analysis.slnx")))
        {
            directory = directory.Parent;
        }
        var root = directory.ShouldNotBeNull().FullName;
        var solution = new FileInfo(Path.Combine(root, "code-flow-analysis.slnx"));
        var source = new FileInfo(Path.Combine(root, "core/src/CompilationsExtensions.cs"));
        var lines = await File.ReadAllLinesAsync(source.FullName);
        var line = Array.FindIndex(lines, text => text.Contains("public static SemanticModel GetSemanticModel")) + 1;
        using var timeout = new CancellationTokenSource(TimeSpan.FromMinutes(2));
        var factory = new ScanSessionFactory();

        // Act
        using var session = await factory.OpenAsync(solution, timeout.Token);
        var first = await session.AnalyzeAsync(source, line, timeout.Token);
        var second = await session.AnalyzeAsync(source, line, timeout.Token);
        var scene = GraphLayout.Build(second, timeout.Token);

        // Assert
        first.Origin.Name.ShouldBe("GetSemanticModel");
        second.Origin.ShouldBe(first.Origin);
        scene.Nodes.Where(node => node.IsOrigin).ShouldHaveSingleItem().Symbol.ShouldBe(second.Origin);
    }
}