Skip to content

Commit

Permalink
test: ut
Browse files Browse the repository at this point in the history
  • Loading branch information
jayzhang committed Dec 17, 2024
1 parent b29f0de commit 469208f
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 75 deletions.
59 changes: 0 additions & 59 deletions packages/vscode-extension/src/pluginDebugger/connectionChecks.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Protocol } from "devtools-protocol";
import * as vscode from "vscode";
import { ANSIColors } from "../debug/common/debugConstants";
import { CopilotDebugLog } from "./copilotDebugLogOutput";
import { VS_CODE_UI } from "../qm/vsc_ui";
interface BotTextMessage {
messageType: string | undefined;
text: string;
Expand All @@ -29,12 +30,14 @@ export class WebSocketEventHandler {
this.convertBotMessageToChannelOutput(botTextMessage);
num++;
}
} else {
console.warn("Parsed response object does not contain item or messages:", parsedObject);
}
}
} catch (error) {
void vscode.window.showErrorMessage(`Error parsing response, ${(error as Error).message}`);
void VS_CODE_UI.showMessage(
"error",
`Error parsing response, ${(error as Error).message}`,
false
);
vscode.debug.activeDebugConsole.appendLine(
`${ANSIColors.RED} (×) Error: ${ANSIColors.WHITE} Error parsing response: ${
(error as Error).message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { featureFlagManager } from "@microsoft/teamsfx-core";
import * as chai from "chai";
import sinon, { SinonFakeTimers, useFakeTimers } from "sinon";
import {
Expand All @@ -10,11 +9,9 @@ import {
isM365CopilotChatDebugConfiguration,
isOfficeChatUrl,
} from "../../src/pluginDebugger/cdpClient";
import { ExtTelemetry } from "../../src/telemetry/extTelemetry";
import { WebSocketEventHandler } from "../../src/pluginDebugger/webSocketEventHandler";
import * as ui from "../../src/qm/vsc_ui";
import { MockTools } from "../mocks/mockTools";
import { utimes } from "fs";
import { ExtTelemetry } from "../../src/telemetry/extTelemetry";

describe("cdpClient", () => {
const sandbox = sinon.createSandbox();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { LogLevel } from "@microsoft/teamsfx-api";
import * as chai from "chai";
import * as sinon from "sinon";
import * as vscode from "vscode";
import { ANSIColors } from "../../src/debug/common/debugConstants";
import { LogLevel } from "@microsoft/teamsfx-api";
import {
CopilotDebugLog,
logToDebugConsole,
writeCopilotLogToFile,
} from "../../src/pluginDebugger/copilotDebugLogOutput";
import * as globalVariables from "../../src/globalVariables";
import { CopilotDebugLog, logToDebugConsole } from "../../src/pluginDebugger/copilotDebugLogOutput";

describe("copilotDebugLogOutput", () => {
const sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -207,12 +204,15 @@ describe("copilotDebugLogOutput", () => {
},
],
});
const logFilePath = `/path/to/log/Copilot log ${"test".replace(/-|:|\.\d+Z$/g, "")}.txt`;
sandbox.stub(globalVariables, "defaultExtensionLogPath").value("/path/to/log");
sandbox.stub(Date.prototype, "toISOString").returns("test");
const copilotDebugLog = new CopilotDebugLog(logJson);
const appendLineStub = sandbox.stub(vscode.debug.activeDebugConsole, "appendLine");
copilotDebugLog.write();
chai.assert.isTrue(
appendLineStub.calledWith(
`${ANSIColors.GREEN} (√) ${ANSIColors.WHITE}Function execution details: ${ANSIColors.GREEN}Status 200`
`${ANSIColors.GREEN} (√) ${ANSIColors.WHITE}Function execution details: ${ANSIColors.GREEN}Status 200, ${ANSIColors.WHITE}refer to ${ANSIColors.BLUE}${logFilePath}${ANSIColors.WHITE} for all details.`
)
);
chai.assert.isTrue(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import * as chai from "chai";
import * as sinon from "sinon";
import * as vscode from "vscode";
import { WebSocketEventHandler } from "../../src/pluginDebugger/WebSocketEventHandler";
import * as ui from "../../src/qm/vsc_ui";
import { CopilotDebugLog } from "../../src/pluginDebugger/copilotDebugLogOutput";

describe("WebSocketEventHandler", () => {
const sandbox = sinon.createSandbox();

beforeEach(() => {});

afterEach(() => {
sandbox.restore();
});

describe("handleEvent", () => {
it("isWebSocketDataRelevant returns false", () => {
sandbox.stub(WebSocketEventHandler, "isWebSocketDataRelevant").returns(false);
const num = WebSocketEventHandler.handleEvent({ payloadData: '{"type":1' } as any);
chai.assert.equal(num, 0);
});
it("throw error", () => {
const appendLineStub = sandbox.stub(vscode.debug.activeDebugConsole, "appendLine");
const mockUi = { showMessage: () => {} } as any;
sandbox.stub(ui, "VS_CODE_UI").value(mockUi);
const showMessageStub = sandbox.stub(mockUi, "showMessage");
sandbox.stub(WebSocketEventHandler, "isWebSocketDataRelevant").returns(true);
sandbox.stub(WebSocketEventHandler, "splitObjects").throws(new Error("Test"));
const num = WebSocketEventHandler.handleEvent({ payloadData: '{"type":1' } as any);
chai.assert.equal(num, 0);
chai.assert.isTrue(showMessageStub.calledOnce);
chai.assert.isTrue(appendLineStub.calledOnce);
});
it("happy", () => {
sandbox.stub(WebSocketEventHandler, "isWebSocketDataRelevant").returns(true);
const obj = { item: { messages: [] } };
sandbox.stub(WebSocketEventHandler, "splitObjects").returns([JSON.stringify(obj)]);
sandbox.stub(WebSocketEventHandler, "selectBotTextMessages").returns([{} as any]);
sandbox.stub(WebSocketEventHandler, "convertBotMessageToChannelOutput").returns();
const num = WebSocketEventHandler.handleEvent({ payloadData: '{"type":1' } as any);
chai.assert.equal(num, 1);
});
});
describe("isWebSocketDataRelevant", () => {
it("true", () => {
const res = WebSocketEventHandler.isWebSocketDataRelevant({
payloadData: '{"type":2',
} as any);
chai.assert.isTrue(res);
});
it("false", () => {
const res = WebSocketEventHandler.isWebSocketDataRelevant({
payloadData: '{"type":1',
} as any);
chai.assert.isFalse(res);
});
});
describe("splitObjects", () => {
it("happy", () => {
const res = WebSocketEventHandler.splitObjects({
payloadData: "abc\x1e123",
} as any);
chai.assert.deepEqual(res, ["abc", "123"]);
});
});
describe("selectBotTextMessages", () => {
it("happy", () => {
const res = WebSocketEventHandler.selectBotTextMessages({
item: { messages: [{ messageType: "DeveloperLogs" }] },
} as any);
chai.assert.deepEqual(res, [{ messageType: "DeveloperLogs" }] as any);
});
});
describe("convertBotMessageToChannelOutput", () => {
it("happy", () => {
const stub = sandbox.stub(CopilotDebugLog.prototype, "write");
WebSocketEventHandler.convertBotMessageToChannelOutput({
messageType: "DeveloperLogs",
text: JSON.stringify({
functionExecutions: [{ requestUrl: "" }],
}),
} as any);
chai.assert.isTrue(stub.calledOnce);
});
});
describe("convertBotMessageToChannelOutputJson", () => {
it("happy", () => {
const stub = sandbox.stub(WebSocketEventHandler, "prettyPrintJson");
stub.returns(
JSON.stringify({
functionExecutions: [{ requestUrl: "" }],
})
);
WebSocketEventHandler.convertBotMessageToChannelOutputJson({
messageType: "DeveloperLogs",
text: JSON.stringify({
functionExecutions: [{ requestUrl: "" }],
}),
} as any);
chai.assert.isTrue(stub.calledOnce);
});
});
describe("prettyPrintJson", () => {
it("happy", () => {
const res = WebSocketEventHandler.prettyPrintJson(JSON.stringify({ a: "b" }));
chai.assert.equal(res, JSON.stringify({ a: "b" }, null, 2));
});
});
});

0 comments on commit 469208f

Please sign in to comment.