<commit_before>﻿namespace Grean.AtomEventStore.UnitTests.FSharp

open System
open System.Reactive
open FSharp.Reactive
open Grean.AtomEventStore
open Grean.AtomEventStore.UnitTests.FSharp.XmlRecords
open Grean.AtomEventStore.UnitTests.FSharp.TestDsl
open Ploeh.AutoFixture
open Xunit.Extensions

module XmlEventStreamFacadeTests =

    [<Theory; InMemoryXmlConventions>]
    let SutCorrectlyRoundTripsASingleElement
        (writer : AtomEventObserver<TestEventF>)
        (reader : FifoEvents<TestEventF>)
        (tef : TestEventF) =
        
        writer.AppendAsync(tef).Wait()
        let actual = reader |> Seq.toList

        Verify <@ actual.Length = 1 @>

    [<Theory; InMemoryXmlConventions>]
    let SutCorrectlyRoundTripsMultipleElements
        (writer : AtomEventObserver<TestEventF>)
        (reader : FifoEvents<TestEventF>)
        (g : Generator<TestEventF>) =

        let tefs = g |> Seq.take 3 |> Seq.toList

        tefs |> List.iter(fun tef -> writer.AppendAsync(tef).Wait())
        let actual = reader |> Seq.toList

        let expected = tefs
        Verify <@ expected = actual @>

    [<Theory; InMemoryXmlConventions>]
    let SutCorrectlyRoundTripsDiscriminatedUnions
        (writer : AtomEventObserver<obj>)
        (reader : FifoEvents<obj>)
        (tef : TestEventF)
        (teg : TestEventG) =

        let extract = function
            | F(x) -> x :> obj
            | G(x) -> x :> obj
        let duObs = Observer.Create(extract >> writer.OnNext)
        duObs.OnNext(tef |> F)
        duObs.OnNext(teg |> G)
        
        let infuse (x : obj) =
            match x with
            | :? TestEventF as f -> f |> F
            | :? TestEventG as g -> g |> G
            | _ -> raise(System.ArgumentException("Unknown event type."))
        let duSeq = reader |> Seq.map infuse
        let actual = duSeq |> Seq.toList

        let expected = [tef |> F; teg |> G]
        Verify <@ expected = actual @>

    [<Theory; InMemoryXmlConventions>]
    let SutCorrectlyRoundTripsChangesetOfDiscriminatedUnions
        (writer : AtomEventStream<SerializableChangeset>)
        (reader : AtomEventStream<SerializableChangeset>)
        (tef : TestEventF)<commit_msg>Migrated a test from AtomEventStream<T><commit_after>﻿namespace Grean.AtomEventStore.UnitTests.FSharp

open System
open System.Reactive
open FSharp.Reactive
open Grean.AtomEventStore
open Grean.AtomEventStore.UnitTests.FSharp.XmlRecords
open Grean.AtomEventStore.UnitTests.FSharp.TestDsl
open Ploeh.AutoFixture
open Xunit.Extensions

module XmlEventStreamFacadeTests =

    [<Theory; InMemoryXmlConventions>]
    let SutCorrectlyRoundTripsASingleElement
        (writer : AtomEventObserver<TestEventF>)
        (reader : FifoEvents<TestEventF>)
        (tef : TestEventF) =
        
        writer.AppendAsync(tef).Wait()
        let actual = reader |> Seq.toList

        Verify <@ actual.Length = 1 @>

    [<Theory; InMemoryXmlConventions>]
    let SutCorrectlyRoundTripsMultipleElements
        (writer : AtomEventObserver<TestEventF>)
        (reader : FifoEvents<TestEventF>)
        (g : Generator<TestEventF>) =

        let tefs = g |> Seq.take 3 |> Seq.toList

        tefs |> List.iter(fun tef -> writer.AppendAsync(tef).Wait())
        let actual = reader |> Seq.toList

        let expected = tefs
        Verify <@ expected = actual @>

    [<Theory; InMemoryXmlConventions>]
    let SutCorrectlyRoundTripsDiscriminatedUnions
        (writer : AtomEventObserver<obj>)
        (reader : FifoEvents<obj>)
        (tef : TestEventF)
        (teg : TestEventG) =

        let extract = function
            | F(x) -> x :> obj
            | G(x) -> x :> obj
        let duObs = Observer.Create(extract >> writer.OnNext)
        duObs.OnNext(tef |> F)
        duObs.OnNext(teg |> G)
        
        let infuse (x : obj) =
            match x with
            | :? TestEventF as f -> f |> F
            | :? TestEventG as g -> g |> G
            | _ -> raise(System.ArgumentException("Unknown event type."))
        let duSeq = reader |> Seq.map infuse
        let actual = duSeq |> Seq.toList

        let expected = [tef |> F; teg |> G]
        Verify <@ expected = actual @>

    [<Theory; InMemoryXmlConventions>]
    let SutCorrectlyRoundTripsChangesetOfDiscriminatedUnions
        (writer : AtomEventObserver<SerializableChangeset>)
        (reader : FifoEvents<SerializableChangeset>)
        (tef : TestEventF)
{'fraction_non_alphanumeric': 0.10386091668548204, 'fraction_numerical': 0.0009031384059607134, 'mean_word_length': 2.5298804780876494, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 82, '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': '3433797', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1401, 'n_words': 293}