using System.Net; using System.Net.Http.Headers; using System.Net.Http.Json; using Paca.Server; using Xunit; namespace Paca.Tests.Server; public class WorkspaceRemoteApiTests { [Fact] public async Task GetWorkspace_ReturnsCurrentAndWorkspacesList() { string currentWs = "작업"; var allWs = new List { "작업", "개인", "학습" }; await using var server = await ServerHost.StartAsync(new ServerOptions { Port = 0, Token = "ws-test-token", WorkspaceProvider = () => new WorkspaceInfoDto(currentWs, allWs) }); using var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "ws-test-token"); var response = await client.GetAsync($"http://127.0.0.1:{server.Port}/api/workspace"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var dto = await response.Content.ReadFromJsonAsync(); Assert.NotNull(dto); Assert.Equal("작업", dto.Current); Assert.Equal(3, dto.Workspaces.Count); Assert.Contains("작업", dto.Workspaces); Assert.Contains("개인", dto.Workspaces); Assert.Contains("학습", dto.Workspaces); } [Fact] public async Task PostWorkspaceSwitch_SwitchesWorkspaceAndReturnsOk() { string currentWs = "작업"; string? switchedTo = null; var allWs = new List { "작업", "개인", "학습" }; await using var server = await ServerHost.StartAsync(new ServerOptions { Port = 0, Token = "ws-test-token", WorkspaceProvider = () => new WorkspaceInfoDto(currentWs, allWs), WorkspaceSwitcher = name => { switchedTo = name; currentWs = name; } }); using var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "ws-test-token"); var response = await client.PostAsJsonAsync($"http://127.0.0.1:{server.Port}/api/workspace/switch", new { name = "개인" }); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("개인", switchedTo); Assert.Equal("개인", currentWs); } [Fact] public async Task PostWorkspaceSwitch_RejectsCorruptedName_WithBadRequest() { await using var server = await ServerHost.StartAsync(new ServerOptions { Port = 0, Token = "ws-test-token", WorkspaceProvider = () => new WorkspaceInfoDto("작업", new[] { "작업" }), WorkspaceSwitcher = _ => { } }); using var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "ws-test-token"); // Attempting to switch to a corrupted unicode replacement string var response = await client.PostAsJsonAsync($"http://127.0.0.1:{server.Port}/api/workspace/switch", new { name = "\uFFFD\u06FE\uFFFD" }); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); } }