(collector: StreamCollector)
| 494 | * Provides helpful error messages when assertions fail. |
| 495 | */ |
| 496 | export function assertStreamSuccess(collector: StreamCollector): void { |
| 497 | const allEvents = collector.getEvents(); |
| 498 | |
| 499 | // Check for stream-end |
| 500 | if (!collector.hasStreamEnd()) { |
| 501 | const errorEvent = allEvents.find((e) => "type" in e && e.type === "stream-error"); |
| 502 | if (errorEvent && "error" in errorEvent) { |
| 503 | collector.logEventDiagnostics( |
| 504 | `Stream did not complete successfully. Got stream-error: ${errorEvent.error}` |
| 505 | ); |
| 506 | throw new Error( |
| 507 | `Stream did not complete successfully. Got stream-error: ${errorEvent.error}\n` + |
| 508 | `See detailed event diagnostics above.` |
| 509 | ); |
| 510 | } |
| 511 | collector.logEventDiagnostics("Stream did not emit stream-end event"); |
| 512 | throw new Error( |
| 513 | `Stream did not emit stream-end event.\n` + `See detailed event diagnostics above.` |
| 514 | ); |
| 515 | } |
| 516 | |
| 517 | // Check for errors |
| 518 | if (collector.hasError()) { |
| 519 | const errorEvent = allEvents.find((e) => "type" in e && e.type === "stream-error"); |
| 520 | const errorMsg = errorEvent && "error" in errorEvent ? errorEvent.error : "unknown"; |
| 521 | collector.logEventDiagnostics(`Stream completed but also has error event: ${errorMsg}`); |
| 522 | throw new Error( |
| 523 | `Stream completed but also has error event: ${errorMsg}\n` + |
| 524 | `See detailed event diagnostics above.` |
| 525 | ); |
| 526 | } |
| 527 | |
| 528 | // Check for final message |
| 529 | const finalMessage = collector.getFinalMessage(); |
| 530 | if (!finalMessage) { |
| 531 | collector.logEventDiagnostics("Stream completed but final message is missing"); |
| 532 | throw new Error( |
| 533 | `Stream completed but final message is missing.\n` + `See detailed event diagnostics above.` |
| 534 | ); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * RAII-style helper that starts a collector, runs a function, and stops the collector. |
no test coverage detected