feat(FN-1469): create settings-loadable example plugin
- Add fusion-plugin-settings-demo package with settings schema (string, number, boolean, enum types) - Add lifecycle hooks (onLoad, onTaskCreated, onTaskCompleted) - Add tools (suggest_tags, status) with settings-driven behavior - Add comprehensive tests with 24 test cases - Add manifest.json for plugin metadata and README with Settings installation flow - Update PLUGIN_AUTHORING.md with new example and install instructions - Also includes: FN-1468 plugin wiring, FN-1456 abandon flow, FN-1429 always-green tests, FN-1440 Authentication in Settings, FN-1133 plugin hot-reload support
This commit is contained in:
@@ -0,0 +1,486 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import plugin from "../index.js";
|
||||
|
||||
// ── Types for mocking ─────────────────────────────────────────────────────────
|
||||
|
||||
interface MockLogger {
|
||||
info: ReturnType<typeof vi.fn>;
|
||||
warn: ReturnType<typeof vi.fn>;
|
||||
error: ReturnType<typeof vi.fn>;
|
||||
debug: ReturnType<typeof vi.fn>;
|
||||
}
|
||||
|
||||
interface MockContext {
|
||||
pluginId: string;
|
||||
settings: Record<string, unknown>;
|
||||
logger: MockLogger;
|
||||
emitEvent: ReturnType<typeof vi.fn>;
|
||||
taskStore: {
|
||||
getTask: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
}
|
||||
|
||||
function createMockContext(overrides: Partial<MockContext> = {}): MockContext {
|
||||
return {
|
||||
pluginId: "fusion-plugin-settings-demo",
|
||||
settings: {
|
||||
greetingMessage: "Hello from Settings Demo!",
|
||||
maxTags: 3,
|
||||
enableLogging: true,
|
||||
logLevel: "info",
|
||||
},
|
||||
logger: {
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
debug: vi.fn(),
|
||||
},
|
||||
emitEvent: vi.fn(),
|
||||
taskStore: {
|
||||
getTask: vi.fn(),
|
||||
},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
// ── Mock Task ─────────────────────────────────────────────────────────────────
|
||||
|
||||
const mockTask = {
|
||||
id: "FN-001",
|
||||
title: "Test Task",
|
||||
description: "Fix the bug in the performance module",
|
||||
column: "todo" as const,
|
||||
dependencies: [],
|
||||
steps: [],
|
||||
currentStep: 0,
|
||||
size: "M" as const,
|
||||
reviewLevel: "full" as const,
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
// ── Test Suite ─────────────────────────────────────────────────────────────────
|
||||
|
||||
describe("settings demo plugin", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("plugin export", () => {
|
||||
it("should export a valid FusionPlugin with correct manifest fields", () => {
|
||||
expect(plugin.manifest.id).toBe("fusion-plugin-settings-demo");
|
||||
expect(plugin.manifest.name).toBe("Settings Demo Plugin");
|
||||
expect(plugin.manifest.version).toBe("0.1.0");
|
||||
expect(plugin.manifest.description).toBe(
|
||||
"Example plugin demonstrating settings schema, hooks, and tools",
|
||||
);
|
||||
expect(plugin.state).toBe("installed");
|
||||
expect(plugin.hooks).toBeDefined();
|
||||
expect(plugin.tools).toBeDefined();
|
||||
});
|
||||
|
||||
it("should have manifest matching manifest.json", () => {
|
||||
// Verify consistency between code and manifest.json
|
||||
expect(plugin.manifest.id).toBe("fusion-plugin-settings-demo");
|
||||
expect(plugin.manifest.name).toBe("Settings Demo Plugin");
|
||||
expect(plugin.manifest.version).toBe("0.1.0");
|
||||
});
|
||||
|
||||
it("should have settings schema defined with all four types", () => {
|
||||
expect(plugin.manifest.settingsSchema).toBeDefined();
|
||||
const schema = plugin.manifest.settingsSchema!;
|
||||
|
||||
// String type
|
||||
expect(schema.greetingMessage).toBeDefined();
|
||||
expect(schema.greetingMessage.type).toBe("string");
|
||||
expect(schema.greetingMessage.label).toBe("Greeting Message");
|
||||
|
||||
// Number type
|
||||
expect(schema.maxTags).toBeDefined();
|
||||
expect(schema.maxTags.type).toBe("number");
|
||||
expect(schema.maxTags.label).toBe("Max Tags");
|
||||
|
||||
// Boolean type
|
||||
expect(schema.enableLogging).toBeDefined();
|
||||
expect(schema.enableLogging.type).toBe("boolean");
|
||||
expect(schema.enableLogging.label).toBe("Enable Logging");
|
||||
|
||||
// Enum type
|
||||
expect(schema.logLevel).toBeDefined();
|
||||
expect(schema.logLevel.type).toBe("enum");
|
||||
expect(schema.logLevel.enumValues).toEqual(["debug", "info", "warn", "error"]);
|
||||
});
|
||||
|
||||
it("should have default values in settings schema", () => {
|
||||
const schema = plugin.manifest.settingsSchema!;
|
||||
|
||||
expect(schema.greetingMessage.defaultValue).toBe("Hello from Settings Demo!");
|
||||
expect(schema.maxTags.defaultValue).toBe(3);
|
||||
expect(schema.enableLogging.defaultValue).toBe(true);
|
||||
expect(schema.logLevel.defaultValue).toBe("info");
|
||||
});
|
||||
|
||||
it("should have tools defined", () => {
|
||||
expect(plugin.tools).toBeDefined();
|
||||
expect(plugin.tools!.length).toBe(2);
|
||||
|
||||
const toolNames = plugin.tools!.map((t) => t.name);
|
||||
expect(toolNames).toContain("settings_demo_suggest_tags");
|
||||
expect(toolNames).toContain("settings_demo_status");
|
||||
});
|
||||
|
||||
it("should have all required hooks defined", () => {
|
||||
expect(plugin.hooks.onLoad).toBeDefined();
|
||||
expect(plugin.hooks.onTaskCreated).toBeDefined();
|
||||
expect(plugin.hooks.onTaskCompleted).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("hooks.onLoad", () => {
|
||||
it("should log greeting message when logging is enabled", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Custom Greeting",
|
||||
maxTags: 3,
|
||||
enableLogging: true,
|
||||
logLevel: "info",
|
||||
},
|
||||
});
|
||||
|
||||
await plugin.hooks.onLoad?.(ctx as any);
|
||||
|
||||
expect(ctx.logger.info).toHaveBeenCalledWith("Custom Greeting");
|
||||
expect(ctx.logger.info).toHaveBeenCalledWith(
|
||||
expect.stringContaining("maxTags: 3"),
|
||||
);
|
||||
});
|
||||
|
||||
it("should not log when logging is disabled", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Custom Greeting",
|
||||
maxTags: 3,
|
||||
enableLogging: false,
|
||||
logLevel: "info",
|
||||
},
|
||||
});
|
||||
|
||||
await plugin.hooks.onLoad?.(ctx as any);
|
||||
|
||||
expect(ctx.logger.info).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should use default greeting when not configured", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "",
|
||||
maxTags: 3,
|
||||
enableLogging: true,
|
||||
logLevel: "info",
|
||||
},
|
||||
});
|
||||
|
||||
await plugin.hooks.onLoad?.(ctx as any);
|
||||
|
||||
expect(ctx.logger.info).toHaveBeenCalledWith("Hello from Settings Demo!");
|
||||
});
|
||||
});
|
||||
|
||||
describe("hooks.onTaskCreated", () => {
|
||||
it("should log when task is created with logging enabled", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 3,
|
||||
enableLogging: true,
|
||||
logLevel: "debug",
|
||||
},
|
||||
});
|
||||
|
||||
await plugin.hooks.onTaskCreated?.(mockTask as any, ctx as any);
|
||||
|
||||
expect(ctx.logger.debug).toHaveBeenCalledWith(
|
||||
expect.stringContaining("FN-001"),
|
||||
);
|
||||
});
|
||||
|
||||
it("should suggest tags for task with description", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 3,
|
||||
enableLogging: true,
|
||||
logLevel: "debug",
|
||||
},
|
||||
});
|
||||
|
||||
await plugin.hooks.onTaskCreated?.(mockTask as any, ctx as any);
|
||||
|
||||
// "performance" keyword matches performance tag
|
||||
expect(ctx.logger.debug).toHaveBeenCalledWith(
|
||||
expect.stringContaining("Suggested tags"),
|
||||
);
|
||||
expect(ctx.logger.debug).toHaveBeenCalledWith(
|
||||
expect.stringContaining("performance"),
|
||||
);
|
||||
});
|
||||
|
||||
it("should not log when logging is disabled", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 3,
|
||||
enableLogging: false,
|
||||
logLevel: "info",
|
||||
},
|
||||
});
|
||||
|
||||
await plugin.hooks.onTaskCreated?.(mockTask as any, ctx as any);
|
||||
|
||||
expect(ctx.logger.debug).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should handle task without description gracefully", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 3,
|
||||
enableLogging: true,
|
||||
logLevel: "debug",
|
||||
},
|
||||
});
|
||||
|
||||
const taskNoDesc = { ...mockTask, description: undefined };
|
||||
|
||||
await plugin.hooks.onTaskCreated?.(taskNoDesc as any, ctx as any);
|
||||
|
||||
// Should log task creation but not tag suggestions
|
||||
expect(ctx.logger.debug).toHaveBeenCalledWith(
|
||||
expect.stringContaining("FN-001"),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("hooks.onTaskCompleted", () => {
|
||||
it("should log when task is completed with logging enabled", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 3,
|
||||
enableLogging: true,
|
||||
logLevel: "info",
|
||||
},
|
||||
});
|
||||
|
||||
await plugin.hooks.onTaskCompleted?.(mockTask as any, ctx as any);
|
||||
|
||||
expect(ctx.logger.info).toHaveBeenCalledWith(
|
||||
expect.stringContaining("Task completed"),
|
||||
);
|
||||
expect(ctx.logger.info).toHaveBeenCalledWith(
|
||||
expect.stringContaining("FN-001"),
|
||||
);
|
||||
});
|
||||
|
||||
it("should not log when logging is disabled", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 3,
|
||||
enableLogging: false,
|
||||
logLevel: "info",
|
||||
},
|
||||
});
|
||||
|
||||
await plugin.hooks.onTaskCompleted?.(mockTask as any, ctx as any);
|
||||
|
||||
expect(ctx.logger.info).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("tools.suggest_tags", () => {
|
||||
it("should suggest tags based on description keywords", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 3,
|
||||
enableLogging: false,
|
||||
logLevel: "info",
|
||||
},
|
||||
});
|
||||
|
||||
const tool = plugin.tools!.find((t) => t.name === "settings_demo_suggest_tags")!;
|
||||
const result = await tool.execute(
|
||||
{ taskDescription: "Fix the bug in the performance module" },
|
||||
ctx as any,
|
||||
);
|
||||
|
||||
expect(result.content[0].text).toContain("Suggested tags:");
|
||||
expect(result.content[0].text).toContain("performance");
|
||||
});
|
||||
|
||||
it("should respect maxTags setting", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 1,
|
||||
enableLogging: false,
|
||||
logLevel: "info",
|
||||
},
|
||||
});
|
||||
|
||||
const tool = plugin.tools!.find((t) => t.name === "settings_demo_suggest_tags")!;
|
||||
const result = await tool.execute(
|
||||
{ taskDescription: "Fix bug fix fix" },
|
||||
ctx as any,
|
||||
);
|
||||
|
||||
// Should only return 1 tag
|
||||
expect(result.details!.count).toBe(1);
|
||||
});
|
||||
|
||||
it("should return no tags for generic description", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 3,
|
||||
enableLogging: false,
|
||||
logLevel: "info",
|
||||
},
|
||||
});
|
||||
|
||||
const tool = plugin.tools!.find((t) => t.name === "settings_demo_suggest_tags")!;
|
||||
const result = await tool.execute(
|
||||
{ taskDescription: "Do something" },
|
||||
ctx as any,
|
||||
);
|
||||
|
||||
expect(result.content[0].text).toContain("No tags could be suggested");
|
||||
});
|
||||
|
||||
it("should handle empty description", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 3,
|
||||
enableLogging: false,
|
||||
logLevel: "info",
|
||||
},
|
||||
});
|
||||
|
||||
const tool = plugin.tools!.find((t) => t.name === "settings_demo_suggest_tags")!;
|
||||
const result = await tool.execute({ taskDescription: "" }, ctx as any);
|
||||
|
||||
expect(result.content[0].text).toContain("No tags could be suggested");
|
||||
});
|
||||
});
|
||||
|
||||
describe("tools.status", () => {
|
||||
it("should return current configuration status", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Custom Greeting",
|
||||
maxTags: 5,
|
||||
enableLogging: true,
|
||||
logLevel: "debug",
|
||||
},
|
||||
});
|
||||
|
||||
const tool = plugin.tools!.find((t) => t.name === "settings_demo_status")!;
|
||||
const result = await tool.execute({}, ctx as any);
|
||||
|
||||
expect(result.content[0].text).toContain("Settings Demo Plugin Status:");
|
||||
expect(result.content[0].text).toContain("Custom Greeting");
|
||||
expect(result.content[0].text).toContain("Max Tags: 5");
|
||||
expect(result.content[0].text).toContain("Logging: enabled");
|
||||
expect(result.content[0].text).toContain("Log Level: debug");
|
||||
|
||||
expect(result.details!.greetingMessage).toBe("Custom Greeting");
|
||||
expect(result.details!.maxTags).toBe(5);
|
||||
expect(result.details!.enableLogging).toBe(true);
|
||||
expect(result.details!.logLevel).toBe("debug");
|
||||
});
|
||||
|
||||
it("should return raw settings values including undefined", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "",
|
||||
maxTags: undefined,
|
||||
enableLogging: undefined,
|
||||
logLevel: undefined,
|
||||
},
|
||||
});
|
||||
|
||||
const tool = plugin.tools!.find((t) => t.name === "settings_demo_status")!;
|
||||
const result = await tool.execute({}, ctx as any);
|
||||
|
||||
// Details returns raw values (not resolved defaults)
|
||||
expect(result.details!.greetingMessage).toBe("");
|
||||
expect(result.details!.maxTags).toBeUndefined();
|
||||
expect(result.details!.enableLogging).toBeUndefined();
|
||||
expect(result.details!.logLevel).toBeUndefined();
|
||||
|
||||
// But display text uses resolved defaults
|
||||
expect(result.content[0].text).toContain("Not configured");
|
||||
expect(result.content[0].text).toContain("Max Tags: 3");
|
||||
expect(result.content[0].text).toContain("Logging: enabled");
|
||||
expect(result.content[0].text).toContain("Log Level: info");
|
||||
});
|
||||
});
|
||||
|
||||
describe("settings-driven behavior", () => {
|
||||
it("should log debug messages only when logLevel allows", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 3,
|
||||
enableLogging: true,
|
||||
logLevel: "error", // Only error level
|
||||
},
|
||||
});
|
||||
|
||||
await plugin.hooks.onLoad?.(ctx as any);
|
||||
await plugin.hooks.onTaskCreated?.(mockTask as any, ctx as any);
|
||||
|
||||
// Debug and info should be filtered out
|
||||
expect(ctx.logger.debug).not.toHaveBeenCalled();
|
||||
expect(ctx.logger.info).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should log all levels when logLevel is debug", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 3,
|
||||
enableLogging: true,
|
||||
logLevel: "debug",
|
||||
},
|
||||
});
|
||||
|
||||
await plugin.hooks.onLoad?.(ctx as any);
|
||||
|
||||
// All log levels should work
|
||||
expect(ctx.logger.info).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should handle maxTags edge case of 0", async () => {
|
||||
const ctx = createMockContext({
|
||||
settings: {
|
||||
greetingMessage: "Hello",
|
||||
maxTags: 0, // Edge case: 0 tags
|
||||
enableLogging: false,
|
||||
logLevel: "info",
|
||||
},
|
||||
});
|
||||
|
||||
const tool = plugin.tools!.find((t) => t.name === "settings_demo_suggest_tags")!;
|
||||
const result = await tool.execute(
|
||||
{ taskDescription: "Fix bug performance ui" },
|
||||
ctx as any,
|
||||
);
|
||||
|
||||
// When maxTags is 0, no tags should be returned
|
||||
expect(result.details!.tags).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user