Skip to content

Commit

Permalink
Tests for stream failure
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Dec 7, 2023
1 parent cb0fc81 commit 6a43d45
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Expand Up @@ -37,6 +37,7 @@ const waitForNextWrite = async (stream: Readable): Promise<void> => {

function createLlmSimulator() {
const stream = new PassThrough();

return {
stream,
next: async (msg: ChunkDelta) => {
Expand Down Expand Up @@ -237,7 +238,25 @@ describe('Observability AI Assistant service', () => {
});

describe('after the LLM errors out', () => {
it('adds an error to the stream and closes it', () => {});
beforeEach(async () => {
await llmSimulator.next({ content: ' again' });

llmSimulator.error(new Error('Unexpected error'));

await finished(stream);
});

it('adds an error to the stream and closes it', () => {
expect(dataHandler).toHaveBeenCalledTimes(3);

expect(JSON.parse(dataHandler.mock.lastCall!)).toEqual({
error: {
message: 'Unexpected error',
stack: expect.any(String),
},
type: StreamingChatResponseEventType.ConversationCompletionError,
});
});
});

describe('when generating a title fails', () => {
Expand Down Expand Up @@ -366,7 +385,7 @@ describe('Observability AI Assistant service', () => {
});
});

describe('when completing a conversation with an initial conversation id', () => {
describe('when completig a conversation with an initial conversation id', () => {
let stream: Readable;

let dataHandler: jest.Mock;
Expand Down Expand Up @@ -537,9 +556,9 @@ describe('Observability AI Assistant service', () => {
)
);

await finished(stream);

await llmSimulator.complete();

await finished(stream);
});

it('ends the stream and writes an error', async () => {
Expand Down Expand Up @@ -967,8 +986,6 @@ describe('Observability AI Assistant service', () => {
});

describe('if the observable errors out', () => {
let endStreamPromise: Promise<void>;

beforeEach(async () => {
response$.next({
created: 0,
Expand Down
Expand Up @@ -422,7 +422,7 @@ export class ObservabilityAIAssistantClient {
}

const response = stream
? (executeResult.data as Readable).pipe(new PassThrough())
? (executeResult.data as Readable)
: (executeResult.data as CreateChatCompletionResponse);

if (response instanceof PassThrough) {
Expand Down
Expand Up @@ -10,6 +10,7 @@ import type { Readable } from 'stream';

export function streamIntoObservable(readable: Readable): Observable<string> {
let lineBuffer = '';

return from(readable).pipe(
map((chunk: Buffer) => chunk.toString('utf-8')),
map((part) => {
Expand Down

0 comments on commit 6a43d45

Please sign in to comment.