eCourt-Doc-Edit/backend/WordVersioning.Api.IntegrationTests/VersionMessageTests.cs

183 lines
8.2 KiB
C#
Raw Normal View History

using System.Net;
using System.Net.Http.Json;
using Microsoft.EntityFrameworkCore;
namespace WordVersioning.Api.IntegrationTests;
/// <summary>FR-13 — commit message ต่อ version: รับจากทุก flow ที่สร้าง version (upload + generate),
/// normalize ว่าง/whitespace → null, trim, จำกัด 2048 อักขระ, echo ใน history (AC-16)</summary>
[Collection(IntegrationTestCollection.Name)]
public sealed class VersionMessageTests(IntegrationTestFixture fixture)
{
[Fact]
public async Task Upload_WithMessage_PersistsRow_AndEchoesInHistory()
{
var documentId = Guid.NewGuid();
using var client = fixture.CreateClient();
using var response = await client.PostAsync(
$"api/documents/{documentId}/versions",
ApiExtensions.VersionForm(
TestDocx.BuildPlainDocx(), "doc.docx", Guid.NewGuid().ToString(),
message: "แก้ไขข้อ 3 หลังตรวจสอบ"));
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
await using var db = fixture.CreateDb();
var version = await db.DocumentVersions.AsNoTracking().SingleAsync(v => v.DocumentId == documentId);
Assert.Equal("แก้ไขข้อ 3 หลังตรวจสอบ", version.Message);
using var history = await client.GetAsync($"api/documents/{documentId}/versions");
var items = await history.Content.ReadFromJsonAsync<List<VersionItem>>(IntegrationTestFixture.Json);
Assert.Equal("แก้ไขข้อ 3 หลังตรวจสอบ", items!.Single().Message);
}
/// <summary>ไม่ส่ง / ส่งค่าว่าง / ส่ง whitespace ล้วน → เก็บ null ทั้ง DB และ history</summary>
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public async Task Upload_BlankOrMissingMessage_StoredAsNull(string? sent)
{
var documentId = Guid.NewGuid();
using var client = fixture.CreateClient();
using var response = await client.PostAsync(
$"api/documents/{documentId}/versions",
ApiExtensions.VersionForm(
TestDocx.BuildPlainDocx(), "doc.docx", Guid.NewGuid().ToString(),
message: sent));
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
await using var db = fixture.CreateDb();
var version = await db.DocumentVersions.AsNoTracking().SingleAsync(v => v.DocumentId == documentId);
Assert.Null(version.Message);
using var history = await client.GetAsync($"api/documents/{documentId}/versions");
var items = await history.Content.ReadFromJsonAsync<List<VersionItem>>(IntegrationTestFixture.Json);
Assert.Null(items!.Single().Message);
}
[Fact]
public async Task Upload_TrimsSurroundingWhitespace()
{
var documentId = Guid.NewGuid();
using var client = fixture.CreateClient();
using var response = await client.PostAsync(
$"api/documents/{documentId}/versions",
ApiExtensions.VersionForm(
TestDocx.BuildPlainDocx(), "doc.docx", Guid.NewGuid().ToString(),
message: " แก้ไขข้อ 3 "));
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
await using var db = fixture.CreateDb();
var version = await db.DocumentVersions.AsNoTracking().SingleAsync(v => v.DocumentId == documentId);
Assert.Equal("แก้ไขข้อ 3", version.Message);
}
[Fact]
public async Task Upload_OversizeMessage_Returns400_NoSideEffect()
{
var documentId = Guid.NewGuid();
using var client = fixture.CreateClient();
using var response = await client.PostAsync(
$"api/documents/{documentId}/versions",
ApiExtensions.VersionForm(
TestDocx.BuildPlainDocx(), "doc.docx", Guid.NewGuid().ToString(),
message: new string('ก', 2049)));
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
await using var db = fixture.CreateDb();
Assert.False(await db.Documents.AsNoTracking().AnyAsync(d => d.Id == documentId)); // ไม่มี side effect
}
[Fact]
public async Task Upload_MessageExactly2048_Succeeds()
{
var documentId = Guid.NewGuid();
using var client = fixture.CreateClient();
var message = new string('ก', 2048);
using var response = await client.PostAsync(
$"api/documents/{documentId}/versions",
ApiExtensions.VersionForm(
TestDocx.BuildPlainDocx(), "doc.docx", Guid.NewGuid().ToString(),
message: message));
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
await using var db = fixture.CreateDb();
var version = await db.DocumentVersions.AsNoTracking().SingleAsync(v => v.DocumentId == documentId);
Assert.Equal(message, version.Message);
}
[Fact]
public async Task SecondUpload_KeepsPerVersionMessages()
{
var documentId = Guid.NewGuid();
using var client = fixture.CreateClient();
using var first = await client.PostAsync(
$"api/documents/{documentId}/versions",
ApiExtensions.VersionForm(
TestDocx.BuildPlainDocx("v1"), "doc.docx", Guid.NewGuid().ToString(),
message: "ฉบับแรก"));
using var second = await client.PostAsync(
$"api/documents/{documentId}/versions",
ApiExtensions.VersionForm(
TestDocx.BuildPlainDocx("v2"), "doc.docx", Guid.NewGuid().ToString(),
message: "แก้ไขเงินเดือน"));
Assert.Equal(HttpStatusCode.Created, second.StatusCode);
// history ใหม่ → เก่า พร้อม message ของแต่ละ version เป็นของตัวเอง
using var history = await client.GetAsync($"api/documents/{documentId}/versions");
var items = await history.Content.ReadFromJsonAsync<List<VersionItem>>(IntegrationTestFixture.Json);
Assert.Equal(2, items!.Count);
Assert.Equal(2, items[0].VersionNumber);
Assert.Equal("แก้ไขเงินเดือน", items[0].Message);
Assert.Equal(1, items[1].VersionNumber);
Assert.Equal("ฉบับแรก", items[1].Message);
}
[Fact]
Phase 11: version PDF (FR-15) - เลข version ใหม่: เว็บ-generate = v0 ฉบับตั้งต้น (ไม่มี PDF), add-in save (Start Versioning + Save & Version) = v1+ มี PDF เสมอ - PDF ผลิตจาก Word จริงผ่าน task pane เท่านั้น (getFileAsync Pdf + runtime guard, manifest ไม่เพิ่ม requirement); capture/upload เป็น best-effort — docx ผ่านแม้ PDF ล้ม + MessageBar เตือนตาม hasPdf - Backend: DocumentVersion.PdfObjectKey (nullable, migration AddVersionPdf) + sibling .pdf ใน bucket docx-versions · multipart part pdf (ผิด type → 400) · /download + /download-link รับ ?format=pdf (ไม่มี → 404) · presigned ฝังชื่อไฟล์ไทย + _vN (RFC 5987) - Web: เปิดไฟล์แทน blob-download — PDF แท็บใหม่, docx ผ่าน ms-word:ofe|u| พร้อม heuristic fallback ลิงก์สำรอง (useVersionOpen, versionDownload) · ปุ่ม "เปิดใน Word" ที่หน้า success ของฟอร์มสร้างเอกสาร - Spec v7 + CONTEXT.md + ADR 0001 + แผน docs/plans/version-pdf.md - Automated QA: backend 68 / add-in 63 / web 55 ผ่าน · manual acceptance ผ่านบน Word for Mac (2026-09-08) รวม fidelity + track changes — ค้างเฉพาะ Windows / Word on the web Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-08 14:01:13 +07:00
public async Task Generate_WithMessage_CreatesV0WithMessage()
{
using var client = fixture.CreateClient();
var (templateId, _) = await TestFlow.CreateConfiguredTemplateAsync(client, "msg-gen");
var generated = await TestFlow.GenerateDocumentAsync(
client, templateId, TestFlow.DefaultValues, message: "ฉบับเสนอลงนามครั้งแรก");
await using var db = fixture.CreateDb();
var version = await db.DocumentVersions.AsNoTracking()
.SingleAsync(v => v.DocumentId == generated.DocumentId);
Phase 11: version PDF (FR-15) - เลข version ใหม่: เว็บ-generate = v0 ฉบับตั้งต้น (ไม่มี PDF), add-in save (Start Versioning + Save & Version) = v1+ มี PDF เสมอ - PDF ผลิตจาก Word จริงผ่าน task pane เท่านั้น (getFileAsync Pdf + runtime guard, manifest ไม่เพิ่ม requirement); capture/upload เป็น best-effort — docx ผ่านแม้ PDF ล้ม + MessageBar เตือนตาม hasPdf - Backend: DocumentVersion.PdfObjectKey (nullable, migration AddVersionPdf) + sibling .pdf ใน bucket docx-versions · multipart part pdf (ผิด type → 400) · /download + /download-link รับ ?format=pdf (ไม่มี → 404) · presigned ฝังชื่อไฟล์ไทย + _vN (RFC 5987) - Web: เปิดไฟล์แทน blob-download — PDF แท็บใหม่, docx ผ่าน ms-word:ofe|u| พร้อม heuristic fallback ลิงก์สำรอง (useVersionOpen, versionDownload) · ปุ่ม "เปิดใน Word" ที่หน้า success ของฟอร์มสร้างเอกสาร - Spec v7 + CONTEXT.md + ADR 0001 + แผน docs/plans/version-pdf.md - Automated QA: backend 68 / add-in 63 / web 55 ผ่าน · manual acceptance ผ่านบน Word for Mac (2026-09-08) รวม fidelity + track changes — ค้างเฉพาะ Windows / Word on the web Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-08 14:01:13 +07:00
Assert.Equal(0, version.VersionNumber);
Assert.Equal("ฉบับเสนอลงนามครั้งแรก", version.Message);
using var history = await client.GetAsync($"api/documents/{generated.DocumentId}/versions");
var items = await history.Content.ReadFromJsonAsync<List<VersionItem>>(IntegrationTestFixture.Json);
Assert.Equal("ฉบับเสนอลงนามครั้งแรก", items!.Single().Message);
}
/// <summary>400 ต้องเกิดก่อนแตะ MinIO/template — ไม่ทิ้ง orphan object ใน bucket</summary>
[Fact]
public async Task Generate_OversizeMessage_Returns400()
{
using var client = fixture.CreateClient();
var (templateId, _) = await TestFlow.CreateConfiguredTemplateAsync(client, "msg-gen-oversize");
using var response = await client.PostAsJsonAsync(
"api/documents/generate",
new { templateId, values = TestFlow.DefaultValues, message = new string('ก', 2049) },
IntegrationTestFixture.Json);
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
}