using Hua.Todo.Application.Models;
using Hua.Todo.Core.Entities;
using Xunit;
namespace Hua.Todo.Host.Tests.Attachments;
///
/// 工单 04 附件与描述数据模型测试。
///
public class AttachmentModelTests
{
[Fact]
public void AttachmentEntity_DefaultValues_AreCorrect()
{
var entity = new AttachmentEntity();
Assert.Equal(Guid.Empty, entity.Id);
Assert.Equal(Guid.Empty, entity.TaskId);
Assert.Equal(string.Empty, entity.FileName);
Assert.Equal(string.Empty, entity.FilePath);
Assert.Equal(0L, entity.FileSize);
Assert.Equal(string.Empty, entity.ContentType);
Assert.Equal(AttachmentType.LocalFile, entity.AttachmentType);
Assert.True(entity.CreatedAt <= DateTime.UtcNow);
}
[Fact]
public void AttachmentType_LocalFile_IsZero()
{
Assert.Equal(0, (int)AttachmentType.LocalFile);
}
[Fact]
public void AttachmentType_ExternalLink_IsOne()
{
Assert.Equal(1, (int)AttachmentType.ExternalLink);
}
[Fact]
public void AttachmentEntity_CanBeCreatedWithValues()
{
var id = Guid.NewGuid();
var taskId = Guid.NewGuid();
var entity = new AttachmentEntity
{
Id = id,
TaskId = taskId,
FileName = "需求文档.pdf",
FilePath = "/attachments/42_需求文档.pdf",
FileSize = 204800,
ContentType = "application/pdf",
AttachmentType = AttachmentType.LocalFile,
CreatedAt = new DateTime(2026, 6, 16, 10, 0, 0, DateTimeKind.Utc)
};
Assert.Equal(id, entity.Id);
Assert.Equal(taskId, entity.TaskId);
Assert.Equal("需求文档.pdf", entity.FileName);
Assert.Equal("/attachments/42_需求文档.pdf", entity.FilePath);
Assert.Equal(204800L, entity.FileSize);
Assert.Equal("application/pdf", entity.ContentType);
Assert.Equal(AttachmentType.LocalFile, entity.AttachmentType);
Assert.Equal(new DateTime(2026, 6, 16, 10, 0, 0, DateTimeKind.Utc), entity.CreatedAt);
}
[Fact]
public void AttachmentEntity_ExternalLinkType_HasDefaultFileSizeZero()
{
var entity = new AttachmentEntity
{
AttachmentType = AttachmentType.ExternalLink,
FilePath = "https://example.com/doc"
};
Assert.Equal(0L, entity.FileSize);
Assert.Equal(string.Empty, entity.ContentType);
}
[Fact]
public void AttachmentDto_Properties_AreMapped()
{
var id = Guid.NewGuid();
var taskId = Guid.NewGuid();
var dto = new AttachmentDto
{
Id = id,
TaskId = taskId,
FileName = "test.txt",
FilePath = "/path/to/test.txt",
FileSize = 1024,
ContentType = "text/plain",
AttachmentType = AttachmentType.LocalFile,
CreatedAt = DateTime.UtcNow
};
Assert.Equal(id, dto.Id);
Assert.Equal(taskId, dto.TaskId);
Assert.Equal("test.txt", dto.FileName);
Assert.Equal(1024L, dto.FileSize);
Assert.Equal("text/plain", dto.ContentType);
Assert.Equal(AttachmentType.LocalFile, dto.AttachmentType);
}
[Fact]
public void UploadAttachmentRequest_CanBeCreated()
{
var taskId = Guid.NewGuid();
var request = new UploadAttachmentRequest
{
TaskId = taskId,
FileName = "report.pdf",
Base64Content = "dGVzdA==",
ContentType = "application/pdf"
};
Assert.Equal(taskId, request.TaskId);
Assert.Equal("report.pdf", request.FileName);
Assert.Equal("dGVzdA==", request.Base64Content);
Assert.Equal("application/pdf", request.ContentType);
}
[Fact]
public void AddLinkRequest_CanBeCreated()
{
var taskId = Guid.NewGuid();
var request = new AddLinkRequest
{
TaskId = taskId,
Url = "https://example.com/doc",
FileName = "参考文档"
};
Assert.Equal(taskId, request.TaskId);
Assert.Equal("https://example.com/doc", request.Url);
Assert.Equal("参考文档", request.FileName);
}
[Fact]
public void AddLinkRequest_FileName_CanBeNull()
{
var taskId = Guid.NewGuid();
var request = new AddLinkRequest
{
TaskId = taskId,
Url = "https://example.com/doc"
};
Assert.Null(request.FileName);
}
[Fact]
public void TaskEntity_DefaultDescription_IsNull()
{
var entity = new TaskEntity();
Assert.Null(entity.Description);
}
[Fact]
public void TaskEntity_Description_CanBeSet()
{
var entity = new TaskEntity
{
Description = "这是一个多行描述,用于记录详细说明。"
};
Assert.Equal("这是一个多行描述,用于记录详细说明。", entity.Description);
}
[Fact]
public void TaskEntity_DefaultAttachments_IsEmpty()
{
var entity = new TaskEntity();
Assert.NotNull(entity.Attachments);
Assert.Empty(entity.Attachments);
}
[Fact]
public void TaskDto_IncludesDescription()
{
var dto = new TaskDto
{
Description = "测试描述"
};
Assert.Equal("测试描述", dto.Description);
}
[Fact]
public void TaskDto_IncludesAttachmentCount()
{
var dto = new TaskDto
{
AttachmentCount = 3
};
Assert.Equal(3, dto.AttachmentCount);
}
[Fact]
public void CreateTaskDto_IncludesDescription()
{
var dto = new CreateTaskDto
{
Title = "测试任务",
Description = "描述内容"
};
Assert.Equal("描述内容", dto.Description);
}
[Fact]
public void UpdateTaskDto_Description_IsOptional()
{
var dto = new UpdateTaskDto
{
Id = Guid.NewGuid(),
Title = "更新标题"
// 不传 Description —— 保持原值
};
Assert.Null(dto.Description);
}
[Fact]
public void OpenAttachmentResponse_OpenedProperty()
{
var response = new OpenAttachmentResponse { Opened = true };
Assert.True(response.Opened);
var failed = new OpenAttachmentResponse { Opened = false };
Assert.False(failed.Opened);
}
[Fact]
public void Description_Property_IsDefinedOnTaskEntity()
{
var property = typeof(TaskEntity).GetProperty("Description");
Assert.NotNull(property);
Assert.Equal(typeof(string), property.PropertyType);
}
[Fact]
public void Attachments_Property_IsDefinedOnTaskEntity()
{
var property = typeof(TaskEntity).GetProperty("Attachments");
Assert.NotNull(property);
Assert.Equal(typeof(List), property.PropertyType);
}
}