import Foundation @main struct MatchExplanationTests { static func main() throws { let old = """ {"id":"old","invoices":[],"payments":[],"category":"交通","matchType":"auto","score":90, "reasons":["金额一致 ¥100.00"],"paymentTotal":100,"expenseAmount":100} """ let legacy = try JSONDecoder().decode(MatchGroup.self, from: Data(old.utf8)) precondition(legacy.explanation == nil) precondition(legacy.evidenceScore == nil) precondition(legacy.evidenceLabel == "评分待核对") let explanation = """ {"version":2,"title":"单张发票与付款截图","rawScore":65,"checks":[ {"title":"共同金额","points":40,"maximum":40,"detail":"金额一致","issue":""}, {"title":"商户相似","points":0,"maximum":20,"detail":"未识别商户","issue":"商户未识别"}, {"title":"日期接近","points":10,"maximum":10,"detail":"日期一致","issue":""}, {"title":"交易单号","points":0,"maximum":15,"detail":"单号未识别","issue":"单号未识别"}, {"title":"批次金额唯一","points":15,"maximum":15,"detail":"金额唯一","issue":""} ],"note":"这是证据评分"} """ var match = legacy match.explanation = try JSONDecoder().decode(MatchExplanation.self, from: Data(explanation.utf8)) precondition(match.explanation?.summary == "商户未识别 · 单号未识别") precondition(match.evidenceScore == 65) precondition(match.evidenceLabel == "65%") precondition(match.score == 90) let restored = try JSONDecoder().decode(MatchGroup.self, from: JSONEncoder().encode(match)) precondition(restored.explanation?.rawScore == 65) precondition(restored.explanation?.checks.count == 5) precondition(restored.evidenceLabel == "65%") precondition(restored.id == legacy.id) var complete = match.explanation! for index in complete.checks.indices { complete.checks[index].points = complete.checks[index].maximum complete.checks[index].issue = "" } complete.rawScore = 100 precondition(complete.hundredPointScore == 100) precondition(complete.summary == "查看匹配依据") var invalid = complete invalid.checks[0].maximum = 75 precondition(invalid.hundredPointScore == nil) invalid = complete invalid.rawScore = 75 precondition(invalid.hundredPointScore == nil) invalid = complete invalid.version = nil precondition(invalid.hundredPointScore == nil) invalid = complete invalid.checks[0].points = -1 precondition(invalid.hundredPointScore == nil) let oldExplanation = explanation.replacingOccurrences(of: "\"version\":2,", with: "") let decodedOld = try JSONDecoder().decode(MatchExplanation.self, from: Data(oldExplanation.utf8)) precondition(decodedOld.version == nil && decodedOld.hundredPointScore == nil) match.matchType = "manual" precondition(match.evidenceScore == nil) complete.checks = [] precondition(complete.hundredPointScore == nil) precondition(complete.summary == "查看历史依据") print("Match explanation tests passed") } }