style: apply oxfmt

This commit is contained in:
Peter Steinberger
2026-01-17 01:55:42 +00:00
parent 767f55b127
commit 3fb699a84b
22 changed files with 375 additions and 377 deletions

View File

@@ -1,12 +1,17 @@
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { loadInternalHooks } from './loader.js';
import { clearInternalHooks, getRegisteredEventKeys, triggerInternalHook, createInternalHookEvent } from './internal-hooks.js';
import type { ClawdbotConfig } from '../config/config.js';
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { loadInternalHooks } from "./loader.js";
import {
clearInternalHooks,
getRegisteredEventKeys,
triggerInternalHook,
createInternalHookEvent,
} from "./internal-hooks.js";
import type { ClawdbotConfig } from "../config/config.js";
describe('loader', () => {
describe("loader", () => {
let tmpDir: string;
let originalBundledDir: string | undefined;
@@ -18,7 +23,7 @@ describe('loader', () => {
// Disable bundled hooks during tests by setting env var to non-existent directory
originalBundledDir = process.env.CLAWDBOT_BUNDLED_HOOKS_DIR;
process.env.CLAWDBOT_BUNDLED_HOOKS_DIR = '/nonexistent/bundled/hooks';
process.env.CLAWDBOT_BUNDLED_HOOKS_DIR = "/nonexistent/bundled/hooks";
});
afterEach(async () => {
@@ -37,8 +42,8 @@ describe('loader', () => {
}
});
describe('loadInternalHooks', () => {
it('should return 0 when internal hooks are not enabled', async () => {
describe("loadInternalHooks", () => {
it("should return 0 when internal hooks are not enabled", async () => {
const cfg: ClawdbotConfig = {
hooks: {
internal: {
@@ -51,21 +56,21 @@ describe('loader', () => {
expect(count).toBe(0);
});
it('should return 0 when hooks config is missing', async () => {
it("should return 0 when hooks config is missing", async () => {
const cfg: ClawdbotConfig = {};
const count = await loadInternalHooks(cfg, tmpDir);
expect(count).toBe(0);
});
it('should load a handler from a module', async () => {
it("should load a handler from a module", async () => {
// Create a test handler module
const handlerPath = path.join(tmpDir, 'test-handler.js');
const handlerPath = path.join(tmpDir, "test-handler.js");
const handlerCode = `
export default async function(event) {
// Test handler
}
`;
await fs.writeFile(handlerPath, handlerCode, 'utf-8');
await fs.writeFile(handlerPath, handlerCode, "utf-8");
const cfg: ClawdbotConfig = {
hooks: {
@@ -73,7 +78,7 @@ describe('loader', () => {
enabled: true,
handlers: [
{
event: 'command:new',
event: "command:new",
module: handlerPath,
},
],
@@ -85,24 +90,24 @@ describe('loader', () => {
expect(count).toBe(1);
const keys = getRegisteredEventKeys();
expect(keys).toContain('command:new');
expect(keys).toContain("command:new");
});
it('should load multiple handlers', async () => {
it("should load multiple handlers", async () => {
// Create test handler modules
const handler1Path = path.join(tmpDir, 'handler1.js');
const handler2Path = path.join(tmpDir, 'handler2.js');
const handler1Path = path.join(tmpDir, "handler1.js");
const handler2Path = path.join(tmpDir, "handler2.js");
await fs.writeFile(handler1Path, 'export default async function() {}', 'utf-8');
await fs.writeFile(handler2Path, 'export default async function() {}', 'utf-8');
await fs.writeFile(handler1Path, "export default async function() {}", "utf-8");
await fs.writeFile(handler2Path, "export default async function() {}", "utf-8");
const cfg: ClawdbotConfig = {
hooks: {
internal: {
enabled: true,
handlers: [
{ event: 'command:new', module: handler1Path },
{ event: 'command:stop', module: handler2Path },
{ event: "command:new", module: handler1Path },
{ event: "command:stop", module: handler2Path },
],
},
},
@@ -112,19 +117,19 @@ describe('loader', () => {
expect(count).toBe(2);
const keys = getRegisteredEventKeys();
expect(keys).toContain('command:new');
expect(keys).toContain('command:stop');
expect(keys).toContain("command:new");
expect(keys).toContain("command:stop");
});
it('should support named exports', async () => {
it("should support named exports", async () => {
// Create a handler module with named export
const handlerPath = path.join(tmpDir, 'named-export.js');
const handlerPath = path.join(tmpDir, "named-export.js");
const handlerCode = `
export const myHandler = async function(event) {
// Named export handler
}
`;
await fs.writeFile(handlerPath, handlerCode, 'utf-8');
await fs.writeFile(handlerPath, handlerCode, "utf-8");
const cfg: ClawdbotConfig = {
hooks: {
@@ -132,9 +137,9 @@ describe('loader', () => {
enabled: true,
handlers: [
{
event: 'command:new',
event: "command:new",
module: handlerPath,
export: 'myHandler',
export: "myHandler",
},
],
},
@@ -145,8 +150,8 @@ describe('loader', () => {
expect(count).toBe(1);
});
it('should handle module loading errors gracefully', async () => {
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
it("should handle module loading errors gracefully", async () => {
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
const cfg: ClawdbotConfig = {
hooks: {
@@ -154,8 +159,8 @@ describe('loader', () => {
enabled: true,
handlers: [
{
event: 'command:new',
module: '/nonexistent/path/handler.js',
event: "command:new",
module: "/nonexistent/path/handler.js",
},
],
},
@@ -165,19 +170,19 @@ describe('loader', () => {
const count = await loadInternalHooks(cfg, tmpDir);
expect(count).toBe(0);
expect(consoleError).toHaveBeenCalledWith(
expect.stringContaining('Failed to load internal hook handler'),
expect.any(String)
expect.stringContaining("Failed to load internal hook handler"),
expect.any(String),
);
consoleError.mockRestore();
});
it('should handle non-function exports', async () => {
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
it("should handle non-function exports", async () => {
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
// Create a module with a non-function export
const handlerPath = path.join(tmpDir, 'bad-export.js');
await fs.writeFile(handlerPath, 'export default "not a function";', 'utf-8');
const handlerPath = path.join(tmpDir, "bad-export.js");
await fs.writeFile(handlerPath, 'export default "not a function";', "utf-8");
const cfg: ClawdbotConfig = {
hooks: {
@@ -185,7 +190,7 @@ describe('loader', () => {
enabled: true,
handlers: [
{
event: 'command:new',
event: "command:new",
module: handlerPath,
},
],
@@ -195,17 +200,15 @@ describe('loader', () => {
const count = await loadInternalHooks(cfg, tmpDir);
expect(count).toBe(0);
expect(consoleError).toHaveBeenCalledWith(
expect.stringContaining('is not a function')
);
expect(consoleError).toHaveBeenCalledWith(expect.stringContaining("is not a function"));
consoleError.mockRestore();
});
it('should handle relative paths', async () => {
it("should handle relative paths", async () => {
// Create a handler module
const handlerPath = path.join(tmpDir, 'relative-handler.js');
await fs.writeFile(handlerPath, 'export default async function() {}', 'utf-8');
const handlerPath = path.join(tmpDir, "relative-handler.js");
await fs.writeFile(handlerPath, "export default async function() {}", "utf-8");
// Get relative path from cwd
const relativePath = path.relative(process.cwd(), handlerPath);
@@ -216,7 +219,7 @@ describe('loader', () => {
enabled: true,
handlers: [
{
event: 'command:new',
event: "command:new",
module: relativePath,
},
],
@@ -228,9 +231,9 @@ describe('loader', () => {
expect(count).toBe(1);
});
it('should actually call the loaded handler', async () => {
it("should actually call the loaded handler", async () => {
// Create a handler that we can verify was called
const handlerPath = path.join(tmpDir, 'callable-handler.js');
const handlerPath = path.join(tmpDir, "callable-handler.js");
const handlerCode = `
let callCount = 0;
export default async function(event) {
@@ -240,7 +243,7 @@ describe('loader', () => {
return callCount;
}
`;
await fs.writeFile(handlerPath, handlerCode, 'utf-8');
await fs.writeFile(handlerPath, handlerCode, "utf-8");
const cfg: ClawdbotConfig = {
hooks: {
@@ -248,7 +251,7 @@ describe('loader', () => {
enabled: true,
handlers: [
{
event: 'command:new',
event: "command:new",
module: handlerPath,
},
],
@@ -259,13 +262,13 @@ describe('loader', () => {
await loadInternalHooks(cfg, tmpDir);
// Trigger the hook
const event = createInternalHookEvent('command', 'new', 'test-session');
const event = createInternalHookEvent("command", "new", "test-session");
await triggerInternalHook(event);
// The handler should have been called, but we can't directly verify
// the call count from this context without more complex test infrastructure
// This test mainly verifies that loading and triggering doesn't crash
expect(getRegisteredEventKeys()).toContain('command:new');
expect(getRegisteredEventKeys()).toContain("command:new");
});
});
});