SPB Git

Repository Public

DecodingTests.swift 9.6 KB 218 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Search iOS9// File             : Core/Tests/SPSearchKitTests/DecodingTests.swift10// Description      : Decoding of live payloads captured from11//                    search.spboucher.ai on 2026-12-01, and of degraded12//                    payloads.13// Module           : SPSearchKitTests14// Repository       : https://git.spboucher.ai/sp-search-ios15// Project Website  : https://search.spboucher.ai16// Created          : 2026-12-0117// Last Modified    : 2026-12-0118//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author           : Simon-Pierre Boucher24// Position         : Professor of Finance25// Institution      : Université du Québec en Outaouais (UQO)26// Department       : Département des sciences administratives27// Organization     : SP Boucher28//29// Website          : https://spboucher.ai30// Git              : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General          : info@spboucher.ai37// Professional     : contact@spboucher.ai38// Direct           : simon@spboucher.ai39// Research         : research@spboucher.ai40// Development      : dev@spboucher.ai41// Support          : support@spboucher.ai42// Security/Admin   : admin@spboucher.ai43// Notifications    : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification   : Public70// Distribution     : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License          : None specified (no license file in this repository)84// Maintainer       : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import Foundation92import Testing93@testable import SPSearchKit9495func fixture(_ name: String) throws -> Data {96    let url = try #require(Bundle.module.url(forResource: name, withExtension: "json", subdirectory: "Fixtures"))97    return try Data(contentsOf: url)98}99100@Suite struct DecodingTests {101    @Test func liveSearchPage() throws {102        let r = try JSONDecoder().decode(SearchResponse.self, from: fixture("search"))103        #expect(r.results.count == 5)104        #expect(r.total.relation == .estimate)105        #expect(r.total.value > 0)106        #expect(r.coverage.documents > 9_000_000)107        #expect(r.coverage.shardsOK == 8)108        #expect(r.coverage.shardsTotal == 8)109        #expect(r.nextCursor?.isEmpty == false)110        #expect(r.notices.isEmpty)111        #expect(r.partial == false)112        #expect(r.region == AppliedRegion(value: "ca", source: .browser))113        #expect(r.facets.candidates == 186)114        #expect(r.facets.domains.first == Facet(value: "ulaval.ca", count: 25))115        #expect(r.facets.languages.map(\.value) == ["fr", "en", "el"])116        #expect(r.requestID.hasPrefix("r-"))117        let first = try #require(r.results.first)118        #expect(first.title == "Accueil | Université Laval")119        #expect(first.domain == "ulaval.ca")120        #expect(first.kind == .university)121        #expect(first.tier == .standard)122        #expect(first.isReferenceSite)123        #expect(first.words == 307)124        #expect(first.readingMinutes == 1)125        #expect(first.language == "fr")126        #expect(first.checkedAt == Date(timeIntervalSince1970: 1_796_107_180))127        #expect(first.publishedAt == nil)128        #expect(first.snippetSource == "query")129        #expect(first.link?.host == "www.ulaval.ca")130    }131132    @Test func livePhrasePageCarriesNoticeAndChosenRegion() throws {133        let r = try JSONDecoder().decode(SearchResponse.self, from: fixture("phrase"))134        #expect(r.notices == [.phraseUnsupported])135        #expect(r.region == AppliedRegion(value: "ca-qc", source: .choice))136        #expect(r.results.count == 3)137    }138139    @Test func liveEmptyPage() throws {140        let r = try JSONDecoder().decode(SearchResponse.self, from: fixture("empty"))141        #expect(r.results.isEmpty)142        #expect(r.total == ResultTotal(value: 0, relation: .exact))143        #expect(r.nextCursor == nil)144        #expect(r.region == AppliedRegion(value: nil, source: AppliedRegion.Source.none))145        #expect(r.facets.candidates == 0)146    }147148    @Test func liveStats() throws {149        let s = try JSONDecoder().decode(IndexStats.self, from: fixture("stats"))150        #expect(s.documents > 9_000_000)151        #expect(s.domains > 400_000)152        #expect(s.languages.first?.language == "en")153        #expect(s.indexBytes > 20_000_000_000)154        #expect(s.updatedAt != nil)155    }156157    @Test func liveSuggestions() throws {158        let s = try JSONDecoder().decode(SuggestResponse.self, from: fixture("suggest"))159        #expect(s.suggestions.count == 8)160        #expect(s.suggestions.first == "university")161    }162163    @Test func missingFieldsDoNotFailThePage() throws {164        let json = #"{"results":[{"url":"https://example.org/a"},{"title":"sans adresse"},{"url":"https://example.org/b","kind":"podcast","tier":9,"authority":4,"words":-3,"checked_at":0,"title":"L&#039;&eacute;t&eacute; &amp; l&#x27;hiver"}]}"#165        let r = try JSONDecoder().decode(SearchResponse.self, from: Data(json.utf8))166        #expect(r.results.count == 2)   // the entry without an address is dropped167        let a = try #require(r.results.first)168        #expect(a.id == "https://example.org/a")169        #expect(a.title == "example.org")170        #expect(a.domain == "example.org")171        #expect(a.kind == .page)172        #expect(a.tier == .unknown)173        #expect(a.readingMinutes == nil)174        let b = try #require(r.results.last)175        #expect(b.title == "L'été & l'hiver")176        #expect(b.kind == .page)177        #expect(b.tier == .unknown)178        #expect(b.authority == 1)179        #expect(b.words == 0)180        #expect(b.checkedAt == nil)181        #expect(r.total.value == 2)182        #expect(r.total.relation == .atLeast)183        #expect(r.region == nil)184        #expect(r.nextCursor == nil)185    }186187    @Test func unknownNoticeAndRelationAreKept() throws {188        let json = #"{"results":[],"notices":["depth_limit","something_new"],"total":{"value":12,"relation":"roughly"},"region":{"value":"","source":"satellite"}}"#189        let r = try JSONDecoder().decode(SearchResponse.self, from: Data(json.utf8))190        #expect(r.notices == [.depthLimit, .unknown("something_new")])191        #expect(r.total.relation == .estimate)192        #expect(r.region == AppliedRegion(value: nil, source: AppliedRegion.Source.none))193    }194195    @Test func onlyWebLinksAreOpened() {196        #expect(SearchResult(id: "1", title: "t", url: "javascript:alert(1)", domain: "x").link == nil)197        #expect(SearchResult(id: "2", title: "t", url: "file:///etc/passwd", domain: "x").link == nil)198        #expect(SearchResult(id: "3", title: "t", url: "https://example.org/é", domain: "x").link != nil)199    }200}201202@Suite struct EntityTests {203    @Test func decodesNumericAndNamedReferences() {204        #expect(HTMLEntities.decode("L&#039;universit&eacute; &amp; l&#x2019;&eacute;cole") == "L'université & l’école")205        #expect(HTMLEntities.decode("&lt;b&gt; &quot;x&quot; &nbsp;&hellip;") == "<b> \"x\" \u{00A0}…")206        #expect(HTMLEntities.decode("&#128269; &#x1F50D;") == "🔍 🔍")207    }208    @Test func leavesWhatItDoesNotKnow() {209        #expect(HTMLEntities.decode("AT&T; R&D &unknown; &#; &#xZZ; & seul") == "AT&T; R&D &unknown; &#; &#xZZ; & seul")210        #expect(HTMLEntities.decode("sans esperluette") == "sans esperluette")211        #expect(HTMLEntities.decode("&#0; &#1114112;") == "&#0; &#1114112;")212    }213    @Test func singlePass() {214        #expect(HTMLEntities.decode("&amp;#039;") == "&#039;")215        #expect(HTMLEntities.decode("&amp;amp;") == "&amp;")216    }217}218
Files main
Menu

Theme