借助网络配置,控制Modbus工具的日志显示,避免卡死界面
智能大石头 authored at 2021-12-11 22:09:19
4.12 KiB
XCoder
using System;
using System.ComponentModel;
using System.IO;
using NewLife.XCoder.Services;
using NewLife.XCoder.Tools.Voice;
using NewLife.XCoder.ViewModels;
using Xunit;

namespace XCoder.App.Tests;

/// <summary>语音助手:命令构建、命令行拆分与文件定位</summary>
public class SpeechViewModelTests
{
    [Fact]
    [DisplayName("Windows 导出音频时命令写入 WAV 文件")]
    public void Build_WindowsOutputFile()
    {
        var command = SpeechCommandBuilder.Build(true, false, "测试", @"C:\Temp\a.wav");

        Assert.Contains("SetOutputToWaveFile", command);
        Assert.Contains(@"C:\Temp\a.wav", command);
        Assert.Contains("$s.Dispose()", command);
    }

    [Fact]
    [DisplayName("Windows 语速与音量写入命令,默认值不写多余设置")]
    public void Build_WindowsRateAndVolume()
    {
        var custom = SpeechCommandBuilder.Build(true, false, "测试", null, 5, 60);
        var defaults = SpeechCommandBuilder.Build(true, false, "测试");

        Assert.Contains("$s.Rate=5", custom);
        Assert.Contains("$s.Volume=60", custom);
        Assert.DoesNotContain("$s.Rate", defaults);
        Assert.DoesNotContain("$s.Volume", defaults);
    }

    [Fact]
    [DisplayName("语速超出范围时被夹到 -10~10")]
    public void Build_RateClamped()
    {
        var command = SpeechCommandBuilder.Build(true, false, "测试", null, 99);

        Assert.Contains("$s.Rate=10", command);
    }

    [Fact]
    [DisplayName("指定音色时三平台命令都带上音色参数")]
    public void Build_WithVoice()
    {
        Assert.Contains("$s.SelectVoice('Huihui')", SpeechCommandBuilder.Build(true, false, "测试", null, 0, 100, "Huihui"));
        Assert.Contains("say -v \"Tingting\"", SpeechCommandBuilder.Build(false, true, "测试", null, 0, 100, "Tingting"));
        Assert.Contains("espeak -v cmn", SpeechCommandBuilder.Build(false, false, "测试", null, 0, 100, "cmn"));

        // 不指定音色时用平台默认(Windows 不选音色,Linux 默认中文)
        Assert.DoesNotContain("SelectVoice", SpeechCommandBuilder.Build(true, false, "测试"));
        Assert.Contains("espeak -v zh", SpeechCommandBuilder.Build(false, false, "测试"));
    }

    [Fact]
    [DisplayName("输出文件路径中的单引号被转义")]
    public void Build_OutputPathEscaped()
    {
        var command = SpeechCommandBuilder.Build(true, false, "测试", "/tmp/it's.wav");

        Assert.Contains("it''s.wav", command);
    }

    [Theory]
    [DisplayName("命令行按首个空格拆出可执行文件与参数")]
    [InlineData("espeak -v zh 你好", "espeak", "-v zh 你好")]
    [InlineData("say -v ?", "say", "-v ?")]
    [InlineData("notepad", "notepad", "")]
    public void SplitCommand_Unquoted(String command, String file, String args)
    {
        var (fileName, arguments) = ProcessRunner.SplitCommand(command);

        Assert.Equal(file, fileName);
        Assert.Equal(args, arguments);
    }

    [Fact]
    [DisplayName("带引号的可执行文件路径能正确拆出")]
    public void SplitCommand_Quoted()
    {
        var (fileName, args) = ProcessRunner.SplitCommand("\"C:\\Program Files\\TTS\\speak.exe\" -t hello");

        Assert.Equal(@"C:\Program Files\TTS\speak.exe", fileName);
        Assert.Equal("-t hello", args);
    }

    [Fact]
    [DisplayName("空命令抛参数错误")]
    public void SplitCommand_Empty_Throws()
    {
        Assert.Throws<ArgumentException>(() => ProcessRunner.SplitCommand(""));
        Assert.Throws<ArgumentException>(() => ProcessRunner.SplitCommand("   "));
    }

    [Fact]
    [DisplayName("文件存在时定位成功,不存在或空路径返回 false")]
    public void Reveal_ChecksFile()
    {
        Assert.False(TextLogExporter.Reveal(null));
        Assert.False(TextLogExporter.Reveal(Path.Combine(Path.GetTempPath(), "no-such-file-" + Guid.NewGuid().ToString("N") + ".txt")));
    }

    [Fact]
    [DisplayName("打开不存在的目录返回 false 而不抛异常")]
    public void OpenPath_Invalid_ReturnsFalse()
    {
        Assert.False(TextLogExporter.OpenPath(null));
        Assert.False(TextLogExporter.OpenPath(""));
    }
}