SPB Git

Repository Public License

DiffAndConsoleTests.swift 5.8 KB 130 lines swift

1// ORIX OS Automation2// File: Packages/ORIXFeatureCode/Tests/ORIXFeatureCodeTests/DiffAndConsoleTests.swift3// Description: Unit tests of the line diff (property: applying a diff reproduces the target) and of the console buffer (ANSI parsing, bounded scrollback).4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9@testable import ORIXFeatureCode1011struct SeededGenerator: RandomNumberGenerator {12    var state: UInt6413    mutating func next() -> UInt64 {14        state = state &* 6364136223846793005 &+ 144269504088896340715        var z = state16        z = (z ^ (z >> 30)) &* 0xBF58476D1CE4E5B917        z = (z ^ (z >> 27)) &* 0x94D049BB133111EB18        return z ^ (z >> 31)19    }20}2122@Suite("Unit · line diff") struct LineDiffTests {23    @Test func identicalTextsHaveNoChange() {24        let d = LineDiff(old: "a\nb\nc\n", new: "a\nb\nc\n")25        #expect(d.isEmpty)26        #expect(d.lines.count == 3)27        #expect(DiffRow.unified(d).count == 3)28    }2930    @Test func countsAndNumbers() {31        let d = LineDiff(old: "a\nb\nc\nd\n", new: "a\nB\nc\nd\ne\n")32        #expect(d.added == 2)33        #expect(d.removed == 1)34        let removed = d.lines.first { $0.kind == .removed }35        #expect(removed?.text == "b")36        #expect(removed?.oldNumber == 2)37        #expect(d.lines.last?.newNumber == 5)38    }3940    @Test func applyingADiffReproducesTheTarget() {41        var rng = SeededGenerator(state: 42)42        let alphabet = ["alpha", "beta", "gamma", "delta", "", "  indented", "x", "y", "z"]43        for round in 0..<400 {44            let n = Int.random(in: 0...40, using: &rng)45            let old = (0..<n).map { _ in alphabet.randomElement(using: &rng)! }46            var new = old47            for _ in 0..<Int.random(in: 0...12, using: &rng) {48                switch Int.random(in: 0...2, using: &rng) {49                case 0: new.insert(alphabet.randomElement(using: &rng)! + "\(round)", at: Int.random(in: 0...new.count, using: &rng))50                case 1: if !new.isEmpty { new.remove(at: Int.random(in: 0..<new.count, using: &rng)) }51                default: if !new.isEmpty { new[Int.random(in: 0..<new.count, using: &rng)] = alphabet.randomElement(using: &rng)! }52                }53            }54            let d = LineDiff(old: old, new: new)55            #expect(d.apply(to: old) == new)56            #expect(d.lines.filter { $0.kind != .added }.map(\.text) == old)57            #expect(d.lines.filter { $0.kind != .removed }.map(\.text) == new)58            #expect(d.added == d.lines.filter { $0.kind == .added }.count)59            // Side-by-side rows carry every line exactly once per side.60            let rows = DiffRow.sideBySide(d, context: 1000)61            let left = rows.compactMap { r -> String? in if case .pair(_, let l, _) = r { return l?.text }; return nil }62            let right = rows.compactMap { r -> String? in if case .pair(_, _, let rr) = r { return rr?.text }; return nil }63            #expect(left == old)64            #expect(right == new)65        }66    }6768    @Test func theDiffIsMinimalOnSmallInputs() {69        // One line replaced in the middle: exactly one removal and one addition.70        let old = (1...200).map { "line \($0)" }71        var new = old; new[100] = "changed"72        let d = LineDiff(old: old, new: new)73        #expect(d.added == 1 && d.removed == 1)74        // Context collapsing leaves a gap on each side.75        let rows = DiffRow.unified(d, context: 3)76        #expect(rows.filter { if case .gap = $0 { return true }; return false }.count == 2)77        #expect(rows.count == 2 + 3 + 2 + 3)78    }7980    @Test func hugeUnrelatedInputsFallBackButStayCorrect() {81        let old = (0..<3000).map { "old \($0)" }, new = (0..<3000).map { "new \($0)" }82        let d = LineDiff(old: old, new: new)83        #expect(d.apply(to: old) == new)84        #expect(d.added == 3000 && d.removed == 3000)85    }86}8788@Suite("Unit · console") @MainActor struct ConsoleTests {89    @Test func ansiColoursAreRenderedAndOtherSequencesRemoved() {90        var p = ANSIParser()91        let lines = p.feed("plain \u{1B}[31mred \u{1B}[1mbold\u{1B}[0m back\u{1B}[2K\u{1B}]0;title\u{07}\n\u{1B}[38;5;2mgreen\u{1B}[39m \u{1B}[38;2;1;2;3mrgb\u{1B}[0m\n")92        #expect(lines.count == 2)93        #expect(lines[0].map(\.text) == ["plain ", "red ", "bold", " back"])94        #expect(lines[0].map(\.color) == [nil, 1, 1, nil])95        #expect(lines[0].map(\.bold) == [false, false, true, false])96        #expect(lines[1].first?.color == 2)97        #expect(lines[1].map(\.text).joined() == "green rgb")98    }99100    @Test func escapeSequencesSplitAcrossChunksAndCarriageReturns() {101        var p = ANSIParser()102        #expect(p.feed("ab\u{1B}[3").isEmpty)103        let lines = p.feed("2mgreen\u{1B}[0m\r\nprogress 10%\rprogress 100%\n")104        #expect(lines.count == 2)105        #expect(lines[0].map(\.text) == ["ab", "green"])106        #expect(lines[0][1].color == 2)107        #expect(lines[1].map(\.text).joined() == "progress 100%")108        #expect(p.feed("tail without newline").isEmpty)109        #expect(p.finish()?.map(\.text).joined() == "tail without newline")110    }111112    @Test func scrollbackIsBoundedAndSaysSo() {113        let buffer = ConsoleBuffer(capacity: 100)114        buffer.append((0..<250).map { "line \($0)\n" }.joined())115        #expect(buffer.lines.count == 100)116        #expect(buffer.dropped == 150)117        #expect(buffer.lines.first?.text == "line 150")118        #expect(buffer.lines.last?.text == "line 249")119        buffer.append("partial", stream: .stderr)120        #expect(buffer.lines.count == 100)121        buffer.flush()122        #expect(buffer.lines.last?.text == "partial")123        #expect(buffer.lines.last?.stream == .stderr)124        #expect(buffer.dropped == 151)125        #expect(ConsoleBuffer.capacity == 20_000)126        buffer.clear()127        #expect(buffer.lines.isEmpty && buffer.dropped == 0)128    }129}130
Files main
Menu

Theme