Skip to content

Commit

Permalink
test: wait for ChatSearch to initialize
Browse files Browse the repository at this point in the history
Update the chat integration tests so they wait for ChatSearch to fully
initialize before continuing.
  • Loading branch information
apotterri committed Sep 14, 2024
1 parent b829dbf commit 3d4773b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/webviews/chatSearchWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type ExplainResponse = {
export default class ChatSearchWebview {
private webviewList = new WebviewList();
private filterStore: FilterStore;
private _onWebview?: (wv: vscode.Webview) => void;

private constructor(
private readonly context: vscode.ExtensionContext,
Expand All @@ -58,6 +59,10 @@ export default class ChatSearchWebview {
return this.webviewList.currentWebview;
}

set onWebview(cb: (wv: vscode.Webview) => void) {
this._onWebview = cb;
}

async doPinFiles(panel: vscode.WebviewPanel, reqs: PinFileRequest[]) {
const maxPinnedFileSize = ExtensionSettings.maxPinnedFileSizeKB * 1024;
type PinFileRequestWithLength = PinFileRequest & { contentLength?: number };
Expand Down Expand Up @@ -144,6 +149,9 @@ export default class ChatSearchWebview {
}
);
this.webviewList.enroll(panel);
if (this._onWebview) {
this._onWebview(panel.webview);
}

panel.webview.html = getWebviewContent(
panel.webview,
Expand Down
30 changes: 15 additions & 15 deletions test/integration/chat/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('chat', () => {

beforeEach(() => (sandbox = sinon.createSandbox()));
afterEach(async () => {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
sandbox.restore();
});

Expand All @@ -44,23 +44,23 @@ describe('chat', () => {
let chatView: vscode.Webview;

beforeEach(async () => {
await vscode.commands.executeCommand('appmap.explain');
const v = chatSearchWebview.currentWebview;
assert(v);

await new Promise<void>((resolve) => {
v.onDidReceiveMessage(async (msg) => {
if (msg.command === 'chat-search-ready') {
resolve();
}
});
const p = new Promise<vscode.Webview>((resolve) => {
const waitForLoaded = (wv: vscode.Webview): void => {
wv.onDidReceiveMessage((msg) => {
if (msg.command === 'chat-search-loaded') {
resolve(wv);
}
});
};
chatSearchWebview.onWebview = waitForLoaded;
});
chatView = v;
await vscode.commands.executeCommand('appmap.explain');
chatView = await p;
});

const waitForPin = () =>
new Promise<PinFileEvent>((resolve) => {
chatView.onDidReceiveMessage(async (msg) => {
chatView.onDidReceiveMessage((msg) => {
if (msg.command === 'pin') {
resolve(msg.event);
}
Expand All @@ -82,7 +82,7 @@ describe('chat', () => {

describe('appmap.addToContext', () => {
it('pins a file', async () => {
return withTmpDir(async (tmpDir) => {
await withTmpDir(async (tmpDir) => {
const files = await newTmpFiles(tmpDir);
sandbox.stub(chatSearchWebview, 'chooseFilesToPin').resolves(files);
const p = waitForPin();
Expand All @@ -107,7 +107,7 @@ describe('chat', () => {

describe('appmap.editor.title.addToContext', () => {
it('pins a file', async () => {
return withTmpDir(async (tmpDir) => {
await withTmpDir(async (tmpDir) => {
const files = await newTmpFiles(tmpDir);
const p = waitForPin();
await vscode.commands.executeCommand('appmap.editor.title.addToContext', files[0]);
Expand Down
4 changes: 4 additions & 0 deletions web/src/chatSearchView.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ export default function mountChatSearchView() {
app.$on('pin', (event) => {
vscode.postMessage({ command: 'pin', event });
});

app.$on('chat-search-loaded', () => {
vscode.postMessage({ command: 'chat-search-loaded'});
});
});

vscode.postMessage({ command: 'chat-search-ready' });
Expand Down

0 comments on commit 3d4773b

Please sign in to comment.