77 lines
2.5 KiB
C#
77 lines
2.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using GsDChat.Client.State;
|
|
using Xunit;
|
|
|
|
namespace GsDChat.Tests;
|
|
|
|
public class ServerHistoryTests : IDisposable
|
|
{
|
|
private readonly string _testFile;
|
|
|
|
public ServerHistoryTests()
|
|
{
|
|
_testFile = Path.Combine(Path.GetTempPath(), "gsdchat_servers_" + Guid.NewGuid().ToString("N") + ".json");
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(_testFile))
|
|
{
|
|
File.Delete(_testFile);
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("localhost:5000", "http://localhost:5000")]
|
|
[InlineData("http://localhost:5000/", "http://localhost:5000")]
|
|
[InlineData("https://chat.example.com", "https://chat.example.com")]
|
|
[InlineData("chat.example.com:443", "https://chat.example.com:443")]
|
|
[InlineData("192.168.1.100:8080", "http://192.168.1.100:8080")]
|
|
public void NormalizeUrl_ShouldFormatProperly(string input, string expected)
|
|
{
|
|
string result = ServerHistoryManager.NormalizeUrl(input);
|
|
Assert.Equal(expected, result);
|
|
}
|
|
|
|
[Fact]
|
|
public void SaveServer_ShouldPersistAndOrderDescending()
|
|
{
|
|
var manager = new ServerHistoryManager(_testFile);
|
|
|
|
manager.SaveServer("http://server1.local:5000", "Server 1", "User1");
|
|
manager.SaveServer("http://server2.local:5000", "Server 2", "User2");
|
|
|
|
var list = manager.LoadServers();
|
|
Assert.Equal(2, list.Count);
|
|
Assert.Equal("http://server2.local:5000", list[0].Url);
|
|
Assert.Equal("http://server1.local:5000", list[1].Url);
|
|
|
|
// Updating server1 should bump it to the top
|
|
manager.SaveServer("http://server1.local:5000", "Server 1 Updated", "User1New");
|
|
var updatedList = manager.LoadServers();
|
|
Assert.Equal(2, updatedList.Count);
|
|
Assert.Equal("http://server1.local:5000", updatedList[0].Url);
|
|
Assert.Equal("Server 1 Updated", updatedList[0].Name);
|
|
Assert.Equal("User1New", updatedList[0].LastUsername);
|
|
}
|
|
|
|
[Fact]
|
|
public void RemoveServer_ShouldDeleteEntry()
|
|
{
|
|
var manager = new ServerHistoryManager(_testFile);
|
|
|
|
manager.SaveServer("http://server1.local:5000", "Server 1", "User1");
|
|
manager.SaveServer("http://server2.local:5000", "Server 2", "User2");
|
|
|
|
manager.RemoveServer("http://server1.local:5000");
|
|
|
|
var list = manager.LoadServers();
|
|
Assert.Single(list);
|
|
Assert.Equal("http://server2.local:5000", list[0].Url);
|
|
}
|
|
}
|