Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adeelehsan committed Jul 17, 2024
1 parent 6a26353 commit 094fd32
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 146 deletions.
2 changes: 1 addition & 1 deletion src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as useChatInterface from "./useChat";

const MIN_PROPS = {
customerId: "mock-customer-id",
corpusIds: ["1", "2"],
corpusKey: "1",
apiKey: "mock-api-key"
};

Expand Down
152 changes: 7 additions & 145 deletions src/useChat.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useChat } from "./useChat";
import { act, renderHook } from "@testing-library/react-hooks";
import { waitFor } from "@testing-library/react";
import * as sendSearchRequestInterface from "./utils/sendSearchRequest";
import * as streamQueryInterface from "@vectara/stream-query-client";

jest.mock("@vectara/stream-query-client", () => {
Expand All @@ -11,43 +10,11 @@ jest.mock("@vectara/stream-query-client", () => {
};
});

const MOCK_API_RESPONSE = {
document: [
{
id: "mock-doc-id",
metadata: [{ name: "mock-name", value: "mock-value" }]
}
],
response: [
{
corpusKey: {
customerId: 0,
corpusId: 1
},
documentIndex: 0,
metadata: [],
score: 0.8,
text: "mock-text"
}
],
summary: [
{
chat: {
conversationId: "mock-conversation-id",
turnId: "mock-turn-id",
text: "mock-answer"
}
}
]
};

describe("useChat", () => {
let sendSearchRequestSpy: jest.SpyInstance;
let streamQuerySpy: jest.SpyInstance;

beforeEach(() => {
sendSearchRequestSpy = jest.spyOn(sendSearchRequestInterface, "sendSearchRequest");
streamQuerySpy = jest.spyOn(streamQueryInterface, "streamQuery");
streamQuerySpy = jest.spyOn(streamQueryInterface, "streamQueryV2");
});

afterEach(() => {
Expand All @@ -57,16 +24,15 @@ describe("useChat", () => {
describe("streaming", () => {
it("should send messages and update hook values", async () => {
const { result } = renderHook(() =>
useChat({ customerId: "mock-customer-id", corpusIds: ["1"], apiKey: "mock-api-key" })
useChat({ customerId: "mock-customer-id", corpusKey: "1", apiKey: "mock-api-key" })
);

streamQuerySpy.mockImplementation(async (_, onStreamUpdate) => {
await onStreamUpdate({
references: [],
detail: null,
streamQuerySpy.mockImplementation(async (_, onStreamEvent) => {
const event = {
type: "generationChunk",
updatedText: "mock-updated-text",
isDone: false
});
};
await onStreamEvent(event);
});

await act(async () => {
Expand Down Expand Up @@ -101,108 +67,4 @@ describe("useChat", () => {
});
});
});

describe("non-streaming", () => {
it("should send messages and update message history", async () => {
const { result } = renderHook(() =>
useChat({ customerId: "mock-customer-id", corpusIds: ["1"], apiKey: "mock-api-key", enableStreaming: false })
);

sendSearchRequestSpy.mockImplementation(() => Promise.resolve(MOCK_API_RESPONSE));

await act(async () => {
await result.current.sendMessage({ query: "mock-query" });
});

expect(sendSearchRequestSpy).toHaveBeenCalledWith(
expect.objectContaining({
queryValue: "mock-query"
})
);

expect(result.current.messageHistory.length).toEqual(1);
});

it("should reflect error state", async () => {
const { result } = renderHook(() =>
useChat({ customerId: "mock-customer-id", corpusIds: ["1"], apiKey: "mock-api-key", enableStreaming: false })
);
sendSearchRequestSpy.mockImplementation(() => {
throw "error";
});

await act(async () => {
result.current.sendMessage({ query: "mock-query" });
});

expect(result.current.hasError).toEqual(true);
});

it("should reflect loading state", async () => {
const { result } = renderHook(() =>
useChat({ customerId: "mock-customer-id", corpusIds: ["1"], apiKey: "mock-api-key" })
);
sendSearchRequestSpy.mockImplementation(() => {
return new Promise(() => {});
});

act(() => {
result.current.sendMessage({ query: "mock-query" });
});

expect(result.current.isLoading).toEqual(true);
});
});

it("should be able to reset the conversation", async () => {
const { result } = renderHook(() =>
useChat({ customerId: "mock-customer-id", corpusIds: ["1"], apiKey: "mock-api-key", enableStreaming: false })
);
sendSearchRequestSpy.mockImplementation(() => Promise.resolve(MOCK_API_RESPONSE));

await act(async () => {
await result.current.sendMessage({ query: "mock-query" });
await result.current.sendMessage({ query: "mock-query-2" });
});

// Assert that the second request uses the current conversation id.
expect(sendSearchRequestSpy).toHaveBeenCalledWith(
expect.objectContaining({
chat: {
conversationId: "mock-conversation-id"
}
})
);

sendSearchRequestSpy.mockImplementation(() =>
Promise.resolve({
...MOCK_API_RESPONSE,
summary: [
{
chat: {
conversationId: "mock-conversation-id-2",
turnId: "mock-turn-id",
text: "mock-answer"
}
}
]
})
);

await act(async () => {
await result.current.startNewConversation();
});

expect(result.current.messageHistory.length).toEqual(0);

await act(async () => {
await result.current.sendMessage({ query: "mock-query-3" });
});

const calls = sendSearchRequestSpy.mock.calls;
const recentSendSearchRequestCall = calls[calls.length - 1][0];

// Assert that the request after reset is has no conversation id.
expect(recentSendSearchRequestCall.chat.conversationId).toEqual(undefined);
});
});

0 comments on commit 094fd32

Please sign in to comment.