import XCTest @testable import LChatCore // Gate for the invite-link parsing shared by both apps: the two entry points — // OS-opened URLs and pasted links — accept/reject the right shapes, in particular // both the new "e1."-prefixed fragments and older unprefixed ones. final class InviteLinkTests: XCTestCase { func testParseJoinLink() { XCTAssertNotNil(parseJoinLink("https://family.example.com/join/join#e1.abc123"), "e1.-prefixed fragment parses") XCTAssertNotNil(parseJoinLink("https://family.example.com/join/join#abc123"), "unprefixed fragment parses (older links)") XCTAssertNotNil(parseJoinLink(" https://h.example.com/join/x#e1.T \n"), "whitespace is trimmed") XCTAssertEqual(parseJoinLink("http://192.168.1.5:8080/join/x#e1.T")?.origin, "http://192.168.1.5:8080", "origin keeps scheme/host/port, drops path+fragment") XCTAssertNil(parseJoinLink("https://h.example.com/join/x#k=legacykey"), "legacy #k= links are rejected") XCTAssertNil(parseJoinLink("https://h.example.com/join/x"), "missing fragment is rejected") XCTAssertNil(parseJoinLink("https://h.example.com/other/x#e1.T"), "non-/join/ path is rejected") XCTAssertNil(parseJoinLink("elevenmessenger://add?url=x"), "custom scheme is not itself a join link") } func testInviteLinkFromOpenedURL() { // The welcome page's custom-scheme form. let encoded = "https%3A%2F%2Ffamily.example.com%2Fjoin%2Fjoin%23e1.abc123" XCTAssertEqual(opened("elevenmessenger://add?url=\(encoded)"), "https://family.example.com/join/join#e1.abc123", "elevenmessenger://add unwraps + percent-decodes the url param") XCTAssertNil(opened("elevenmessenger://add"), "custom scheme without a url param is rejected") XCTAssertNil(opened("elevenmessenger://other?url=\(encoded)"), "custom scheme with an unknown host is rejected") // …and the universal-link form: the https invite passes straight through, // fragment intact. XCTAssertEqual(opened("https://family.example.com/join/join#e1.abc123"), "https://family.example.com/join/join#e1.abc123", "https universal link passes through with its fragment") XCTAssertNil(opened("ftp://h/join/x#e1.T"), "other schemes are rejected") } private func opened(_ s: String) -> String? { URL(string: s).flatMap { inviteLink(fromOpenedURL: $0) } } }