Skip to content

Commit

Permalink
Playbook Generation UI error cases (#1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
TamiTakamiya authored Apr 17, 2024
1 parent 7210b2d commit 2112335
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
13 changes: 10 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ export async function activate(context: ExtensionContext): Promise<void> {
context.subscriptions.push(
vscode.commands.registerCommand(
LightSpeedCommands.LIGHTSPEED_PLAYBOOK_GENERATION,
() => {
showPlaybookGenerationPage(
async () => {
await showPlaybookGenerationPage(
context.extensionUri,
client,
lightSpeedManager.lightSpeedAuthenticationProvider,
Expand Down Expand Up @@ -639,13 +639,20 @@ async function resyncAnsibleInventory(): Promise<void> {
}
}

async function getAuthToken(): Promise<void> {
export async function isLightspeedEnabled(): Promise<boolean> {
if (
!(await workspace.getConfiguration("ansible").get("lightspeed.enabled"))
) {
await window.showErrorMessage(
"Enable lightspeed services from settings to use the feature.",
);
return false;
}
return true;
}

async function getAuthToken(): Promise<void> {
if (!(await isLightspeedEnabled())) {
return;
}
lightSpeedManager.currentModelValue = undefined;
Expand Down
22 changes: 20 additions & 2 deletions src/features/lightspeed/playbookGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Webview, Uri } from "vscode";
import { getNonce } from "../utils/getNonce";
import { getUri } from "../utils/getUri";
import { SettingsManager } from "../../settings";
import { isLightspeedEnabled } from "../../extension";

async function openNewPlaybookEditor(playbook: string) {
const options = {
Expand All @@ -31,8 +32,13 @@ async function generatePlaybook(
client: LanguageClient,
lightSpeedAuthProvider: LightSpeedAuthenticationProvider,
settingsManager: SettingsManager,
panel: vscode.WebviewPanel,
) {
const accessToken = await lightSpeedAuthProvider.grantAccessToken();
if (!accessToken) {
panel.webview.postMessage({ command: "exception" });
}

const playbook: string = await client.sendRequest("playbook/generation", {
accessToken,
URL: settingsManager.settings.lightSpeedService.URL,
Expand All @@ -47,8 +53,13 @@ async function summarizeInput(
client: LanguageClient,
lightSpeedAuthProvider: LightSpeedAuthenticationProvider,
settingsManager: SettingsManager,
panel: vscode.WebviewPanel,
) {
const accessToken = await lightSpeedAuthProvider.grantAccessToken();
if (!accessToken) {
panel.webview.postMessage({ command: "exception" });
}

const summary: string = await client.sendRequest("playbook/summary", {
accessToken,
URL: settingsManager.settings.lightSpeedService.URL,
Expand All @@ -58,12 +69,17 @@ async function summarizeInput(
return summary;
}

export function showPlaybookGenerationPage(
export async function showPlaybookGenerationPage(
extensionUri: vscode.Uri,
client: LanguageClient,
lightSpeedAuthProvider: LightSpeedAuthenticationProvider,
settingsManager: SettingsManager,
) {
// Check if Lightspeed is enabled or not. If it is not, return without opening the panel.
if (!(await isLightspeedEnabled())) {
return;
}

// Create a new panel and update the HTML
const panel = vscode.window.createWebviewPanel(
"noteDetailView",
Expand Down Expand Up @@ -91,6 +107,7 @@ export function showPlaybookGenerationPage(
client,
lightSpeedAuthProvider,
settingsManager,
panel,
);
panel?.dispose();
await openNewPlaybookEditor(playbook);
Expand All @@ -102,6 +119,7 @@ export function showPlaybookGenerationPage(
client,
lightSpeedAuthProvider,
settingsManager,
panel,
);
panel.webview.postMessage({ command: "summary", summary });
break;
Expand Down Expand Up @@ -173,7 +191,7 @@ export function getWebviewContent(webview: Webview, extensionUri: Uri) {
</div>
</div>
<div class="bigIconButtonContainer">
<vscode-button class="bigIconButton" id="submit-button">
<vscode-button class="bigIconButton" id="submit-button" disabled>
<span class="codicon codicon-send" id="submit-icon"></span>
</vscode-button>
</div>
Expand Down
19 changes: 19 additions & 0 deletions src/webview/apps/lightspeed/playbookGeneration/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ window.addEventListener("message", (event) => {
element.value = savedSummary;
break;
}
// When summaries or generations API was processed normally (e.g., API error)
// dismiss the spinner icon here.
case "exception": {
changeDisplay("spinnerContainer", "none");
break;
}
}
});

Expand All @@ -64,6 +70,17 @@ function setListener(id: string, func: any) {
}
}

function setListenerOnTextArea() {
const textArea = document.getElementById("playbook-text-area") as TextArea;
const submitButton = document.getElementById("submit-button") as Button;
if (textArea) {
textArea.addEventListener("input", async () => {
const input = textArea.value;
submitButton.disabled = input.length === 0;
});
}
}

function main() {
setListener("submit-button", submitInput);
setListener("generate-button", generatePlaybook);
Expand All @@ -72,6 +89,8 @@ function main() {
setListener("thumbsdown-button", sendThumbsdown);
setListener("back-button", back);

setListenerOnTextArea();

savedInput = "";
savedSummary = "";
}
Expand Down
35 changes: 31 additions & 4 deletions test/ui-test/lightspeedUiTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ export function lightspeedUIAssetsTest(): void {
await workbench.executeCommand(
"Ansible Lightspeed: Enable experimental features",
);
await new Promise((res) => {
setTimeout(res, 2000);
});
await workbench.executeCommand("View: Close All Editor Groups");
}
});
Expand Down Expand Up @@ -185,12 +182,20 @@ export function lightspeedUIAssetsTest(): void {
);
expect(textArea, "textArea should not be undefined").not.to.be
.undefined;
await textArea.sendKeys("Create an azure network.");
const submitButton = await webView.findWebElement(
By.xpath("//vscode-button[@id='submit-button']"),
);
expect(submitButton, "submitButton should not be undefined").not.to.be
.undefined;
//
// Note: Following line should succeed, but fails for some unknown reasons.
//
// expect((await submitButton.isEnabled()), "submit button should be disabled by default").is.false;
await textArea.sendKeys("Create an azure network.");
expect(
await submitButton.isEnabled(),
"submit button should be enabled now",
).is.true;
submitButton.click();
await new Promise((res) => {
setTimeout(res, 1000);
Expand Down Expand Up @@ -323,4 +328,26 @@ export function lightspeedUIAssetsTest(): void {
}
});
});

describe("Verify playbook generation page is not opened when Lightspeed is not enabled", function () {
let workbench: Workbench;

before(async function () {
workbench = new Workbench();
await workbench.executeCommand(
"Ansible Lightspeed: Enable experimental features",
);
await workbench.executeCommand("View: Close All Editor Groups");
});

it("Playbook generation command shows an error message when Lightspeed is not enabled", async function () {
// Open playbook generation webview.
await workbench.executeCommand("Ansible Lightspeed: Playbook generation");
const notifications = await new Workbench().getNotifications();
const notification = notifications[0];
expect(await notification.getMessage()).equals(
"Enable lightspeed services from settings to use the feature.",
);
});
});
}

0 comments on commit 2112335

Please sign in to comment.