p2p-swift / Tests / IrdP2PSignalingTests / DescriptorTests.swift
207 words
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Foundation
import Testing
@testable import IrdP2PSignaling

@Suite struct DescriptorTests {
    @Test func decodesFullDescriptor() throws {
        let d = try AppDescriptor.decode(fixture("descriptor-full"))
        #expect(d.protocolName == "ap2p-signal")
        #expect(d.version == "1")
        #expect(d.endpoints.ws.absoluteString == "wss://app.example.test/ws")
        #expect(d.endpoints.turnCredentials.absoluteString == "https://app.example.test/turn-credentials")
        #expect(d.relay?.peerId.hasPrefix("12D3KooW") == true)
        #expect(d.relay?.multiaddrs.count == 1)
        #expect(d.mailbox?.endpoint.absoluteString == "https://app.example.test/mailbox/v1")
        #expect(d.mailbox?.openMint == true)
        #expect(d.mailbox?.mintEndpoint?.absoluteString == "https://app.example.test/mailbox-credentials")
        #expect(d.mailbox?.mintIPPerMin == 60)
        #expect(d.mailbox?.maxMessageKB == 64)
        #expect(d.limits.maxFrameBytes == 65536)
        #expect(d.limits.maxConnsPerIP == 48)
        #expect(!d.isFallback)
    }

    @Test func minimalDescriptorHasNoRelayNoMailboxAndIgnoresUnknownKeys() throws {
        let d = try AppDescriptor.decode(fixture("descriptor-minimal"))
        #expect(d.relay == nil)
        #expect(d.mailbox == nil)
        #expect(d.limits.maxFrameBytes == 4096)
        #expect(d.limits.messagesPerSec == 3)
    }

    @Test func closedMintHasNoMintEndpoint() throws {
        let d = try AppDescriptor.decode(fixture("descriptor-closed-mint"))
        #expect(d.mailbox != nil)
        #expect(d.mailbox?.openMint == false)
        #expect(d.mailbox?.mintEndpoint == nil)
        #expect(d.mailbox?.mintIPPerMin == nil)
    }

    @Test func fallbackSynthesizesStandardPaths() {
        let d = AppDescriptor.fallback(host: "x.ap2p.ro")
        #expect(d.isFallback)
        #expect(d.endpoints.ws.absoluteString == "wss://x.ap2p.ro/ws")
        #expect(d.endpoints.turnCredentials.absoluteString == "https://x.ap2p.ro/turn-credentials")
        #expect(d.mailbox == nil)
        #expect(d.limits == .defaults)
    }

    @Test func rejectsDescriptorWithoutEndpoints() {
        #expect(throws: (any Error).self) { try AppDescriptor.decode(Data(#"{"protocol":"ap2p-signal"}"#.utf8)) }
    }

    @Test func endpointOverridesWin() throws {
        let d = try AppDescriptor.decode(fixture("descriptor-full"))
        let std = AppEndpoints.standard(host: "app.example.test")
        #expect(std.descriptor.absoluteString == "https://app.example.test/.well-known/ap2p")
        let r = std.resolved(with: d, host: "app.example.test")
        #expect(r.ws == d.endpoints.ws)
        #expect(r.mailbox == d.mailbox?.endpoint)
        #expect(r.mailboxMint == d.mailbox?.mintEndpoint)
        let local = AppEndpoints(descriptor: URL(string: "http://127.0.0.1:9020/.well-known/ap2p")!, ws: URL(string: "ws://127.0.0.1:9020/ws")!, mailbox: URL(string: "http://127.0.0.1:9023/mailbox/v1")!)
        let lr = local.resolved(with: d, host: "127.0.0.1")
        #expect(lr.ws.absoluteString == "ws://127.0.0.1:9020/ws")
        #expect(lr.mailbox?.absoluteString == "http://127.0.0.1:9023/mailbox/v1")
        #expect(lr.turnCredentials == d.endpoints.turnCredentials)
    }
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/p2p-swift.git