test: add utils

This commit is contained in:
tomasz-io 2024-12-05 16:41:22 -08:00
parent 7e2dfb9dd8
commit 608a157ef5
14 changed files with 239 additions and 95 deletions

View file

@ -1,7 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import Mock from "../../../llm/llms/Mock";
import MockLLM from "../../../llm/llms/Mock";
import { testConfigHandler, testIde } from "../../../test/fixtures";
import { CompletionProvider } from "../../CompletionProvider";
import { AutocompleteInput } from "../../util/types";
@ -30,7 +30,7 @@ export async function testAutocompleteFiltering(
const { prefix, suffix } = parseFimExample(test.input);
// Setup necessary objects
const llm = new Mock({
const llm = new MockLLM({
model: "mock",
});
llm.completion = test.llmOutput;

1
core/index.d.ts vendored
View file

@ -807,6 +807,7 @@ type ModelProvider =
| "nvidia"
| "vllm"
| "mock"
| "test"
| "cerebras"
| "askSage"
| "vertexai"

View file

@ -1,7 +1,7 @@
import { ChatMessage, CompletionOptions, ModelProvider } from "../../index.js";
import { BaseLLM } from "../index.js";
class Mock extends BaseLLM {
class MockLLM extends BaseLLM {
public completion: string = "Test Completion";
static providerName: ModelProvider = "mock";
@ -27,4 +27,4 @@ class Mock extends BaseLLM {
}
}
export default Mock;
export default MockLLM;

39
core/llm/llms/Test.ts Normal file
View file

@ -0,0 +1,39 @@
import { ChatMessage, CompletionOptions, ModelProvider } from "../../index.js";
import { BaseLLM } from "../index.js";
const RESPONSES: Record<string, string> = {
"How are you?": "I'm fine",
};
const HARDCODED_CHAT_RESPONSE = "THIS IS A HARDCODED RESPONSE";
class TestLLM extends BaseLLM {
static providerName: ModelProvider = "test";
private findResponse(prompt: string) {
return Object.entries(RESPONSES).find(([key]) => prompt.includes(key))?.[1];
}
protected async *_streamComplete(
prompt: string,
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<string> {
yield this.findResponse(prompt) || `PROMPT: ${prompt}`;
}
protected async *_streamChat(
messages: ChatMessage[],
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<ChatMessage> {
for (const char of HARDCODED_CHAT_RESPONSE) {
yield {
role: "assistant",
content: char,
};
}
}
}
export default TestLLM;

View file

@ -31,7 +31,7 @@ import LlamaCpp from "./LlamaCpp";
import Llamafile from "./Llamafile";
import LMStudio from "./LMStudio";
import Mistral from "./Mistral";
import Mock from "./Mock";
import MockLLM from "./Mock";
import Moonshot from "./Moonshot";
import Msty from "./Msty";
import Nebius from "./Nebius";
@ -50,6 +50,7 @@ import Vllm from "./Vllm";
import WatsonX from "./WatsonX";
import xAI from "./xAI";
import SiliconFlow from "./SiliconFlow";
import TestLLM from "./Test";
const LLMs = [
Anthropic,
@ -87,7 +88,8 @@ const LLMs = [
Nvidia,
Vllm,
SambaNova,
Mock,
MockLLM,
TestLLM,
Cerebras,
Asksage,
Nebius,

View file

@ -1,86 +0,0 @@
import {
Workbench,
EditorView,
WebView,
By,
WebElement,
WebDriver,
Key,
} from "vscode-extension-tester";
import { expect } from "chai";
const switchToReactIframe = async (driver: WebDriver) => {
const iframes = await driver.findElements(By.css("iframe"));
let continueIFrame: WebElement | undefined = undefined;
for (let i = 0; i < iframes.length; i++) {
const iframe = iframes[i];
const src = await iframe.getAttribute("src");
if (src.includes("extensionId=Continue.continue")) {
continueIFrame = iframe;
break;
}
}
if (!continueIFrame) {
throw new Error("Could not find continue iframe");
}
await driver.switchTo().frame(continueIFrame);
await new Promise((res) => {
setTimeout(res, 500);
});
const reactIFrame = await driver.findElement(By.css("iframe"));
if (!reactIFrame) {
throw new Error("Could not find React iframe");
}
await driver.switchTo().frame(reactIFrame);
};
describe("GUI Test", () => {
let view: WebView;
let driver: WebDriver;
before(async function () {
this.timeout(10000);
await new Workbench().executeCommand("continue.focusContinueInput");
view = new WebView();
driver = view.getDriver();
await switchToReactIframe(driver);
});
after(async () => {
await view.switchBack();
await new EditorView().closeAllEditors();
});
it("should display correct panel description", async () => {
const description = await view.findWebElement(
By.xpath("//*[contains(text(), 'Quickly')]"),
);
expect(await description.getText()).has.string(
"Quickly get up and running using our API keys.",
);
});
it("should allow typing text in the editor", async () => {
const tiptap = await view.findWebElement(By.className("tiptap"));
await tiptap.sendKeys("Hello world!");
expect(await tiptap.getText()).has.string("Hello world!");
// Just to show that we can
await tiptap.sendKeys(Key.ENTER);
await new Promise((res) => {
setTimeout(res, 5000);
});
}).timeout(6000);
});

View file

@ -0,0 +1,58 @@
import {
WebDriver,
WebElement,
WebView,
Workbench,
} from "vscode-extension-tester";
import { GUISelectors } from "../selectors/GUI.selectors";
export class GUIActions {
public static switchToReactIframe = async (driver: WebDriver) => {
const iframes = await GUISelectors.getAllIframes(driver);
let continueIFrame: WebElement | undefined = undefined;
for (let i = 0; i < iframes.length; i++) {
const iframe = iframes[i];
const src = await iframe.getAttribute("src");
if (src.includes("extensionId=Continue.continue")) {
continueIFrame = iframe;
break;
}
}
if (!continueIFrame) {
throw new Error("Could not find continue iframe");
}
await driver.switchTo().frame(continueIFrame);
await new Promise((res) => {
setTimeout(res, 500);
});
const reactIFrame = await GUISelectors.getReactIframe(driver);
if (!reactIFrame) {
throw new Error("Could not find React iframe");
}
await driver.switchTo().frame(reactIFrame);
};
public static openGui = async () => {
return new Workbench().executeCommand("continue.focusContinueInput");
};
public static selectModelFromDropdown = async (
view: WebView,
option: string,
) => {
const dropdownButton = await GUISelectors.getModelDropdownButton(view);
await dropdownButton.click();
const dropdownOption = await GUISelectors.getModelDropdownOption(
view,
option,
);
await dropdownOption.click();
};
}

View file

@ -0,0 +1,44 @@
import { By, WebDriver, WebView } from "vscode-extension-tester";
import { SelectorUtils } from "./SelectorUtils";
export class GUISelectors {
public static getDescription(view: WebView) {
return view.findWebElement(By.xpath("//*[contains(text(), 'Quickly')]"));
}
public static getTipTapEditor(view: WebView) {
return view.findWebElement(By.className("tiptap"));
}
public static getAllIframes(driver: WebDriver) {
return driver.findElements(By.css("iframe"));
}
public static getReactIframe(driver: WebDriver) {
return driver.findElement(By.css("iframe"));
}
public static getSubmitInputButton(view: WebView) {
return SelectorUtils.getElementByDataTestId(view, "submit-input-button");
}
public static getModelDropdownButton(view: WebView) {
return SelectorUtils.getElementByDataTestId(view, "model-select-button");
}
public static getListbox(view: WebView) {
const listbox = view.findWebElement(By.css('[role="listbox"]'));
}
public static getModelDropdownOption(view: WebView, option: string) {
return view.findWebElement(
By.xpath(`//*[@role="listbox"]//*[contains(text(), "${option}")]`),
);
}
public static getThreadMessageByText(view: WebView, text: string) {
return view.findWebElement(
By.xpath(`//*[@class="thread-message"]//*[contains(text(), "${text}")]`),
);
}
}

View file

@ -0,0 +1,16 @@
import { By, WebElement, WebView } from "vscode-extension-tester";
export class SelectorUtils {
/**
* Finds a web element by its data-testid attribute within a WebView.
* @param view - The WebView to search within.
* @param testId - The data-testid value to search for.
* @returns A promise that resolves to the WebElement found.
*/
public static getElementByDataTestId(
view: WebView,
testId: string,
): Promise<WebElement> {
return view.findWebElement(By.css(`[data-testid='${testId}']`));
}
}

View file

@ -0,0 +1,4 @@
{
"name": "Untitled.txt",
"content": "Hello, world!"
}

View file

@ -0,0 +1,61 @@
import {
EditorView,
WebView,
WebDriver,
Key,
VSBrowser,
} from "vscode-extension-tester";
import { expect } from "chai";
import { GUIActions } from "../actions/GUI.actions";
import { GUISelectors } from "../selectors/GUI.selectors";
import * as path from "path";
describe("GUI Test", () => {
let view: WebView;
let driver: WebDriver;
before(async function () {
this.timeout(10000000);
await VSBrowser.instance.openResources(path.join("e2e/test-files"));
await GUIActions.openGui();
view = new WebView();
driver = view.getDriver();
await GUIActions.switchToReactIframe(driver);
// await new Promise((res) => {
// setTimeout(res, 10000000);
// });
});
after(async () => {
await view.switchBack();
await new EditorView().closeAllEditors();
});
it("should display correct panel description", async () => {
const description = await GUISelectors.getDescription(view);
expect(await description.getText()).has.string(
"Quickly get up and running using our API keys.",
);
});
it.only("should allow typing text in the editor", async () => {
await GUIActions.selectModelFromDropdown(view, "Mock");
await GUIActions.selectModelFromDropdown(view, "TEST LLM");
const tiptap = await GUISelectors.getTipTapEditor(view);
await tiptap.sendKeys("How are you?");
(await GUISelectors.getSubmitInputButton(view)).click();
await new Promise((res) => {
setTimeout(res, 600000000);
});
await GUISelectors.getThreadMessageByText(view, "I'm fine");
}).timeout(600000000);
});

View file

@ -2,7 +2,7 @@
"name": "continue",
"icon": "media/icon.png",
"author": "Continue Dev, Inc",
"version": "0.9.244",
"version": "0.9.251",
"repository": {
"type": "git",
"url": "https://github.com/continuedev/continue"
@ -613,6 +613,7 @@
"quick-test": "npm run build-test && node ./out/runTestOnVSCodeHost.js",
"prepackage": "node scripts/prepackage.js",
"package": "node scripts/package.js",
"e2e:package": "CONTINUE_GLOBAL_DIR=e2e/test-files node scripts/package.js",
"package-all": "node scripts/package-all.js",
"package:pre-release": "node scripts/package.js --pre-release",
"build:rust": "cargo-cp-artifact -ac sync sync.node -- cargo build --manifest-path ../../sync/Cargo.toml --message-format=json-render-diagnostics",
@ -625,8 +626,10 @@
"e2e:sign-vscode": "codesign --entitlements entitlements.plist --deep --force -s - './e2e/storage/Visual Studio Code.app'",
"e2e:copy-vsix": "chmod +x ./e2e/get-latest-vsix.sh && bash ./e2e/get-latest-vsix.sh",
"e2e:install-vsix": "extest install-vsix -f ./e2e/vsix/continue.vsix --extensions_dir ./e2e/.test-extensions --storage ./e2e/storage",
"e2e:test": "extest run-tests './e2e/_output/*.test.js' --code_settings settings.json --extensions_dir ./e2e/.test-extensions --storage ./e2e/storage",
"e2e:all": "npm run e2e:compile && npm run e2e:create-storage && npm run e2e:get-chromedriver && npm run e2e:get-vscode && npm run e2e:sign-vscode && npm run e2e:copy-vsix && npm run e2e:install-vsix && npm run e2e:test",
"e2e:test": "extest run-tests './e2e/_output/tests/*.test.js' --code_settings settings.json --extensions_dir ./e2e/.test-extensions --storage ./e2e/storage",
"e2e:clean": "rm -rf ./e2e/_output",
"e2e:all": "npm run e2e:compile && npm run e2e:create-storage && npm run e2e:get-chromedriver && npm run e2e:get-vscode && npm run e2e:sign-vscode && npm run e2e:copy-vsix && npm run e2e:install-vsix && npm run e2e:test && npm run e2e:clean",
"e2e:quick": "CONTINUE_GLOBAL_DIR=e2e/test-files npm run e2e:compile && npm run e2e:test && npm run e2e:clean",
"e2e:ci": "npm run e2e:compile && npm run e2e:create-storage && npm run e2e:get-chromedriver && npm run e2e:get-vscode && npm run e2e:copy-vsix && npm run e2e:install-vsix && npm run e2e:test"
},
"devDependencies": {

View file

@ -208,6 +208,7 @@ function InputToolbar(props: InputToolbarProps) {
)}
<EnterButton
data-testid="submit-input-button"
onClick={async (e) => {
if (props.onEnter) {
props.onEnter({

View file

@ -316,6 +316,7 @@ function ModelSelect() {
>
<div className="relative">
<StyledListboxButton
data-testid="model-select-button"
ref={buttonRef}
className="h-[18px] overflow-hidden"
style={{ padding: 0 }}