Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

2.8 KB
Raw Blame
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";

import { StoreProvider, type Bot } from "@/state/store";
import { formFromVisibility, mixedRooms, visibilityFromForm, VisibilitySection } from "./VisibilitySection";

const render = (target: Bot) => renderToStaticMarkup(createElement(StoreProvider, null, createElement(VisibilitySection, { bot: target })));

const bot = (visibility?: Bot["visibility"]) => ({ id: "b1", name: "Payroll", visibility } as unknown as Bot);

describe("bot visibility form", () => {
  it("round-trips the three settings", () => {
    expect(formFromVisibility(undefined)).toEqual({ mode: "everyone", people: "" });
    expect(formFromVisibility("everyone")).toEqual({ mode: "everyone", people: "" });
    expect(formFromVisibility("admins")).toEqual({ mode: "admins", people: "" });
    expect(formFromVisibility({ people: ["[email protected]", "@acme.test"] })).toEqual({ mode: "people", people: "[email protected]\n@acme.test" });
    expect(visibilityFromForm("everyone", "ignored")).toEqual({ ok: true, visibility: "everyone" });
    expect(visibilityFromForm("admins", "")).toEqual({ ok: true, visibility: "admins" });
    expect(visibilityFromForm("people", " [email protected],\n@acme.test ; [email protected] ")).toEqual({ ok: true, visibility: { people: ["[email protected]", "@acme.test"] } });
    expect(visibilityFromForm("people", " \n ")).toEqual({ ok: false });
  });

  it("names the rooms this bot shares with bots other people can see", () => {
    const bots = [
      { id: "b1", visibility: { people: ["[email protected]"] } },
      { id: "b2", visibility: undefined },
      { id: "b3", visibility: { people: ["[email protected]"] } },
    ] as Array<Pick<Bot, "id" | "visibility">>;
    const groups = [
      { id: "g1", name: "Pay questions", memberIds: ["b1", "b2"] },
      { id: "g2", name: "HR desk", memberIds: ["b1", "b3"] },
      { id: "g3", name: "b1 ⇄ b2", memberIds: ["b1", "b2"], dm: true },
      { id: "g4", name: "Old severance room", memberIds: ["b2"], audienceFloor: "admins" as const },
    ];
    expect(mixedRooms(bots[0]!, groups, bots)).toEqual([{ id: "g1", name: "Pay questions" }]);
    expect(mixedRooms(bots[2]!, groups, bots)).toEqual([]);
    expect(mixedRooms(bots[1]!, groups, bots).map((room) => room.id)).toEqual(["g1", "g4"]);
  });

  it("renders the stored choice, with the list when it names people", () => {
    const people = render(bot({ people: ["[email protected]"] }));
    expect(people).toContain("Only these people");
    expect(people).toMatch(/checked="" value="people"/);
    expect(people).toContain("[email protected]</textarea>");
    const everyone = render(bot());
    expect(everyone).toMatch(/checked="" value="everyone"/);
    expect(everyone).not.toContain("<textarea");
  });
});