Files
ClawdDotNet/tests/ClawdDotNet.Web.Tests/InstanceSocketTests.cs
T
RichardandClaude Opus 5.5 837a634a22 Stufe 4c (#764): Kommentare in C#, WebUI-Skripten, CSS und HTML auf Englisch
Rund 2.600 Kommentarblöcke in 310 C#-Dateien übersetzt, Begründungen vollständig
erhalten: blockweise gleiche Anzahl wie vorher, kein Block unter 60 % seiner
Länge, der Code außerhalb der Kommentare per Tokenizer byte-identisch geprüft.
Zitate deutscher Oberflächentexte und Testdaten bleiben deutsch.

WebUI: Kommentare in JS/CSS/HTML übersetzt, JSDoc-@param an die in 4b
umbenannten Parameter angeglichen.

Dabei behoben:
- 4b hatte "tag" (HTML-Tag) als deutsches "Tag" gelesen und zu "dayValue"
  gemacht (makeElement, svg, Tagesübersicht) - zurück auf tag/day.
- Aufgaben "InProgress" bekamen die Plakettenart "laufend", für die es nie eine
  CSS-Klasse gab - jetzt "success" wie laufende Instanzen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 12:45:03 +02:00

274 lines
9.8 KiB
C#

using ClawdDotNet.App;
using ClawdDotNet.App.Services;
using ClawdDotNet.App.Settings;
using ClawdDotNet.Core.Config;
using ClawdDotNet.Core.Instances;
using ClawdDotNet.Core.Tools;
using ClawdDotNet.Web.Endpoints;
using ClawdDotNet.Web.Tests.Infrastructure;
using Microsoft.Extensions.Logging.Abstractions;
using Shouldly;
namespace ClawdDotNet.Web.Tests;
/// <summary>
/// The socket in the instance directory (#528, stage 2 / E-M1).
///
/// <para>The instance manager will reach the instance through it. This test records three
/// promises: it is <b>always</b> there, even without a web UI. It needs
/// <b>no token</b> — its boundary is the file system. And the TCP port still
/// needs one, because there is none there.</para>
///
/// <para>The last point is the one where a mix-up would be expensive: whoever wanted to
/// guess the distinction from missing IP addresses would, in case of doubt, let a
/// TCP request through without a token. It therefore comes from a marker that the
/// socket listener sets itself.</para>
/// </summary>
public sealed class InstanceSocketTests : IDisposable
{
private readonly string _root = Path.Combine(
Path.GetTempPath(), "clawd-tests", "sock-" + Guid.NewGuid().ToString("N")[..8]);
private const string Token = "ein-hinreichend-langes-testtoken";
public void Dispose()
{
try { Directory.Delete(_root, recursive: true); }
catch { /* A leftover test folder does not turn a run red. */ }
}
private AppHost NeuerHost(bool mitWeboberflaeche, int port = 0)
{
var instancesDir = Path.Combine(_root, "Instances");
var instancePath = Path.Combine(instancesDir, "Test");
Directory.CreateDirectory(instancePath);
return new AppHost
{
Settings = new SettingsManager(_root),
Directories = new InstanceDirectoryManager(instancesDir),
Instance = new InstanceConfig
{
InstanceName = "Test",
WorkingDirectory = instancePath,
WebUiEnabled = mitWeboberflaeche,
WebUiBinding = "loopback",
WebUiToken = mitWeboberflaeche ? Token : "",
WebServerPort = port
},
InstancePath = instancePath,
LogDirectory = Path.Combine(instancePath, "Logs"),
LoggerFactory = NullLoggerFactory.Instance,
Tools = new ToolRegistry()
};
}
private static int FreierPort()
{
using var probe = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
probe.Start();
var port = ((System.Net.IPEndPoint)probe.LocalEndpoint).Port;
probe.Stop();
return port;
}
// ─── The socket holds ───
[Fact]
public async Task Der_Socket_antwortet_auch_ohne_Weboberflaeche()
{
// The default: webUiEnabled is off. Formerly nothing ran at all then, and the
// instance was not addressable from outside.
await using var host = NeuerHost(mitWeboberflaeche: false);
await using var web = InstanceHttpHost.TryStart(host, NullLoggerFactory.Instance);
web.ShouldNotBeNull();
web!.Url.ShouldBeNull();
using var client = InstanceEndpoint.CreateClient(host.InstancePath);
var antwort = await client.GetAsync("/api/info");
antwort.EnsureSuccessStatusCode();
(await antwort.Content.ReadAsStringAsync()).ShouldContain("Test");
}
[Fact]
public async Task Ueber_den_Socket_braucht_es_kein_Token()
{
await using var host = NeuerHost(mitWeboberflaeche: true, port: FreierPort());
await using var web = InstanceHttpHost.TryStart(host, NullLoggerFactory.Instance);
web.ShouldNotBeNull();
// No Authorization header, on purpose.
using var client = InstanceEndpoint.CreateClient(host.InstancePath);
(await client.GetAsync("/api/info")).EnsureSuccessStatusCode();
}
// ─── The port stays guarded ───
[Fact]
public async Task Ueber_TCP_gilt_die_Token_Pflicht_weiter()
{
var port = FreierPort();
await using var host = NeuerHost(mitWeboberflaeche: true, port: port);
await using var web = InstanceHttpHost.TryStart(host, NullLoggerFactory.Instance);
web.ShouldNotBeNull();
web!.Url.ShouldNotBeNull();
using var client = new HttpClient { BaseAddress = new Uri($"http://127.0.0.1:{port}") };
(await client.GetAsync("/api/info")).StatusCode
.ShouldBe(System.Net.HttpStatusCode.Unauthorized);
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {Token}");
(await client.GetAsync("/api/info")).EnsureSuccessStatusCode();
}
// ─── Addresses ───
[Fact]
public void Zwei_Instanzen_bekommen_zwei_Adressen()
{
var a = Path.Combine(_root, "Instances", "Eins");
var b = Path.Combine(_root, "Instances", "Zwei");
InstanceEndpoint.Describe(a).ShouldNotBe(InstanceEndpoint.Describe(b));
InstanceEndpoint.PipeName(a).ShouldNotBe(InstanceEndpoint.PipeName(b));
}
[Fact]
public void Dieselbe_Instanz_bekommt_dieselbe_Adresse()
{
// The manager computes the address from the path it knows itself — so it
// must come out the same from every spelling of the same directory.
var a = Path.Combine(_root, "Instances", "Test");
var b = Path.Combine(_root, "Instances", "Unterwegs", "..", "Test");
InstanceEndpoint.PipeName(a).ShouldBe(InstanceEndpoint.PipeName(b));
InstanceEndpoint.SocketPath(a).ShouldBe(InstanceEndpoint.SocketPath(b));
}
// ─── Cleaning up ───
/// <summary>
/// A leftover socket file must not prevent the next start.
///
/// <para>A Unix domain socket survives the process that created it. After
/// a hard end — exactly the case from #529 — the next start would get
/// <c>EADDRINUSE</c> from a process that no longer exists.</para>
/// </summary>
[SocketFileFact]
public async Task Eine_liegengebliebene_Socketdatei_haelt_den_Start_nicht_auf()
{
await using var host = NeuerHost(mitWeboberflaeche: false);
var leiche = InstanceEndpoint.SocketPath(host.InstancePath);
File.WriteAllText(leiche, "Rest eines abgestuerzten Prozesses");
await using var web = InstanceHttpHost.TryStart(host, NullLoggerFactory.Instance);
web.ShouldNotBeNull();
using var client = InstanceEndpoint.CreateClient(host.InstancePath);
(await client.GetAsync("/api/info")).EnsureSuccessStatusCode();
}
[SocketFileFact]
public async Task Beim_Beenden_bleibt_keine_Socketdatei_liegen()
{
await using var host = NeuerHost(mitWeboberflaeche: false);
var web = InstanceHttpHost.TryStart(host, NullLoggerFactory.Instance);
web.ShouldNotBeNull();
await web!.DisposeAsync();
File.Exists(InstanceEndpoint.SocketPath(host.InstancePath)).ShouldBeFalse();
}
}
/// <summary>
/// Control of the instance process: end it and bring its window to the front
/// (#528, stages 3 and 5).
///
/// <para><b>Both exist exclusively via the socket.</b> Over TCP the
/// endpoints do not exist — not even with a valid token, and there they answer with 404 instead of
/// 403: what does not exist need not be explained. The lifetime of an instance
/// belongs to the instance manager; a browser session should be able neither to end the process
/// serving it nor to tear open its window.</para>
/// </summary>
public sealed class InstanceControlTests : IDisposable
{
private readonly string _root = Path.Combine(
Path.GetTempPath(), "clawd-tests", "ctl-" + Guid.NewGuid().ToString("N")[..8]);
private const string Token = "ein-hinreichend-langes-testtoken";
public void Dispose()
{
try { Directory.Delete(_root, recursive: true); }
catch { /* A leftover test folder does not turn a run red. */ }
}
private AppHost NeuerHost(int port)
{
var instancesDir = Path.Combine(_root, "Instances");
var instancePath = Path.Combine(instancesDir, "Test");
Directory.CreateDirectory(instancePath);
return new AppHost
{
Settings = new SettingsManager(_root),
Directories = new InstanceDirectoryManager(instancesDir),
Instance = new InstanceConfig
{
InstanceName = "Test",
WorkingDirectory = instancePath,
WebUiEnabled = true,
WebUiBinding = "loopback",
WebUiToken = Token,
WebServerPort = port
},
InstancePath = instancePath,
LogDirectory = Path.Combine(instancePath, "Logs"),
LoggerFactory = NullLoggerFactory.Instance,
Tools = new ToolRegistry()
};
}
private static int FreierPort()
{
using var probe = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0);
probe.Start();
var port = ((System.Net.IPEndPoint)probe.LocalEndpoint).Port;
probe.Stop();
return port;
}
[Fact]
public async Task Ueber_TCP_gibt_es_die_Steuerung_nicht()
{
var port = FreierPort();
await using var host = NeuerHost(port);
await using var web = InstanceHttpHost.TryStart(host, NullLoggerFactory.Instance);
web.ShouldNotBeNull();
web!.Url.ShouldNotBeNull();
var gebeten = false;
web.Control.ShutdownRequested += () => gebeten = true;
using var client = new HttpClient { BaseAddress = new Uri($"http://127.0.0.1:{port}") };
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {Token}");
// With a valid token - and still not present.
(await client.PostAsync("/api/instanz/beenden", content: null)).StatusCode
.ShouldBe(System.Net.HttpStatusCode.NotFound);
gebeten.ShouldBeFalse();
}
}