Skip to content

Commit

Permalink
More test (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
lontivero authored May 7, 2023
1 parent 3060727 commit 162f657
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 39 deletions.
51 changes: 33 additions & 18 deletions Nostra.Tests/RelayIntegrationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ type ``Relay Accept Queries``(output:ITestOutputHelper) =
[<Fact>]
let ``Can receive immediate event subscription`` () =
``start relay`` ()
$ ``as`` Alice
$ given Alice
$ ``connect to relay``
$ ``subscribe to all events``
$ ``as`` Bob
$ given Bob
$ ``connect to relay``
$ ``send event`` (note "hello")
$ ``as`` Alice
$ given Alice
$ ``wait for event`` "All"
|> verify (fun test ->
let alice = test.Users[Alice]
Expand All @@ -31,61 +31,76 @@ type ``Relay Accept Queries``(output:ITestOutputHelper) =
[<Fact>]
let ``Can receive stored event subscription`` () =
``start relay`` ()
$ ``as`` Alice
$ given Alice
$ ``connect to relay``
$ ``send event`` (note "hello")
$ ``as`` Bob
$ given Bob
$ ``connect to relay``
$ ``subscribe to all events``
|> verify (fun test ->
let bob = test.Users[Bob]
let contents = bob.ReceivedEvents |> Seq.map (fun x -> x.Content)
should contain "hello" contents)

type ``Relay Nip09``(output:ITestOutputHelper) =

type ``Relay Nip16``(output:ITestOutputHelper) =

[<Fact>]
let ``Can delete events`` () =
``start relay`` ()
$ ``as`` Alice
$ given Alice
$ ``connect to relay``
$ ``send event`` (note "hello 1")
$ ``send event`` (note "hello 2")
$ ``send event`` (deleteNote ["hello 1"; "hello 2"])
$ ``as`` Bob
$ given Bob
$ ``connect to relay``
$ ``subscribe to`` "all" """{"kinds":[1]}"""
$ ``subscribe to all events``
|> verify (fun test ->
let user = currentUser test
should be Empty user.ReceivedEvents)
let user = currentUser test
should equal 1 user.ReceivedEvents.Count
should equal Kind.Delete user.ReceivedEvents[0].Kind)


type ``Relay Nip16``(output:ITestOutputHelper) =


[<Fact>]
let ``Can Replace events`` () =
let ``Can Replace replaceable events`` () =
``start relay`` ()
$ ``as`` Alice
$ given Alice
$ ``connect to relay``
$ ``send event`` (replaceableNote "replaceable")
$ ``send event`` (replaceableNote "replacement")
$ ``as`` Bob
$ given Bob
$ ``connect to relay``
$ ``subscribe to all events``
|> verify (fun test ->
let user = currentUser test
should equal 1 user.ReceivedEvents.Count
should equal "replacement" user.ReceivedEvents[0].Content)

[<Fact>]
let ``Can send ephemeral events`` () =
``start relay`` ()
$ ``given`` Bob $ ``connect to relay``
$ ``given`` Alice $ ``connect to relay``
$ ``subscribe to`` "Bob's events" (eventsFrom Bob)
$ ``given`` Bob $ ``send event`` (ephemeralNote "hi there!")
$ ``given`` Alice $ ``wait for event`` "Bob's events"
|> verify (fun test ->
let user = currentUser test
should equal 1 user.ReceivedEvents.Count
should equal "hi there!" user.ReceivedEvents[0].Content)

type ``Relay Nip33``(output:ITestOutputHelper) =

[<Fact>]
let ``Can Replace Dtag events`` () =
``start relay`` ()
$ ``as`` Alice
$ given Alice
$ ``connect to relay``
$ ``send event`` (parameterizedNote "replaceable" "dtag")
$ ``send event`` (parameterizedNote "replacement" "dtag")
$ ``as`` Bob
$ given Bob
$ ``connect to relay``
$ ``subscribe to all events``
|> verify (fun test ->
Expand Down
61 changes: 40 additions & 21 deletions Nostra.Tests/TestingFramework.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type TestContext = {
}

type TestStep = TestContext -> Async<TestContext>
type FilterFactory = TestContext -> string
type EventFactory = TestContext -> UnsignedEvent

let ($) prev next = prev |> Async.bind next

let currentUser ctx = ctx.Users[ctx.CurrentUser]
Expand All @@ -40,7 +43,7 @@ let ``start relay`` () = async {
return testContext
}

let ``as`` user : TestStep =
let ``given`` user : TestStep =
fun ctx ->
let alreadyExists, knownUser = ctx.Users.TryGetValue(user)
if not alreadyExists then
Expand Down Expand Up @@ -80,7 +83,7 @@ let ``wait for event`` subscriptionId : TestStep =
return ctx
}

let ``subscribe to`` subscriptionId filter: TestStep =
let ``subscribe to`` subscriptionId (filterFactory: FilterFactory): TestStep =
let rec receiveEvents subscriptionId receiver (events: Event ResizeArray) = async {
let! response = receiver
match response with
Expand All @@ -96,16 +99,28 @@ let ``subscribe to`` subscriptionId filter: TestStep =
let user = currentUser ctx
match user.Connection with
| Some conn ->
do! conn.Sender $"""["REQ","{subscriptionId}",{filter}]"""
do! conn.Sender $"""["REQ","{subscriptionId}",{filterFactory ctx}]"""
do! receiveEvents subscriptionId conn.Receiver user.ReceivedEvents
| None _ ->
failwith $"User '{ctx.CurrentUser}' is not connected."
return ctx
}

let ``subscribe to all events`` : TestStep =
``subscribe to`` "all" "{}"
``subscribe to`` "all" (fun _ -> "{}")

let notes : FilterFactory =
fun ctx -> """{"kinds": [1]}"""

let events : FilterFactory =
fun ctx -> """{}"""

let eventsFrom who : FilterFactory =
fun ctx ->
let user = ctx.Users[who]
let pubkey = user.Secret |> Key.getPubKey |> fun x -> x.ToBytes() |> Utils.toHex
$"""{{"authors": ["{pubkey}"]}}"""

let ``send event`` eventFactory : TestStep =
fun ctx -> async {
let user = currentUser ctx
Expand Down Expand Up @@ -133,26 +148,30 @@ let verify f ctx =
let note content ctx =
Event.createNoteEvent content

let replaceableNote content ctx =
Event.createEvent Kind.ReplaceableStart [] content
let replaceableNote content : EventFactory =
fun ctx -> Event.createEvent Kind.ReplaceableStart [] content

let parameterizedNote content dtag ctx =
Event.createEvent Kind.ParameterizableReplaceableStart [("d", [dtag])] content

let deleteNote evnts (ctx : TestContext) =
let allEvents =
ctx.Users
|> Seq.map (fun x -> x.Key, x.Value)
|> Seq.map (fun (_, u) -> u.SentEvents)
|> Seq.concat
|> Seq.toList
let parameterizedNote content dtag : EventFactory =
fun ctx -> Event.createEvent Kind.ParameterizableReplaceableStart [("d", [dtag])] content

let ids =
allEvents
|> List.filter (fun e -> List.contains (e.Content) evnts)
|> List.map (fun e -> e.Id)
let ephemeralNote content : EventFactory =
fun ctx -> Event.createEvent Kind.EphemeralStart [] content

Event.createDeleteEvent ids "nothing"
let deleteNote evnts : EventFactory =
fun ctx ->
let allEvents =
ctx.Users
|> Seq.map (fun x -> x.Key, x.Value)
|> Seq.map (fun (_, u) -> u.SentEvents)
|> Seq.concat
|> Seq.toList

let ids =
allEvents
|> List.filter (fun e -> List.contains (e.Content) evnts)
|> List.map (fun e -> e.Id)

Event.createDeleteEvent ids "nothing"

[<Literal>]
let Alice = "Alice"
Expand Down

0 comments on commit 162f657

Please sign in to comment.