| 1 | import Foundation |
| 2 | import RastrilloApple |
| 3 | import XCTest |
| 4 | @testable import DocsKit |
| 5 | |
| 6 | final class DocumentAssetsTests: XCTestCase { |
| 7 | @MainActor func testRelativeImageURLsAndCachedImagesUseCurrentDocument() async throws { |
| 8 | let origin = try XCTUnwrap(URL(string: "https://example.com")) |
| 9 | XCTAssertEqual(DocsImageStore.resolvedURL(source: "picture.png", documentPath: "/d/first", origin: origin)?.absoluteString, "https://example.com/d/picture.png") |
| 10 | XCTAssertEqual(DocsImageStore.resolvedURL(source: "?picture=1", documentPath: "/d/second", origin: origin)?.absoluteString, "https://example.com/d/second?picture=1") |
| 11 | let client = NativeHTTP(origin: origin) |
| 12 | let store = DocsImageStore() |
| 13 | let source = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII=" |
| 14 | await store.load(sources: [source], client: client, documentPath: "/d/first") |
| 15 | XCTAssertNotNil(store.images(forDocumentPath: "/d/first", client: client)[source]) |
| 16 | XCTAssertTrue(store.images(forDocumentPath: "/d/second", client: client).isEmpty) |
| 17 | await store.load(sources: [], client: client, documentPath: "/d/second") |
| 18 | XCTAssertTrue(store.images.isEmpty) |
| 19 | } |
| 20 | @MainActor func testAllTwelveBundledFontFacesResolve() { |
| 21 | for (key, family) in [("font_sans", "Sans"), ("font_serif", "Serif"), ("font_mono", "Mono")] { |
| 22 | for (bold, italic, face) in [(false, false, "Regular"), (true, false, "Bold"), (false, true, "Italic"), (true, true, "BoldItalic")] { |
| 23 | var marks: [String: JSONValue] = [key: .object([:])] |
| 24 | if bold { marks["strong"] = .object([:]) } |
| 25 | if italic { marks["em"] = .object([:]) } |
| 26 | let font = DocsFonts.font(marks: marks, size: 12, heading: false) |
| 27 | XCTAssertEqual(font.fontName, "Liberation\(family)" + (face == "Regular" ? "" : "-\(face)")) |
| 28 | } |
| 29 | } |
| 30 | XCTAssertEqual(DocsFonts.font(marks: [:], size: 12, heading: false).fontName, "LiberationSans") |
| 31 | XCTAssertEqual(DocsFonts.font(marks: ["size_24": .object([:])], size: 12, heading: false).pointSize, 24) |
| 32 | } |
| 33 | @MainActor func testAttachmentsOccupyOneUTF16UnitAndInvalidImagesFail() { |
| 34 | XCTAssertEqual(DocsAttachments.image(nil, alt: "long image description").length, 1) |
| 35 | XCTAssertEqual(DocsAttachments.rule().length, 1) |
| 36 | XCTAssertNil(DocsAttachments.decode(Data("not an image".utf8))) |
| 37 | let png = Data(base64Encoded: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII=")! |
| 38 | XCTAssertNotNil(DocsAttachments.decode(png)) |
| 39 | } |
| 40 | } |
| 41 | |