import CoreText
import Foundation
import RastrilloApple
import XCTest
@testable import DocsKit
#if os(macOS)
import AppKit
#endif
/// The picker and the commands it drives, against the shared list in
/// internal/fonts/fonts.json (DocsFontsTests reads it the same way).
final class DocsFontPickerTests: XCTestCase {
/// The picker lists the shared list's families, in its order and by its
/// names, each drawn in its own face. A row is the name alone: a metric
/// twin does not say so (Paul, 2026-10-26), though it is still one.
@MainActor func testPickerEntriesAreDrawnInTheirOwnFace() throws {
let shared = try DocsFontsTests.shared()
let entries = DocsFontPicker.entries
XCTAssertEqual(entries.map(\.key), shared.families.map(\.key))
XCTAssertEqual(entries.map(\.name), shared.families.map(\.name))
XCTAssertEqual(entries.first?.key, "carlito")
for (entry, family) in zip(entries, shared.families) {
XCTAssertEqual(DocsFontsTests.url(of: entry.font), try DocsFontsTests.bundled(family, "regular"), "\(family.name) is drawn in \(family.name)")
}
#if os(macOS)
// The toolbar's pop-up button: each item's title in its own face, the
// current family checked, its key what a choice reports.
let menu = DocsFontPicker.menu(selected: "sans")
XCTAssertEqual(menu.items.count, shared.families.count)
for (item, family) in zip(menu.items, shared.families) {
let title = try XCTUnwrap(item.attributedTitle, family.name)
XCTAssertEqual(title.string, family.name, "\(family.name)'s row is its name and nothing else")
var face = NSRange()
let font = try XCTUnwrap(title.attribute(.font, at: 0, effectiveRange: &face) as? NSFont, family.name)
XCTAssertEqual(DocsFontsTests.url(of: font), try DocsFontsTests.bundled(family, "regular"), family.name)
XCTAssertEqual(face.length, (family.name as NSString).length, "\(family.name): the name, and only the name, is in its face")
XCTAssertEqual(item.state, family.key == "sans" ? .on : .off)
XCTAssertEqual(item.representedObject as? String, family.key)
}
#endif
}
/// setFont is the picker's command: Sans is font_sans, the default clears
/// the group, one font at a time, and an unknown key changes nothing.
@MainActor func testSetFontKeepsSansAndDefaultApart() throws {
let model = DocsModel(client: NativeHTTP(origin: URL(string: "https://example.invalid")!))
model.detail = DocumentDetail(sqid: "fixture", title: "Fixture", guid: "fixture", can_write: true, can_share: false)
model.projection = CRDTProjection(children: [CRDTNode(id: "p", type: "paragraph", children: [CRDTNode(id: "t", type: "text", runs: [
CRDTRun(insert: "plain words"),
])])], canUndo: false, canRedo: false)
let commands = DocumentCommands(model: model, content: NativeTextProjection.render(model.projection!))
model.selection = NSRange(location: 2, length: 0)
XCTAssertTrue(commands.setFont("sans"))
XCTAssertEqual(model.typingMarks?["font_sans"], .object([:]), "Sans is a mark, not the default")
XCTAssertTrue(commands.setFont("inter"))
XCTAssertEqual(model.typingMarks?["font_inter"], .object([:]))
XCTAssertNil(model.typingMarks?["font_sans"], "one font at a time")
XCTAssertTrue(commands.setFont("carlito"))
XCTAssertEqual(model.typingMarks?.keys.filter { $0.hasPrefix("font_") } ?? ["missing"], [], "the default clears every font mark")
model.typingMarks = ["font_playfair": .object([:])]
XCTAssertFalse(commands.setFont("futura"), "a family this build does not know changes nothing")
XCTAssertEqual(model.typingMarks, ["font_playfair": .object([:])])
}
/// The face a collapsed-caret pick will type in, so IME marked text is
/// drawn in it rather than in its neighbour's.
@MainActor func testTypingFontFollowsAPickAtTheCaret() throws {
let projection = CRDTProjection(children: [
CRDTNode(id: "h", type: "heading", attrs: ["level": "2"], children: [CRDTNode(id: "ht", type: "text", runs: [CRDTRun(insert: "Title")])]),
], canUndo: false, canRedo: false)
let content = NativeTextProjection.render(projection)
let font = try XCTUnwrap(NativeTextProjection.typingFont(in: content, at: 2, marks: ["font_playfair": .object([:])]))
XCTAssertEqual(DocsFontsTests.url(of: font)?.lastPathComponent, "PlayfairDisplay-Bold.ttf", "a heading is bold")
XCTAssertEqual(font.pointSize, 15.6, accuracy: 0.01)
}
/// Pasted font names map onto the shared list once and deterministically.
@MainActor func testPastedFontNamesMapByTheSharedList() {
XCTAssertEqual(DocsFonts.family(cssStack: "Calibri, sans-serif")?.key, "carlito", "Calibri is Carlito")
XCTAssertEqual(DocsFonts.family(cssStack: "Cambria")?.key, "caladea")
XCTAssertEqual(DocsFonts.family(cssStack: "'Liberation Sans', Arial")?.key, "sans")
XCTAssertEqual(DocsFonts.family(cssStack: "Arial")?.key, "sans")
XCTAssertEqual(DocsFonts.family(cssStack: "Helvetica")?.key, "sans")
XCTAssertEqual(DocsFonts.family(cssStack: "\"Times New Roman\", serif")?.key, "serif")
XCTAssertEqual(DocsFonts.family(cssStack: "Georgia")?.key, "caladea")
XCTAssertEqual(DocsFonts.family(cssStack: "Wingdings, Menlo, monospace")?.key, "jetbrains")
XCTAssertEqual(DocsFonts.family(cssStack: "monospace")?.key, "mono")
XCTAssertEqual(DocsFonts.family(cssStack: "Source Serif 4")?.key, "sourceserif")
XCTAssertEqual(DocsFonts.family(cssStack: "sans-serif")?.key, "carlito")
XCTAssertNil(DocsFonts.family(cssStack: "Wingdings"))
// A stack is read in order: the first name that maps wins, whether it
// maps by its own name or as a name a family stands in for.
XCTAssertEqual(DocsFonts.family(cssStack: "Georgia, \"Times New Roman\", serif")?.key, "caladea")
XCTAssertEqual(DocsFonts.family(cssStack: "Menlo, \"Courier New\", monospace")?.key, "jetbrains")
XCTAssertEqual(DocsFonts.family(cssStack: "Helvetica, Calibri, sans-serif")?.key, "sans")
XCTAssertEqual(DocsFonts.family(cssStack: "Wingdings, Cambria, Georgia")?.key, "caladea")
XCTAssertEqual(DocsFonts.family(cssStack: "Wingdings, Calibri, Arial")?.key, "carlito")
}
/// fixtures/vectors/font-stacks.json is the one list of paste cases the
/// web's familyForStack and this resolver are both held to.
@MainActor func testSharedFontStackVectors() throws {
let url = DocsFontsTests.repository.appendingPathComponent("fixtures/vectors/font-stacks.json")
let json = try XCTUnwrap(try JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any])
let cases = try XCTUnwrap(json["cases"] as? [[Any]])
XCTAssertGreaterThanOrEqual(cases.count, 23)
for pair in cases {
let stack = try XCTUnwrap(pair.first as? String)
let expected = pair.last as? String
XCTAssertEqual(DocsFonts.family(cssStack: stack)?.key, expected, stack)
}
}
/// Nested paste, against the shared vectors' "nested" cases: an inner
/// font-family that maps to the default or to nothing clears the outer
/// font, one that maps to another family replaces it, and a span with no
/// font-family keeps the outer font, as the web's parse rule does.
@MainActor func testNestedPastedFontsFollowTheSharedVectors() throws {
let url = DocsFontsTests.repository.appendingPathComponent("fixtures/vectors/font-stacks.json")
let json = try XCTUnwrap(try JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any])
let cases = try XCTUnwrap(json["nested"] as? [[Any]])
XCTAssertGreaterThanOrEqual(cases.count, 8)
func attribute(_ stack: String) -> String { "font-family: " + stack.replacingOccurrences(of: "\"", with: """) }
for pair in cases {
let outer = try XCTUnwrap(pair[0] as? String), inner = pair[1] as? String, expected = pair[2] as? String
let innerOpen = inner.map { "" } ?? ""
let html = " Outer \(innerOpen)Inner
Serif
") guard case .object(let paragraph)? = blocks.first, case .array(let children)? = paragraph["children"], case .object(let text)? = children.first, case .array(let runs)? = text["runs"], case .object(let run)? = runs.first, case .object(let marks)? = run["marks"] else { return XCTFail("no run: \(blocks)") } XCTAssertEqual(marks.keys.filter { $0.hasPrefix("font_") }, ["font_caladea"]) } }