import CottonValues
import SwiftUI
import XCTest
@testable import CottonFaces
@testable import CottonUI
/// The money and document faces' readings.
@MainActor
final class MoneyDocumentFacesTests: XCTestCase {
/// RULE: a fact the core phrased through its line ("Received {said}") is drawn as the line's words for its label and what was said beside them; words the line did not phrase stay whole.
func testAPhrasedFactIsItsLinesWordsAndWhatWasSaid() throws {
let received = ShellLines.line("cards", "field.received", ["said": PhrasedFact.mark])
let said = ShellLines.line("cards", "field.received", ["said": "18 Oct"])
let parted = try XCTUnwrap(PhrasedFact.part(said, line: received))
XCTAssertEqual(parted.said, "18 Oct")
XCTAssertEqual(parted.label + " " + parted.said, said)
let from = ShellLines.line("cards", "field.issuer", ["said": PhrasedFact.mark])
XCTAssertEqual(PhrasedFact.part(ShellLines.line("cards", "field.issuer", ["said": "Invented Power"]), line: from)?.said, "Invented Power")
XCTAssertNil(PhrasedFact.part("18 Oct", line: received), "a date the line did not phrase has no label")
XCTAssertNil(PhrasedFact.part(received.replacingOccurrences(of: PhrasedFact.mark, with: ""), line: received), "nothing said, nothing parted")
}
}
#if canImport(UIKit)
import ImageIO
import UniformTypeIdentifiers
/// A capture utility, not a guard: with `MONEY_DOCUMENT_OUT=
` set
/// (through xcodebuild, `TEST_RUNNER_MONEY_DOCUMENT_OUT`) it draws the
/// transaction and document faces on the numbers of the house references
/// (money 15/45, 69, 70, 71; document 7/37, 8/38, 80, 81) — each on its
/// stock, a 360 x 450 card at scale 3 — into ``, to be laid beside the
/// references. Every name, number and page is invented; a person's picture
/// is the gallery's own, a bill's page the gallery's invented bill, and a
/// paper with no file here its drawn blank page. Without the out variable it
/// skips.
@MainActor
final class MoneyDocumentShotsTests: XCTestCase {
private let join = ShellLines.line("cards", "join")
func testDrawTheMoneyAndDocumentReferences() throws {
guard let dir = ProcessInfo.processInfo.environment["MONEY_DOCUMENT_OUT"], !dir.isEmpty else {
throw XCTSkip("set MONEY_DOCUMENT_OUT= to draw the money and document references")
}
let out = URL(fileURLWithPath: dir, isDirectory: true)
try FileManager.default.createDirectory(at: out, withIntermediateDirectories: true)
let bill = card("document", [
"title": .text("Electricity bill"), "subtitle": .text("PDF\(join)2 pages"),
"date": .text("Received 18 Oct"), "source": .text("From Example Power Company"), "group": .text("Home"),
])
let passport = card("document", [
"title": .text("Passport"), "subtitle": .text("PDF\(join)1 page"),
"date": .text("Received 13 Mar 2021"), "group": .text("Travel"),
])
let prescription = card("document", [
"title": .text("Prescription"), "subtitle": .text("JPEG\(join)1 page"),
"date": .text("Received 21 Oct"), "source": .text("From Dr Shah"), "group": .text("Health"),
])
let statement = card("document", [
"title": .text("Example Bank statement Sep 2026"), "subtitle": .text("PDF\(join)6 pages"),
"date": .text("Received 2 Oct"), "source": .text("From Example Bank"), "group": .text("Statements"),
"state": .text("Not read yet"),
])
let billPage = firstPage("bill", "pdf")
shoot("document-7", bill, DocumentFace(card: bill, page: billPage.map { Image(uiImage: $0).resizable().scaledToFill() },
pageAspect: billPage.map { $0.size.width / $0.size.height } ?? DocumentFaceKnobs.blankAspect), to: out)
// Papers with no file here: their drawn blank page, at the paper's shape.
shoot("document-8", passport, DocumentFace(card: passport, page: nil, pageAspect: 1.42), to: out)
shoot("document-80", prescription, DocumentFace(card: prescription, page: nil, pageAspect: 1.3), to: out)
shoot("document-81", statement, DocumentFace(card: statement, page: nil, pageAspect: 1.4), to: out)
}
private func card(_ face: String, corner: String? = nil, _ fields: [String: RecordValue]) -> DeckCard {
DeckCard(face: face, corner: corner, pressing: false, pace: nil, plate: nil, fields: fields)
}
/// A gallery file's first page, as the deck draws a document's.
private func firstPage(_ name: String, _ ext: String) -> UIImage? {
FixturePictures.url(name, ext).flatMap(FirstPageThumbnail.draw)
}
private func shoot(_ name: String, _ card: DeckCard, _ face: some View, to out: URL) {
let theme = Theme.paper
let renderer = ImageRenderer(content: face
.environment(\.theme, theme)
.environment(\.colorScheme, .light)
.environment(\.facePictureDrawer, DeckFace.pictures)
.environment(\.facePictures) { FixturePictures.image($0) }
.frame(width: 360, height: 450)
.background(theme.colors.paper.color))
renderer.scale = 3
guard let image = renderer.cgImage,
let dest = CGImageDestinationCreateWithURL(out.appendingPathComponent("\(name).png") as CFURL, UTType.png.identifier as CFString, 1, nil)
else { return XCTFail("\(name) drew nothing") }
CGImageDestinationAddImage(dest, image, nil)
XCTAssertTrue(CGImageDestinationFinalize(dest), "\(name) was not written")
}
}
#endif