Skip to content

Commit 3237ee4

Browse files
authored
Add CodeTrans UI test (#240)
Signed-off-by: Yue, Wenjiao <wenjiao.yue@intel.com> Signed-off-by: Sun, Xuehao <xuehao.sun@intel.com>
1 parent 9fb917b commit 3237ee4

8 files changed

Lines changed: 147 additions & 32 deletions

File tree

CodeTrans/docker/ui/svelte/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
BASIC_URL = 'http://x.x.x.x:yyyy'
1+
BASE_URL = 'http://backend_address::7777/v1/codetrans'
22

33

CodeTrans/docker/ui/svelte/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"svelte": "^4.0.0"
1515
},
1616
"devDependencies": {
17+
"@playwright/test": "^1.44.1",
1718
"@sveltejs/adapter-auto": "^3.0.0",
1819
"@sveltejs/kit": "^2.0.0",
1920
"@sveltejs/package": "^2.0.0",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (C) 2024 Intel Corporation
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { defineConfig, devices } from "@playwright/test";
5+
6+
/**
7+
* Read environment variables from file.
8+
* https://github.com/motdotla/dotenv
9+
*/
10+
// require('dotenv').config();
11+
12+
/**
13+
* See https://playwright.dev/docs/test-configuration.
14+
*/
15+
export default defineConfig({
16+
testDir: "./tests",
17+
/* Maximum time one test can run for. */
18+
timeout: 30 * 1000,
19+
expect: {
20+
/**
21+
* Maximum time expect() should wait for the condition to be met.
22+
* For example in `await expect(locator).toHaveText();`
23+
*/
24+
timeout: 5000,
25+
},
26+
/* Run tests in files in parallel */
27+
fullyParallel: true,
28+
/* Fail the build on CI if you accidentally left test.only in the source code. */
29+
forbidOnly: !!process.env.CI,
30+
/* Retry on CI only */
31+
retries: process.env.CI ? 2 : 0,
32+
/* Opt out of parallel tests on CI. */
33+
workers: process.env.CI ? 1 : undefined,
34+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
35+
reporter: [["html", { open: "never" }]],
36+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
37+
use: {
38+
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
39+
actionTimeout: 0,
40+
/* Base URL to use in actions like `await page.goto('/')`. */
41+
baseURL: "http://localhost:5173",
42+
43+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
44+
trace: "on-first-retry",
45+
},
46+
47+
/* Configure projects for major browsers */
48+
projects: [
49+
{
50+
name: "webkit",
51+
use: { ...devices["Desktop Safari"] },
52+
},
53+
],
54+
});

CodeTrans/docker/ui/svelte/src/lib/shared/Network.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,7 @@
1414
import { env } from "$env/dynamic/public";
1515
import { SSE } from "sse.js";
1616

17-
const BASIC_URL = env.BASIC_URL;
18-
19-
async function fetchPostRes(url, init) {
20-
try {
21-
const response = await fetch(url, init);
22-
if (!response.ok) throw response.status;
23-
return await response.json();
24-
} catch (error) {
25-
console.error("network error: ", error);
26-
return undefined;
27-
}
28-
}
29-
30-
export async function fetchLanguageResponse(input: string, transform: string, transTo: string) {
31-
const url = `${BASIC_URL}/v1/translation`;
32-
const transData = {
33-
language_from: transform,
34-
language_to: transTo,
35-
source_language: input,
36-
};
37-
38-
const init: RequestInit = {
39-
method: "POST",
40-
body: JSON.stringify(transData),
41-
};
42-
43-
return fetchPostRes(url, init);
44-
}
17+
const BASE_URL = env.BASE_URL;
4518

4619
export async function fetchTextStream(query: string, langFrom, langTo) {
4720
const payload = {
@@ -50,7 +23,7 @@ export async function fetchTextStream(query: string, langFrom, langTo) {
5023
source_code: query,
5124
};
5225

53-
let url = `${BASIC_URL}/v1/code_translation_stream`;
26+
let url = `${BASE_URL}`;
5427

5528
return new SSE(url, {
5629
headers: { "Content-Type": "application/json" },

CodeTrans/docker/ui/svelte/src/routes/+page.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
7171
let copyText = "copy";
7272
// Set default language
73-
let langFrom: string = "Go";
74-
let langTo: string = "Python";
73+
let langFrom: string = "Python";
74+
let langTo: string = "Go";
7575
let languages: Language[] = languagesList;
7676
// Initialize disabled state of input
7777
let inputDisabled: boolean = false;
@@ -103,6 +103,7 @@
103103
104104
eventSource.addEventListener("message", (e: any) => {
105105
let Msg = e.data;
106+
console.log('Msg', Msg);
106107
107108
if (Msg.startsWith("b")) {
108109
const trimmedData = Msg.slice(2, -1);
@@ -176,6 +177,7 @@
176177
rows="25"
177178
placeholder="Input"
178179
bind:value={input}
180+
data-testid="code-input"
179181
/>
180182
{:else}
181183
<div
@@ -196,6 +198,7 @@
196198

197199
<div
198200
class="h-[40rem] bg-[#011627] rounded overflow-auto code-format-style divide-y hiddenScroll"
201+
data-testid="code-output"
199202
>
200203
{#if output !== ""}
201204
<div class="bg-[#282c34] p-2 px-6 text-white flex justify-end border-2 border-none border-b-gray-800">
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (C) 2024 Intel Corporation
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { test, expect, type Page } from "@playwright/test";
5+
6+
// Initialization before each test
7+
test.beforeEach(async ({ page }) => {
8+
await page.goto("/");
9+
});
10+
11+
// Constants definition
12+
const CHAT_ITEMS = ["print('hello')"];
13+
14+
// Helper function: Enter message to chat
15+
async function enterMessageToChat(page: Page, message: string) {
16+
await page.getByTestId("code-input").click();
17+
await page.getByTestId("code-input").fill(message);
18+
await page.waitForTimeout(10000);
19+
await expect(page.getByTestId("code-output")).toContainText("copy");
20+
}
21+
22+
// Test description: New Doc Summary
23+
test.describe("New Doc Summary", () => {
24+
// Test: Enter message to summary
25+
test("should enter message to summary", async ({ page }) => {
26+
await enterMessageToChat(page, CHAT_ITEMS[0]);
27+
});
28+
});

CodeTrans/tests/test_codetrans_on_gaudi.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function start_services() {
3838
export LLM_SERVICE_HOST_IP=${ip_address}
3939
export BACKEND_SERVICE_ENDPOINT="http://${ip_address}:7777/v1/codetrans"
4040

41+
sed -i "s/backend_address/$ip_address/g" $WORKPATH/docker/ui/svelte/.env
42+
4143
# Start Docker Containers
4244
# TODO: Replace the container name with a test-specific name
4345
docker compose -f docker_compose.yaml up -d
@@ -101,6 +103,31 @@ function validate_megaservice() {
101103
'{"language_from": "Golang","language_to": "Python","source_code": "package main\n\nimport \"fmt\"\nfunc main() {\n fmt.Println(\"Hello, World!\");\n}"}'
102104
}
103105

106+
function validate_frontend() {
107+
cd $WORKPATH/docker/ui/svelte
108+
local conda_env_name="CodeTrans_e2e"
109+
export PATH=${HOME}/miniforge3/bin/:$PATH
110+
conda remove -n ${conda_env_name} --all -y
111+
conda create -n ${conda_env_name} python=3.12 -y
112+
source activate ${conda_env_name}
113+
114+
sed -i "s/localhost/$ip_address/g" playwright.config.ts
115+
116+
conda install -c conda-forge nodejs -y && npm install && npm ci && npx playwright install --with-deps
117+
node -v && npm -v && pip list
118+
119+
exit_status=0
120+
npx playwright test || exit_status=$?
121+
122+
if [ $exit_status -ne 0 ]; then
123+
echo "[TEST INFO]: ---------frontend test failed---------"
124+
exit $exit_status
125+
else
126+
echo "[TEST INFO]: ---------frontend test passed---------"
127+
fi
128+
129+
}
130+
104131
function stop_docker() {
105132
cd $WORKPATH/docker/gaudi
106133
container_list=$(cat docker_compose.yaml | grep container_name | cut -d':' -f2)
@@ -119,6 +146,7 @@ function main() {
119146

120147
validate_microservices
121148
validate_megaservice
149+
validate_frontend
122150

123151
stop_docker
124152
echo y | docker system prune

CodeTrans/tests/test_codetrans_on_xeon.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ function start_services() {
3535
export LLM_SERVICE_HOST_IP=${ip_address}
3636
export BACKEND_SERVICE_ENDPOINT="http://${ip_address}:7777/v1/codetrans"
3737

38+
sed -i "s/backend_address/$ip_address/g" $WORKPATH/docker/ui/svelte/.env
39+
3840
# Start Docker Containers
3941
# TODO: Replace the container name with a test-specific name
4042
docker compose -f docker_compose.yaml up -d
@@ -100,6 +102,31 @@ function validate_megaservice() {
100102

101103
}
102104

105+
function validate_frontend() {
106+
cd $WORKPATH/docker/ui/svelte
107+
local conda_env_name="CodeTrans_e2e"
108+
export PATH=${HOME}/miniforge3/bin/:$PATH
109+
conda remove -n ${conda_env_name} --all -y
110+
conda create -n ${conda_env_name} python=3.12 -y
111+
source activate ${conda_env_name}
112+
113+
sed -i "s/localhost/$ip_address/g" playwright.config.ts
114+
115+
conda install -c conda-forge nodejs -y && npm install && npm ci && npx playwright install --with-deps
116+
node -v && npm -v && pip list
117+
118+
exit_status=0
119+
npx playwright test || exit_status=$?
120+
121+
if [ $exit_status -ne 0 ]; then
122+
echo "[TEST INFO]: ---------frontend test failed---------"
123+
exit $exit_status
124+
else
125+
echo "[TEST INFO]: ---------frontend test passed---------"
126+
fi
127+
128+
}
129+
103130
function stop_docker() {
104131
cd $WORKPATH/docker/xeon
105132
container_list=$(cat docker_compose.yaml | grep container_name | cut -d':' -f2)
@@ -118,6 +145,7 @@ function main() {
118145

119146
validate_microservices
120147
validate_megaservice
148+
validate_frontend
121149

122150
stop_docker
123151
echo y | docker system prune

0 commit comments

Comments
 (0)