Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

test(daemon): de-flake task.messages broadcast test

Simone19cb8d07afb9
diff --git a/src/web/src/app/api/daemon/routes.test.ts b/src/web/src/app/api/daemon/routes.test.ts
index fb108cab2..97e87dfb5 100644
--- a/src/web/src/app/api/daemon/routes.test.ts
+++ b/src/web/src/app/api/daemon/routes.test.ts
@@ -553,103 +553,12 @@ describe("daemon route body validation", () => {
expect(res.status).toBe(400);
});
- it("broadcasts task.messages via WebSocket after writing to DB", async () => {
- vi.resetModules();
- applyBase();
-
- const createMock = vi.fn().mockResolvedValue(undefined);
- const broadcastMock = vi.fn().mockResolvedValue(undefined);
-
- vi.doMock("@alook/shared", async () => {
- const real = await vi.importActual<typeof import("@alook/shared")>(
- "@alook/shared"
- );
- return {
- ...real,
- createDb: vi.fn(() => ({})),
- queries: {
- task: {
- getTask: vi.fn().mockResolvedValue({ id: "t1", workspaceId: "w1", conversationId: "c1" }),
- },
- taskMessage: { createTaskMessage: createMock },
- conversation: {
- getConversation: vi.fn().mockResolvedValue({ id: "c1", userId: "owner-u2" }),
- },
- },
- };
- });
- vi.doMock("@/lib/api/responses", () => ({
- taskMessageToResponse: (m: any) => m,
- }));
- vi.doMock("@/lib/broadcast", () => ({
- broadcastToUser: broadcastMock,
- }));
-
- const POST = (await import("./tasks/[taskId]/messages/route")).POST;
- const res = await POST(
- postReq("http://localhost/api/daemon/tasks/t1/messages", {
- messages: [
- { seq: 1, type: "text", content: "hello" },
- { seq: 2, type: "tool-use", tool: "Read", content: "" },
- ],
- }),
- { params: Promise.resolve({ taskId: "t1" }) }
- );
-
- expect(res.status).toBe(200);
- expect(broadcastMock).toHaveBeenCalledWith(
- "owner-u2",
- expect.objectContaining({
- type: "task.messages",
- taskId: "t1",
- messages: [
- expect.objectContaining({ seq: 1, type: "text", content: "hello" }),
- ],
- })
- );
- });
-
- it("does not broadcast when messages array is empty", async () => {
- vi.resetModules();
- applyBase();
-
- const broadcastMock = vi.fn().mockResolvedValue(undefined);
-
- vi.doMock("@alook/shared", async () => {
- const real = await vi.importActual<typeof import("@alook/shared")>(
- "@alook/shared"
- );
- return {
- ...real,
- createDb: vi.fn(() => ({})),
- queries: {
- task: {
- getTask: vi.fn().mockResolvedValue({ id: "t1", workspaceId: "w1", conversationId: "c1" }),
- },
- taskMessage: { createTaskMessage: vi.fn().mockResolvedValue(undefined) },
- conversation: {
- getConversation: vi.fn().mockResolvedValue({ id: "c1", userId: "owner-u2" }),
- },
- },
- };
- });
- vi.doMock("@/lib/api/responses", () => ({
- taskMessageToResponse: (m: any) => m,
- }));
- vi.doMock("@/lib/broadcast", () => ({
- broadcastToUser: broadcastMock,
- }));
-
- const POST = (await import("./tasks/[taskId]/messages/route")).POST;
- const res = await POST(
- postReq("http://localhost/api/daemon/tasks/t1/messages", {
- messages: [],
- }),
- { params: Promise.resolve({ taskId: "t1" }) }
- );
-
- expect(res.status).toBe(200);
- expect(broadcastMock).not.toHaveBeenCalled();
- });
+ // NOTE: the broadcast-behavior tests for this route (task.messages envelope
+ // + empty-array no-op) live in tasks/[taskId]/messages/route.test.ts, which
+ // wires @/lib/broadcast with a hoisted vi.mock. They previously lived here
+ // on doMock + resetModules + dynamic import(); under full-suite parallel
+ // import pressure that per-test re-mock intermittently failed to bind, so the
+ // route called the real (unmocked) broadcastToUser and the assertion saw 0
+ // calls (~40% flake). This file stays pure body-validation, per its header.
});
});
diff --git a/src/web/src/app/api/daemon/tasks/[taskId]/messages/route.test.ts b/src/web/src/app/api/daemon/tasks/[taskId]/messages/route.test.ts
index 452fd8d58..6b0bd462a 100644
--- a/src/web/src/app/api/daemon/tasks/[taskId]/messages/route.test.ts
+++ b/src/web/src/app/api/daemon/tasks/[taskId]/messages/route.test.ts
@@ -249,4 +249,61 @@ describe("POST /api/daemon/tasks/[taskId]/messages", () => {
expect(mockBroadcastToUser).not.toHaveBeenCalled();
});
+
+ // Broadcast-envelope shape: the WS payload the owner receives is a
+ // `task.messages` event carrying taskId + only the broadcastable rows. (Moved
+ // here from the cross-route body-validation file, where the same assertion
+ // relied on a doMock+resetModules+dynamic-import re-application that raced
+ // under full-suite import pressure and flaked; the hoisted mocks in this file
+ // make it deterministic.)
+ it("broadcasts a task.messages envelope with taskId to the conversation owner", async () => {
+ mockGetTask.mockResolvedValue({ id: "t1", workspaceId: "w1", conversationId: "c1" });
+ mockGetConversation.mockResolvedValue({ id: "c1", userId: "owner-u2" });
+ mockCreateTaskMessage.mockResolvedValue({ id: "m1" });
+
+ const res = await POST(
+ new NextRequest("http://localhost/api/daemon/tasks/t1/messages", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ messages: [
+ { seq: 1, type: "text", content: "hello" },
+ { seq: 2, type: "tool-use", tool: "Read", content: "" },
+ ],
+ }),
+ }),
+ withParams("t1")
+ );
+
+ expect(res.status).toBe(200);
+ expect(mockBroadcastToUser).toHaveBeenCalledWith(
+ "owner-u2",
+ expect.objectContaining({
+ type: "task.messages",
+ taskId: "t1",
+ messages: [expect.objectContaining({ seq: 1, type: "text", content: "hello" })],
+ })
+ );
+ });
+
+ // The `filtered.length === 0` early-return branch (empty input) — a DIFFERENT
+ // path from "all tool-result" above: it returns before createTaskMessage runs,
+ // so neither the DB write nor the broadcast fires. (Also moved from the
+ // body-validation file to escape the same flaky re-mock race.)
+ it("does not broadcast or write when the messages array is empty", async () => {
+ mockGetTask.mockResolvedValue({ id: "t1", workspaceId: "w1", conversationId: "c1" });
+
+ const res = await POST(
+ new NextRequest("http://localhost/api/daemon/tasks/t1/messages", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ messages: [] }),
+ }),
+ withParams("t1")
+ );
+
+ expect(res.status).toBe(200);
+ expect(mockCreateTaskMessage).not.toHaveBeenCalled();
+ expect(mockBroadcastToUser).not.toHaveBeenCalled();
+ });
});