Repository Public License
1import Foundation2import NavigatorCore3import Persistence4import Testing5@testable import MemoryEngine67@Suite struct MemoryOptInTests {8 @Test func nothingIsIndexedOrSearchedUntilTheUserOptsIn() async throws {9 let fixture = try await Fixture.make(enabled: false)10 #expect(try await fixture.index.isEnabled() == false)11 #expect(try await fixture.database.settings.isSet("memory.enabled") == false)12 let outcome = try await fixture.index.index(fixture.page("https://example.org/a", text: english))13 #expect(outcome == .skipped(.disabled))14 // Not even the tables exist: the module has touched nothing.15 let tables = try await fixture.database.sqlite.read { try $0.scalar("SELECT count(*) FROM sqlite_master WHERE name LIKE 'memory%'", as: Int.self) }16 #expect(tables == 0)17 let response = try await fixture.index.search("fox")18 #expect(response.isEnabled == false && response.hits.isEmpty && response.isFullTextOnly)1920 try await fixture.index.setEnabled(true, purge: false)21 #expect(try await fixture.index.index(fixture.page("https://example.org/a", text: english)).report?.chunks == 1)22 #expect(try await fixture.index.search("fox").hits.count == 1)23 #expect(try await fixture.database.settings.value(MemorySettings.schemaVersion) == MemoryIndex.currentSchemaVersion)2425 // Turning it off without purge keeps the data but hides it; with purge it is gone.26 try await fixture.index.setEnabled(false, purge: false)27 #expect(try await fixture.index.search("fox").hits.isEmpty)28 #expect(try await fixture.index.index(fixture.page("https://example.org/b", text: english)) == .skipped(.disabled))29 #expect(try await fixture.count("memory_documents") == 1)30 try await fixture.index.setEnabled(true, purge: false)31 try await fixture.index.setEnabled(false, purge: true)32 #expect(try await fixture.tableCounts() == [0, 0, 0])33 await fixture.database.close()34 }3536 @Test func ghostContentIsNeverIndexed() async throws {37 let fixture = try await Fixture.make()38 // 1. The caller says the window is private.39 #expect(try await fixture.index.index(fixture.page("https://example.org/a", text: english, facts: MemoryPageFacts(isPrivate: true))) == .skipped(.ghost))40 // 2. The profile is declared ghost in the registry.41 let ghost = Profile(name: "Ghost", kind: .ghost)42 let ghostSpace = SpaceID()43 fixture.database.ghosts.declare(profile: ghost.id, spaces: [ghostSpace])44 let ghostPage = MemoryInput.page(url: URL(string: "https://example.org/a")!, title: "t", text: english, profileID: ghost.id, facts: MemoryPageFacts())45 #expect(try await fixture.index.index(ghostPage) == .skipped(.ghost))46 // 3. A note with no profile but a ghost space.47 #expect(try await fixture.index.index(MemoryInput(kind: .note, itemID: "n", spaceID: ghostSpace, title: "t", text: english)) == .skipped(.ghost))48 // 4. A profile that is simply not stored (a ghost the registry was not told about).49 let unknown = MemoryInput.page(url: URL(string: "https://example.org/a")!, title: "t", text: english, profileID: ProfileID(), facts: MemoryPageFacts())50 #expect(try await fixture.index.index(unknown) == .skipped(.profileNotPersisted))51 try await fixture.index.prepare()52 #expect(try await fixture.tableCounts() == [0, 0, 0])53 await fixture.database.close()54 }55}5657@Suite struct MemoryPolicyTests {58 let policy = MemoryPolicy(isEnabled: true)5960 func reason(_ address: String, facts: MemoryPageFacts = MemoryPageFacts(), policy: MemoryPolicy? = nil) -> MemorySkipReason? {61 let input = MemoryInput(kind: .page, itemID: address, profileID: ProfileID(), url: URL(string: address), title: "t", text: "some text", facts: facts)62 return (policy ?? self.policy).skipReason(for: input, profileIsStored: true, isGhost: false)63 }6465 @Test(arguments: [66 ("https://example.com/login", MemorySensitiveCategory.authentication),67 ("https://example.com/users/sign-in?next=/", .authentication),68 ("https://example.com/users/sign_in", .authentication),69 ("https://example.com/auth/callback", .authentication),70 ("https://example.com/password/reset", .authentication),71 ("https://example.com/Account/Security/2FA", .authentication),72 ("https://login.example.com/", .authentication),73 ("https://accounts.google.com/v3/signin/identifier", .authentication),74 ("https://id.apple.example/", .authentication),75 ("https://secure.shop.example/", .authentication),76 ("https://example.com/account", .account),77 ("https://example.com/my/billing/history", .account),78 ("https://shop.example/checkout/step-2", .payment),79 ("https://example.com/payment/methods", .payment),80 ("https://www.mybank.com/statements", .banking),81 ("https://www.bankofexample.com/", .banking),82 ("https://accweb.mouv.desjardins.com/", .banking),83 ("https://www.banque-example.fr/comptes", .banking),84 ("https://example.com/online-banking/overview", .banking),85 ("https://www.paypal.com/myaccount/summary", .banking),86 ("https://mychart.examplehealth.org/", .health),87 ("https://www.clinique-example.ca/resultats", .health),88 ("https://patient.portal.example/", .health),89 ("https://mail.google.com/mail/u/0/", .webmail),90 ("https://outlook.office.com/mail/inbox", .webmail),91 ("https://webmail.example.org/", .webmail),92 ("https://mail.example.org/inbox", .webmail),93 ("https://www.icloud.com/mail/", .webmail),94 ("https://example.com/cb?code=abc123&state=xyz", .credentialsInURL),95 ("https://example.com/doc?access_token=eyJhbGciOi", .credentialsInURL),96 ("https://example.com/share?sig=deadbeef", .credentialsInURL),97 ("https://example.com/#access_token=abc", .credentialsInURL),98 ("https://user:pw@example.com/", .credentialsInURL),99 ("http://localhost:3000/", .localNetwork),100 ("http://192.168.1.1/admin", .localNetwork),101 ("http://10.0.0.5/", .localNetwork),102 ("http://printer.local/", .localNetwork),103 ("http://intranet/wiki", .localNetwork),104 ("http://[::1]:8080/", .localNetwork),105 ("https://wiki.corp.internal/page", .localNetwork),106 ] as [(String, MemorySensitiveCategory)])107 func sensitivePagesAreExcludedByDefault(address: String, category: MemorySensitiveCategory) {108 #expect(reason(address) == .sensitiveURL(category), "\(address)")109 }110111 @Test(arguments: ["https://en.wikipedia.org/wiki/Bank", "https://example.org/articles/how-passwords-are-hashed", "https://news.example.com/2026/11/markets",112 "https://www.swift.org/blog/", "https://example.com/search?q=login+page+design", "https://mail.com/", "https://example.org/docs/authors",113 "https://example.org/a?page=2&sort=asc", "https://developer.apple.com/documentation/naturallanguage", "https://example.org/paying-attention"])114 func ordinaryPagesAreIndexable(address: String) {115 #expect(reason(address) == nil, "\(address)")116 }117118 @Test func noStoreAndPasswordFieldsAlwaysWin() {119 #expect(reason("https://example.org/article", facts: MemoryPageFacts(cacheControlNoStore: true)) == .noStore)120 #expect(reason("https://example.org/article", facts: MemoryPageFacts(hasPasswordField: true)) == .passwordField)121 // Not even an explicit exemption of the host overrides them.122 var lenient = MemoryPolicy(isEnabled: true, allowedHosts: [MemoryHostPattern("example.org")!], usesDefaultExclusions: false)123 #expect(reason("https://example.org/article", facts: MemoryPageFacts(cacheControlNoStore: true), policy: lenient) == .noStore)124 #expect(reason("https://example.org/article", facts: MemoryPageFacts(hasPasswordField: true), policy: lenient) == .passwordField)125 #expect(reason("https://example.org/article", facts: MemoryPageFacts(isPrivate: true), policy: lenient) == .ghost)126 lenient.isEnabled = false127 #expect(reason("https://example.org/article", policy: lenient) == .disabled)128 }129130 @Test func userExclusionsAndExemptions() {131 var custom = MemoryPolicy(isEnabled: true, excludedHosts: ["example.org", "*.corp.example", "=exact.example"].compactMap(MemoryHostPattern.init))132 #expect(reason("https://example.org/a", policy: custom) == .excludedHost(pattern: "example.org"))133 #expect(reason("https://WWW.Example.ORG/a", policy: custom) == .excludedHost(pattern: "example.org"))134 #expect(reason("https://notexample.org/a", policy: custom) == nil)135 #expect(reason("https://example.org.evil.test/a", policy: custom) == nil)136 #expect(reason("https://wiki.corp.example/a", policy: custom) == .excludedHost(pattern: "*.corp.example"))137 #expect(reason("https://corp.example/a", policy: custom) == nil)138 #expect(reason("https://exact.example/a", policy: custom) == .excludedHost(pattern: "=exact.example"))139 #expect(reason("https://sub.exact.example/a", policy: custom) == nil)140 // A false positive of the heuristics can be exempted; an explicit exclusion still wins.141 custom.allowedHosts = [MemoryHostPattern("foodbank.example")!, MemoryHostPattern("example.org")!]142 #expect(reason("https://www.foodbank.example/volunteer", policy: custom) == nil)143 #expect(reason("https://www.foodbank.example/volunteer") == .sensitiveURL(.banking))144 #expect(reason("https://example.org/a", policy: custom) == .excludedHost(pattern: "example.org"))145 custom.usesDefaultExclusions = false146 #expect(reason("https://shop.example/checkout", policy: custom) == nil)147 #expect(MemoryHostPattern("https://Sub.Example.org/path?q")?.text == "sub.example.org")148 for invalid in ["", " ", "exa mple.org", "a..b", ".example.org", "example.org.", "*.", "a/b", "a%b", "'; DROP TABLE memory_documents;--"] {149 #expect(MemoryHostPattern(invalid) == nil, "\(invalid)")150 }151 }152153 @Test func schemesAndEmptyContent() {154 for address in ["file:///Users/me/secret.txt", "about:blank", "orix://settings", "data:text/html,hi", "javascript:void(0)", "ftp://example.org/f", "blob:https://example.org/1"] {155 #expect(reason(address) == .unsupportedScheme, "\(address)")156 }157 let blank = MemoryInput(kind: .page, itemID: "x", url: URL(string: "https://example.org/")!, title: "t", text: " \n\t ")158 #expect(policy.skipReason(for: blank, profileIsStored: nil, isGhost: false) == .emptyText)159 #expect(policy.skipReason(for: MemoryInput(kind: .page, itemID: "x", title: "t", text: "t"), profileIsStored: nil, isGhost: false) == .unsupportedScheme)160 // A note has no URL: only the opt-in, ghost and empty rules apply.161 #expect(policy.skipReason(for: MemoryInput(kind: .note, itemID: "n", title: "t", text: "t"), profileIsStored: nil, isGhost: false) == nil)162 // A note anchored to a sensitive page is not indexed either.163 let anchored = MemoryInput(kind: .note, itemID: "n", url: URL(string: "https://www.mybank.com/statements"), title: "t", text: "balance 12")164 #expect(policy.skipReason(for: anchored, profileIsStored: nil, isGhost: false) == .sensitiveURL(.banking))165 }166167 @Test func exclusionsAppliedThroughTheIndexPurgeWhatWasAlreadyThere() async throws {168 let fixture = try await Fixture.make()169 try await fixture.index.index(fixture.page("https://blog.example.org/a", text: english))170 try await fixture.index.index(fixture.page("https://example.org/b", text: english))171 try await fixture.index.index(fixture.page("https://other.test/c", text: english))172 #expect(try await fixture.index.addExclusion("not a host") == false)173 #expect(try await fixture.index.addExclusion("Example.org"))174 #expect(try await fixture.index.excludedHosts() == ["example.org"])175 #expect(try await fixture.count("memory_documents") == 1)176 #expect(try await fixture.index.index(fixture.page("https://example.org/b", text: english)) == .skipped(.excludedHost(pattern: "example.org")))177 try await fixture.index.removeExclusion("example.org")178 #expect(try await fixture.index.index(fixture.page("https://example.org/b", text: english)).report != nil)179 #expect(try await fixture.index.index(fixture.page("https://www.foodbank.example/x", text: english)) == .skipped(.sensitiveURL(.banking)))180 #expect(try await fixture.index.setAllowed("foodbank.example", true))181 #expect(try await fixture.index.index(fixture.page("https://www.foodbank.example/x", text: english)).report != nil)182 try await fixture.index.setUsesDefaultExclusions(false)183 #expect(try await fixture.index.index(fixture.page("https://shop.example/checkout", text: english)).report != nil)184 #expect(try await fixture.index.index(fixture.page("https://shop.example/pay", text: english, facts: MemoryPageFacts(hasPasswordField: true))) == .skipped(.passwordField))185 await fixture.database.close()186 }187}188