Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions packages/extension-core/__tests__/command-registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Command registry tests
* @fileoverview Tests for the command registry functionality
*/

import { CommandRegistryImpl } from "../src/core/command-registry";
import { ExtensionEventEmitter } from "../src/events/event-emitter";
import type { CommandDefinition } from "../src/types/index";

describe("CommandRegistry", () => {
let commandRegistry: CommandRegistryImpl;
let eventEmitter: ExtensionEventEmitter;

beforeEach(() => {
eventEmitter = new ExtensionEventEmitter();
commandRegistry = new CommandRegistryImpl(eventEmitter);
});

describe("command registration", () => {
it("should register a command successfully", () => {
const command: CommandDefinition = {
id: "test.command",
title: "Test Command",
handler: jest.fn(),
};

commandRegistry.registerCommand(command);

expect(commandRegistry.hasCommand("test.command")).toBe(true);
expect(commandRegistry.getCommands()).toHaveLength(1);
});

it("should throw error for invalid command definition", () => {
const invalidCommand = {
title: "Invalid Command",
handler: jest.fn(),
} as CommandDefinition;

expect(() => commandRegistry.registerCommand(invalidCommand)).toThrow();
});

it("should unregister a command", () => {
const command: CommandDefinition = {
id: "test.command",
title: "Test Command",
handler: jest.fn(),
};

commandRegistry.registerCommand(command);
expect(commandRegistry.hasCommand("test.command")).toBe(true);

commandRegistry.unregisterCommand("test.command");
expect(commandRegistry.hasCommand("test.command")).toBe(false);
});
});

describe("command execution", () => {
it("should execute a registered command", async () => {
const mockHandler = jest.fn().mockResolvedValue("success");
const command: CommandDefinition = {
id: "test.command",
title: "Test Command",
handler: mockHandler,
};

commandRegistry.registerCommand(command);

const result = await commandRegistry.executeCommand("test.command", "arg1", "arg2");

expect(mockHandler).toHaveBeenCalledWith("arg1", "arg2");
expect(result).toBe("success");
});

it("should throw error for unregistered command", async () => {
await expect(commandRegistry.executeCommand("nonexistent.command")).rejects.toThrow(
"Command nonexistent.command is not registered",
);
});

it("should throw error for disabled command", async () => {
const command: CommandDefinition = {
id: "test.command",
title: "Test Command",
handler: jest.fn(),
enabled: false,
};

commandRegistry.registerCommand(command);

await expect(commandRegistry.executeCommand("test.command")).rejects.toThrow(
"Command test.command is disabled",
);
});
});
});
72 changes: 72 additions & 0 deletions packages/extension-core/__tests__/extension-core.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Extension core tests
* @fileoverview Tests for the main extension core functionality
*/

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Missing tests for error scenarios during initialization and disposal.

Add tests that cover error handling during ExtensionCore initialization and disposal, ensuring proper logging and propagation.

import { ExtensionCoreImpl, createExtensionCore } from "../src/core/extension-core";

describe("ExtensionCore", () => {
let extensionCore: ExtensionCoreImpl;

beforeEach(() => {
extensionCore = new ExtensionCoreImpl();
});

afterEach(async () => {
if (extensionCore.isInitialized()) {
await extensionCore.dispose();
}
});

describe("initialization", () => {
it("should initialize successfully", async () => {
expect(extensionCore.isInitialized()).toBe(false);

await extensionCore.initialize();

expect(extensionCore.isInitialized()).toBe(true);
});

it("should not initialize twice", async () => {
await extensionCore.initialize();

// Second initialization should not throw
await expect(extensionCore.initialize()).resolves.toBeUndefined();
expect(extensionCore.isInitialized()).toBe(true);
});

it("should provide access to all components", async () => {
await extensionCore.initialize();

expect(extensionCore.getCommandRegistry()).toBeDefined();
expect(extensionCore.getWorkspaceContextProvider()).toBeDefined();
expect(extensionCore.getAICommandHandler()).toBeDefined();
expect(extensionCore.getProgressStreamer()).toBeDefined();
expect(extensionCore.getConfigurationManager()).toBeDefined();
});
});

describe("disposal", () => {
it("should dispose successfully", async () => {
await extensionCore.initialize();
expect(extensionCore.isInitialized()).toBe(true);

await extensionCore.dispose();
expect(extensionCore.isInitialized()).toBe(false);
});

it("should handle disposal when not initialized", async () => {
expect(extensionCore.isInitialized()).toBe(false);

await expect(extensionCore.dispose()).resolves.toBeUndefined();
});
});

describe("factory function", () => {
it("should create extension core instance", () => {
const core = createExtensionCore();
expect(core).toBeInstanceOf(ExtensionCoreImpl);
expect(core.isInitialized()).toBe(false);
});
});
});
105 changes: 105 additions & 0 deletions packages/extension-core/__tests__/progress-streamer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Progress streamer tests
* @fileoverview Tests for the progress streaming functionality
*/

import { ProgressStreamerImpl } from "../src/core/progress-streamer";
import { ExtensionEventEmitter } from "../src/events/event-emitter";

describe("ProgressStreamer", () => {
let progressStreamer: ProgressStreamerImpl;
let eventEmitter: ExtensionEventEmitter;

beforeEach(() => {
eventEmitter = new ExtensionEventEmitter();
progressStreamer = new ProgressStreamerImpl(eventEmitter);
});

describe("progress stream management", () => {
it("should start a progress stream", () => {
const stream = progressStreamer.startProgress("test-op", "Test Operation");

expect(stream.operationId).toBe("test-op");
expect(stream.title).toBe("Test Operation");
expect(stream.isActive()).toBe(true);
expect(progressStreamer.getProgress("test-op")).toBe(stream);
});

it("should update progress", () => {
const stream = progressStreamer.startProgress("test-op", "Test Operation");

stream.update({
progress: 50,
message: "Half way done",
});

const currentProgress = stream.getCurrentProgress();
expect(currentProgress.progress).toBe(50);
expect(currentProgress.message).toBe("Half way done");
});

it("should complete progress", () => {
const stream = progressStreamer.startProgress("test-op", "Test Operation");

stream.complete({ result: "success" });

expect(stream.isActive()).toBe(false);
const currentProgress = stream.getCurrentProgress();
expect(currentProgress.completed).toBe(true);
expect(currentProgress.result).toEqual({ result: "success" });
});

it("should fail progress", () => {
const stream = progressStreamer.startProgress("test-op", "Test Operation");
const error = new Error("Test error");

stream.fail(error);

expect(stream.isActive()).toBe(false);
const currentProgress = stream.getCurrentProgress();
expect(currentProgress.completed).toBe(true);
expect(currentProgress.error).toBe("Test error");
});

it("should cancel progress", () => {
const stream = progressStreamer.startProgress("test-op", "Test Operation");

stream.cancel();

expect(stream.isActive()).toBe(false);
const currentProgress = stream.getCurrentProgress();
expect(currentProgress.cancelled).toBe(true);
});
});

describe("stream collection management", () => {
it("should get active streams", () => {
const stream1 = progressStreamer.startProgress("op1", "Operation 1");
const stream2 = progressStreamer.startProgress("op2", "Operation 2");

expect(progressStreamer.getActiveStreams()).toHaveLength(2);

stream1.complete();
expect(progressStreamer.getActiveStreams()).toHaveLength(1);
});

it("should stop progress stream", () => {
const stream = progressStreamer.startProgress("test-op", "Test Operation");
expect(stream.isActive()).toBe(true);

progressStreamer.stopProgress("test-op");
expect(stream.isActive()).toBe(false);
expect(progressStreamer.getProgress("test-op")).toBeUndefined();
});

it("should clear all streams", () => {
progressStreamer.startProgress("op1", "Operation 1");
progressStreamer.startProgress("op2", "Operation 2");

expect(progressStreamer.getActiveStreams()).toHaveLength(2);

progressStreamer.clearAllStreams();
expect(progressStreamer.getActiveStreams()).toHaveLength(0);
});
});
});
11 changes: 11 additions & 0 deletions packages/extension-core/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import baseConfig from "../../eslint.config.mjs";

export default [
...baseConfig,
{
files: ["**/*.ts"],
rules: {
// Extension-specific rules can be added here
},
},
];
14 changes: 14 additions & 0 deletions packages/extension-core/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import('jest').Config} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
roots: ["<rootDir>/src", "<rootDir>/__tests__"],
testMatch: ["**/__tests__/**/*.test.ts", "**/*.test.ts"],
collectCoverageFrom: ["src/**/*.ts", "!src/**/*.d.ts", "!src/index.ts"],
coverageDirectory: "coverage",
coverageReporters: ["text", "lcov", "html"],
setupFilesAfterEnv: ["../../jest.setup.js"],
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
};
29 changes: 29 additions & 0 deletions packages/extension-core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@lighthouse-tooling/extension-core",
"version": "0.1.0",
"description": "Shared IDE extension core module for Lighthouse AI integration",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc -p tsconfig.build.json",
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier --write .",
"format:check": "prettier --check .",
"clean": "rm -rf dist"
},
"devDependencies": {
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"@types/jest": "^29.5.5",
"typescript": "^5.0.0"
},
"dependencies": {
"@lighthouse-tooling/types": "workspace:*",
"@lighthouse-tooling/shared": "workspace:*",
"@lighthouse-tooling/sdk-wrapper": "workspace:*"
}
}
Loading
Loading