Implemented phase 1 for the POC prototype in the Website service.

This commit is contained in:
javier committed 2026-09-21 11:16:52 +02:00
1 parent 6d7ab4bcba
commit ea89ea892a
37 files changed
+4173 -43

No files matched your search

@@ -0,0 +1,132 @@
import Hummingbird
import HummingbirdTesting
import Logging
import Persistence
import Testing
@testable import WebsiteLibrary
@Suite(
"CatalogueController controller",
.tags(.controller)
)
struct CatalogueControllerTests {
// MARK: Functional tests
@Test
func `serves the search results narrowed by the query parameters`() async throws {
try await withApp { client in
try await client.execute(
uri: "/search?q=nightward+1&ships-from=anywhere&sort=price-asc",
method: .get
) { response in
let body = String(buffer: response.body)
#expect(response.status == .ok)
#expect(response.headers[.contentType] == "text/html; charset=utf-8")
#expect(body.contains("4 listings"))
#expect(body.contains("from 4 shops"))
#expect(!body.contains("Ferrous Saint"))
}
}
}
@Test
func `keeps listings from outside the EU out of the default search`() async throws {
try await withApp { client in
try await client.execute(
uri: "/search",
method: .get
) { response in
let body = String(buffer: response.body)
#expect(body.contains("17 listings"))
#expect(!body.contains("Longbox Atlantic"))
}
}
}
@Test
func `serves a listing with more from its shop`() async throws {
try await withApp { client in
try await client.execute(
uri: "/listing/nightward-1-cgc-9-8",
method: .get
) { response in
let body = String(buffer: response.body)
#expect(response.status == .ok)
#expect(body.contains("cert 4312087-002"))
#expect(body.contains("More from Kestrel Row Comics"))
#expect(body.contains("Echo Country #3"))
}
}
}
@Test
func `serves a shop wherever it ships from, narrowed by its quick filters`() async throws {
try await withApp { client in
try await client.execute(
uri: "/shop/longbox-atlantic?grading=slabbed",
method: .get
) { response in
let body = String(buffer: response.body)
#expect(response.status == .ok)
#expect(body.contains("All · 3"))
#expect(body.contains("Vermilion Lake #7"))
#expect(!body.contains("Solar Divide, Vol. 1"))
}
}
}
@Test(arguments: ["/listing/missing", "/shop/missing"])
func `does not find an unknown slug`(
uri: String
) async throws {
try await withApp { client in
try await client.execute(
uri: uri,
method: .get
) { response in
#expect(response.status == .notFound)
}
}
}
}
// MARK: - Helpers
private extension CatalogueControllerTests {
/// Runs the body against the controller's routes over a migrated, seeded in-memory catalogue, shutting the pool down after.
func withApp(
_ body: @escaping @Sendable (any TestClientProtocol) async throws -> Void
) async throws {
let fluent = try Service(
driver: .inMemory,
logger: Logger(label: "test")
)()
do {
await PrepareDB()(for: fluent)
try await fluent.migrate()
let router = Router(context: WebsiteRequestContext.self)
router.addRoutes(CatalogueController<WebsiteRequestContext>(repository: .init(fluent: fluent)).routes)
try await Application(router: router).test(.router, body)
} catch {
try? await fluent.shutdown()
throw error
}
try await fluent.shutdown()
}
}