oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2import Testing
3@testable import EquipoKit
4
5@Suite struct ChangeFilesTests {
6 static let base = String(repeating: "a", count: 40)
7 static let sha = String(repeating: "b", count: 40)
8 static func facts() -> ThinChangeFiles {
9 .init(base: base, sha: sha, files: 1, additions: 1, deletions: 0, binary: 0,
10 entries: [.init(path: "Sources/Hello 日本語.swift", status: "added", additions: 1, deletions: 0, binary: false)], truncated: false)
11 }
12 @Test func currentCandidateLatestEngineeringOnlyAndOldRunnerFallback() throws {
13 var task = try TaskDetailTests.task(); task.candidateSha = Self.sha
14 var step = task.steps![0]; step.specialist = "engineering"; step.state = "done"; step.pushed = true
15 step.baseSha = Self.base; step.candidateSha = Self.sha; step.changeFiles = Self.facts()
16 task.steps = [step]
17 #expect(ChangedFilesPresentation.facts(task) == Self.facts())
18 var old = step; old.candidateSha = Self.base; old.changeFiles?.sha = Self.base
19 task.steps = [step, old]
20 #expect(ChangedFilesPresentation.facts(task) == Self.facts())
21 var newest = step; newest.id = "new"; newest.changeFiles = nil
22 task.steps = [step, newest]
23 #expect(ChangedFilesPresentation.facts(task) == nil)
24 for specialist in ["review", "copy", "checks"] {
25 step.specialist = specialist; task.steps = [step]
26 #expect(ChangedFilesPresentation.facts(task) == nil)
27 }
28 step.specialist = "engineering"; step.changeFiles?.sha = Self.base; task.steps = [step]
29 #expect(ChangedFilesPresentation.facts(task) == nil)
30 step.changeFiles = Self.facts(); step.baseSha = Self.sha; task.steps = [step]
31 #expect(ChangedFilesPresentation.facts(task) == nil)
32 step.baseSha = Self.base; step.state = "running"; task.steps = [step]
33 #expect(ChangedFilesPresentation.facts(task) == nil)
34 step.state = "done"; step.changeFiles?.additions = -1; task.steps = [step]
35 #expect(ChangedFilesPresentation.facts(task) == nil)
36 let legacy: ThinTask = try Fixture.decode("thin/task")
37 #expect(legacy.steps?.allSatisfy { $0.changeFiles == nil } == true)
38 }
39 @Test func fileLinksArePinnedEncodedAndAllowlisted() throws {
40 var task = try TaskDetailTests.task(); task.repo = "amadan.net/oficina/equipo"
41 var file = Self.facts().entries[0]; file.path = "docs/a+b & 日本語 #%.md"
42 let url = try #require(ChangedFilesPresentation.fileURL(task: task, facts: Self.facts(), file: file))
43 #expect(url.host == "amadan.net")
44 #expect(url.path == "/oficina/equipo")
45 #expect(url.absoluteString.contains("%2B"))
46 let query = try #require(URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems)
47 #expect(query.first { $0.name == "ref" }?.value == Self.sha)
48 #expect(query.first { $0.name == "path" }?.value == file.path)
49 var opened: URL?
50 SettingsNavigation.openChanges(origin: URL(string: "https://equipo.example")!, url: url) { opened = $0 }
51 #expect(opened == url)
52 file.status = "deleted"
53 let deleted = try #require(ChangedFilesPresentation.fileURL(task: task, facts: Self.facts(), file: file))
54 #expect(URLComponents(url: deleted, resolvingAgainstBaseURL: false)?.queryItems?.first { $0.name == "ref" }?.value == Self.base)
55 for repo in ["evil.example/oficina/equipo", "https://evil.example/oficina/equipo", "amadan.net@evil.example/o/r", "../evil", "oficina/equipo/extra"] {
56 task.repo = repo
57 #expect(ChangedFilesPresentation.fileURL(task: task, facts: Self.facts(), file: file) == nil)
58 }
59 task.repo = "oficina/equipo"; file.path = "../secret"
60 #expect(ChangedFilesPresentation.fileURL(task: task, facts: Self.facts(), file: file) == nil)
61 }
62 @Test func boundsAndGroupingKeepEveryDisplayedPath() {
63 var facts = Self.facts()
64 facts.entries = (0..<200).map { .init(path: "folder\($0 % 3)/file\($0).txt", status: "added", additions: 1, deletions: 0, binary: false) }
65 facts.files = 203; facts.additions = 203; facts.truncated = true
66 #expect(facts.isValid)
67 let groups = ChangedFilesPresentation.groups(facts)
68 #expect(groups.count == 3)
69 #expect(Set(groups.flatMap(\.files).map(\.path)) == Set(facts.entries.map(\.path)))
70 facts.entries.append(.init(path: "extra", status: "added", additions: 1, deletions: 0, binary: false))
71 #expect(!facts.isValid)
72 facts = Self.facts(); facts.files = 2
73 #expect(!facts.isValid)
74 facts = Self.facts(); facts.entries[0].additions = -1
75 #expect(!facts.isValid)
76 facts = Self.facts(); facts.entries[0].status = "future"
77 #expect(!facts.isValid)
78 facts = Self.facts(); facts.entries[0].binary = true; facts.binary = 1
79 #expect(!facts.isValid)
80 }
81 @Test func encodedBytesAndDuplicatePathsAreBounded() {
82 var facts = Self.facts(); facts.files = 200; facts.additions = 200
83 facts.entries = (0..<200).map { .init(path: "\($0)/" + String(repeating: "x", count: 3900), status: "added", additions: 1, deletions: 0, binary: false) }
84 #expect(!facts.isValid)
85 facts = Self.facts(); facts.files = 2; facts.additions = 2; facts.entries.append(facts.entries[0])
86 #expect(!facts.isValid)
87 }
88 @Test func byteBoundUsesTheServersJSONEscaping() {
89 var facts = Self.facts(); facts.files = 200; facts.additions = 200
90 facts.entries = (0..<200).map { .init(path: String(repeating: "a/", count: 150) + "file\($0)", status: "added", additions: 1, deletions: 0, binary: false) }
91 // Go leaves slashes unescaped: this complete listing fits its 96 KiB bound.
92 #expect(facts.isValid)
93 facts.entries = (0..<200).map { .init(path: String(repeating: "<", count: 150) + "file\($0)", status: "added", additions: 1, deletions: 0, binary: false) }
94 // Go escapes HTML characters, so these shorter paths exceed that bound.
95 #expect(!facts.isValid)
96 }
97 @Test func initialCommitAndReviewedStatusWords() throws {
98 var facts = Self.facts(); facts.base = ""
99 #expect(facts.isValid)
100 var task = try TaskDetailTests.task(); task.repo = "oficina/equipo"
101 #expect(ChangedFilesPresentation.fileURL(task: task, facts: facts, file: facts.entries[0]) != nil)
102 for (status, words) in [("added", "Added"), ("modified", "Changed"), ("deleted", "Deleted"), ("renamed", "Renamed")] {
103 var file = facts.entries[0]; file.status = status
104 #expect(ChangedFilesPresentation.status(file) == words)
105 }
106 }
107 @Test func reviewedCountsPluralizeFilesAndLines() {
108 #expect(ChangedFilesPresentation.total(files: 1, additions: 1, deletions: 0) == "1 file, 1 line added, 0 removed")
109 #expect(ChangedFilesPresentation.total(files: 2, additions: 1, deletions: 3) == "2 files, 1 line added, 3 removed")
110 #expect(ChangedFilesPresentation.total(files: 1, additions: 2, deletions: 1) == "1 file, 2 lines added, 1 removed")
111 #expect(ChangedFilesPresentation.total(files: 0, additions: 0, deletions: 0) == "0 files, 0 lines added, 0 removed")
112 }
113}
114
115extension ChangeFilesTests {
116 @Test func sharedPathRulesAndRenameSources() throws {
117 struct Case: Decodable { let path: String; let valid: Bool }
118 var root = URL(fileURLWithPath: #filePath)
119 for _ in 0..<5 { root.deleteLastPathComponent() }
120 let cases = try JSONDecoder().decode([Case].self, from: Data(contentsOf: root.appendingPathComponent("internal/wire/testdata/change-paths.json")))
121 for item in cases {
122 #expect(ThinChangeFiles.path(item.path) == item.valid, "path: \(item.path.debugDescription)")
123 var facts = Self.facts(); facts.entries[0].status = "renamed"; facts.entries[0].oldPath = item.path
124 #expect(facts.isValid == item.valid)
125 }
126 }
127 @Test func invalidFactsAreDiscardedAtStepDecodeAndAssignment() throws {
128 let encoder = EquipoJSON.encoder; encoder.dateEncodingStrategy = .iso8601
129 let original = try TaskDetailTests.task().steps![0]
130 var valid = original; valid.changeFiles = Self.facts()
131 #expect(try EquipoJSON.decoder.decode(ThinStep.self, from: encoder.encode(valid)).changeFiles == Self.facts())
132 let old = try JSONSerialization.jsonObject(with: encoder.encode(original)) as! [String: Any]
133 #expect(old["change_files"] == nil)
134 for path in ["/absolute", "a//b", "a\nb", "a\\b", "a\u{202e}b"] {
135 var facts = Self.facts(); facts.entries[0].path = path
136 var body = try JSONSerialization.jsonObject(with: encoder.encode(original)) as! [String: Any]
137 body["change_files"] = try JSONSerialization.jsonObject(with: encoder.encode(facts))
138 let step = try EquipoJSON.decoder.decode(ThinStep.self, from: JSONSerialization.data(withJSONObject: body))
139 #expect(step.changeFiles == nil)
140 var assigned = original; assigned.changeFiles = facts
141 #expect(assigned.changeFiles == nil)
142 }
143 }
144 @Test func repositoryNamesMustMatchTheForgePattern() throws {
145 var task = try TaskDetailTests.task()
146 for repo in ["oficina/a?b", "oficina/a%2Fb", "name space/repo", "oficina/💻"] {
147 task.repo = repo
148 #expect(ChangedFilesPresentation.fileURL(task: task, facts: Self.facts(), file: Self.facts().entries[0]) == nil)
149 }
150 }
151}
152
153
154extension ChangeFilesTests {
155 @Test func completeTotalsMustEqualTheirEntries() {
156 for field in [\ThinChangeFiles.additions, \ThinChangeFiles.deletions, \ThinChangeFiles.binary] {
157 var facts = Self.facts()
158 facts[keyPath: field] += 1
159 #expect(!facts.isValid)
160 facts.files = 2; facts.truncated = true
161 #expect(facts.isValid)
162 }
163 }
164 @Test func folderGroupingStartsAfterTenFiles() {
165 for count in [9, 10, 11] {
166 var facts = Self.facts()
167 facts.entries = (0..<count).map { .init(path: "folder\($0 % 2)/file\($0)", status: "added", additions: 1, deletions: 0, binary: false) }
168 facts.files = count; facts.additions = count
169 let groups = ChangedFilesPresentation.groups(facts)
170 if count <= 10 {
171 #expect(groups.count == 1)
172 #expect(groups.first?.folder == "")
173 #expect(groups.first?.files == facts.entries)
174 } else {
175 #expect(groups.map(\.folder) == ["folder0", "folder1"])
176 #expect(Set(groups.flatMap(\.files).map(\.path)) == Set(facts.entries.map(\.path)))
177 }
178 }
179 }
180 @Test func malformedOptionalFactsCannotHideTheStep() throws {
181 let encoder = EquipoJSON.encoder; encoder.dateEncodingStrategy = .iso8601
182 let original = try TaskDetailTests.task().steps![0]
183 let good = try JSONSerialization.jsonObject(with: encoder.encode(Self.facts())) as! [String: Any]
184 var wrongEntries = good; wrongEntries["entries"] = "not an array"
185 var wrongCount = good; wrongCount["files"] = "one"
186 var wrongEntry = good; wrongEntry["entries"] = [["path": "only-a-path"]]
187 let malformedValues: [Any] = ["not an object", 42, [1, 2], [:] as [String: Any], wrongEntries, wrongCount, wrongEntry]
188 for malformed in malformedValues {
189 var body = try JSONSerialization.jsonObject(with: encoder.encode(original)) as! [String: Any]
190 body["change_files"] = malformed
191 let decoded = try EquipoJSON.decoder.decode(ThinStep.self, from: JSONSerialization.data(withJSONObject: body))
192 #expect(decoded.changeFiles == nil)
193 #expect(decoded.id == original.id)
194 #expect(decoded.state == original.state)
195 }
196 }
197}
198