SPB Git

spb/zyquo-cloud Public MIT

Native macOS AI chat client for 12 cloud providers — your keys, every cloud model, one beautiful chat.

Swift 97.4% Shell 1.7% Makefile 1%
4.5 KB · 109 lines swift
Raw Blame History
1//2//  SyntaxHighlighterTests.swift3//  Zyquo Cloud4//5//  Author: Simon-Pierre Boucher6//  Mail: contact@spboucher.ai7//8//  Sanity tests for the code syntax highlighter and the Markdown block parser9//  (streaming resilience: incomplete input must never crash).10//1112import Testing13import SwiftUI14@testable import ZyquoCloud1516@Suite("SyntaxHighlighter")17struct SyntaxHighlighterTests {18    /// Foreground color of the first run whose text equals `token`.19    private func color(of token: String, in attributed: AttributedString) -> Color? {20        for run in attributed.runs where String(attributed[run.range].characters) == token {21            return run.foregroundColor22        }23        return nil24    }2526    @Test func preservesTextExactly() {27        let samples: [(String, String)] = [28            ("swift", "func greet(name: String) -> String { return \"hi \\(name)\" } // done"),29            ("python", "def add(a, b):\n    # sum\n    return a + b"),30            ("javascript", "const x = `tpl ${y}`; /* block */"),31            ("sql", "SELECT id FROM users WHERE name = 'zy' -- lookup"),32            ("html", "<div class=\"box\"><!-- note --></div>"),33        ]34        for (language, code) in samples {35            let out = SyntaxHighlighter.highlight(code, language: language, baseColor: .primary)36            #expect(String(out.characters) == code)37        }38    }3940    @Test func swiftKeywordsStringsAndCommentsAreColored() {41        let code = "let name = \"Zyquo\" // cloud"42        let out = SyntaxHighlighter.highlight(code, language: "swift", baseColor: .primary)43        #expect(color(of: "let", in: out) == SyntaxHighlighter.theme.keyword)44        #expect(color(of: "\"Zyquo\"", in: out) == SyntaxHighlighter.theme.string)45        #expect(color(of: "// cloud", in: out) == SyntaxHighlighter.theme.comment)46    }4748    @Test func pythonCommentAndKeyword() {49        let out = SyntaxHighlighter.highlight("def run():  # go", language: "python", baseColor: .primary)50        #expect(color(of: "def", in: out) == SyntaxHighlighter.theme.keyword)51        #expect(color(of: "# go", in: out) == SyntaxHighlighter.theme.comment)52    }5354    @Test func numbersAreColored() {55        let out = SyntaxHighlighter.highlight("x = 42", language: "python", baseColor: .primary)56        #expect(color(of: "42", in: out) == SyntaxHighlighter.theme.number)57    }5859    @Test func sqlKeywordsAreCaseInsensitive() {60        let out = SyntaxHighlighter.highlight("select id from t", language: "sql", baseColor: .primary)61        #expect(color(of: "select", in: out) == SyntaxHighlighter.theme.keyword)62        let upper = SyntaxHighlighter.highlight("SELECT id FROM t", language: "sql", baseColor: .primary)63        #expect(color(of: "SELECT", in: upper) == SyntaxHighlighter.theme.keyword)64    }6566    @Test func unknownLanguageIsUniformBaseColor() {67        let code = "let x = 1 // whatever"68        let out = SyntaxHighlighter.highlight(code, language: "brainfudge", baseColor: .red)69        #expect(String(out.characters) == code)70        for run in out.runs {71            #expect(run.foregroundColor == .red)72        }73    }7475    @Test func survivesIncompleteInput() {76        // Unterminated string, unterminated block comment, empty input —77        // all must return the exact text without crashing (streaming case).78        let unterminatedString = "let s = \"never closed"79        let out1 = SyntaxHighlighter.highlight(unterminatedString, language: "swift", baseColor: .primary)80        #expect(String(out1.characters) == unterminatedString)8182        let unterminatedComment = "int a; /* still going"83        let out2 = SyntaxHighlighter.highlight(unterminatedComment, language: "c", baseColor: .primary)84        #expect(String(out2.characters) == unterminatedComment)8586        let empty = SyntaxHighlighter.highlight("", language: "swift", baseColor: .primary)87        #expect(String(empty.characters).isEmpty)88    }89}9091@Suite("MarkdownBlockParser")92struct MarkdownBlockParserTests {93    @Test func extractsFencedCodeBlock() {94        let blocks = MarkdownBlockParser.parse(text: "Hello\n\n```swift\nlet a = 1\n```", fontSize: 13.5)95        #expect(blocks.count == 2)96        guard case .code(let code, let language) = blocks[1].kind else {97            Issue.record("Expected a code block as the second block")98            return99        }100        #expect(code == "let a = 1")101        #expect(language == "swift")102    }103104    @Test func survivesUnterminatedFenceWhileStreaming() {105        let blocks = MarkdownBlockParser.parse(text: "Intro\n\n```swift\nlet a =", fontSize: 13.5)106        #expect(!blocks.isEmpty)107    }108}109