Skip to content

Commit

Permalink
added tests for limitConversationContext
Browse files Browse the repository at this point in the history
  • Loading branch information
ahiipsa committed Nov 10, 2023
1 parent 4893b41 commit a876637
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/modules/open-ai/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* eslint-disable */
import { limitConversationContext } from './helpers'
import { describe, expect, test, it } from "@jest/globals";
import { CreateChatCompletionRequestMessage } from "openai/resources/chat";

describe('limitConversationContext', () => {
it('waits for the promise to resolve', () => {

let conversation: CreateChatCompletionRequestMessage[] = [
{ role: "assistant", content: "Welcome to the platform!" },
{ role: "user", content: "Your order has been confirmed." },
{ role: "assistant", content: "Please adhere to the community guidelines." },
];

let limitedConversation = limitConversationContext(conversation, 42)

expect(limitedConversation.length).toEqual(1)
})

it('shouldAttachPartOfAMessage', () => {
let conversation: CreateChatCompletionRequestMessage[] = [
{ role: "assistant", content: "Welcome to the platform!"},
{ role: "user", content: "Your order has been confirmed."},
{ role: "assistant", content: "Please adhere to the community guidelines."},
];

let limitedConversation = limitConversationContext(conversation, 52)

expect(limitedConversation.length).toEqual(2)
expect(limitedConversation[0].content).toEqual("confirmed.")
expect(limitedConversation[1].content).toEqual("Please adhere to the community guidelines.")
})

it('shouldReturnAllMessages', () => {
let conversation: CreateChatCompletionRequestMessage[] = [
{ role: "assistant", content: "Welcome to the platform!"},
{ role: "user", content: "Your order has been confirmed."},
{ role: "assistant", content: "Please adhere to the community guidelines."},
];

let limitedConversation = limitConversationContext(conversation, 100)

expect(limitedConversation.length).toEqual(3)
expect(limitedConversation[0].content).toEqual("Welcome to the platform!")
expect(limitedConversation[1].content).toEqual("Your order has been confirmed.")
expect(limitedConversation[2].content).toEqual("Please adhere to the community guidelines.")
})

it('shouldFilterEmpty', () => {
let emptyConversation: CreateChatCompletionRequestMessage[] = [];

let limitedEmpty = limitConversationContext(emptyConversation, 100)

expect(limitedEmpty.length).toEqual(0)
})

it('shouldFilterEmptyMessages', () => {
let conversation: CreateChatCompletionRequestMessage[] = [
{ role: "assistant", content: ""},
{ role: "assistant", content: "Please adhere to the community guidelines."},
{ role: "assistant", content: ""},
{ role: "assistant", content: null},
];

let cleanConversation = limitConversationContext(conversation, 100)

expect(cleanConversation.length).toEqual(1)
expect(cleanConversation[0].content).toEqual("Please adhere to the community guidelines.")
})

it('shouldPreserveOrderOfMessages', () => {
let conversation: CreateChatCompletionRequestMessage[] = [
{ role: "assistant", content: "one"},
{ role: "assistant", content: "two"},
{ role: "assistant", content: "three"},
];

let limitedc = limitConversationContext(conversation, 100)

expect(limitedc[0].content).toEqual("one")
expect(limitedc[1].content).toEqual("two")
expect(limitedc[2].content).toEqual("three")
})
})

0 comments on commit a876637

Please sign in to comment.