2022-03-23 23:19:12 +08:00
|
|
|
import { test, describe, assert, afterEach } from "vitest";
|
2023-07-04 22:41:50 +08:00
|
|
|
import { cleanup, render } from "@gradio/tootils";
|
2022-03-23 23:19:12 +08:00
|
|
|
import Chatbot from "./Chatbot.svelte";
|
2022-07-22 02:12:46 +08:00
|
|
|
import type { LoadingStatus } from "../StatusTracker/types";
|
|
|
|
|
2023-03-13 22:21:03 +08:00
|
|
|
const loading_status: LoadingStatus = {
|
2022-07-22 02:12:46 +08:00
|
|
|
eta: 0,
|
|
|
|
queue_position: 1,
|
2022-08-18 02:17:56 +08:00
|
|
|
queue_size: 1,
|
2023-03-13 22:21:03 +08:00
|
|
|
status: "complete",
|
2022-07-22 02:12:46 +08:00
|
|
|
scroll_to_output: false,
|
|
|
|
visible: true,
|
2023-06-15 23:24:17 +08:00
|
|
|
fn_index: 0,
|
|
|
|
show_progress: "full"
|
2022-07-22 02:12:46 +08:00
|
|
|
};
|
2022-03-23 23:19:12 +08:00
|
|
|
|
2023-07-04 22:41:50 +08:00
|
|
|
describe("Chatbot", () => {
|
2022-03-23 23:19:12 +08:00
|
|
|
afterEach(() => cleanup());
|
|
|
|
|
2023-06-15 23:24:17 +08:00
|
|
|
test("renders user and bot messages", async () => {
|
2023-06-29 02:37:21 +08:00
|
|
|
const { getAllByTestId } = await render(Chatbot, {
|
2022-07-22 02:12:46 +08:00
|
|
|
loading_status,
|
2023-07-04 22:41:50 +08:00
|
|
|
label: "chatbot",
|
2023-03-04 10:17:48 +08:00
|
|
|
value: [["user message one", "bot message one"]],
|
2023-03-13 22:21:03 +08:00
|
|
|
root: "",
|
2023-06-15 23:24:17 +08:00
|
|
|
root_url: "",
|
|
|
|
latex_delimiters: [{ left: "$$", right: "$$", display: true }],
|
|
|
|
theme_mode: "dark"
|
2022-03-23 23:19:12 +08:00
|
|
|
});
|
|
|
|
|
2022-05-02 16:41:20 +08:00
|
|
|
const bot = getAllByTestId("user")[0];
|
|
|
|
const user = getAllByTestId("bot")[0];
|
2022-03-23 23:19:12 +08:00
|
|
|
|
2023-07-04 22:41:50 +08:00
|
|
|
assert.exists(bot);
|
|
|
|
assert.exists(user);
|
2022-03-23 23:19:12 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("renders additional message as they are passed", async () => {
|
2023-06-29 02:37:21 +08:00
|
|
|
const { component, getAllByTestId } = await render(Chatbot, {
|
2022-07-22 02:12:46 +08:00
|
|
|
loading_status,
|
2023-07-04 22:41:50 +08:00
|
|
|
label: "chatbot",
|
2023-03-04 10:17:48 +08:00
|
|
|
value: [["user message one", "bot message one"]],
|
2023-03-13 22:21:03 +08:00
|
|
|
root: "",
|
2023-06-15 23:24:17 +08:00
|
|
|
root_url: "",
|
|
|
|
latex_delimiters: [{ left: "$$", right: "$$", display: true }],
|
|
|
|
theme_mode: "dark"
|
2022-03-23 23:19:12 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
await component.$set({
|
|
|
|
value: [
|
2022-05-02 16:41:20 +08:00
|
|
|
["user message one", "bot message one"],
|
|
|
|
["user message two", "bot message two"]
|
2022-03-23 23:19:12 +08:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
const user_2 = getAllByTestId("user");
|
2022-05-02 16:41:20 +08:00
|
|
|
const bot_2 = getAllByTestId("bot");
|
2022-03-23 23:19:12 +08:00
|
|
|
|
|
|
|
assert.equal(user_2.length, 2);
|
2022-05-02 16:41:20 +08:00
|
|
|
assert.equal(bot_2.length, 2);
|
2022-03-23 23:19:12 +08:00
|
|
|
|
2023-07-04 22:41:50 +08:00
|
|
|
assert.exists(user_2[1]);
|
|
|
|
assert.exists(bot_2[1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("renders image bot and user messages", async () => {
|
|
|
|
const { component, getAllByTestId } = await render(Chatbot, {
|
|
|
|
loading_status,
|
|
|
|
label: "chatbot",
|
|
|
|
value: null,
|
|
|
|
root: "",
|
|
|
|
root_url: "",
|
|
|
|
latex_delimiters: null,
|
|
|
|
theme_mode: "dark"
|
|
|
|
});
|
|
|
|
|
|
|
|
let value = Array(2).fill([
|
|
|
|
{
|
2023-07-07 01:43:49 +08:00
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/cheetah1.jpg",
|
|
|
|
mime_type: "image/jpeg",
|
|
|
|
alt_text: null,
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
2023-07-04 22:41:50 +08:00
|
|
|
}
|
2023-07-07 01:43:49 +08:00
|
|
|
]);
|
2023-07-04 22:41:50 +08:00
|
|
|
|
|
|
|
await component.$set({
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
|
|
|
|
const image = getAllByTestId("chatbot-image");
|
|
|
|
assert.isTrue(image[0].src.includes("cheetah1.jpg"));
|
|
|
|
assert.isTrue(image[1].src.includes("cheetah1.jpg"));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("renders video bot and user messages", async () => {
|
|
|
|
const { component, getAllByTestId } = await render(Chatbot, {
|
|
|
|
loading_status,
|
|
|
|
label: "chatbot",
|
|
|
|
value: null,
|
|
|
|
root: "",
|
|
|
|
root_url: "",
|
|
|
|
latex_delimiters: null,
|
|
|
|
theme_mode: "dark"
|
|
|
|
});
|
|
|
|
let value = Array(2).fill([
|
|
|
|
{
|
2023-07-07 01:43:49 +08:00
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/video_sample.mp4",
|
|
|
|
mime_type: "video/mp4",
|
|
|
|
alt_text: null,
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
2023-07-04 22:41:50 +08:00
|
|
|
}
|
2023-07-07 01:43:49 +08:00
|
|
|
]);
|
2023-07-04 22:41:50 +08:00
|
|
|
await component.$set({
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
|
|
|
|
const video = getAllByTestId("chatbot-video");
|
|
|
|
assert.isTrue(video[0].src.includes("video_sample.mp4"));
|
|
|
|
assert.isTrue(video[1].src.includes("video_sample.mp4"));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("renders audio bot and user messages", async () => {
|
|
|
|
const { component, getAllByTestId } = await render(Chatbot, {
|
|
|
|
loading_status,
|
|
|
|
label: "chatbot",
|
|
|
|
value: null,
|
|
|
|
root: "",
|
|
|
|
root_url: "",
|
|
|
|
latex_delimiters: null,
|
|
|
|
theme_mode: "dark"
|
|
|
|
});
|
|
|
|
|
|
|
|
let value = Array(2).fill([
|
|
|
|
{
|
2023-07-07 01:43:49 +08:00
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/audio_sample.wav",
|
|
|
|
mime_type: "audio/wav",
|
|
|
|
alt_text: null,
|
|
|
|
data: null,
|
|
|
|
is_file: true
|
2023-07-04 22:41:50 +08:00
|
|
|
}
|
2023-07-07 01:43:49 +08:00
|
|
|
]);
|
2023-07-04 22:41:50 +08:00
|
|
|
|
|
|
|
await component.$set({
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
|
|
|
|
const audio = getAllByTestId("chatbot-audio");
|
|
|
|
assert.isTrue(audio[0].src.includes("audio_sample.wav"));
|
|
|
|
assert.isTrue(audio[1].src.includes("audio_sample.wav"));
|
2022-03-23 23:19:12 +08:00
|
|
|
});
|
|
|
|
});
|