public async Task TrackingService_GetLatestSpansAsync(
            bool scheduleInitialTrackingBeforeOpenDoc
            )
        {
            var source1 =
                "class C { void F() => G(1); void G(int a) => System.Console.WriteLine(1); }";
            var source2 = "class D { }";

            using var workspace = new TestWorkspace();

            var span11 = new LinePositionSpan(new LinePosition(0, 10), new LinePosition(0, 15));
            var span12 = new LinePositionSpan(new LinePosition(0, 20), new LinePosition(0, 25));
            var span21 = new LinePositionSpan(new LinePosition(0, 11), new LinePosition(0, 16));
            var span22 = new LinePositionSpan(new LinePosition(0, 21), new LinePosition(0, 26));

            var encService = new MockEditAndContinueWorkspaceService();

            encService.GetBaseActiveStatementSpansImpl = (_, documentIds) =>
                                                         ImmutableArray.Create(
                ImmutableArray.Create(
                    (span11, ActiveStatementFlags.IsNonLeafFrame),
                    (span12, ActiveStatementFlags.IsLeafFrame)
                    ),
                ImmutableArray <(LinePositionSpan, ActiveStatementFlags)> .Empty
                );

            encService.GetAdjustedActiveStatementSpansImpl = (document, _) =>
                                                             document.Name switch
            {
                "1.cs"
                => ImmutableArray.Create(
                    (span21, ActiveStatementFlags.IsNonLeafFrame),
                    (span22, ActiveStatementFlags.IsLeafFrame)
                    ),
                "2.cs" => ImmutableArray <(LinePositionSpan, ActiveStatementFlags)> .Empty,
                _ => throw ExceptionUtilities.Unreachable
            };

            var testDocument1 = new TestHostDocument(
                text: source1,
                displayName: "1.cs",
                exportProvider: workspace.ExportProvider
                );
            var testDocument2 = new TestHostDocument(
                text: source2,
                displayName: "2.cs",
                exportProvider: workspace.ExportProvider
                );

            workspace.AddTestProject(
                new TestHostProject(workspace, documents: new[] { testDocument1, testDocument2 })
                );

            // opens the documents
            var textBuffer1 = testDocument1.GetTextBuffer();
            var textBuffer2 = testDocument2.GetTextBuffer();

            var solution  = workspace.CurrentSolution;
            var project   = solution.Projects.Single();
            var document1 = project.Documents.Single(d => d.Name == "1.cs");
            var document2 = project.Documents.Single(d => d.Name == "2.cs");
            var snapshot1 = textBuffer1.CurrentSnapshot;
            var snapshot2 = textBuffer2.CurrentSnapshot;

            Assert.Same(
                snapshot1,
                document1
                .GetTextSynchronously(CancellationToken.None)
                .FindCorrespondingEditorTextSnapshot()
                );
            Assert.Same(
                snapshot2,
                document2
                .GetTextSynchronously(CancellationToken.None)
                .FindCorrespondingEditorTextSnapshot()
                );

            var trackingSession = new ActiveStatementTrackingService.TrackingSession(
                workspace,
                encService
                );

            if (scheduleInitialTrackingBeforeOpenDoc)
            {
                await trackingSession.TrackActiveSpansAsync(solution, CancellationToken.None);

                var spans1 = trackingSession.Test_GetTrackingSpans();
                AssertEx.Equal(
                    new[] { $"V0 →←@[10..15): IsNonLeafFrame", $"V0 →←@[20..25): IsLeafFrame" },
                    spans1[document1.Id].Select(s => $"{s.Span}: {s.Flags}")
                    );

                var spans2 = await trackingSession.GetSpansAsync(document1, CancellationToken.None);

                AssertEx.Equal(new[] { "[10..15)", "[20..25)" }, spans2.Select(s => s.ToString()));

                var spans3 = await trackingSession.GetSpansAsync(document2, CancellationToken.None);

                Assert.Empty(spans3);
            }

            var spans4 = await trackingSession.GetAdjustedTrackingSpansAsync(
                document1,
                snapshot1,
                CancellationToken.None
                );

            AssertEx.Equal(
                new[] { $"V0 →←@[11..16): IsNonLeafFrame", $"V0 →←@[21..26): IsLeafFrame" },
                spans4.Select(s => $"{s.Span}: {s.Flags}")
                );

            AssertEx.Empty(
                await trackingSession.GetAdjustedTrackingSpansAsync(
                    document2,
                    snapshot2,
                    CancellationToken.None
                    )
                );

            if (!scheduleInitialTrackingBeforeOpenDoc)
            {
                await trackingSession.TrackActiveSpansAsync(solution, CancellationToken.None);

                var spans5 = trackingSession.Test_GetTrackingSpans();
                AssertEx.Equal(
                    new[] { $"V0 →←@[11..16): IsNonLeafFrame", $"V0 →←@[21..26): IsLeafFrame" },
                    spans5[document1.Id].Select(s => $"{s.Span}: {s.Flags}")
                    );
            }

            // we are not able to determine active statements in a document:
            encService.GetAdjustedActiveStatementSpansImpl = (_, _) => default;

            var spans6 = await trackingSession.GetAdjustedTrackingSpansAsync(
                document1,
                snapshot1,
                CancellationToken.None
                );

            AssertEx.Equal(
                new[] { $"V0 →←@[11..16): IsNonLeafFrame", $"V0 →←@[21..26): IsLeafFrame" },
                spans6.Select(s => $"{s.Span}: {s.Flags}")
                );
        }
    }
        public async Task TrackingService_GetLatestSpansAsync(bool scheduleInitialTrackingBeforeOpenDoc)
        {
            var sourceV1 = "class C { void F() => G(1); void G(int a) => System.Console.WriteLine(1); }";

            using var workspace = new TestWorkspace();

            var span11 = new LinePositionSpan(new LinePosition(0, 10), new LinePosition(0, 15));
            var span12 = new LinePositionSpan(new LinePosition(0, 20), new LinePosition(0, 25));
            var span21 = new LinePositionSpan(new LinePosition(0, 11), new LinePosition(0, 16));
            var span22 = new LinePositionSpan(new LinePosition(0, 21), new LinePosition(0, 26));

            var encService = new MockEditAndContinueWorkspaceService();

            encService.GetBaseActiveStatementSpansAsyncImpl = documentIds => ImmutableArray.Create(ImmutableArray.Create(
                                                                                                       (span11, ActiveStatementFlags.IsNonLeafFrame),
                                                                                                       (span12, ActiveStatementFlags.IsLeafFrame)));

            encService.GetAdjustedDocumentActiveStatementSpansAsyncImpl = document => ImmutableArray.Create(
                (span21, ActiveStatementFlags.IsNonLeafFrame),
                (span22, ActiveStatementFlags.IsLeafFrame));

            var testDocument = new TestHostDocument(text: sourceV1, exportProvider: workspace.ExportProvider);

            workspace.AddTestProject(new TestHostProject(workspace, testDocument));
            var textBuffer = testDocument.GetTextBuffer();

            var solution = workspace.CurrentSolution;
            var project  = solution.Projects.Single();
            var document = project.Documents.Single();
            var snapshot = textBuffer.CurrentSnapshot;

            Assert.Same(snapshot, document.GetTextSynchronously(CancellationToken.None).FindCorrespondingEditorTextSnapshot());

            var trackingSession = new ActiveStatementTrackingService.TrackingSession(workspace, encService);

            if (scheduleInitialTrackingBeforeOpenDoc)
            {
                await trackingSession.TrackActiveSpansAsync().ConfigureAwait(false);

                var spans1 = trackingSession.Test_GetTrackingSpans();
                AssertEx.Equal(new[]
                {
                    $"V0 →←@[10..15): IsNonLeafFrame",
                    $"V0 →←@[20..25): IsLeafFrame"
                }, spans1[document.Id].Select(s => $"{s.Span}: {s.Flags}"));
            }

            var spans2 = await trackingSession.GetAdjustedTrackingSpansAsync(document, snapshot, CancellationToken.None).ConfigureAwait(false);

            AssertEx.Equal(new[]
            {
                $"V0 →←@[11..16): IsNonLeafFrame",
                $"V0 →←@[21..26): IsLeafFrame"
            }, spans2.Select(s => $"{s.Span}: {s.Flags}"));

            if (!scheduleInitialTrackingBeforeOpenDoc)
            {
                await trackingSession.TrackActiveSpansAsync().ConfigureAwait(false);

                var spans1 = trackingSession.Test_GetTrackingSpans();
                AssertEx.Equal(new[]
                {
                    $"V0 →←@[11..16): IsNonLeafFrame",
                    $"V0 →←@[21..26): IsLeafFrame"
                }, spans1[document.Id].Select(s => $"{s.Span}: {s.Flags}"));
            }

            // we are not able to determine active statements in a document:
            encService.GetAdjustedDocumentActiveStatementSpansAsyncImpl = document => default;

            var spans3 = await trackingSession.GetAdjustedTrackingSpansAsync(document, snapshot, CancellationToken.None).ConfigureAwait(false);

            AssertEx.Equal(new[]
            {
                $"V0 →←@[11..16): IsNonLeafFrame",
                $"V0 →←@[21..26): IsLeafFrame"
            }, spans3.Select(s => $"{s.Span}: {s.Flags}"));
        }