ai-chatbot/tests/routes/openclaw-durable-task-state.test.ts

52 lines
1.9 KiB
TypeScript

import { expect, test } from "@playwright/test";
import {
appendClawdTaskProgress,
isPendingClawdTaskStatus,
normalizeClawdTaskMetadata,
} from "@/lib/openclaw-chat/task-state";
test.describe("OpenClaw durable task state", () => {
test("restores a persisted running task after the page remounts", () => {
const task = normalizeClawdTaskMetadata({
composerMode: "clawd",
source: "openclaw",
taskKind: "clawd_chat",
status: "running",
userMessageId: "user-message-id",
assistantMessageId: "assistant-message-id",
processingHint: "正在处理",
progressEvents: [{ event_id: 1, message: "请求已接收" }],
upstreamTaskId: "task_test_123",
upstreamTaskKey: "enc:v1:iv:tag:ciphertext",
upstreamEventsEndpoint: "/api/chat/tasks/task_test_123/events",
upstreamLastEventId: 18,
createdAt: 100,
updatedAt: 200,
});
expect(task).not.toBeNull();
expect(task?.userMessageId).toBe("user-message-id");
expect(task && isPendingClawdTaskStatus(task.status)).toBe(true);
expect(task?.progressEvents).toEqual([
{ event_id: 1, message: "请求已接收" },
]);
expect(task?.upstreamTaskId).toBe("task_test_123");
expect(task?.upstreamLastEventId).toBe(18);
});
test("deduplicates replayed progress events while keeping new steps", () => {
const first = { event_id: 8, message: "任务已恢复" };
const second = { event_id: 9, message: "正在选择工具" };
let events = appendClawdTaskProgress([], first);
events = appendClawdTaskProgress(events, first);
events = appendClawdTaskProgress(events, second);
expect(events).toEqual([first, second]);
});
test("does not classify completed tasks as pending", () => {
expect(isPendingClawdTaskStatus("completed")).toBe(false);
expect(isPendingClawdTaskStatus("failed")).toBe(false);
});
});