eCourt-Doc-Edit/backend/WordVersioning.Api.IntegrationTests/TestDtos.cs
Suphonchai Phoonsawat b3c1c667f7 Phase 12: draft generation (FR-16)
ไฟล์ร่าง (draft) — generate docx จาก template แบบ stateless ไม่สร้าง
Document/version; stamp เฉพาะ custom property TemplateId เพื่อให้ add-in
เห็นเป็น untracked; Start Versioning ส่ง form field templateId ให้ backend
ผูก template ตอน auto-create — เอกสารเริ่มที่ v1 (ไม่มี v0) + field sync เต็มระบบ

- Backend: POST /api/templates/{id}/generate-draft (200 คืนไฟล์) +
  DocxGenerationService.GenerateDraft + แยก TemplateFieldValidation ใช้ร่วมกับ
  generate + UploadVersion รับ templateId (non-GUID/unknown → 400,
  deactivated → ผูกได้, document เดิม → เฉย) + CORS expose Content-Disposition
- Add-in: readTrackedProperties อ่าน TemplateId + ส่ง templateId เฉพาะ
  startVersioning + hint ไฟล์ร่างใน task pane
- Web: ปุ่ม "สร้างไฟล์ร่าง" บนฟอร์มเดิม + downloadFile รับ init (POST → blob)
- Docs: spec v8 (FR-16, AC-19) + Phase 12 + CONTEXT.md + ADR 0002 +
  acceptance-review AC-19
- Tests: backend 82 / add-in 66 / web 59 + build ครบ; QA reviewer PASS
  (2 rounds); ค้าง manual บน Word จริง

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-10 10:51:13 +07:00

78 lines
4.1 KiB
C#

using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
namespace WordVersioning.Api.IntegrationTests;
/// <summary>DTO ของ response ฝั่ง test — mirror ของ API (deserialize ด้วย camelCase เหมือนที่ API คืน)</summary>
public record UploadVersionResult(Guid VersionId, int VersionNumber, bool HasPdf);
public record DownloadLinkResult(string Url, DateTimeOffset ExpiresAt);
public record GenerateResult(Guid DocumentId, Guid VersionId, int VersionNumber, string DownloadUrl);
public record VersionItem(Guid VersionId, int VersionNumber, DateTimeOffset CreatedAt, string CreatedByUserId, string ContentHash, JsonElement? Fields, string? Message, bool HasPdf);
public record TemplateFieldItem(Guid Id, string Key, string Label, string Type, string Editor, bool IsRequired, int SortOrder);
public record CreateTemplateResult(Guid Id, List<TemplateFieldItem> Fields);
public record TemplateDetailResult(Guid Id, string Name, string? Description, bool IsActive, DateTimeOffset CreatedAt, DateTimeOffset UpdatedAt, List<TemplateFieldItem> Fields);
public record ReplaceFileResult(Guid Id, List<TemplateFieldItem> Fields, List<string> AddedTags, List<string> RemovedTags);
public record TemplateListItemResult(Guid Id, string Name, string? Description, bool IsActive, int FieldCount, DateTimeOffset CreatedAt, DateTimeOffset UpdatedAt);
public record TemplateDocItem(Guid DocumentId, string FileName, DateTimeOffset CreatedAt, int VersionCount, int LatestVersionNumber, DateTimeOffset? LatestVersionAt);
public record DocFieldItem(string Key, string? Label, string? Type, bool? IsRequired, string Value, Guid? SourceVersionId, string? UpdatedByUserId, DateTimeOffset? UpdatedAt);
public record DocDetail(Guid Id, string FileName, DateTimeOffset CreatedAt, Guid? TemplateId, string? TemplateName, bool TemplateIsActive, List<DocFieldItem> Fields);
public static class ApiExtensions
{
/// <summary>multipart form ของ POST versions: file + clientVersionId + fields (option) + message (option)
/// + templateId (option — FR-16 ไฟล์ร่าง: ส่งเฉพาะตอน Start Versioning)</summary>
public static MultipartFormDataContent VersionForm(
byte[] docx, string fileName, string clientVersionId, string? fieldsJson = null, string? message = null,
byte[]? pdf = null, string? templateId = null)
{
var form = new MultipartFormDataContent();
var file = new ByteArrayContent(docx);
file.Headers.ContentType = new MediaTypeHeaderValue(TestDocx.DocxContentType);
form.Add(file, "file", fileName);
form.Add(new StringContent(clientVersionId), "clientVersionId");
if (fieldsJson is not null)
form.Add(new StringContent(fieldsJson), "fields");
if (message is not null)
form.Add(new StringContent(message), "message");
if (pdf is not null)
{
var part = new ByteArrayContent(pdf);
part.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
form.Add(part, "pdf", "document.pdf");
}
if (templateId is not null)
form.Add(new StringContent(templateId), "templateId");
return form;
}
/// <summary>multipart form ของ POST templates / PUT file: name + description (option) + file</summary>
public static MultipartFormDataContent TemplateForm(
byte[] docx, string name, string? description = null, string fileName = "template.docx")
{
var form = new MultipartFormDataContent();
form.Add(new StringContent(name), "name");
if (description is not null)
form.Add(new StringContent(description), "description");
var file = new ByteArrayContent(docx);
file.Headers.ContentType = new MediaTypeHeaderValue(TestDocx.DocxContentType);
form.Add(file, "file", fileName);
return form;
}
public static async Task<T?> ReadJsonAsync<T>(this HttpResponseMessage response) =>
response.Content.Headers.ContentType is null
? default
: await response.Content.ReadFromJsonAsync<T>(IntegrationTestFixture.Json);
}