using System.ComponentModel;
using Agent.Service;
using Shared.Infrastructure.Constants;
using Xunit;
namespace Agent.Tests;
///
/// The agent service's registration (the CLI's install, which the MSI runs too) through
/// the service-control seam: plain Automatic, never Delayed Start; and an earlier Delayed Start
/// switched when the agent starts.
///
public sealed class ServiceStartTypeTests
{
private const uint AutoStart = 0x2;
private const uint DemandStart = 0x3;
private sealed class FakeServiceConfigApi : IServiceConfigApi
{
private static readonly IntPtr Scm = new(1);
private static readonly IntPtr Service = new(2);
public bool Exists { get; set; }
public uint StartType { get; set; } = AutoStart;
public bool Delayed { get; set; }
public List Calls { get; } = [];
public IntPtr OpenSCManager(uint access) => Scm;
public IntPtr OpenService(IntPtr scm, string name, uint access)
{
Assert.Equal(Branding.AgentServiceName, name);
return Exists ? Service : IntPtr.Zero;
}
public IntPtr CreateService(IntPtr scm, string name, string displayName, uint startType, string binaryPath)
{
Calls.Add($"create start={startType}");
StartType = startType;
Exists = true;
return Service;
}
public bool ChangeServiceConfig(IntPtr service, uint startType, string binaryPath, string displayName)
{
Calls.Add($"change start={startType}");
StartType = startType;
return true;
}
public bool SetDescription(IntPtr service, string description) => true;
public bool SetDelayedAutoStart(IntPtr service, bool delayed)
{
Calls.Add($"delayed={delayed}");
Delayed = delayed;
return true;
}
public bool SetFailureActions(IntPtr service, TimeSpan restartDelay, int restarts, TimeSpan resetPeriod)
{
Calls.Add($"recovery {restarts}x{restartDelay.TotalSeconds:F0}s");
return true;
}
public uint? QueryStartType(IntPtr service) => StartType;
public bool? QueryDelayedAutoStart(IntPtr service) => Delayed;
public void Close(IntPtr handle)
{
}
public Win32Exception LastError(string operation) => new(5, operation);
}
[Fact]
public void ANewInstallIsPlainAutomatic()
{
var api = new FakeServiceConfigApi();
Assert.Equal(0, ServiceLifecycle.Install(api, "\"C:\\Program Files\\ExecuteR\\ExecuteR-Agent.exe\""));
Assert.Equal(["create start=2", "delayed=False", "recovery 3x30s"], api.Calls);
Assert.Equal((AutoStart, false), (api.StartType, api.Delayed));
}
[Fact]
public void InstallingOverAnEarlierDelayedStartSwitchesIt()
{
var api = new FakeServiceConfigApi { Exists = true, StartType = AutoStart, Delayed = true };
ServiceLifecycle.Install(api, "\"agent.exe\"");
Assert.Equal(["change start=2", "delayed=False", "recovery 3x30s"], api.Calls);
Assert.False(api.Delayed);
}
[Fact]
public void AtStartADelayedStartBecomesAutomatic()
{
var api = new FakeServiceConfigApi { Exists = true, StartType = AutoStart, Delayed = true };
Assert.True(ServiceLifecycle.EnsureAutomaticStart(api));
Assert.Equal(["delayed=False"], api.Calls);
}
[Theory]
[InlineData(AutoStart, false)] // already plain Automatic
[InlineData(DemandStart, true)] // an administrator's Manual stays
public void AtStartAnythingElseIsLeftAlone(uint startType, bool delayed)
{
var api = new FakeServiceConfigApi { Exists = true, StartType = startType, Delayed = delayed };
Assert.False(ServiceLifecycle.EnsureAutomaticStart(api));
Assert.Empty(api.Calls);
}
}