Project updates from Template (#1)

This PR contains the latest updates from the generic Website template, which have been added while working on #loud-amsterdam.

Reviewed-on: #1
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #1.
This commit is contained in:
2026-09-04 13:40:35 +00:00
committed by javier
parent 08b4d80064
commit 65b62681eb
60 changed files with 2347 additions and 326 deletions
@@ -32,6 +32,17 @@ struct AssetExtensionTests {
#expect(fileExtension.folder == folder)
}
// MARK: CaseIterable tests
@Test
func `covers every case in the parameterised tests`() {
// `zip` stops at the shorter sequence, so a case missing from the arrays below is silently untested rather
// than failing this is what catches that.
#expect(Self.extensions == AssetExtension.allCases)
#expect(Self.contentTypes.count == AssetExtension.allCases.count)
#expect(Self.folders.count == AssetExtension.allCases.count)
}
}
// MARK: - Helpers
@@ -42,32 +53,44 @@ private extension AssetExtensionTests {
static let extensions: [AssetExtension] = [
.css,
.js,
.png,
.ico,
.jpg,
.js,
.mp4,
.png,
.svg,
.txt,
.webmanifest,
.webp,
.woff2,
.xml
]
static let contentTypes: [String] = [
"text/css",
"text/javascript",
"image/png",
"image/vnd.microsoft.icon",
"image/jpeg",
"text/javascript",
"video/mp4",
"image/png",
"image/svg+xml",
"text/plain",
"application/manifest+json",
"image/webp",
"font/woff2",
"application/xml"
]
static let folders: [String?] = [
"css",
nil,
"img",
"js",
"video",
nil,
nil,
nil,
nil,
nil,
"img",
"font",
nil
]
@@ -0,0 +1,59 @@
import Elementary
import Testing
@testable import Infrastructure
@Suite(
"Analytics.Event+Tagging extension",
.tags(.extension)
)
struct AnalyticsEventTaggingTests {
// MARK: Methods tests
@Test
func `applies the event name to the tag`() {
let markup = a(.href("/")) { "Listen" }
.attributes(contentsOf: Analytics.Event(name: "instagram").tagging())
#expect(markup.render().contains(#"data-umami-event="instagram""#))
}
@Test
func `applies every event attribute to the tag`() {
let event = Analytics.Event(
name: "playlist",
properties: ["set": "avc-xi"]
)
let markup = button {}
.attributes(contentsOf: event.tagging())
let rendered = markup.render()
#expect(rendered.contains(#"data-umami-event="playlist""#))
#expect(rendered.contains(#"data-umami-event-set="avc-xi""#))
}
@Test
func `returns one attribute per event attribute`() {
let event = Analytics.Event(
name: "playlist",
properties: ["set": "avc-xi"]
)
let attributes: [HTMLAttribute<HTMLTag.a>] = event.tagging()
#expect(attributes.count == event.attributes.count)
}
@Test
func `applies the event name to an SVG tag`() {
// `SVGTag.path` is no `HTMLTrait.Attributes.Global`, so this stops compiling if the method narrows back to HTML tags.
let rendered = SVG.path {}
.attributes(contentsOf: Analytics.Event(name: "logo").tagging())
.render()
#expect(rendered.contains(#"data-umami-event="logo""#))
}
}
@@ -0,0 +1,179 @@
import HTTPTypes
import Hummingbird
import HummingbirdTesting
import Testing
@testable import Infrastructure
@Suite(
"HTTPSRedirectMiddleware middleware",
.tags(.middleware)
)
struct HTTPSRedirectMiddlewareTests {
// MARK: Functional tests
@Test
func `redirects a request forwarded over plain http`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello",
method: .get,
headers: [.xForwardedProto: "http"]
) { response in
#expect(response.status == .movedPermanently)
#expect(response.headers[.location] == "https://example.com/hello")
}
}
}
@Test
func `preserves the path and the query of the redirected request`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello?utm_source=test&utm_medium=email",
method: .get,
headers: [.xForwardedProto: "http"]
) { response in
#expect(response.headers[.location] == "https://example.com/hello?utm_source=test&utm_medium=email")
}
}
}
@Test
func `matches the forwarded scheme regardless of its casing`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello",
method: .get,
headers: [.xForwardedProto: "HTTP"]
) { response in
#expect(response.status == .movedPermanently)
}
}
}
@Test
func `reads the leftmost entry of a proxy chain`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello",
method: .get,
headers: [.xForwardedProto: "http, https"]
) { response in
#expect(response.status == .movedPermanently)
}
}
}
@Test
func `passes a request forwarded over https through`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello",
method: .get,
headers: [.xForwardedProto: "https"]
) { response in
#expect(response.status == .ok)
#expect(response.headers[.location] == nil)
}
}
}
@Test
func `passes a request without the forwarded header through`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello",
method: .get
) { response in
#expect(response.status == .ok)
}
}
}
@Test
func `passes every request through when the header is not trusted`() async throws {
try await app(
configuration: .init(
origin: "https://example.com",
trustForwardedProto: false
)
).test(.router) { client in
try await client.execute(
uri: "/hello",
method: .get,
headers: [.xForwardedProto: "http"]
) { response in
#expect(response.status == .ok)
}
}
}
@Test
func `leaves the well-known space on plain http`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/.well-known/acme-challenge/token",
method: .get,
headers: [.xForwardedProto: "http"]
) { response in
#expect(response.status == .ok)
#expect(response.headers[.location] == nil)
}
}
}
@Test
func `passes every request through when the origin is not itself https`() async throws {
try await app(
configuration: .init(
origin: "http://example.com",
trustForwardedProto: true
)
).test(.router) { client in
try await client.execute(
uri: "/hello",
method: .get,
headers: [.xForwardedProto: "http"]
) { response in
#expect(response.status == .ok)
}
}
}
}
// MARK: - Helpers
private extension HTTPSRedirectMiddlewareTests {
// MARK: Methods
/// Builds an application whose router applies the HTTPS-redirect middleware ahead of a `/hello`
/// route returning a plain body and a `/.well-known/acme-challenge/token` route standing in for
/// a certificate authority's challenge file.
func app(
configuration: HTTPSRedirectMiddleware<BasicRequestContext>.Configuration = .init(
origin: "https://example.com",
trustForwardedProto: true
)
) -> some ApplicationProtocol {
let router = Router()
router.addMiddleware {
HTTPSRedirectMiddleware(configuration: configuration)
}
router.get("hello") { _, _ in
"Hello!"
}
router.get(".well-known/acme-challenge/token") { _, _ in
"token"
}
return Application(router: router)
}
}
@@ -25,6 +25,9 @@ struct LocalizationMiddlewareTests {
router.get("language") { _, context in
context.language
}
router.get("de/language") { _, context in
context.language
}
return router
}())
@@ -69,4 +72,44 @@ struct LocalizationMiddlewareTests {
}
}
@Test
func `honours a supported lang query parameter over the header`() async throws {
try await app.test(.router) { client in
try await client.execute(
uri: "/language?lang=de",
method: .get,
headers: [.acceptLanguage: "en"]
) { response in
#expect(String(buffer: response.body) == "de")
}
}
}
/// A language's whole URL prefix answers in its language, so its not-found page does too.
@Test
func `pins the language a leading path segment names over the header`() async throws {
try await app.test(.router) { client in
try await client.execute(
uri: "/de/language",
method: .get,
headers: [.acceptLanguage: "en"]
) { response in
#expect(String(buffer: response.body) == "de")
}
}
}
@Test
func `negotiates the header when the lang query parameter is unsupported`() async throws {
try await app.test(.router) { client in
try await client.execute(
uri: "/language?lang=fr",
method: .get,
headers: [.acceptLanguage: "de"]
) { response in
#expect(String(buffer: response.body) == "de")
}
}
}
}
@@ -0,0 +1,150 @@
import HTTPTypes
import Hummingbird
import HummingbirdTesting
import Testing
@testable import Infrastructure
@Suite(
"TrailingSlashRedirectMiddleware middleware",
.tags(.middleware)
)
struct TrailingSlashRedirectMiddlewareTests {
// MARK: Functional tests
@Test
func `redirects a path carrying a trailing slash`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello/",
method: .get
) { response in
#expect(response.status == .movedPermanently)
#expect(response.headers[.location] == "/hello")
}
}
}
@Test
func `preserves the query of the redirected request`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello/?utm_source=test&utm_medium=email",
method: .get
) { response in
#expect(response.headers[.location] == "/hello?utm_source=test&utm_medium=email")
}
}
}
@Test
func `collapses a path of nothing but slashes onto the root`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "//",
method: .get
) { response in
#expect(response.status == .movedPermanently)
#expect(response.headers[.location] == "/")
}
}
}
@Test
func `strips every trailing slash at once`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello///",
method: .get
) { response in
#expect(response.headers[.location] == "/hello")
}
}
}
@Test
func `passes the canonical path through`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello",
method: .get
) { response in
#expect(response.status == .ok)
#expect(response.headers[.location] == nil)
}
}
}
@Test
func `leaves the root path alone`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/",
method: .get
) { response in
#expect(response.status == .ok)
#expect(response.headers[.location] == nil)
}
}
}
@Test
func `redirects a head request as it does a get`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello/",
method: .head
) { response in
#expect(response.status == .movedPermanently)
#expect(response.headers[.location] == "/hello")
}
}
}
@Test
func `passes a post through so its body survives`() async throws {
try await app().test(.router) { client in
try await client.execute(
uri: "/hello/",
method: .post
) { response in
#expect(response.status == .ok)
#expect(response.headers[.location] == nil)
}
}
}
}
// MARK: - Helpers
private extension TrailingSlashRedirectMiddlewareTests {
// MARK: Methods
/// Builds an application whose router applies the trailing-slash middleware ahead of a `/hello`
/// route answering both `GET` and `POST`, and a root route standing in for the landing page.
func app() -> some ApplicationProtocol {
let router = Router()
router.addMiddleware {
TrailingSlashRedirectMiddleware()
}
router.get("hello") { _, _ in
"Hello!"
}
router.post("hello") { _, _ in
"Posted!"
}
router.get("/") { _, _ in
"Root!"
}
return Application(router: router)
}
}
@@ -18,6 +18,7 @@ struct SocialCardTests {
url: "https://site.example/",
siteName: "A Site",
locale: "en",
alternateLocales: ["nl_NL", "de_DE"],
image: .init(
url: "https://site.example/img/card.png",
width: 2400,
@@ -33,6 +34,8 @@ struct SocialCardTests {
.init("A summary.", name: .description),
.init("https://site.example/", name: .url),
.init("en", name: .locale),
.init("nl_NL", name: .localeAlternate),
.init("de_DE", name: .localeAlternate),
.init("https://site.example/img/card.png", name: .image),
.init("2400", name: .imageWidth),
.init("1260", name: .imageHeight),
@@ -17,6 +17,7 @@ struct StructuredDataTests {
name: "A Site",
url: "https://site.example/",
logo: "https://site.example/logo.png",
inLanguage: ["en", "nl"],
profiles: [
"https://social.example/a-site",
"https://videos.example/a-site",
@@ -26,10 +27,64 @@ struct StructuredDataTests {
#expect(data.payload == #"{"@context":"https://schema.org","@graph":["# +
#"{"@type":"Organization","@id":"https://site.example/#organization","name":"A Site","url":"https://site.example/","logo":"https://site.example/logo.png","# +
#""sameAs":["https://social.example/a-site","https://videos.example/a-site"]},"# +
#"{"@type":"WebSite","name":"A Site","url":"https://site.example/","publisher":{"@id":"https://site.example/#organization"}}]}"#
#"{"@type":"WebSite","name":"A Site","url":"https://site.example/","publisher":{"@id":"https://site.example/#organization"},"inLanguage":["en","nl"]}]}"#
)
}
@Test
func `carries the organization's alternate name, description, area, and address in that order`() {
let data = StructuredData(
name: "A Site",
url: "https://site.example/",
alternateName: "The Site",
description: "What the site is.",
areaServed: "A City",
email: "hello@site.example"
)
#expect(data.payload.contains(
#""name":"A Site","url":"https://site.example/","alternateName":"The Site","description":"What the site is.","areaServed":"A City","email":"hello@site.example""#
))
}
@Test
func `references the organization's founder by identifier`() {
let data = StructuredData(
name: "A Site",
url: "https://site.example/",
profiles: ["https://social.example/a-site"],
founder: "https://site.example/who#person"
)
// Last of the organization's properties, after the profiles.
#expect(data.payload.contains(
#""sameAs":["https://social.example/a-site"],"founder":{"@id":"https://site.example/who#person"}}"#
))
}
@Test
func `omits the founder when the data carries none`() {
let data = StructuredData(
name: "A Site",
url: "https://site.example/"
)
#expect(!data.payload.contains("founder"))
}
@Test
func `derives the organization's identifier from the site URL`() {
let url = "https://site.example/"
let data = StructuredData(
name: "A Site",
url: url
)
// Another page references the organization by this, so it must name the node the initializer actually builds.
#expect(StructuredData.organizationID(forSiteURL: url) == "https://site.example/#organization")
#expect(data.payload.contains(##""@id":"\##(StructuredData.organizationID(forSiteURL: url))""##))
}
@Test
func `omits the properties of the facts minimal data does not carry`() {
let data = StructuredData(
@@ -120,6 +175,9 @@ struct StructuredDataTests {
#expect(StructuredData.Value.array([.string("A"), .string("B")]).fragment == #"["A","B"]"#)
#expect(StructuredData.Value.reference("https://site.example/#organization").fragment == #"{"@id":"https://site.example/#organization"}"#)
#expect(StructuredData.Value.node(.init(type: "Place", properties: [])).fragment == #"{"@type":"Place"}"#)
// Unquoted, or a consumer reads the position of a list item as text and cannot order by it.
#expect(StructuredData.Value.number(1).fragment == "1")
#expect(StructuredData.Value.number(-3).fragment == "-3")
}
@Test