Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

3.6 KB
Raw Blame
import { afterEach, describe, expect, it } from "vitest";
import type { Session, TenantMembership } from "./api";
import {
  appView,
  clearPendingInvite,
  deliveryNotice,
  inviteTokenFrom,
  inviteUrl,
  readPendingInvite,
  storePendingInvite,
} from "./sessionState";

const acme: TenantMembership = {
  id: "t1",
  slug: "acme",
  name: "Acme",
  role: "member",
  organization_role: "member",
  organization_id: "o1",
};

function session(overrides: Partial<Session> = {}): Session {
  return {
    user: { id: "u1", name: "Ada", email: "[email protected]", is_operator: false },
    tenant: acme,
    tenants: [acme],
    state: "ready",
    can_create_workspace: true,
    can_add_workspace: false,
    invite_email_enabled: false,
    ...overrides,
  };
}

describe("appView", () => {
  it("is signed out without a session, pending invite or not", () => {
    expect(appView(null, null)).toBe("signed_out");
    expect(appView(null, "tok")).toBe("signed_out");
  });

  it("follows the server's state when there is no pending invite", () => {
    expect(appView(session(), null)).toBe("ready");
    expect(appView(session({ state: "needs_workspace", tenant: null, tenants: [] }), null)).toBe(
      "needs_workspace",
    );
    expect(appView(session({ state: "needs_selection", tenant: null }), null)).toBe(
      "needs_selection",
    );
  });

  it("puts a pending invite ahead of every signed-in state", () => {
    for (const state of ["ready", "needs_workspace", "needs_selection"] as const) {
      expect(appView(session({ state }), "tok")).toBe("accept_invite");
    }
  });
});

describe("invite tokens", () => {
  afterEach(() => clearPendingInvite());

  it("reads a token from a full link or a bare token", () => {
    expect(inviteTokenFrom("https://switch.example.com/invite#token=abc123")).toBe("abc123");
    expect(inviteTokenFrom("  abc123  ")).toBe("abc123");
  });

  it("finds none in empty input, a link without one, or prose", () => {
    expect(inviteTokenFrom("   ")).toBeNull();
    expect(inviteTokenFrom("https://switch.example.com/invite")).toBeNull();
    expect(inviteTokenFrom("https://switch.example.com/invite?token=abc123")).toBeNull();
    expect(inviteTokenFrom("please let me in")).toBeNull();
  });

  it("builds links that keep the token out of the query string", () => {
    const link = inviteUrl("https://switch.example.com", "a+b/c=");
    expect(new URL(link).search).toBe("");
    expect(inviteTokenFrom(link)).toBe("a+b/c=");
  });

  it("keeps a pending invite in session storage until cleared", () => {
    expect(readPendingInvite()).toBeNull();
    storePendingInvite("tok");
    expect(readPendingInvite()).toBe("tok");
    clearPendingInvite();
    expect(readPendingInvite()).toBeNull();
  });
});

describe("deliveryNotice", () => {
  it("confirms a sent e-mail", () => {
    const notice = deliveryNotice("sent", "[email protected]");
    expect(notice.severity).toBe("success");
    expect(notice.text).toContain("e-mailed to [email protected]");
  });

  it("says plainly when nothing was sent, and why", () => {
    const off = deliveryNotice("not_configured", "[email protected]");
    expect(off.severity).toBe("warning");
    expect(off.text).toContain("No e-mail was sent");
    expect(off.text).toContain("[email protected]");

    const failed = deliveryNotice("failed", "[email protected]");
    expect(failed.severity).toBe("error");
    expect(failed.text).toContain("failed");
  });

  it("just hands over the link when no address was given", () => {
    const notice = deliveryNotice("not_requested", null);
    expect(notice.severity).toBe("success");
    expect(notice.text).not.toContain("null");
  });
});