From d8080d72094670e0dde868d5c4c5abb76f59f0f6 Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Tue, 24 Dec 2024 17:34:18 -0800 Subject: [PATCH] Fix test configuration and add basic tests --- .vscode-test.mjs | 5 +++- package.json | 2 +- src/test/extension.test.ts | 22 +++++++++++++---- src/utils/path.test.ts | 48 ++++++++++++++++++++++++++++++++++++++ tsconfig.test.json | 13 +++++++++++ 5 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 src/utils/path.test.ts create mode 100644 tsconfig.test.json diff --git a/.vscode-test.mjs b/.vscode-test.mjs index 605b44f596..da0108114e 100644 --- a/.vscode-test.mjs +++ b/.vscode-test.mjs @@ -1,5 +1,8 @@ import { defineConfig } from "@vscode/test-cli" export default defineConfig({ - files: "out/test/**/*.test.js", + files: "out/**/*.test.js", + mocha: { + timeout: 20000, // Maximum time (in ms) that a test can run before failing + }, }) diff --git a/package.json b/package.json index 5267056b28..bd19359929 100644 --- a/package.json +++ b/package.json @@ -133,7 +133,7 @@ "watch:esbuild": "node esbuild.js --watch", "watch:tsc": "tsc --noEmit --watch --project tsconfig.json", "package": "npm run build:webview && npm run check-types && npm run lint && node esbuild.js --production", - "compile-tests": "tsc -p . --outDir out", + "compile-tests": "tsc -p ./tsconfig.test.json --outDir out", "watch-tests": "tsc -p . -w --outDir out", "pretest": "npm run compile-tests && npm run compile && npm run lint", "check-types": "tsc --noEmit", diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index 35bee7b874..c4381abd33 100644 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -5,11 +5,25 @@ import * as assert from "assert" import * as vscode from "vscode" // import * as myExtension from '../../extension'; +import { readFile } from "fs/promises" +import path from "path" + +const packagePath = path.join(__dirname, "..", "..", "..", "package.json") + suite("Extension Test Suite", () => { - vscode.window.showInformationMessage("Start all tests.") + suiteTeardown(() => { + vscode.window.showInformationMessage("All tests done!") + }) + + test("Sanity check", async () => { + const packageJSON = JSON.parse(await readFile(packagePath, "utf8")) + const id = packageJSON.publisher + "." + packageJSON.name + const clineExtensionApi = vscode.extensions.getExtension(id) + assert.equal(clineExtensionApi?.id, id) + }) - test("Sample test", () => { - assert.strictEqual(-1, [1, 2, 3].indexOf(5)) - assert.strictEqual(-1, [1, 2, 3].indexOf(0)) + test("Ensure that the extension is correctly loaded by running a command", async () => { + await new Promise((resolve) => setTimeout(resolve, 400)) + await vscode.commands.executeCommand("cline.plusButtonClicked") }) }) diff --git a/src/utils/path.test.ts b/src/utils/path.test.ts new file mode 100644 index 0000000000..7fd3ada5ba --- /dev/null +++ b/src/utils/path.test.ts @@ -0,0 +1,48 @@ +import * as assert from "assert" +import * as path from "path" +import * as os from "os" +import { arePathsEqual, getReadablePath } from "./path" + +suite("Path Utils", () => { + test("arePathsEqual handles undefined paths", () => { + assert.strictEqual(arePathsEqual(undefined, undefined), true) + assert.strictEqual(arePathsEqual("foo", undefined), false) + assert.strictEqual(arePathsEqual(undefined, "foo"), false) + }) + + test("arePathsEqual handles case sensitivity based on platform", () => { + if (process.platform === "win32") { + assert.strictEqual(arePathsEqual("FOO/BAR", "foo/bar"), true) + } else { + assert.strictEqual(arePathsEqual("FOO/BAR", "foo/bar"), false) + } + }) + + test("arePathsEqual handles normalized paths", () => { + assert.strictEqual(arePathsEqual("/tmp/./dir", "/tmp/../tmp/dir"), true) + assert.strictEqual(arePathsEqual("/tmp/./dir", "/tmp/../dir"), false) + }) + + test("getReadablePath handles desktop path", () => { + const desktop = path.join(os.homedir(), "Desktop") + const testPath = path.join(desktop, "test.txt") + assert.strictEqual(getReadablePath(desktop, "test.txt"), testPath.replace(/\\/g, "/")) + }) + + test("getReadablePath shows relative paths within cwd", () => { + const cwd = "/home/user/project" + const filePath = "/home/user/project/src/file.txt" + assert.strictEqual(getReadablePath(cwd, filePath), "src/file.txt") + }) + + test("getReadablePath shows basename when path equals cwd", () => { + const cwd = "/home/user/project" + assert.strictEqual(getReadablePath(cwd, cwd), "project") + }) + + test("getReadablePath shows absolute path when outside cwd", () => { + const cwd = "/home/user/project" + const filePath = "/home/user/other/file.txt" + assert.strictEqual(getReadablePath(cwd, filePath), filePath) + }) +}) diff --git a/tsconfig.test.json b/tsconfig.test.json new file mode 100644 index 0000000000..b0340129a8 --- /dev/null +++ b/tsconfig.test.json @@ -0,0 +1,13 @@ +{ + // This separate tsconfig is necessary because VS Code's test runner requires CommonJS modules, + // while our main project uses ES Modules (ESM). This config inherits most settings from the base + // tsconfig.json but overrides the module system for test files only. This doesn't affect how + // tests interact with the main codebase - it only changes how the test files themselves are + // compiled to make them compatible with VS Code's test runner. + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "moduleResolution": "node" + }, + "include": ["src/**/*.test.ts"] +}