Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
4.7 KB
Raw Blame
import { describe, expect, it } from "vitest";
import {
  applyNotificationPreferencePatch,
  deriveNotificationPreferencePatch,
  notificationPreferenceValue,
  rollbackNotificationPreferencePatch,
} from "./patch";

describe("notification preference patches", () => {
  it("persists, enables and rolls back chat replies independently of comments", () => {
    const current = { comments: "muted" } as const;
    const muted = { ...current, chat_replies: "muted" } as const;
    expect(deriveNotificationPreferencePatch(current, muted)).toEqual({ chat_replies: "muted" });
    expect(applyNotificationPreferencePatch(current, { chat_replies: "muted" })).toEqual(muted);
    expect(deriveNotificationPreferencePatch(muted, current)).toEqual({ chat_replies: "all" });
    expect(applyNotificationPreferencePatch(muted, { chat_replies: "all" })).toEqual(current);
    expect(rollbackNotificationPreferencePatch(muted, { chat_replies: "muted" }, current)).toEqual(current);
  });

  it("sends only the changed key when another muted preference is present", () => {
    expect(
      deriveNotificationPreferencePatch(
        { status_changes: "muted" },
        { status_changes: "muted", comments: "muted" },
      ),
    ).toEqual({ comments: "muted" });
  });

  it("uses an explicit all value when enabling a previously muted group", () => {
    expect(
      deriveNotificationPreferencePatch(
        { updates: "muted", comments: "muted" },
        { comments: "muted" },
      ),
    ).toEqual({ updates: "all" });
  });

  it("treats a missing status_changes as muted, so turning it on is stored", () => {
    expect(notificationPreferenceValue({}, "status_changes")).toBe("muted");
    expect(
      deriveNotificationPreferencePatch({}, { status_changes: "all" }),
    ).toEqual({ status_changes: "all" });
    // The server echoes the default explicitly; enabling from there is the same patch.
    expect(
      deriveNotificationPreferencePatch({ status_changes: "muted" }, { status_changes: "all" }),
    ).toEqual({ status_changes: "all" });
    expect(
      deriveNotificationPreferencePatch({ status_changes: "all" }, {}),
    ).toEqual({ status_changes: "muted" });
    expect(
      applyNotificationPreferencePatch({}, { status_changes: "all" }),
    ).toEqual({ status_changes: "all" });
    expect(
      applyNotificationPreferencePatch({ status_changes: "all" }, { status_changes: "muted" }),
    ).toEqual({});
    expect(
      rollbackNotificationPreferencePatch({ status_changes: "all" }, { status_changes: "all" }, {}),
    ).toEqual({});
  });

  it("merges optimistic patches without dropping unrelated preferences", () => {
    expect(
      applyNotificationPreferencePatch(
        { status_changes: "muted" },
        { comments: "muted" },
      ),
    ).toEqual({ status_changes: "muted", comments: "muted" });
  });

  it("carries the mentions group independently of comments", () => {
    // The point of the split (#6468): muting comment volume must not travel
    // to mentions. A missing key in NOTIFICATION_GROUP_KEYS would make the
    // toggle silently un-sendable, so derive and apply are both covered.
    expect(
      deriveNotificationPreferencePatch(
        { comments: "muted" },
        { comments: "muted", mentions: "muted" },
      ),
    ).toEqual({ mentions: "muted" });

    expect(
      deriveNotificationPreferencePatch({ comments: "muted" }, {}),
    ).toEqual({ comments: "all" });

    expect(
      applyNotificationPreferencePatch(
        { comments: "muted" },
        { mentions: "muted" },
      ),
    ).toEqual({ comments: "muted", mentions: "muted" });
  });

  it("treats a missing others_issues as key updates, not all", () => {
    expect(notificationPreferenceValue({}, "others_issues")).toBe("key_updates");
    expect(
      deriveNotificationPreferencePatch({}, { others_issues: "all" }),
    ).toEqual({ others_issues: "all" });
    expect(
      deriveNotificationPreferencePatch({ others_issues: "all" }, {}),
    ).toEqual({ others_issues: "key_updates" });
    expect(
      applyNotificationPreferencePatch({ comments: "muted" }, { others_issues: "all" }),
    ).toEqual({ comments: "muted", others_issues: "all" });
    expect(
      applyNotificationPreferencePatch({ others_issues: "all" }, { others_issues: "key_updates" }),
    ).toEqual({});
    expect(
      rollbackNotificationPreferencePatch({ others_issues: "all" }, { others_issues: "all" }, {}),
    ).toEqual({});
  });

  it("does not roll back a key that a later mutation already changed", () => {
    expect(
      rollbackNotificationPreferencePatch(
        { comments: "muted" },
        { comments: "muted" },
        {},
      ),
    ).toEqual({});

    expect(
      rollbackNotificationPreferencePatch(
        {},
        { comments: "muted" },
        {},
      ),
    ).toEqual({});
  });
});