mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-18 10:37:27 +00:00
test: table-drive internal hook type-guard cases
This commit is contained in:
@@ -165,96 +165,141 @@ describe("hooks", () => {
|
||||
});
|
||||
|
||||
describe("isAgentBootstrapEvent", () => {
|
||||
it("returns true for agent:bootstrap events with expected context", () => {
|
||||
const context: AgentBootstrapHookContext = {
|
||||
workspaceDir: "/tmp",
|
||||
bootstrapFiles: [],
|
||||
};
|
||||
const event = createInternalHookEvent("agent", "bootstrap", "test-session", context);
|
||||
expect(isAgentBootstrapEvent(event)).toBe(true);
|
||||
});
|
||||
const cases: Array<{
|
||||
name: string;
|
||||
event: ReturnType<typeof createInternalHookEvent>;
|
||||
expected: boolean;
|
||||
}> = [
|
||||
{
|
||||
name: "returns true for agent:bootstrap events with expected context",
|
||||
event: createInternalHookEvent("agent", "bootstrap", "test-session", {
|
||||
workspaceDir: "/tmp",
|
||||
bootstrapFiles: [],
|
||||
} satisfies AgentBootstrapHookContext),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "returns false for non-bootstrap events",
|
||||
event: createInternalHookEvent("command", "new", "test-session"),
|
||||
expected: false,
|
||||
},
|
||||
];
|
||||
|
||||
it("returns false for non-bootstrap events", () => {
|
||||
const event = createInternalHookEvent("command", "new", "test-session");
|
||||
expect(isAgentBootstrapEvent(event)).toBe(false);
|
||||
});
|
||||
for (const testCase of cases) {
|
||||
it(testCase.name, () => {
|
||||
expect(isAgentBootstrapEvent(testCase.event)).toBe(testCase.expected);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe("isGatewayStartupEvent", () => {
|
||||
it("returns true for gateway:startup events with expected context", () => {
|
||||
const context: GatewayStartupHookContext = {
|
||||
cfg: {},
|
||||
};
|
||||
const event = createInternalHookEvent("gateway", "startup", "gateway:startup", context);
|
||||
expect(isGatewayStartupEvent(event)).toBe(true);
|
||||
});
|
||||
const cases: Array<{
|
||||
name: string;
|
||||
event: ReturnType<typeof createInternalHookEvent>;
|
||||
expected: boolean;
|
||||
}> = [
|
||||
{
|
||||
name: "returns true for gateway:startup events with expected context",
|
||||
event: createInternalHookEvent("gateway", "startup", "gateway:startup", {
|
||||
cfg: {},
|
||||
} satisfies GatewayStartupHookContext),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "returns false for non-startup gateway events",
|
||||
event: createInternalHookEvent("gateway", "shutdown", "gateway:shutdown", {}),
|
||||
expected: false,
|
||||
},
|
||||
];
|
||||
|
||||
it("returns false for non-startup gateway events", () => {
|
||||
const event = createInternalHookEvent("gateway", "shutdown", "gateway:shutdown", {});
|
||||
expect(isGatewayStartupEvent(event)).toBe(false);
|
||||
});
|
||||
for (const testCase of cases) {
|
||||
it(testCase.name, () => {
|
||||
expect(isGatewayStartupEvent(testCase.event)).toBe(testCase.expected);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe("isMessageReceivedEvent", () => {
|
||||
it("returns true for message:received events with expected context", () => {
|
||||
const context: MessageReceivedHookContext = {
|
||||
from: "+1234567890",
|
||||
content: "Hello world",
|
||||
channelId: "whatsapp",
|
||||
conversationId: "chat-123",
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
const event = createInternalHookEvent("message", "received", "test-session", context);
|
||||
expect(isMessageReceivedEvent(event)).toBe(true);
|
||||
});
|
||||
const cases: Array<{
|
||||
name: string;
|
||||
event: ReturnType<typeof createInternalHookEvent>;
|
||||
expected: boolean;
|
||||
}> = [
|
||||
{
|
||||
name: "returns true for message:received events with expected context",
|
||||
event: createInternalHookEvent("message", "received", "test-session", {
|
||||
from: "+1234567890",
|
||||
content: "Hello world",
|
||||
channelId: "whatsapp",
|
||||
conversationId: "chat-123",
|
||||
timestamp: Date.now(),
|
||||
} satisfies MessageReceivedHookContext),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "returns false for message:sent events",
|
||||
event: createInternalHookEvent("message", "sent", "test-session", {
|
||||
to: "+1234567890",
|
||||
content: "Hello world",
|
||||
success: true,
|
||||
channelId: "whatsapp",
|
||||
} satisfies MessageSentHookContext),
|
||||
expected: false,
|
||||
},
|
||||
];
|
||||
|
||||
it("returns false for message:sent events", () => {
|
||||
const context: MessageSentHookContext = {
|
||||
to: "+1234567890",
|
||||
content: "Hello world",
|
||||
success: true,
|
||||
channelId: "whatsapp",
|
||||
};
|
||||
const event = createInternalHookEvent("message", "sent", "test-session", context);
|
||||
expect(isMessageReceivedEvent(event)).toBe(false);
|
||||
});
|
||||
for (const testCase of cases) {
|
||||
it(testCase.name, () => {
|
||||
expect(isMessageReceivedEvent(testCase.event)).toBe(testCase.expected);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe("isMessageSentEvent", () => {
|
||||
it("returns true for message:sent events with expected context", () => {
|
||||
const context: MessageSentHookContext = {
|
||||
to: "+1234567890",
|
||||
content: "Hello world",
|
||||
success: true,
|
||||
channelId: "telegram",
|
||||
conversationId: "chat-456",
|
||||
messageId: "msg-789",
|
||||
};
|
||||
const event = createInternalHookEvent("message", "sent", "test-session", context);
|
||||
expect(isMessageSentEvent(event)).toBe(true);
|
||||
});
|
||||
const cases: Array<{
|
||||
name: string;
|
||||
event: ReturnType<typeof createInternalHookEvent>;
|
||||
expected: boolean;
|
||||
}> = [
|
||||
{
|
||||
name: "returns true for message:sent events with expected context",
|
||||
event: createInternalHookEvent("message", "sent", "test-session", {
|
||||
to: "+1234567890",
|
||||
content: "Hello world",
|
||||
success: true,
|
||||
channelId: "telegram",
|
||||
conversationId: "chat-456",
|
||||
messageId: "msg-789",
|
||||
} satisfies MessageSentHookContext),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "returns true when success is false (error case)",
|
||||
event: createInternalHookEvent("message", "sent", "test-session", {
|
||||
to: "+1234567890",
|
||||
content: "Hello world",
|
||||
success: false,
|
||||
error: "Network error",
|
||||
channelId: "whatsapp",
|
||||
} satisfies MessageSentHookContext),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "returns false for message:received events",
|
||||
event: createInternalHookEvent("message", "received", "test-session", {
|
||||
from: "+1234567890",
|
||||
content: "Hello world",
|
||||
channelId: "whatsapp",
|
||||
} satisfies MessageReceivedHookContext),
|
||||
expected: false,
|
||||
},
|
||||
];
|
||||
|
||||
it("returns true when success is false (error case)", () => {
|
||||
const context: MessageSentHookContext = {
|
||||
to: "+1234567890",
|
||||
content: "Hello world",
|
||||
success: false,
|
||||
error: "Network error",
|
||||
channelId: "whatsapp",
|
||||
};
|
||||
const event = createInternalHookEvent("message", "sent", "test-session", context);
|
||||
expect(isMessageSentEvent(event)).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for message:received events", () => {
|
||||
const context: MessageReceivedHookContext = {
|
||||
from: "+1234567890",
|
||||
content: "Hello world",
|
||||
channelId: "whatsapp",
|
||||
};
|
||||
const event = createInternalHookEvent("message", "received", "test-session", context);
|
||||
expect(isMessageSentEvent(event)).toBe(false);
|
||||
});
|
||||
for (const testCase of cases) {
|
||||
it(testCase.name, () => {
|
||||
expect(isMessageSentEvent(testCase.event)).toBe(testCase.expected);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe("message type-guard shared negatives", () => {
|
||||
|
||||
Reference in New Issue
Block a user