test: tighten task registry store assertions

This commit is contained in:
Peter Steinberger
2026-05-09 22:04:12 +01:00
parent eda94a434d
commit f183b16289

View File

@@ -21,7 +21,10 @@ import type { TaskRecord } from "./task-registry.types.js";
const ORIGINAL_STATE_DIR = process.env.OPENCLAW_STATE_DIR;
function requireFirstUpsertParams(upsertTaskWithDeliveryState: ReturnType<typeof vi.fn>): unknown {
function requireFirstUpsertParams(upsertTaskWithDeliveryState: ReturnType<typeof vi.fn>): {
task?: { taskId?: string };
deliveryState?: { lastNotifiedEventAt?: number };
} {
const params = upsertTaskWithDeliveryState.mock.calls[0]?.[0];
if (!params) {
throw new Error("expected task upsert params");
@@ -73,10 +76,9 @@ describe("task-registry store runtime", () => {
},
});
expect(findTaskByRunId("run-restored")).toMatchObject({
taskId: "task-restored",
task: "Restored task",
});
const restored = findTaskByRunId("run-restored");
expect(restored?.taskId).toBe("task-restored");
expect(restored?.task).toBe("Restored task");
expect(loadSnapshot).toHaveBeenCalledTimes(1);
createTaskRecord({
@@ -115,11 +117,10 @@ describe("task-registry store runtime", () => {
},
});
expect(findTaskByRunId("run-restored")).toMatchObject({
runId: "run-restored",
taskId: "task-restored",
task: "Restored task",
});
const restored = findTaskByRunId("run-restored");
expect(restored?.runId).toBe("run-restored");
expect(restored?.taskId).toBe("task-restored");
expect(restored?.task).toBe("Restored task");
const created = createTaskRecord({
runtime: "acp",
ownerKey: "agent:main:main",
@@ -133,18 +134,26 @@ describe("task-registry store runtime", () => {
expect(deleteTaskRecordById(created.taskId)).toBe(true);
expect(events.map((event) => event.kind)).toEqual(["restored", "upserted", "deleted"]);
expect(events[0]).toMatchObject({
kind: "restored",
tasks: [expect.objectContaining({ taskId: "task-restored" })],
});
expect(events[1]).toMatchObject({
kind: "upserted",
task: expect.objectContaining({ taskId: created.taskId }),
});
expect(events[2]).toMatchObject({
kind: "deleted",
taskId: created.taskId,
});
const restoredEvent = events[0];
expect(restoredEvent?.kind).toBe("restored");
if (restoredEvent?.kind !== "restored") {
throw new Error("Expected restored observer event");
}
expect(restoredEvent.tasks.map((task) => task.taskId)).toEqual(["task-restored"]);
const upsertedEvent = events[1];
expect(upsertedEvent?.kind).toBe("upserted");
if (upsertedEvent?.kind !== "upserted") {
throw new Error("Expected upserted observer event");
}
expect(upsertedEvent.task.taskId).toBe(created.taskId);
const deletedEvent = events[2];
expect(deletedEvent?.kind).toBe("deleted");
if (deletedEvent?.kind !== "deleted") {
throw new Error("Expected deleted observer event");
}
expect(deletedEvent.taskId).toBe(created.taskId);
});
it("uses atomic task-plus-delivery store methods when available", async () => {
@@ -182,11 +191,7 @@ describe("task-registry store runtime", () => {
expect(deleteTaskRecordById(created.taskId)).toBe(true);
expect(upsertTaskWithDeliveryState).toHaveBeenCalled();
expect(requireFirstUpsertParams(upsertTaskWithDeliveryState)).toMatchObject({
task: expect.objectContaining({
taskId: created.taskId,
}),
});
expect(requireFirstUpsertParams(upsertTaskWithDeliveryState).task?.taskId).toBe(created.taskId);
expect(
upsertTaskWithDeliveryState.mock.calls.some((call) => {
const params = call[0] as { deliveryState?: { lastNotifiedEventAt?: number } };
@@ -211,11 +216,10 @@ describe("task-registry store runtime", () => {
resetTaskRegistryForTests({ persist: false });
expect(findTaskByRunId("run-sqlite")).toMatchObject({
taskId: created.taskId,
sourceId: "job-123",
task: "Run nightly cron",
});
const restored = findTaskByRunId("run-sqlite");
expect(restored?.taskId).toBe(created.taskId);
expect(restored?.sourceId).toBe("job-123");
expect(restored?.task).toBe("Run nightly cron");
});
it("persists parentFlowId with task rows", () => {
@@ -238,10 +242,9 @@ describe("task-registry store runtime", () => {
resetTaskRegistryForTests({ persist: false });
expect(findTaskByRunId("run-flow-linked")).toMatchObject({
taskId: created.taskId,
parentFlowId: flow.flowId,
});
const restored = findTaskByRunId("run-flow-linked");
expect(restored?.taskId).toBe(created.taskId);
expect(restored?.parentFlowId).toBe(flow.flowId);
});
it("preserves requesterSessionKey when it differs from ownerKey across sqlite restore", () => {
@@ -260,12 +263,11 @@ describe("task-registry store runtime", () => {
resetTaskRegistryForTests({ persist: false });
expect(findTaskByRunId("run-requester-session-restore")).toMatchObject({
taskId: created.taskId,
requesterSessionKey: "agent:main:workspace:channel:C1234567890",
ownerKey: "agent:main:main",
childSessionKey: "agent:main:workspace:channel:C1234567890",
});
const restored = findTaskByRunId("run-requester-session-restore");
expect(restored?.taskId).toBe(created.taskId);
expect(restored?.requesterSessionKey).toBe("agent:main:workspace:channel:C1234567890");
expect(restored?.ownerKey).toBe("agent:main:main");
expect(restored?.childSessionKey).toBe("agent:main:workspace:channel:C1234567890");
});
it("preserves taskKind across sqlite restore", () => {
@@ -284,11 +286,10 @@ describe("task-registry store runtime", () => {
resetTaskRegistryForTests({ persist: false });
expect(findTaskByRunId("run-task-kind-restore")).toMatchObject({
taskId: created.taskId,
taskKind: "video_generation",
runId: "run-task-kind-restore",
});
const restored = findTaskByRunId("run-task-kind-restore");
expect(restored?.taskId).toBe(created.taskId);
expect(restored?.taskKind).toBe("video_generation");
expect(restored?.runId).toBe("run-task-kind-restore");
});
it("hardens the sqlite task store directory and file modes", async () => {
@@ -392,13 +393,12 @@ describe("task-registry store runtime", () => {
resetTaskRegistryForTests({ persist: false });
expect(findTaskByRunId("legacy-cron-run")).toMatchObject({
taskId: "legacy-cron-task",
ownerKey: "system:cron:nightly-digest",
scopeKind: "system",
deliveryStatus: "not_applicable",
notifyPolicy: "silent",
});
const restored = findTaskByRunId("legacy-cron-run");
expect(restored?.taskId).toBe("legacy-cron-task");
expect(restored?.ownerKey).toBe("system:cron:nightly-digest");
expect(restored?.scopeKind).toBe("system");
expect(restored?.deliveryStatus).toBe("not_applicable");
expect(restored?.notifyPolicy).toBe("silent");
},
);
});
@@ -473,23 +473,19 @@ describe("task-registry store runtime", () => {
resetTaskRegistryForTests({ persist: false });
expect(
markTaskLostById({
taskId: "legacy-session-task",
endedAt: 200,
lastEventAt: 200,
error: "session missing",
}),
).toMatchObject({
const lost = markTaskLostById({
taskId: "legacy-session-task",
status: "lost",
error: "session missing",
});
expect(findTaskByRunId("legacy-session-run")).toMatchObject({
taskId: "legacy-session-task",
status: "lost",
endedAt: 200,
lastEventAt: 200,
error: "session missing",
});
expect(lost?.taskId).toBe("legacy-session-task");
expect(lost?.status).toBe("lost");
expect(lost?.error).toBe("session missing");
const restored = findTaskByRunId("legacy-session-run");
expect(restored?.taskId).toBe("legacy-session-task");
expect(restored?.status).toBe("lost");
expect(restored?.error).toBe("session missing");
},
);
});