// // SyntaxHighlighterTests.swift // Zyquo Cloud // // Author: Simon-Pierre Boucher // Mail: contact@spboucher.ai // // Sanity tests for the code syntax highlighter and the Markdown block parser // (streaming resilience: incomplete input must never crash). // import Testing import SwiftUI @testable import ZyquoCloud @Suite("SyntaxHighlighter") struct SyntaxHighlighterTests { /// Foreground color of the first run whose text equals `token`. private func color(of token: String, in attributed: AttributedString) -> Color? { for run in attributed.runs where String(attributed[run.range].characters) == token { return run.foregroundColor } return nil } @Test func preservesTextExactly() { let samples: [(String, String)] = [ ("swift", "func greet(name: String) -> String { return \"hi \\(name)\" } // done"), ("python", "def add(a, b):\n # sum\n return a + b"), ("javascript", "const x = `tpl ${y}`; /* block */"), ("sql", "SELECT id FROM users WHERE name = 'zy' -- lookup"), ("html", "
"), ] for (language, code) in samples { let out = SyntaxHighlighter.highlight(code, language: language, baseColor: .primary) #expect(String(out.characters) == code) } } @Test func swiftKeywordsStringsAndCommentsAreColored() { let code = "let name = \"Zyquo\" // cloud" let out = SyntaxHighlighter.highlight(code, language: "swift", baseColor: .primary) #expect(color(of: "let", in: out) == SyntaxHighlighter.theme.keyword) #expect(color(of: "\"Zyquo\"", in: out) == SyntaxHighlighter.theme.string) #expect(color(of: "// cloud", in: out) == SyntaxHighlighter.theme.comment) } @Test func pythonCommentAndKeyword() { let out = SyntaxHighlighter.highlight("def run(): # go", language: "python", baseColor: .primary) #expect(color(of: "def", in: out) == SyntaxHighlighter.theme.keyword) #expect(color(of: "# go", in: out) == SyntaxHighlighter.theme.comment) } @Test func numbersAreColored() { let out = SyntaxHighlighter.highlight("x = 42", language: "python", baseColor: .primary) #expect(color(of: "42", in: out) == SyntaxHighlighter.theme.number) } @Test func sqlKeywordsAreCaseInsensitive() { let out = SyntaxHighlighter.highlight("select id from t", language: "sql", baseColor: .primary) #expect(color(of: "select", in: out) == SyntaxHighlighter.theme.keyword) let upper = SyntaxHighlighter.highlight("SELECT id FROM t", language: "sql", baseColor: .primary) #expect(color(of: "SELECT", in: upper) == SyntaxHighlighter.theme.keyword) } @Test func unknownLanguageIsUniformBaseColor() { let code = "let x = 1 // whatever" let out = SyntaxHighlighter.highlight(code, language: "brainfudge", baseColor: .red) #expect(String(out.characters) == code) for run in out.runs { #expect(run.foregroundColor == .red) } } @Test func survivesIncompleteInput() { // Unterminated string, unterminated block comment, empty input — // all must return the exact text without crashing (streaming case). let unterminatedString = "let s = \"never closed" let out1 = SyntaxHighlighter.highlight(unterminatedString, language: "swift", baseColor: .primary) #expect(String(out1.characters) == unterminatedString) let unterminatedComment = "int a; /* still going" let out2 = SyntaxHighlighter.highlight(unterminatedComment, language: "c", baseColor: .primary) #expect(String(out2.characters) == unterminatedComment) let empty = SyntaxHighlighter.highlight("", language: "swift", baseColor: .primary) #expect(String(empty.characters).isEmpty) } } @Suite("MarkdownBlockParser") struct MarkdownBlockParserTests { @Test func extractsFencedCodeBlock() { let blocks = MarkdownBlockParser.parse(text: "Hello\n\n```swift\nlet a = 1\n```", fontSize: 13.5) #expect(blocks.count == 2) guard case .code(let code, let language) = blocks[1].kind else { Issue.record("Expected a code block as the second block") return } #expect(code == "let a = 1") #expect(language == "swift") } @Test func survivesUnterminatedFenceWhileStreaming() { let blocks = MarkdownBlockParser.parse(text: "Intro\n\n```swift\nlet a =", fontSize: 13.5) #expect(!blocks.isEmpty) } }