forked from jupjohn/MiniTwitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
603 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Test (Helix) | ||
|
||
on: | ||
push: | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
jobs: | ||
|
||
build: | ||
|
||
runs-on: windows-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Install .NET | ||
uses: actions/setup-dotnet@v3 | ||
with: | ||
dotnet-version: 8.0.x | ||
|
||
- name: Run tests | ||
run: | | ||
cd .\MiniTwitch.Helix.Test\ | ||
dotnet test | ||
105 changes: 105 additions & 0 deletions
105
MiniTwitch.Helix.Test/Converters/ConduitTransportConverterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System.Text.Json; | ||
using MiniTwitch.Helix.Internal; | ||
using MiniTwitch.Helix.Internal.Json; | ||
using MiniTwitch.Helix.Responses; | ||
|
||
namespace MiniTwitch.Helix.Test.Converters; | ||
|
||
public class ConduitTransportConverterTest | ||
{ | ||
static JsonSerializerOptions options = HelixApiClient.SerializerOptions; | ||
|
||
[Fact] | ||
public void Convert_Webhook() | ||
{ | ||
string json = """ | ||
{ | ||
"id": "0", | ||
"status": "enabled", | ||
"transport": { | ||
"method": "webhook", | ||
"callback": "https://this-is-a-callback.com" | ||
} | ||
} | ||
"""; | ||
|
||
var element = JsonDocument.Parse(json).RootElement.GetProperty("transport"); | ||
var res = ConduitTransportConverter.ReadTransport(element, options); | ||
Assert.NotNull(res); | ||
var t = Assert.IsType<ConduitTransport.Webhook>(res); | ||
Assert.Equal("https://this-is-a-callback.com", t.Callback); | ||
} | ||
|
||
[Fact] | ||
public void Convert_WebSocket_Alive() | ||
{ | ||
string json = """ | ||
{ | ||
"id": "2", | ||
"status": "enabled", | ||
"transport": { | ||
"method": "websocket", | ||
"session_id": "9fd5164a-a958-4c60-b7f4-6a7202506ca0", | ||
"connected_at": "2020-11-10T14:32:18.730260295Z" | ||
} | ||
} | ||
"""; | ||
|
||
var element = JsonDocument.Parse(json).RootElement.GetProperty("transport"); | ||
var res = ConduitTransportConverter.ReadTransport(element, options); | ||
Assert.NotNull(res); | ||
var t = Assert.IsType<ConduitTransport.WebSocket>(res); | ||
Assert.Equal("9fd5164a-a958-4c60-b7f4-6a7202506ca0", t.SessionId); | ||
// DateTime parsing floors 730260295 -> 7302602 | ||
Assert.Equal(DateTime.Parse("2020-11-10T14:32:18.7302602Z").ToUniversalTime(), t.ConnectedAt); | ||
Assert.Null(t.DisconnectedAt); | ||
} | ||
|
||
[Fact] | ||
public void Convert_WebSocket_Dead() | ||
{ | ||
string json = """ | ||
{ | ||
"id": "4", | ||
"status": "websocket_disconnected", | ||
"transport": { | ||
"method": "websocket", | ||
"session_id": "ad1c9fc3-0d99-4eb7-8a04-8608e8ff9ec9", | ||
"connected_at": "2020-11-10T14:32:18.730260295Z", | ||
"disconnected_at": "2020-11-11T14:32:18.730260295Z" | ||
} | ||
} | ||
"""; | ||
|
||
var element = JsonDocument.Parse(json).RootElement.GetProperty("transport"); | ||
var res = ConduitTransportConverter.ReadTransport(element, options); | ||
Assert.NotNull(res); | ||
var t = Assert.IsType<ConduitTransport.WebSocket>(res); | ||
Assert.Equal("ad1c9fc3-0d99-4eb7-8a04-8608e8ff9ec9", t.SessionId); | ||
// DateTime parsing floors 730260295 -> 7302602 | ||
Assert.Equal(DateTime.Parse("2020-11-10T14:32:18.7302602Z").ToUniversalTime(), t.ConnectedAt); | ||
Assert.Equal(DateTime.Parse("2020-11-11T14:32:18.7302602Z").ToUniversalTime(), t.DisconnectedAt); | ||
} | ||
|
||
[Fact] | ||
public void Convert_Invalid() | ||
{ | ||
string json = """ | ||
{ | ||
"id": "2", | ||
"status": "enabled", | ||
"transport": { | ||
"method": "melon095", | ||
"session_id": "9fd5164a-a958-4c60-b7f4-6a7202506ca0", | ||
"connected_at": "2020-11-10T14:32:18.730260295Z" | ||
} | ||
} | ||
"""; | ||
|
||
var element = JsonDocument.Parse(json).RootElement.GetProperty("transport"); | ||
var ex = Assert.Throws<JsonException>(() => | ||
{ | ||
var res = ConduitTransportConverter.ReadTransport(element, options); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using MiniTwitch.Helix.Internal.Json; | ||
|
||
namespace MiniTwitch.Helix.Test.Converters; | ||
|
||
public class EnumConverterTest | ||
{ | ||
[Fact] | ||
public void Convert() | ||
{ | ||
var e = EnumConverter<TestEnum>.ReadEnum("Forsaan"); | ||
Assert.Equal(TestEnum.Forsaan, e); | ||
} | ||
|
||
[Fact] | ||
public void Convert_From_SnakeCase() | ||
{ | ||
var e = EnumConverter<TestEnum>.ReadEnum("i_eat_rocks"); | ||
Assert.Equal(TestEnum.IEatRocks, e); | ||
} | ||
|
||
[Fact] | ||
public void Convert_From_Null() | ||
{ | ||
var e = EnumConverter<TestEnum>.ReadEnum(null); | ||
Assert.Equal(default(TestEnum), e); | ||
} | ||
|
||
[Fact] | ||
public void Convert_From_Undefined() | ||
{ | ||
var e = EnumConverter<TestEnum>.ReadEnum("my life is doge"); | ||
Assert.Equal(default(TestEnum), e); | ||
} | ||
|
||
enum TestEnum | ||
{ | ||
Unknown, | ||
Forshon, | ||
Forsen, | ||
Forsaan, | ||
IEatRocks, | ||
XD | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Text; | ||
using MiniTwitch.Helix.Internal.Json; | ||
|
||
namespace MiniTwitch.Helix.Test.Converters; | ||
|
||
public class IntConverterTest | ||
{ | ||
[Fact] | ||
public void Read_Int() | ||
{ | ||
var r = IntConverter.ReadInt(Encoding.UTF8.GetBytes("12345")); | ||
Assert.Equal(12345, r); | ||
} | ||
|
||
[Fact] | ||
public void Read_Long() | ||
{ | ||
var r = IntConverter.ReadInt(Encoding.UTF8.GetBytes("8223372036854780000")); | ||
Assert.Equal(0, r); | ||
} | ||
|
||
[Fact] | ||
public void Read_Empty() | ||
{ | ||
var r = IntConverter.ReadInt(Span<byte>.Empty); | ||
Assert.Equal(0, r); | ||
} | ||
|
||
[Fact] | ||
public void Read_Null() | ||
{ | ||
var r2 = IntConverter.ReadInt(Encoding.UTF8.GetBytes("null")); | ||
Assert.Equal(0, r2); | ||
} | ||
|
||
[Fact] | ||
public void Read_Undefined() | ||
{ | ||
var r2 = IntConverter.ReadInt(Encoding.UTF8.GetBytes("forsen")); | ||
Assert.Equal(0, r2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Text; | ||
using MiniTwitch.Helix.Internal.Json; | ||
|
||
namespace MiniTwitch.Helix.Test.Converters; | ||
|
||
public class LongConverterTest | ||
{ | ||
[Fact] | ||
public void Read_Int() | ||
{ | ||
var r = LongConverter.ReadLong(Encoding.UTF8.GetBytes("12345")); | ||
Assert.Equal(12345, r); | ||
} | ||
|
||
[Fact] | ||
public void Read_Long() | ||
{ | ||
var r = LongConverter.ReadLong(Encoding.UTF8.GetBytes("8223372036854780000")); | ||
Assert.Equal(8223372036854780000, r); | ||
} | ||
|
||
[Fact] | ||
public void Read_Empty() | ||
{ | ||
var r = LongConverter.ReadLong(Span<byte>.Empty); | ||
Assert.Equal(0, r); | ||
} | ||
|
||
[Fact] | ||
public void Read_Null() | ||
{ | ||
var r2 = LongConverter.ReadLong(Encoding.UTF8.GetBytes("null")); | ||
Assert.Equal(0, r2); | ||
} | ||
|
||
[Fact] | ||
public void Read_Undefined() | ||
{ | ||
var r2 = LongConverter.ReadLong(Encoding.UTF8.GetBytes("forsen")); | ||
Assert.Equal(0, r2); | ||
} | ||
} |
Oops, something went wrong.