Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

3.4 KB
Raw Blame
import { BUSINESS_SCHEMA_VERSION } from "../src/business-schema-version";
import { describe, expect, it } from "vitest";
import { parseMessageDeliveryBatch } from "../src/community-message-delivery";

const messageEvent = {
  type: "community:message.create" as const,
  profiles: [{ id: "u1", name: "Alice", discriminator: "0042", avatar: null, avatarVersion: 0 }],
  channelId: "thread-1",
  serverId: "server-1",
  parentChannelId: "forum-1",
  message: {
    id: "message-1", channelId: "thread-1", replyToId: null, clientNonce: null, attachments: [],
    seq: 4,
    authorId: "u1",
    authorName: "Alice",
    authorAvatarVersion: 0,
    content: "hello",
    type: "chat" as const,
    createdAt: "2026-08-18T00:00:00.000Z",
  },
};

const valid = {
  communityContract: BUSINESS_SCHEMA_VERSION,
  messageId: "message-1",
  messageEvent,
  contentUserIds: ["u1", "u2"],
  unreadPlainUserIds: [],
  unreadMentionUserIds: ["u2"],
  mentionUserIds: ["u2"],
  parentProjection: {
    type: "community:channel.child_update" as const,
    parentChannelId: "forum-1",
    channelId: "thread-1",
    changes: { messageCount: 4, lastMessageAt: "2026-08-18T00:00:00.000Z" },
  },
  parentProjectionUserIds: ["u1", "u2", "u3"],
};

describe("message delivery batch", () => {
  it("accepts the committed join set and rejects incomplete or unrelated roster changes", () => {
    const joined = { ...valid, joinedParticipantUserIds: ["u1", "u2"], rosterRefreshUserId: "u1" };
    expect(parseMessageDeliveryBatch(joined)).toEqual({ ok: true, batch: joined });
    for (const batch of [
      { ...joined, rosterRefreshUserId: undefined },
      { ...valid, rosterRefreshUserId: "u1" },
      { ...joined, joinedParticipantUserIds: ["u1", "u1"] },
      { ...joined, joinedParticipantUserIds: ["outside"] },
      { ...joined, messageEvent: { ...messageEvent, serverId: undefined, parentChannelId: undefined }, memberAdded: undefined, parentProjection: undefined, parentProjectionUserIds: undefined },
    ]) expect(parseMessageDeliveryBatch(batch)).toEqual({ ok: false, reason: "invalid-payload" });
  });
  it("accepts a strict message-specific batch with separate parent audience", () => {
    expect(parseMessageDeliveryBatch(valid)).toEqual({ ok: true, batch: valid });
  });

  it("accepts a thread mention-only nonparticipant", () => {
    const batch = {
      ...valid,
      contentUserIds: ["u1"],
      unreadMentionUserIds: [],
      mentionUserIds: ["parent-visible-nonparticipant"],
    };
    expect(parseMessageDeliveryBatch(batch).ok).toBe(true);
  });

  it.each([
    { ...valid, extra: true },
    { ...valid, messageId: "other" },
    { ...valid, contentUserIds: ["u1", "u1"] },
    { ...valid, unreadPlainUserIds: ["outside"] },
    { ...valid, unreadPlainUserIds: ["u2"], unreadMentionUserIds: ["u2"] },
    { ...valid, parentProjectionUserIds: undefined },
    { ...valid, memberAdded: { userId: "u1", serverId: "server-1", channelId: "other" } },
    { ...valid, memberAdded: { userId: "u1", serverId: "other", channelId: "thread-1" } },
    {
      ...valid,
      messageEvent: { ...messageEvent, serverId: undefined, parentChannelId: undefined },
      memberAdded: { userId: "u1", serverId: "server-1", channelId: "thread-1" },
      parentProjection: undefined,
      parentProjectionUserIds: undefined,
    },
  ])("rejects an invalid batch", (batch) => {
    expect(parseMessageDeliveryBatch(batch).ok).toBe(false);
  });
});