31 lines
1.8 KiB
TypeScript
31 lines
1.8 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import type { CapabilityId, Cursor, Event, EventId, ProjectId, ServerId, SessionId, Task, TaskId, Timestamp } from "../../protocol/model";
|
|
import { MemoryActivityAdapter } from "./memoryAdapter";
|
|
|
|
const scope = { server_id: "s" as ServerId, project_id: "p" as ProjectId, session_id: "x" as SessionId };
|
|
const event: Event = { event_id: "e" as EventId, type: "TASK_UPDATED", timestamp: "2026-01-01T00:00:00Z" as Timestamp, cursor: "current" as Cursor, context: scope, payload: {} };
|
|
const task: Task = { ...scope, task_id: "t" as TaskId, capability_id: "c" as CapabilityId, state: "RUNNING", cancellable: true, created_at: event.timestamp, updated_at: event.timestamp };
|
|
|
|
describe("MemoryActivityAdapter", () => {
|
|
it("从有效游标之后恢复标准事件", async () => {
|
|
const received = vi.fn();
|
|
new MemoryActivityAdapter([event]).subscribe(scope, undefined, received, vi.fn());
|
|
await Promise.resolve();
|
|
expect(received).toHaveBeenCalledWith(event);
|
|
});
|
|
|
|
it("游标失效时报告缺口但继续恢复", async () => {
|
|
const phase = vi.fn();
|
|
new MemoryActivityAdapter([event]).subscribe(scope, "expired" as Cursor, vi.fn(), phase);
|
|
await Promise.resolve();
|
|
expect(phase).toHaveBeenCalledWith("streaming", expect.objectContaining({ requested_cursor: "expired", resumed_cursor: "current" }));
|
|
});
|
|
|
|
it("断开订阅不会取消任务,取消必须显式调用", async () => {
|
|
const adapter = new MemoryActivityAdapter([event], [task]);
|
|
adapter.subscribe(scope, undefined, vi.fn(), vi.fn()).close();
|
|
await expect(adapter.cancelTask(task)).resolves.toMatchObject({ state: "CANCELLED", cancellable: false });
|
|
await expect(adapter.cancelTask({ ...task, state: "SUCCEEDED" })).rejects.toThrow("不可取消");
|
|
});
|
|
});
|