import Foundation import Testing @testable import EquipoKit @Suite struct ChangeFilesTests { static let base = String(repeating: "a", count: 40) static let sha = String(repeating: "b", count: 40) static func facts() -> ThinChangeFiles { .init(base: base, sha: sha, files: 1, additions: 1, deletions: 0, binary: 0, entries: [.init(path: "Sources/Hello 日本語.swift", status: "added", additions: 1, deletions: 0, binary: false)], truncated: false) } @Test func currentCandidateLatestEngineeringOnlyAndOldRunnerFallback() throws { var task = try TaskDetailTests.task(); task.candidateSha = Self.sha var step = task.steps![0]; step.specialist = "engineering"; step.state = "done"; step.pushed = true step.baseSha = Self.base; step.candidateSha = Self.sha; step.changeFiles = Self.facts() task.steps = [step] #expect(ChangedFilesPresentation.facts(task) == Self.facts()) var old = step; old.candidateSha = Self.base; old.changeFiles?.sha = Self.base task.steps = [step, old] #expect(ChangedFilesPresentation.facts(task) == Self.facts()) var newest = step; newest.id = "new"; newest.changeFiles = nil task.steps = [step, newest] #expect(ChangedFilesPresentation.facts(task) == nil) for specialist in ["review", "copy", "checks"] { step.specialist = specialist; task.steps = [step] #expect(ChangedFilesPresentation.facts(task) == nil) } step.specialist = "engineering"; step.changeFiles?.sha = Self.base; task.steps = [step] #expect(ChangedFilesPresentation.facts(task) == nil) step.changeFiles = Self.facts(); step.baseSha = Self.sha; task.steps = [step] #expect(ChangedFilesPresentation.facts(task) == nil) step.baseSha = Self.base; step.state = "running"; task.steps = [step] #expect(ChangedFilesPresentation.facts(task) == nil) step.state = "done"; step.changeFiles?.additions = -1; task.steps = [step] #expect(ChangedFilesPresentation.facts(task) == nil) let legacy: ThinTask = try Fixture.decode("thin/task") #expect(legacy.steps?.allSatisfy { $0.changeFiles == nil } == true) } @Test func fileLinksArePinnedEncodedAndAllowlisted() throws { var task = try TaskDetailTests.task(); task.repo = "amadan.net/oficina/equipo" var file = Self.facts().entries[0]; file.path = "docs/a+b & 日本語 #%.md" let url = try #require(ChangedFilesPresentation.fileURL(task: task, facts: Self.facts(), file: file)) #expect(url.host == "amadan.net") #expect(url.path == "/oficina/equipo") #expect(url.absoluteString.contains("%2B")) let query = try #require(URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems) #expect(query.first { $0.name == "ref" }?.value == Self.sha) #expect(query.first { $0.name == "path" }?.value == file.path) var opened: URL? SettingsNavigation.openChanges(origin: URL(string: "https://equipo.example")!, url: url) { opened = $0 } #expect(opened == url) file.status = "deleted" let deleted = try #require(ChangedFilesPresentation.fileURL(task: task, facts: Self.facts(), file: file)) #expect(URLComponents(url: deleted, resolvingAgainstBaseURL: false)?.queryItems?.first { $0.name == "ref" }?.value == Self.base) for repo in ["evil.example/oficina/equipo", "https://evil.example/oficina/equipo", "amadan.net@evil.example/o/r", "../evil", "oficina/equipo/extra"] { task.repo = repo #expect(ChangedFilesPresentation.fileURL(task: task, facts: Self.facts(), file: file) == nil) } task.repo = "oficina/equipo"; file.path = "../secret" #expect(ChangedFilesPresentation.fileURL(task: task, facts: Self.facts(), file: file) == nil) } @Test func boundsAndGroupingKeepEveryDisplayedPath() { var facts = Self.facts() facts.entries = (0..<200).map { .init(path: "folder\($0 % 3)/file\($0).txt", status: "added", additions: 1, deletions: 0, binary: false) } facts.files = 203; facts.additions = 203; facts.truncated = true #expect(facts.isValid) let groups = ChangedFilesPresentation.groups(facts) #expect(groups.count == 3) #expect(Set(groups.flatMap(\.files).map(\.path)) == Set(facts.entries.map(\.path))) facts.entries.append(.init(path: "extra", status: "added", additions: 1, deletions: 0, binary: false)) #expect(!facts.isValid) facts = Self.facts(); facts.files = 2 #expect(!facts.isValid) facts = Self.facts(); facts.entries[0].additions = -1 #expect(!facts.isValid) facts = Self.facts(); facts.entries[0].status = "future" #expect(!facts.isValid) facts = Self.facts(); facts.entries[0].binary = true; facts.binary = 1 #expect(!facts.isValid) } @Test func encodedBytesAndDuplicatePathsAreBounded() { var facts = Self.facts(); facts.files = 200; facts.additions = 200 facts.entries = (0..<200).map { .init(path: "\($0)/" + String(repeating: "x", count: 3900), status: "added", additions: 1, deletions: 0, binary: false) } #expect(!facts.isValid) facts = Self.facts(); facts.files = 2; facts.additions = 2; facts.entries.append(facts.entries[0]) #expect(!facts.isValid) } @Test func byteBoundUsesTheServersJSONEscaping() { var facts = Self.facts(); facts.files = 200; facts.additions = 200 facts.entries = (0..<200).map { .init(path: String(repeating: "a/", count: 150) + "file\($0)", status: "added", additions: 1, deletions: 0, binary: false) } // Go leaves slashes unescaped: this complete listing fits its 96 KiB bound. #expect(facts.isValid) facts.entries = (0..<200).map { .init(path: String(repeating: "<", count: 150) + "file\($0)", status: "added", additions: 1, deletions: 0, binary: false) } // Go escapes HTML characters, so these shorter paths exceed that bound. #expect(!facts.isValid) } @Test func initialCommitAndReviewedStatusWords() throws { var facts = Self.facts(); facts.base = "" #expect(facts.isValid) var task = try TaskDetailTests.task(); task.repo = "oficina/equipo" #expect(ChangedFilesPresentation.fileURL(task: task, facts: facts, file: facts.entries[0]) != nil) for (status, words) in [("added", "Added"), ("modified", "Changed"), ("deleted", "Deleted"), ("renamed", "Renamed")] { var file = facts.entries[0]; file.status = status #expect(ChangedFilesPresentation.status(file) == words) } } @Test func reviewedCountsPluralizeFilesAndLines() { #expect(ChangedFilesPresentation.total(files: 1, additions: 1, deletions: 0) == "1 file, 1 line added, 0 removed") #expect(ChangedFilesPresentation.total(files: 2, additions: 1, deletions: 3) == "2 files, 1 line added, 3 removed") #expect(ChangedFilesPresentation.total(files: 1, additions: 2, deletions: 1) == "1 file, 2 lines added, 1 removed") #expect(ChangedFilesPresentation.total(files: 0, additions: 0, deletions: 0) == "0 files, 0 lines added, 0 removed") } } extension ChangeFilesTests { @Test func sharedPathRulesAndRenameSources() throws { struct Case: Decodable { let path: String; let valid: Bool } var root = URL(fileURLWithPath: #filePath) for _ in 0..<5 { root.deleteLastPathComponent() } let cases = try JSONDecoder().decode([Case].self, from: Data(contentsOf: root.appendingPathComponent("internal/wire/testdata/change-paths.json"))) for item in cases { #expect(ThinChangeFiles.path(item.path) == item.valid, "path: \(item.path.debugDescription)") var facts = Self.facts(); facts.entries[0].status = "renamed"; facts.entries[0].oldPath = item.path #expect(facts.isValid == item.valid) } } @Test func invalidFactsAreDiscardedAtStepDecodeAndAssignment() throws { let encoder = EquipoJSON.encoder; encoder.dateEncodingStrategy = .iso8601 let original = try TaskDetailTests.task().steps![0] var valid = original; valid.changeFiles = Self.facts() #expect(try EquipoJSON.decoder.decode(ThinStep.self, from: encoder.encode(valid)).changeFiles == Self.facts()) let old = try JSONSerialization.jsonObject(with: encoder.encode(original)) as! [String: Any] #expect(old["change_files"] == nil) for path in ["/absolute", "a//b", "a\nb", "a\\b", "a\u{202e}b"] { var facts = Self.facts(); facts.entries[0].path = path var body = try JSONSerialization.jsonObject(with: encoder.encode(original)) as! [String: Any] body["change_files"] = try JSONSerialization.jsonObject(with: encoder.encode(facts)) let step = try EquipoJSON.decoder.decode(ThinStep.self, from: JSONSerialization.data(withJSONObject: body)) #expect(step.changeFiles == nil) var assigned = original; assigned.changeFiles = facts #expect(assigned.changeFiles == nil) } } @Test func repositoryNamesMustMatchTheForgePattern() throws { var task = try TaskDetailTests.task() for repo in ["oficina/a?b", "oficina/a%2Fb", "name space/repo", "oficina/💻"] { task.repo = repo #expect(ChangedFilesPresentation.fileURL(task: task, facts: Self.facts(), file: Self.facts().entries[0]) == nil) } } } extension ChangeFilesTests { @Test func completeTotalsMustEqualTheirEntries() { for field in [\ThinChangeFiles.additions, \ThinChangeFiles.deletions, \ThinChangeFiles.binary] { var facts = Self.facts() facts[keyPath: field] += 1 #expect(!facts.isValid) facts.files = 2; facts.truncated = true #expect(facts.isValid) } } @Test func folderGroupingStartsAfterTenFiles() { for count in [9, 10, 11] { var facts = Self.facts() facts.entries = (0..