Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

7.5 KB
Raw Blame
import { describe, it, expect, vi } from "vitest";
import * as notif from "../../src/db/queries/community/notification-setting";

// Each `db.select().from().where()` resolves to the next queued result set.
function createSelectMock(resultSets: unknown[][]) {
  let call = 0;
  const chain: any = {};
  chain.from = vi.fn(() => chain);
  chain.where = vi.fn(() => Promise.resolve(resultSets[call++] ?? []));
  const db: any = { select: vi.fn(() => chain), __chain: chain };
  return db;
}

const TEXT_CHANNEL = { id: "c1", serverId: "s1", parentChannelId: null };
const POST = { id: "p1", serverId: "s1", parentChannelId: "c1" };
// A DM channel: no server, no parent (post-#412 DMs are channels, but a DM row
// has serverId null and parentChannelId null).
const DM = { id: "dm1", serverId: null as unknown as string, parentChannelId: null };

describe("computeEffectiveLevel — 3-tier climb", () => {
  it("no setting rows at all → global default 'all'", () => {
    expect(notif.computeEffectiveLevel([], TEXT_CHANNEL)).toBe("all");
  });

  it("server-level setting wins when no channel override exists", () => {
    const settings = [{ channelId: null, serverId: "s1", level: "mentions" }];
    expect(notif.computeEffectiveLevel(settings, TEXT_CHANNEL)).toBe("mentions");
  });

  it("channel override beats the server default", () => {
    const settings = [
      { channelId: null, serverId: "s1", level: "all" },
      { channelId: "c1", serverId: null, level: "nothing" },
    ];
    expect(notif.computeEffectiveLevel(settings, TEXT_CHANNEL)).toBe("nothing");
  });

  it("only reads the server row matching the channel's serverId", () => {
    const settings = [{ channelId: null, serverId: "other", level: "nothing" }];
    expect(notif.computeEffectiveLevel(settings, TEXT_CHANNEL)).toBe("all");
  });
});

describe("computeEffectiveLevel — post/thread parent inheritance", () => {
  it("post inherits the parent channel's override when it has none of its own", () => {
    const settings = [{ channelId: "c1", serverId: null, level: "nothing" }];
    expect(notif.computeEffectiveLevel(settings, POST)).toBe("nothing");
  });

  it("post inherits the server default when neither post nor parent has an override", () => {
    const settings = [{ channelId: null, serverId: "s1", level: "mentions" }];
    expect(notif.computeEffectiveLevel(settings, POST)).toBe("mentions");
  });

  it("a sub-channel override beats the parent override", () => {
    const settings = [
      { channelId: "c1", serverId: null, level: "nothing" },
      { channelId: "p1", serverId: null, level: "all" },
    ];
    expect(notif.computeEffectiveLevel(settings, POST)).toBe("all");
  });

  it("post override beats server default and parent absence", () => {
    const settings = [
      { channelId: null, serverId: "s1", level: "all" },
      { channelId: "p1", serverId: null, level: "mentions" },
    ];
    expect(notif.computeEffectiveLevel(settings, POST)).toBe("mentions");
  });

  it("post with no rows → global default 'all'", () => {
    expect(notif.computeEffectiveLevel([], POST)).toBe("all");
  });
});

describe("computeEffectiveLevel — DM independence (Gener/Melly #4)", () => {
  it("a DM with no own setting → 'all' (self-contained, defaults to all)", () => {
    expect(notif.computeEffectiveLevel([], DM)).toBe("all");
  });

  it("a DM honors its OWN 'nothing' setting (active mute)", () => {
    const settings = [{ channelId: "dm1", serverId: null, level: "nothing" }];
    expect(notif.computeEffectiveLevel(settings, DM)).toBe("nothing");
  });

  it("a 'nothing' set on some OTHER server does NOT cascade onto a DM (no inheritance)", () => {
    // The DM's serverId is null, so a server-scoped setting can never match it.
    const settings = [{ channelId: null, serverId: "s1", level: "nothing" }];
    expect(notif.computeEffectiveLevel(settings, DM)).toBe("all");
  });

  it("a 'nothing' set on another channel does NOT touch a DM", () => {
    const settings = [{ channelId: "c1", serverId: null, level: "nothing" }];
    expect(notif.computeEffectiveLevel(settings, DM)).toBe("all");
  });
});

describe("resolveEffectiveLevel — single user", () => {
  it("returns 'all' when the channel does not exist", async () => {
    const db = createSelectMock([[] /* channel lookup empty */]);
    expect(await notif.resolveEffectiveLevel(db, "u1", "missing")).toBe("all");
  });

  it("climbs to the channel override for the user", async () => {
    const db = createSelectMock([
      [TEXT_CHANNEL],
      [{ userId: "u1", channelId: "c1", serverId: null, level: "nothing" }],
    ]);
    expect(await notif.resolveEffectiveLevel(db, "u1", "c1")).toBe("nothing");
  });

  it("falls back to the server default when the user has no channel row", async () => {
    const db = createSelectMock([
      [TEXT_CHANNEL],
      [{ userId: "u1", channelId: null, serverId: "s1", level: "mentions" }],
    ]);
    expect(await notif.resolveEffectiveLevel(db, "u1", "c1")).toBe("mentions");
  });
});

describe("resolveEffectiveLevelForUsers — batch", () => {
  it("returns an empty map for no users without touching the db", async () => {
    const db = createSelectMock([]);
    const map = await notif.resolveEffectiveLevelForUsers(db, [], "c1");
    expect(map.size).toBe(0);
    expect(db.select).not.toHaveBeenCalled();
  });

  it("defaults every user to 'all' when the channel is missing", async () => {
    const db = createSelectMock([[] /* channel lookup empty */]);
    const map = await notif.resolveEffectiveLevelForUsers(db, ["u1", "u2"], "gone");
    expect(map.get("u1")).toBe("all");
    expect(map.get("u2")).toBe("all");
  });

  it("resolves distinct per-user levels on the same channel", async () => {
    const db = createSelectMock([
      [TEXT_CHANNEL],
      [
        { userId: "u1", channelId: "c1", serverId: null, level: "nothing" },
        { userId: "u2", channelId: null, serverId: "s1", level: "mentions" },
      ],
    ]);
    const map = await notif.resolveEffectiveLevelForUsers(db, ["u1", "u2", "u3"], "c1");
    expect(map.get("u1")).toBe("nothing");
    expect(map.get("u2")).toBe("mentions");
    expect(map.get("u3")).toBe("all");
  });

  it("fetches the channel once and scopes settings to that channel (single select round for settings)", async () => {
    const db = createSelectMock([
      [POST],
      [
        { userId: "u1", channelId: "c1", serverId: null, level: "nothing" },
        { userId: "u2", channelId: "p1", serverId: null, level: "all" },
      ],
    ]);
    const map = await notif.resolveEffectiveLevelForUsers(db, ["u1", "u2"], "p1");
    // u1 inherits the parent 'nothing'; u2 has a post-level 'all' override.
    expect(map.get("u1")).toBe("nothing");
    expect(map.get("u2")).toBe("all");
    // 1 channel lookup + 1 settings fetch = 2 selects total, regardless of user count.
    expect(db.select).toHaveBeenCalledTimes(2);
  });

  it("chunks more than 90 recipients without dropping all/mentions/nothing groups", async () => {
    const userIds = Array.from({ length: 150 }, (_, index) => `u${index}`);
    const db = createSelectMock([
      [POST],
      [{ userId: "u0", channelId: "p1", serverId: null, level: "nothing" }],
      [{ userId: "u120", channelId: null, serverId: "s1", level: "mentions" }],
    ]);

    const map = await notif.resolveEffectiveLevelForUsers(db, userIds, "p1");

    expect(map.size).toBe(150);
    expect(map.get("u0")).toBe("nothing");
    expect(map.get("u120")).toBe("mentions");
    expect(map.get("u149")).toBe("all");
    // 1 channel lookup + 2 bounded recipient chunks, never one query/user.
    expect(db.select).toHaveBeenCalledTimes(3);
  });
});