Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Commit 8601d04

Browse files
committed
centralize collectEvents
1 parent 010f345 commit 8601d04

3 files changed

Lines changed: 21 additions & 24 deletions

File tree

src/models/__tests__/bedrock.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,9 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
22
import { BedrockRuntimeClient } from '@aws-sdk/client-bedrock-runtime'
33
import { BedrockModel } from '../bedrock'
44
import { ContextWindowOverflowError } from '../../errors'
5+
import { collectEvents } from './test-utils'
56
import type { Message } from '../../types/messages'
67
import type { StreamOptions } from '../model'
7-
import type { ModelStreamEvent } from '../streaming'
8-
9-
/**
10-
* Helper function to collect all events from a stream.
11-
*/
12-
async function collectEvents(stream: AsyncIterable<ModelStreamEvent>): Promise<ModelStreamEvent[]> {
13-
const events: ModelStreamEvent[] = []
14-
for await (const event of stream) {
15-
events.push(event)
16-
}
17-
return events
18-
}
198

209
/**
2110
* Helper function to setup mock send with custom stream generator.

src/models/__tests__/openai.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,8 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
33
import OpenAI from 'openai'
44
import { OpenAIModel } from '../openai'
55
import { ContextWindowOverflowError } from '../../errors'
6+
import { collectEvents } from './test-utils'
67
import type { Message } from '../../types/messages'
7-
import type { ModelStreamEvent } from '../streaming'
8-
9-
/**
10-
* Helper function to collect all events from a stream.
11-
*/
12-
async function collectEvents(stream: AsyncIterable<ModelStreamEvent>): Promise<ModelStreamEvent[]> {
13-
const events: ModelStreamEvent[] = []
14-
for await (const event of stream) {
15-
events.push(event)
16-
}
17-
return events
18-
}
198

209
/**
2110
* Helper to create a mock OpenAI client with streaming support

src/models/__tests__/test-utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ABOUTME: Shared test utilities for model tests
2+
// ABOUTME: Contains helper functions for collecting stream events and other common test operations
3+
4+
import type { ModelStreamEvent } from '../streaming'
5+
6+
/**
7+
* Helper function to collect all events from a stream.
8+
* Useful for testing streaming model responses.
9+
*
10+
* @param stream - An async iterable of ModelStreamEvent
11+
* @returns Promise resolving to an array of all emitted events
12+
*/
13+
export async function collectEvents(stream: AsyncIterable<ModelStreamEvent>): Promise<ModelStreamEvent[]> {
14+
const events: ModelStreamEvent[] = []
15+
for await (const event of stream) {
16+
events.push(event)
17+
}
18+
return events
19+
}

0 commit comments

Comments
 (0)