using System.Net; using System.Net.Http.Json; namespace WordVersioning.Api.IntegrationTests; /// setup ที่ใช้ร่วมกันหลาย test class: สร้าง template + ตั้ง field definitions ครบชุด /// (employee_name = required text, position = text, salary = number, start_date = date) public static class TestFlow { public static readonly Dictionary DefaultValues = new() { ["employee_name"] = "สมหญิง รักงาน", ["position"] = "วิศวกรซอฟต์แวร์", ["salary"] = "75000", ["start_date"] = "2026-08-01", }; public static async Task<(Guid TemplateId, List Fields)> CreateConfiguredTemplateAsync( HttpClient client, string baseName) { // create (detect defaults) using var create = await client.PostAsync( "api/templates", ApiExtensions.TemplateForm(TestDocx.BuildTemplateDocx(), $"{baseName} {Guid.NewGuid():N}")); create.EnsureSuccessStatusCode(); var created = await create.Content.ReadFromJsonAsync(IntegrationTestFixture.Json); // ตั้ง label/type/required ให้ครบทุกรูปแบบ (มี required, number, date) using var put = await client.PutAsJsonAsync($"api/templates/{created!.Id}", new { fields = created.Fields.Select(f => new { key = f.Key, label = LabelOf(f.Key), type = TypeOf(f.Key), isRequired = f.Key == "employee_name", sortOrder = f.SortOrder, }), }, IntegrationTestFixture.Json); put.EnsureSuccessStatusCode(); var detail = await put.Content.ReadFromJsonAsync(IntegrationTestFixture.Json); return (created.Id, detail!.Fields); } /// generate เอกสาร v1 จาก template ที่ตั้งค่าแล้ว — คืน response ของ generate public static Task GenerateDocumentAsync(HttpClient client, Guid templateId) => GenerateDocumentAsync(client, templateId, DefaultValues); /// generate พร้อมค่าที่กำหนดเอง (ทดสอบ validation หรือค่าเกินนิยาม) /// + commit message ทางเลือก (FR-13) public static async Task GenerateDocumentAsync( HttpClient client, Guid templateId, Dictionary values, string? message = null) { using var response = await client.PostAsJsonAsync( "api/documents/generate", new { templateId, values, message }, IntegrationTestFixture.Json); Assert.Equal(HttpStatusCode.Created, response.StatusCode); return (await response.Content.ReadFromJsonAsync(IntegrationTestFixture.Json))!; } public static string LabelOf(string key) => key switch { "employee_name" => "ชื่อพนักงาน", "position" => "ตำแหน่ง", "salary" => "เงินเดือน", "start_date" => "วันเริ่มงาน", _ => key, }; public static string TypeOf(string key) => key switch { "salary" => "number", "start_date" => "date", _ => "text", }; }