Skip to content

Commit

Permalink
fix: 对话框标题错误
Browse files Browse the repository at this point in the history
test: 修复问题并添加测试用例
  • Loading branch information
Zaitonn committed Dec 12, 2024
1 parent a24ba39 commit 061e05d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:

- shell: powershell
run: |
Remove-Item ./downloads/* -Exclude *.exe,*.Cli -Recurse
Remove-Item ./downloads/* -Exclude Serein.* -Recurse
- uses: actions/attest-build-provenance@v1
continue-on-error: true
Expand Down
4 changes: 3 additions & 1 deletion src/Serein.Core/Services/Servers/ServerWithPty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ protected override void StartProcess()
}

_isPreparing = true;
WriteInfoLine("正在使用虚拟终端启动服务器进程。若出现问题请尝试关闭虚拟终端功能。");
PtyProvider
.SpawnAsync(
new()
Expand Down Expand Up @@ -108,6 +107,9 @@ protected override void StartProcess()
);

OnServerStatusChanged();
WriteInfoLine(
"正在使用虚拟终端启动服务器进程。若出现问题请尝试关闭虚拟终端功能。"
);
ReadLineLoop();
}
);
Expand Down
65 changes: 61 additions & 4 deletions src/Serein.Tests/Services/JsPlugin/RunTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using Jint.Runtime;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serein.Core.Models.Plugins;
using Serein.Core.Models.Plugins.Js;
using Serein.Core.Services.Plugins;
using Serein.Core.Services.Plugins.Js;
using Serein.Core.Utils;
using Xunit;
Expand All @@ -18,16 +21,23 @@ public sealed class RunTimeTests : IDisposable
{
private readonly IHost _host;
private readonly JsPluginLoader _jsPluginLoader;
private readonly EventDispatcher _eventDispatcher;

public RunTimeTests()
{
_host = HostFactory.BuildNew();
_eventDispatcher = _host.Services.GetRequiredService<EventDispatcher>();
_jsPluginLoader = _host.Services.GetRequiredService<JsPluginLoader>();

Directory.CreateDirectory(PathConstants.PluginsDirectory);
File.WriteAllText(Path.Join(PathConstants.PluginsDirectory, "114514.js"), "");
_jsPluginLoader.JsPlugins.TryAdd(
"test",
new Core.Models.Plugins.Js.JsPlugin(
_host.Services,
new() { Id = "test" },
Path.Join(PathConstants.PluginsDirectory, "114514.js"),
JsPluginConfig.Default
)
);
_host.Start();
Task.Delay(1000).Wait();
}

public void Dispose()
Expand Down Expand Up @@ -100,4 +110,51 @@ public void ShouldBeAbleToOutput()
kv.Value.Engine.Evaluate("serein.console.log('test')");
kv.Value.Engine.Evaluate("console.log('test')");
}

[Fact]
public async Task ShouldBeAbleToSetListener()
{
var kv = _jsPluginLoader.Plugins.First();
kv.Value.Engine.Execute(
"var called = false; serein.setListener('PluginsLoaded', () => { called = true; });"
);
_eventDispatcher.Dispatch(Event.PluginsLoaded);
await Task.Delay(1000);
Assert.True(kv.Value.Engine.Evaluate("called").AsBoolean());

Check failure on line 123 in src/Serein.Tests/Services/JsPlugin/RunTimeTests.cs

View workflow job for this annotation

GitHub Actions / test

Serein.Tests.Services.JsPlugin.RunTimeTests.ShouldBeAbleToSetListener

Assert.True() Failure Expected: True Actual: False
}

[Fact]
public async Task ShouldBeAbleToSetTimeout()
{
var kv = _jsPluginLoader.Plugins.First();
kv.Value.Engine.Execute(
"""
var called = false;
setTimeout(() => { called = true; }, 100);
"""
);

await Task.Delay(1000);
Assert.True(kv.Value.Engine.Evaluate("called").AsBoolean());
}

[Fact]
public async Task ShouldBeAbleToSetInterval()
{
var kv = _jsPluginLoader.Plugins.First();
kv.Value.Engine.Execute(
"""
var count = 0;
var interval = setInterval(() => {
count++;
if (count > 5) {
clearInterval(interval);
}
}, 100);
"""
);

await Task.Delay(1000);
Assert.True(kv.Value.Engine.Evaluate("count").AsNumber() > 5);
}
}
14 changes: 0 additions & 14 deletions src/Serein.Tests/Services/NetPlugin/EventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,6 @@ protected override Task OnServerExited(ServerBase server, int exitcode, DateTime
return Task.CompletedTask;
}
protected override Task<bool> OnServerOutput(ServerBase server, string line)
{
Events.Add(nameof(OnServerOutput));
return Task.FromResult(true);
}
protected override Task<bool> OnServerRawOutput(ServerBase server, string line)
{
Events.Add(nameof(OnServerRawOutput));
return Task.FromResult(true);
}
protected override Task OnServerInput(ServerBase server, string line)
{
Events.Add(nameof(OnServerInput));
Expand Down Expand Up @@ -189,8 +177,6 @@ protected override Task OnServerInput(ServerBase server, string line)
Assert.Contains("OnServerStarted", list);
Assert.Contains("OnServerStopping", list);
Assert.Contains("OnServerExited", list);
Assert.Contains("OnServerOutput", list);
Assert.Contains("OnServerRawOutput", list);
Assert.Contains("OnServerInput", list);
}
}
11 changes: 2 additions & 9 deletions src/Serein.Tests/Services/NetPlugin/LoadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ public override void Dispose() { }
await Task.Delay(100);

Assert.Single(_netPluginLoader.Plugins);

Check failure on line 159 in src/Serein.Tests/Services/NetPlugin/LoadTests.cs

View workflow job for this annotation

GitHub Actions / test

Serein.Tests.Services.NetPlugin.LoadTests.ShouldLoadNetPluginWithValidAssembly

Assert.Single() Failure: The collection was empty
Assert.Equal(
nameof(ShouldLoadNetPluginWithValidAssembly),
_netPluginLoader.Plugins.First().Key
);
Assert.Equal(nameof(PluginBase), _netPluginLoader.Plugins.First().Key);
}

[Fact]
Expand All @@ -179,11 +176,7 @@ public override void Dispose() { }
}
""",
nameof(ShouldLoadNetPluginWithoutSpecifyingEntryFile),
Path.Join(
PathConstants.PluginsDirectory,
"test",
nameof(ShouldLoadNetPluginWithoutSpecifyingEntryFile) + ".dll"
)
Path.Join(PathConstants.PluginsDirectory, "test", nameof(PluginBase) + ".dll")
);

File.WriteAllText(
Expand Down
16 changes: 16 additions & 0 deletions src/Serein.Tests/Services/Server/ConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,20 @@ public async Task ShouldOutputToFile()
Directory.GetFiles(string.Format(PathConstants.ServerLogDirectory, server.Id))
);
}

[Fact]
public void ShouldCreateCommonServer()
{
var server = _serverManager.Add("test", new() { FileName = "cmd", UsePty = false });

Assert.Equal(typeof(Core.Services.Servers.Server), server.GetType());
}

[Fact]
public void ShouldCreateServerWithPty()
{
var server = _serverManager.Add("test", new() { FileName = "cmd", UsePty = true });

Assert.Equal(typeof(ServerWithPty), server.GetType());
}
}
31 changes: 31 additions & 0 deletions src/Serein.Tests/Static/OutputFilterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Serein.Core.Utils;
using Xunit;

namespace Serein.Tests.Static;

public static class OutputFilterTests
{
[Theory]
[InlineData("\u001b[31mHello\u001b[0m", "Hello")]
[InlineData("\u001b[38;2;114;111;23mHello\u001b[0m", "Hello")]
[InlineData("\u001b[?25lHello\u001b[?25h", "Hello")]
[InlineData("\u001b[KHello", "Hello")]
[InlineData("\u001b[0KHello", "Hello")]
[InlineData("\u001b[0JHello", "Hello")]
[InlineData("\u001b[=0HHello", "Hello")]
public static void ShouldRemoveANSIEscapeSequences(string input, string expected)
{
Assert.Equal(expected, OutputFilter.RemoveANSIEscapeChars(input));
}

[Fact]
public static void ShouldRemoveControlChars()
{
Assert.Empty(OutputFilter.RemoveControlChars("\x10"));
Assert.Empty(OutputFilter.RemoveControlChars("\x11"));
Assert.Empty(OutputFilter.RemoveControlChars("\x12"));
Assert.Empty(OutputFilter.RemoveControlChars("\x13"));
Assert.Empty(OutputFilter.RemoveControlChars("\x14"));
Assert.Empty(OutputFilter.RemoveControlChars("\x15"));
}
}
5 changes: 3 additions & 2 deletions src/Shared/Utils/DialogFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Serein.Core;
using Serein.Core.Utils;
using Serein.Core.Utils.Extensions;
#if LITE
Expand Down Expand Up @@ -41,7 +42,7 @@ public static void ShowWelcomeDialog()
+ $"Copyright © 2022 <a href=\"{UrlConstants.Author}\">Zaitonn</a>. All Rights Reserved.",
FooterIcon = TaskDialogIcon.Information,
MainInstruction = "欢迎使用Serein!!",
WindowTitle = "Serein.Lite",
WindowTitle = "Serein." + SereinApp.Type,
ButtonStyle = TaskDialogButtonStyle.CommandLinks,
};

Expand Down Expand Up @@ -87,7 +88,7 @@ public static void ShowErrorDialog(Exception e)
ExpandedInformation = e.StackTrace,
MainIcon = TaskDialogIcon.Error,
MainInstruction = "唔……崩溃了(っ °Д °;)っ",
WindowTitle = "Serein.Lite",
WindowTitle = "Serein." + SereinApp.Type,
ButtonStyle = TaskDialogButtonStyle.CommandLinks,
};

Expand Down

0 comments on commit 061e05d

Please sign in to comment.