Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX OS Automation9// File : Packages/ORIXProviderAdapters/Tests/ORIXProviderAdaptersTests/FixtureSSEParserTests.swift10// Description : Unit tests of the incremental SSE parser: line endings,11// comments, multi-line data, arbitrary chunk boundaries.12// Module : ORIXProviderAdaptersTests13// Repository : https://git.spboucher.ai/orix-automation14// Created : 2026-11-2115// Last Modified : 2026-11-2116//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author : Simon-Pierre Boucher22// Position : Professor of Finance23// Institution : Université du Québec en Outaouais (UQO)24// Department : Département des sciences administratives25// Organization : SP Boucher26//27// Website : https://spboucher.ai28// Git : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General : info@spboucher.ai35// Professional : contact@spboucher.ai36// Direct : simon@spboucher.ai37// Research : research@spboucher.ai38// Development : dev@spboucher.ai39// Support : support@spboucher.ai40// Security/Admin : admin@spboucher.ai41// Notifications : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification : Public68// Distribution : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License : Proprietary, all rights reserved (see license file)82// License File : LICENSE83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Testing91import Foundation92@testable import ORIXProviderAdapters9394@Suite("Fixture SSE parser (unit)")95struct FixtureSSEParserTests {96 @Test func basicEventWithName() {97 var p = SSEParser()98 let events = p.feed("event: message_start\ndata: {\"a\":1}\n\n")99 #expect(events == [SSEEvent(event: "message_start", data: "{\"a\":1}")])100 }101102 @Test func multiLineDataIsJoinedWithNewlines() {103 var p = SSEParser()104 let events = p.feed("data: line one\ndata: line two\ndata:\ndata: line four\n\n")105 #expect(events.map(\.data) == ["line one\nline two\n\nline four"])106 }107108 @Test func commentsAndKeepAlivesAreIgnored() {109 var p = SSEParser()110 let events = p.feed(": OPENROUTER PROCESSING\n\n: ping\ndata: x\n: inside\n\n:\n\n")111 #expect(events == [SSEEvent(data: "x")])112 }113114 @Test func crlfAndLoneCRLineEndings() {115 var p = SSEParser()116 #expect(p.feed("event: a\r\ndata: 1\r\n\r\n").map(\.data) == ["1"])117 #expect(p.feed("data: 2\rdata: 3\r\r").map(\.data) == ["2\n3"])118 // CR then LF arriving in different chunks is one line ending, not two (no phantom dispatch).119 #expect(p.feed("data: 4\r").isEmpty)120 #expect(p.feed("\ndata: 5\r").isEmpty)121 #expect(p.feed("\n\r\n").map(\.data) == ["4\n5"])122 }123124 @Test func anyChunkBoundaryGivesTheSameEvents() {125 let transcript = "event: delta\r\ndata: {\"text\":\"héllo — 日本語 🚀\"}\r\n\r\n: keep-alive\n\ndata: a\ndata: b\n\nevent: end\ndata: [DONE]\n\n"126 var whole = SSEParser()127 let expected = whole.feed(transcript)128 #expect(expected.count == 3)129 let bytes = Array(transcript.utf8)130 for size in [1, 2, 3, 5, 7, 11, 64] {131 var p = SSEParser()132 var got: [SSEEvent] = []133 for start in stride(from: 0, to: bytes.count, by: size) { got += p.feed(bytes[start..<min(start + size, bytes.count)]) }134 #expect(got == expected, "chunk size \(size)")135 }136 #expect(expected[0].data.contains("日本語 🚀"))137 }138139 @Test func eventWithoutTrailingBlankLineIsNotDispatched() {140 var p = SSEParser()141 #expect(p.feed("data: complete\n\ndata: cut off").map(\.data) == ["complete"])142 p.finish()143 #expect(p.feed("\n\n").isEmpty)144 }145146 @Test func eventNameWithoutDataIsDroppedAndDoesNotLeak() {147 var p = SSEParser()148 let events = p.feed("event: ping\n\ndata: x\n\n")149 #expect(events == [SSEEvent(event: nil, data: "x")])150 }151152 @Test func fieldFormsIdRetryAndBOM() {153 var p = SSEParser()154 let events = p.feed("\u{FEFF}data:no-space\nid: 7\nretry: 1500\nunknown: y\ndata\n\ndata: two spaces\n\n")155 #expect(events.count == 2)156 #expect(events[0] == SSEEvent(data: "no-space\n", id: "7", retry: 1500))157 #expect(events[1] == SSEEvent(data: " two spaces", id: "7"))158 }159}160