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";
|
2023-08-04 06:01:18 +08:00
|
|
|
import Chatbot from "./index.svelte";
|
2023-08-16 02:21:41 +08:00
|
|
|
import type { LoadingStatus } from "@gradio/statustracker";
|
2023-07-14 00:41:47 +08:00
|
|
|
import type { FileData } from "@gradio/upload";
|
2022-07-22 02:12:46 +08:00
|
|
|
|
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",
|
2023-07-14 00:41:47 +08:00
|
|
|
value: undefined,
|
2023-07-04 22:41:50 +08:00
|
|
|
root: "",
|
|
|
|
root_url: "",
|
2023-07-14 00:41:47 +08:00
|
|
|
latex_delimiters: [],
|
2023-07-04 22:41:50 +08:00
|
|
|
theme_mode: "dark"
|
|
|
|
});
|
|
|
|
|
2023-07-14 00:41:47 +08:00
|
|
|
let value: [string | FileData | null, string | FileData | null][] = Array(
|
|
|
|
2
|
|
|
|
).fill([
|
2023-07-04 22:41:50 +08:00
|
|
|
{
|
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
|
|
|
|
});
|
|
|
|
|
2023-07-14 00:41:47 +08:00
|
|
|
const image = getAllByTestId("chatbot-image") as HTMLImageElement[];
|
2023-07-04 22:41:50 +08:00
|
|
|
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",
|
|
|
|
root: "",
|
|
|
|
root_url: "",
|
2023-07-14 00:41:47 +08:00
|
|
|
latex_delimiters: [],
|
2023-07-04 22:41:50 +08:00
|
|
|
theme_mode: "dark"
|
|
|
|
});
|
2023-07-14 00:41:47 +08:00
|
|
|
let value: Array<[string | FileData | null, string | FileData | null]> =
|
|
|
|
Array(2).fill([
|
|
|
|
{
|
|
|
|
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
|
|
|
await component.$set({
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
|
2023-07-14 00:41:47 +08:00
|
|
|
const video = getAllByTestId("chatbot-video") as HTMLVideoElement[];
|
2023-07-04 22:41:50 +08:00
|
|
|
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",
|
|
|
|
root: "",
|
|
|
|
root_url: "",
|
2023-07-14 00:41:47 +08:00
|
|
|
latex_delimiters: [],
|
2023-07-04 22:41:50 +08:00
|
|
|
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
|
|
|
|
});
|
|
|
|
|
2023-07-14 00:41:47 +08:00
|
|
|
const audio = getAllByTestId("chatbot-audio") as HTMLAudioElement[];
|
2023-07-04 22:41:50 +08:00
|
|
|
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
|
|
|
});
|
2023-07-10 22:34:01 +08:00
|
|
|
|
|
|
|
test("renders hyperlinks to file bot and user messages", async () => {
|
|
|
|
const { component, getAllByTestId } = await render(Chatbot, {
|
|
|
|
loading_status,
|
|
|
|
label: "chatbot",
|
|
|
|
root: "",
|
|
|
|
root_url: "",
|
2023-07-14 00:41:47 +08:00
|
|
|
latex_delimiters: [],
|
2023-07-10 22:34:01 +08:00
|
|
|
theme_mode: "dark"
|
|
|
|
});
|
|
|
|
|
|
|
|
let value = Array(2).fill([
|
|
|
|
{
|
|
|
|
name: "https://gradio-builds.s3.amazonaws.com/demo-files/titanic.csv",
|
|
|
|
mime_type: "text/csv",
|
|
|
|
alt_text: null,
|
|
|
|
data: "https://gradio-builds.s3.amazonaws.com/demo-files/titanic.csv",
|
|
|
|
is_file: true
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
await component.$set({
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
|
2023-07-14 00:41:47 +08:00
|
|
|
const file_link = getAllByTestId("chatbot-file") as HTMLAnchorElement[];
|
2023-07-10 22:34:01 +08:00
|
|
|
assert.isTrue(file_link[0].href.includes("titanic.csv"));
|
|
|
|
assert.isTrue(file_link[0].href.includes("titanic.csv"));
|
|
|
|
});
|
2022-03-23 23:19:12 +08:00
|
|
|
});
|