gitbayTests/WebURLRouteTests.swift
132 lines · 6622 bytes
16 symbols in this file
routewebURLOwnerAndRepowebURLOwnerPageswebURLTopLevelPageswebURLIssuesMRsBuildswebURLIgnoresQueryAndFragmentwebURLFileswebURLDecodesSegmentswebURLHistoryAndRefswebURLRepoSectionswebURLUnroutableaccountwebURLAccountPrefersTheCurrentOnewebURLAccountFallsBackToOneOnTheInstancewebURLAccountIsNilWithoutOneOnTheInstancewebURLAccountMatchesAnExplicitDefaultPort
1import Foundation
2import Testing
3@testable import gitbay
4
5private func route(_ path: String) -> WebURLRoute? {
6 WebURLRoute(url: URL(string: "https://gitbay.org\(path)")!)
7}
8
9@Test func webURLOwnerAndRepo() {
10 #expect(route("/krz") == .repo(.profile("krz")))
11 #expect(route("/krz/") == .repo(.profile("krz")))
12 #expect(route("/krz/gitbay") == .repo(.repo("krz/gitbay")))
13 #expect(route("/krz/gitbay/") == .repo(.repo("krz/gitbay")))
14}
15
16@Test func webURLOwnerPages() {
17 #expect(route("/krz/-/repositories") == .repo(.profile("krz")))
18 #expect(route("/krz/-/bookmarks") == .repo(.profile("krz")))
19 #expect(route("/krz/-/people") == .repo(.profile("krz")))
20 #expect(route("/krz/-/labels") == .org(.labels("krz")))
21 #expect(route("/krz/-/milestones") == .org(.milestones("krz")))
22 #expect(route("/cmc/-/snippets") == .snippet(.list(owner: "cmc")))
23 #expect(route("/cmc/-/snippets/a1b2") == .snippet(.snippet(id: "a1b2")))
24}
25
26@Test func webURLTopLevelPages() {
27 #expect(route("/bookmarks") == .repo(.bookmarks))
28 #expect(route("/notifications") == .repo(.notifications))
29}
30
31@Test func webURLIssuesMRsBuilds() {
32 #expect(route("/krz/gitbay/issues") == .issue(.list(repo: "krz/gitbay")))
33 #expect(route("/krz/gitbay/issues/12") == .issue(.issue(repo: "krz/gitbay", number: 12)))
34 #expect(route("/krz/gitbay/mrs") == .mr(.list(repo: "krz/gitbay")))
35 #expect(route("/krz/gitbay/mrs/7") == .mr(.mr(repo: "krz/gitbay", number: 7)))
36 #expect(route("/krz/gitbay/builds") == .build(.list(repo: "krz/gitbay")))
37 #expect(route("/krz/gitbay/builds/1510") == .build(.detail(repo: "krz/gitbay", number: 1510)))
38}
39
40// Query and fragment do not change the screen: a comment anchor or a
41// list filter still lands on the page it belongs to.
42@Test func webURLIgnoresQueryAndFragment() {
43 #expect(route("/krz/gitbay/issues/12#comment-3") == .issue(.issue(repo: "krz/gitbay", number: 12)))
44 #expect(route("/krz/gitbay/issues?state=closed") == .issue(.list(repo: "krz/gitbay")))
45}
46
47@Test func webURLFiles() {
48 #expect(route("/krz/gitbay/tree/main") == .repo(.tree(repo: "krz/gitbay", directory: "", ref: "main")))
49 #expect(route("/krz/gitbay/tree/main/") == .repo(.tree(repo: "krz/gitbay", directory: "", ref: "main")))
50 #expect(route("/krz/gitbay/tree/main/internal/httpd")
51 == .repo(.tree(repo: "krz/gitbay", directory: "internal/httpd", ref: "main")))
52 #expect(route("/krz/gitbay/blob/v1.0.0/README.org")
53 == .repo(.file(repo: "krz/gitbay", path: "README.org", ref: "v1.0.0")))
54 #expect(route("/krz/gitbay/blame/main/cmd/gitbay/main.go")
55 == .repo(.blame(repo: "krz/gitbay", path: "cmd/gitbay/main.go", ref: "main")))
56}
57
58// Each segment is decoded on its own, so an escaped slash stays inside
59// the ref instead of splitting it.
60@Test func webURLDecodesSegments() {
61 #expect(route("/krz/gitbay/blob/main/docs/My%20Notes.md")
62 == .repo(.file(repo: "krz/gitbay", path: "docs/My Notes.md", ref: "main")))
63 #expect(route("/krz/gitbay/tree/feature%2Fx/docs")
64 == .repo(.tree(repo: "krz/gitbay", directory: "docs", ref: "feature/x")))
65}
66
67@Test func webURLHistoryAndRefs() {
68 #expect(route("/krz/gitbay/commit/d0c63c4") == .repo(.commit(repo: "krz/gitbay", sha: "d0c63c4")))
69 #expect(route("/krz/gitbay/log") == .repo(.log(repo: "krz/gitbay", ref: nil, path: nil)))
70 #expect(route("/krz/gitbay/log/main") == .repo(.log(repo: "krz/gitbay", ref: "main", path: nil)))
71 #expect(route("/krz/gitbay/refs") == .repo(.refs(repo: "krz/gitbay")))
72 #expect(route("/krz/gitbay/compare") == .repo(.compare(repo: "krz/gitbay", defaultBranch: nil)))
73}
74
75@Test func webURLRepoSections() {
76 #expect(route("/krz/gitbay/releases") == .release(.list(repo: "krz/gitbay")))
77 #expect(route("/krz/gitbay/milestones") == .repo(.milestones(repo: "krz/gitbay")))
78 #expect(route("/krz/gitbay/labels") == .repo(.labels(repo: "krz/gitbay")))
79 #expect(route("/krz/gitbay/wiki") == .repo(.wiki(repo: "krz/gitbay")))
80 #expect(route("/krz/gitbay/wiki/Home") == .repo(.wikiPage(repo: "krz/gitbay", page: "Home")))
81 #expect(route("/krz/gitbay/wiki/Architecture/00-Overview")
82 == .repo(.wikiPage(repo: "krz/gitbay", page: "Architecture/00-Overview")))
83}
84
85// Pages the app has no screen for, and malformed ids, are nil: the
86// caller hands those to Safari.
87@Test func webURLUnroutable() {
88 for path in [
89 "", "/",
90 "/krz/gitbay/issues/new", "/krz/gitbay/issues/abc", "/krz/gitbay/issues/12/extra",
91 "/krz/gitbay/mrs/new", "/krz/gitbay/mrs/7/range-diff", "/krz/gitbay/builds/0",
92 "/krz/gitbay/blob/main", "/krz/gitbay/tree", "/krz/gitbay/commit",
93 "/krz/gitbay/raw/main/README.org", "/krz/gitbay/archive/main.tar.gz",
94 "/krz/gitbay/releases/download/v1/x.tgz", "/krz/gitbay/settings",
95 "/krz/gitbay/wiki/_raw/a.svg", "/krz/gitbay/compare/main...x",
96 "/krz/gitbay/log/main/extra", "/krz/-", "/krz/-/queries",
97 "/krz/-/snippets/a1b2/raw/x.txt",
98 ] {
99 #expect(route(path) == nil, "\(path)")
100 }
101 #expect(WebURLRoute(url: URL(string: "mailto:cmc@gitbay.org")!) == nil)
102 #expect(WebURLRoute(url: URL(string: "ftp://gitbay.org/krz")!) == nil)
103}
104
105private func account(_ host: String, _ user: String) throws -> Account {
106 Account(instance: try GitbayInstance(url: "https://\(host)"), username: user)
107}
108
109@Test func webURLAccountPrefersTheCurrentOne() throws {
110 let accounts = [try account("gitbay.org", "cmc"), try account("gitbay.org", "ios-smoke")]
111 let url = URL(string: "https://gitbay.org/krz")!
112 #expect(WebURLRoute.account(for: url, current: accounts[1], accounts: accounts) == accounts[1])
113}
114
115@Test func webURLAccountFallsBackToOneOnTheInstance() throws {
116 let accounts = [try account("dev.local", "cmc"), try account("gitbay.org", "cmc")]
117 let url = URL(string: "https://gitbay.org/krz")!
118 #expect(WebURLRoute.account(for: url, current: accounts[0], accounts: accounts) == accounts[1])
119}
120
121@Test func webURLAccountIsNilWithoutOneOnTheInstance() throws {
122 let accounts = [try account("dev.local", "cmc")]
123 let url = URL(string: "https://gitbay.org/krz")!
124 #expect(WebURLRoute.account(for: url, current: accounts[0], accounts: accounts) == nil)
125 #expect(WebURLRoute.account(for: url, current: nil, accounts: []) == nil)
126}
127
128@Test func webURLAccountMatchesAnExplicitDefaultPort() throws {
129 let accounts = [try account("gitbay.org", "cmc")]
130 let url = URL(string: "https://gitbay.org:443/krz")!
131 #expect(WebURLRoute.account(for: url, current: nil, accounts: accounts) == accounts[0])
132}