127 lines
3.9 KiB
TypeScript
127 lines
3.9 KiB
TypeScript
import { CHAT_EVENT_NAMES } from "@hivegrid/realtime";
|
|
import { MemoryAdapter, createSharedStore } from "@hivegrid/shared-store";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
CHAT_PERSISTENT_REALTIME_OUTBOX_EVENT_TYPE,
|
|
CHAT_REALTIME_SHARED_STORE_NAMESPACE,
|
|
CHAT_REALTIME_WAKEUP_OUTBOX_QUEUE,
|
|
ChatRealtimeWakeupJobError,
|
|
parseChatRealtimeWakeupJob,
|
|
runChatRealtimeWakeupJob,
|
|
} from "./chat-realtime-wakeup.js";
|
|
|
|
function persistentEvent() {
|
|
return {
|
|
event: CHAT_EVENT_NAMES.readReceipt,
|
|
cursor: "stream_1:7",
|
|
requestId: "request_1",
|
|
tenantId: "tenant_1",
|
|
chatId: "chat_1",
|
|
occurredAt: "2026-07-16T08:00:00.000Z",
|
|
data: {
|
|
messageId: "message_1",
|
|
userId: "user_1",
|
|
readAt: "2026-07-16T08:00:00.000Z",
|
|
},
|
|
};
|
|
}
|
|
|
|
function validJob() {
|
|
return {
|
|
outboxEventId: "outbox_1",
|
|
tenantId: "tenant_1",
|
|
eventType: CHAT_PERSISTENT_REALTIME_OUTBOX_EVENT_TYPE,
|
|
payload: { event: persistentEvent() },
|
|
dispatchKey: "chat-realtime:ignored-by-consumer",
|
|
retries: 0,
|
|
};
|
|
}
|
|
|
|
describe("chat realtime wakeup job", () => {
|
|
it("uses the dedicated queue, event type and shared-store namespace", () => {
|
|
expect(CHAT_REALTIME_WAKEUP_OUTBOX_QUEUE).toBe("outbox.chat-realtime");
|
|
expect(CHAT_PERSISTENT_REALTIME_OUTBOX_EVENT_TYPE).toBe("chat.realtime.persistent");
|
|
expect(CHAT_REALTIME_SHARED_STORE_NAMESPACE).toBe("chat-realtime");
|
|
});
|
|
|
|
it("parses a dispatcher envelope and normalizes its persistent event", () => {
|
|
expect(parseChatRealtimeWakeupJob(validJob())).toEqual({
|
|
outboxEventId: "outbox_1",
|
|
tenantId: "tenant_1",
|
|
eventType: CHAT_PERSISTENT_REALTIME_OUTBOX_EVENT_TYPE,
|
|
event: persistentEvent(),
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
["missing envelope", null],
|
|
["unsupported event type", { ...validJob(), eventType: "chat.realtime.ephemeral" }],
|
|
[
|
|
"cross-tenant payload",
|
|
{
|
|
...validJob(),
|
|
payload: { event: { ...persistentEvent(), tenantId: "tenant_2" } },
|
|
},
|
|
],
|
|
[
|
|
"ephemeral event",
|
|
{
|
|
...validJob(),
|
|
payload: {
|
|
event: {
|
|
event: CHAT_EVENT_NAMES.typingStarted,
|
|
cursor: null,
|
|
requestId: "request_1",
|
|
tenantId: "tenant_1",
|
|
chatId: "chat_1",
|
|
occurredAt: "2026-07-16T08:00:00.000Z",
|
|
data: { userId: "user_1" },
|
|
},
|
|
},
|
|
},
|
|
],
|
|
])("rejects %s", (_label, input) => {
|
|
expect(() => parseChatRealtimeWakeupJob(input)).toThrow(ChatRealtimeWakeupJobError);
|
|
});
|
|
|
|
it("publishes the full event to the gateway's persistent room channel", async () => {
|
|
const adapter = new MemoryAdapter();
|
|
const store = createSharedStore({ namespace: CHAT_REALTIME_SHARED_STORE_NAMESPACE, adapter });
|
|
const received: string[] = [];
|
|
const unsubscribe = await store.subscribe("persistent:tenant_1:chat_1", (raw) => {
|
|
received.push(raw);
|
|
});
|
|
|
|
const result = await runChatRealtimeWakeupJob(validJob(), { store });
|
|
|
|
expect(result).toEqual({
|
|
published: true,
|
|
channel: "persistent:tenant_1:chat_1",
|
|
cursor: "stream_1:7",
|
|
subscriberCount: 1,
|
|
});
|
|
expect(received).toEqual([JSON.stringify(persistentEvent())]);
|
|
await unsubscribe();
|
|
});
|
|
|
|
it("propagates publish failures so BullMQ can retry the outbox delivery", async () => {
|
|
const publish = vi.fn(async () => {
|
|
throw new Error("redis unavailable");
|
|
});
|
|
|
|
await expect(
|
|
runChatRealtimeWakeupJob(validJob(), {
|
|
store: { namespace: CHAT_REALTIME_SHARED_STORE_NAMESPACE, publish },
|
|
})
|
|
).rejects.toThrow("redis unavailable");
|
|
});
|
|
|
|
it("rejects an incorrectly namespaced store before publishing", async () => {
|
|
const publish = vi.fn(async () => 0);
|
|
|
|
await expect(runChatRealtimeWakeupJob(validJob(), { store: { namespace: "worker", publish } })).rejects.toMatchObject({ code: "INVALID_STORE_NAMESPACE" });
|
|
expect(publish).not.toHaveBeenCalled();
|
|
});
|
|
});
|