using Jiaowu.Api.Controllers; using Jiaowu.Api.Domain.Academic; using Jiaowu.Api.Infrastructure.Persistence; using Jiaowu.Api.Infrastructure.Scheduling; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; namespace Jiaowu.Api.Tests; public sealed class SchedulesControllerTests { [Fact] public async Task Latest_auto_schedule_job_remains_available_after_completion() { await using var connection = new SqliteConnection("Data Source=:memory:"); await connection.OpenAsync(); var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; await using var db = new AppDbContext(options); await db.Database.EnsureCreatedAsync(); var term = new AcademicTerm { Code = "2026-F", Name = "2026 秋季", AcademicYear = "2026-2027", Season = TermSeason.Autumn, StartDate = new DateOnly(2026, 9, 7), EndDate = new DateOnly(2027, 1, 17) }; var plan = new SchedulePlan { AcademicTerm = term, Name = "排课草稿", Version = "V1" }; var completed = new AutomaticScheduleJob { SchedulePlan = plan, Status = AutomaticScheduleJobStatus.Succeeded, CreatedEntries = 12, CompletedTasks = 5, TotalTasks = 6, ProcessedTasks = 6, MessagesJson = "[\"TASK-06 仍有 2 学时无法安排\"]", CompletedAt = DateTime.UtcNow }; db.AddRange(term, plan, completed); await db.SaveChangesAsync(); var controller = new SchedulesController( db, new AutomaticScheduleJobQueue()); var result = await controller.GetLatestAutomaticScheduleJob( plan.Id, CancellationToken.None); var ok = Assert.IsType(result.Result); var response = Assert.IsType(ok.Value); Assert.Equal(completed.Id, response.Id); Assert.Equal(AutomaticScheduleJobStatus.Succeeded, response.Status); Assert.Equal( "TASK-06 仍有 2 学时无法安排", Assert.Single(response.Messages)); } }