import Elementary import Foundation import Infrastructure import Testing @testable import WebsiteLibrary @Suite( "Page+Defaults extension", .tags(.extensionTests) ) struct PageDefaultsTests { // MARK: Computed tests @Test func `pairs english with a territory for its social card locale`() { #expect(StubPage().ogLocale == "en_US") } /// Open Graph prefers `language_TERRITORY`, but a scraper accepts the bare code — better than pinning a territory the site never named. @Test func `leaves an unpaired language as a bare code`() { #expect(StubPage(locale: .init(identifier: "nl")).ogLocale == "nl") } // MARK: Method tests @Test func `renders no language alternates without an origin`() { let html = StubPage().languageAlternates( origin: nil, path: "/", languages: Self.languages ).render() #expect(html.isEmpty) } @Test func `renders no language alternates for a single language`() { let html = StubPage().languageAlternates( origin: "https://example.com", path: "/", languages: [.default] ).render() #expect(html.isEmpty) } /// The default language keeps the bare root; the prefixed edition collapses onto its prefix rather than carrying a trailing slash. @Test func `renders an alternate per language and an x-default at the root`() { let html = StubPage().languageAlternates( origin: "https://example.com", path: "/", languages: Self.languages ).render() #expect(html.contains(#""#)) #expect(html.contains(#""#)) #expect(html.contains(#""#)) } @Test func `prefixes the alternates of a nested page`() { let html = StubPage().languageAlternates( origin: "https://example.com", path: "/privacy", languages: Self.languages ).render() #expect(html.contains(#""#)) #expect(html.contains(#""#)) #expect(html.contains(#""#)) } } // MARK: - Helpers private extension PageDefaultsTests { // MARK: Constants /// A two-language set, standing in for the multi-language catalog the template itself does not ship. static let languages: [Language] = [ .default, .init(identifier: "nl") ] // MARK: Types /// The barest ``Page`` conformance, so the shared defaults can be exercised without a real page's content. struct StubPage: Page { // MARK: Properties let assetVersion: String? = nil let locale: Locale // MARK: Initializers init(locale: Locale = .init(identifier: "en")) { self.locale = locale } // MARK: Properties var content: some HTML { HTMLRaw("") } var scripts: [any Asset] { [] } var stylesheets: [any Asset] { [] } var title: String { "Stub" } } }