This commit is contained in:
csj
2026-09-17 11:12:57 +08:00
parent 45b6f693d6
commit ced5db5473
11 changed files with 550 additions and 3 deletions
+64
View File
@@ -0,0 +1,64 @@
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")
}
}