package cn.iocoder.yudao.module.qcreport.engine.context;
import cn.iocoder.yudao.module.qcreport.dal.dataobject.version.ReportTemplateSchema;
import cn.iocoder.yudao.module.qcreport.engine.QualityReportEngine;
import cn.iocoder.yudao.module.qcreport.engine.render.RenderOutcome;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* 数据快照的编解码。
*
* 这里是「历史报告可复现」的唯一实现路径,所以第一条用例不是比对快照本身,
* 而是走完整链路:出件 → 冻结快照 → 从快照读回 → 重渲,产物必须与首次逐字相同。
* 只要这条断了,报告实例就会出现「生成时一个样、重新生成后另一个样」,
* 而库里的数据看不出任何异常——这种问题事后极难追。
*
* 用的是与对拍同一份 fixture({@code schema.json} / {@code context.json} /
* {@code expected-html.html}),所以「重渲一致」与「前后端一致」在这里是一次性验证的。
*/
class ReportContextCodecTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String FIXTURE = "qcreport/";
@Test
@DisplayName("冻结快照读回后重渲,产物与首次出件逐字相同")
void roundTripReproducesHtml() throws IOException {
ReportTemplateSchema schema = readSchema();
// 1. 首次出件:判定 + 渲染
RenderOutcome first = QualityReportEngine.render(schema, readContext());
assertEquals(readFixture("expected-html.html"), first.html(), "首次出件就与前端引擎不一致");
// 2. 冻结快照(存的就是判定后的上下文)
Map snapshot = ReportContextCodec.toMap(first.context());
// 3. 从快照读回,重渲
RenderOutcome second = QualityReportEngine.render(schema, ReportContextCodec.fromMap(snapshot));
assertEquals(first.html(), second.html(),
"从数据快照重渲的 HTML 与首次出件不一致,历史报告不再可复现");
assertEquals(first.errors(), second.errors(), "重渲的数据缺口清单与首次出件不一致");
}
@Test
@DisplayName("快照里带着算好的判定结果与合格率")
void snapshotCarriesEvaluation() throws IOException {
RenderOutcome outcome = QualityReportEngine.render(readSchema(), readContext());
Map snapshot = ReportContextCodec.toMap(outcome.context());
@SuppressWarnings("unchecked")
Map report = (Map) snapshot.get("report");
assertEquals("FAIL", report.get("result"));
assertEquals(3, report.get("total"));
assertEquals("33.33%", report.get("passRate"));
@SuppressWarnings("unchecked")
List