Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

8.3 KB
Raw Blame
import { describe, expect, it } from "vitest";
import type { AuthorizedSpaceId } from "@/features/space/types";
import {
  createSpaceContentScope,
  createSpaceDTO,
  inferIndustry,
  isSpaceAttributionHidden,
  isSpaceBrandingActive,
} from "@/features/space/utils";

const spaceId = "space-1" as AuthorizedSpaceId;

describe("createSpaceContentScope", () => {
  it("does not restrict members of a shared space", () => {
    expect(
      createSpaceContentScope({
        space: { id: spaceId, shared: true },
        userId: "user-1",
      }),
    ).toEqual({ spaceId });
  });

  it("restricts members of an unshared space to their own content", () => {
    expect(
      createSpaceContentScope({
        space: { id: spaceId, shared: false },
        userId: "user-1",
      }),
    ).toEqual({ spaceId, createdBy: "user-1" });
  });
});

describe("isSpaceBrandingActive", () => {
  it("applies branding for a pro space that turned it on", () => {
    expect(
      isSpaceBrandingActive({
        tier: "pro",
        showBranding: true,
        spaceBrandingAllowed: true,
      }),
    ).toBe(true);
  });

  it("ignores a stored colour once the space is on hobby", () => {
    expect(
      isSpaceBrandingActive({
        tier: "hobby",
        showBranding: true,
        spaceBrandingAllowed: true,
      }),
    ).toBe(false);
  });

  it("respects the switch being off", () => {
    expect(
      isSpaceBrandingActive({
        tier: "pro",
        showBranding: false,
        spaceBrandingAllowed: true,
      }),
    ).toBe(false);
  });

  it("defers to instance policy", () => {
    expect(
      isSpaceBrandingActive({
        tier: "pro",
        showBranding: true,
        spaceBrandingAllowed: false,
      }),
    ).toBe(false);
  });
});

describe("isSpaceAttributionHidden", () => {
  it("hides attribution for a pro space that turned it on", () => {
    expect(
      isSpaceAttributionHidden({
        tier: "pro",
        hideAttribution: true,
        spaceAttributionConfigurable: true,
      }),
    ).toBe(true);
  });

  it("ignores the stored setting once the space is on hobby", () => {
    expect(
      isSpaceAttributionHidden({
        tier: "hobby",
        hideAttribution: true,
        spaceAttributionConfigurable: true,
      }),
    ).toBe(false);
  });

  it("respects the switch being off", () => {
    expect(
      isSpaceAttributionHidden({
        tier: "pro",
        hideAttribution: false,
        spaceAttributionConfigurable: true,
      }),
    ).toBe(false);
  });

  it("defers to instance policy", () => {
    expect(
      isSpaceAttributionHidden({
        tier: "pro",
        hideAttribution: true,
        spaceAttributionConfigurable: false,
      }),
    ).toBe(false);
  });
});

describe("inferIndustry", () => {
  it("infers education from an academic domain", () => {
    expect(inferIndustry({ email: "[email protected]" })).toBe("education");
    expect(inferIndustry({ email: "[email protected]" })).toBe("education");
    expect(inferIndustry({ email: "[email protected]" })).toBe("education");
  });

  it("infers government from a public sector domain", () => {
    expect(inferIndustry({ email: "[email protected]" })).toBe("government");
    expect(inferIndustry({ email: "[email protected]" })).toBe("government");
    expect(inferIndustry({ email: "[email protected]" })).toBe("government");
  });

  it("infers non-profit from a .org domain", () => {
    expect(inferIndustry({ email: "[email protected]" })).toBe("non_profit");
  });

  it("matches domain labels whole, not as substrings", () => {
    expect(inferIndustry({ email: "[email protected]" })).toBeUndefined();
    expect(inferIndustry({ email: "[email protected]" })).toBeUndefined();
  });

  it("does not let a label anywhere but the suffix claim a sector", () => {
    // Each of these carries a sector label while ending in .com or .co, so
    // anyone could register one and be classified. Only the suffix counts.
    expect(inferIndustry({ email: "[email protected]" })).toBeUndefined();
    expect(
      inferIndustry({ email: "[email protected]" }),
    ).toBeUndefined();
    expect(inferIndustry({ email: "[email protected]" })).toBeUndefined();
    expect(inferIndustry({ email: "[email protected]" })).toBeUndefined();
    expect(inferIndustry({ email: "[email protected]" })).toBeUndefined();
    expect(inferIndustry({ email: "[email protected]" })).toBeUndefined();
  });

  it("requires a label in front of the suffix", () => {
    // A bare suffix is not somebody's organization.
    expect(inferIndustry({ email: "ada@edu" })).toBeUndefined();
    expect(inferIndustry({ email: "ada@org" })).toBeUndefined();
  });

  it("still matches a genuine multi-label suffix", () => {
    expect(inferIndustry({ email: "[email protected]" })).toBe("government");
    expect(inferIndustry({ email: "[email protected]" })).toBe(
      "education",
    );
    expect(inferIndustry({ email: "[email protected]" })).toBe("education");
    expect(inferIndustry({ email: "[email protected]" })).toBe("non_profit");
  });

  it("falls through free-mail domains to the organization name", () => {
    expect(
      inferIndustry({
        email: "[email protected]",
        organizationName: "Northside Dental Clinic",
      }),
    ).toBe("healthcare");
    expect(inferIndustry({ email: "[email protected]" })).toBeUndefined();
  });

  it("prefers the domain over the organization name", () => {
    expect(
      inferIndustry({
        email: "[email protected]",
        organizationName: "Acme Software",
      }),
    ).toBe("education");
  });

  it("infers legal and sports from the organization name", () => {
    expect(inferIndustry({ organizationName: "Smith & Jones LLP" })).toBe(
      "legal",
    );
    expect(inferIndustry({ organizationName: "Harbour Law" })).toBe("legal");
    expect(inferIndustry({ organizationName: "Riverside FC" })).toBe(
      "sports_and_recreation",
    );
    expect(inferIndustry({ organizationName: "Oakwood Tennis Club" })).toBe(
      "sports_and_recreation",
    );
  });

  it("matches organization name keywords as whole words", () => {
    expect(inferIndustry({ organizationName: "Acme Software" })).toBe(
      "technology",
    );
    expect(inferIndustry({ organizationName: "ACME SOFTWARE" })).toBe(
      "technology",
    );
    expect(inferIndustry({ organizationName: "Software-Werk" })).toBe(
      "technology",
    );
    // "care" must not fire on "Careers", nor "vet" on "Velvet"
    expect(inferIndustry({ organizationName: "Careers Ltd" })).toBeUndefined();
    expect(inferIndustry({ organizationName: "Velvet Ltd" })).toBeUndefined();
  });

  it("resolves overlapping keywords by table order", () => {
    // "church" (religious) is checked before "services" (professional)
    expect(inferIndustry({ organizationName: "Grace Church Services" })).toBe(
      "religious_organisation",
    );
  });

  it("returns undefined when nothing matches", () => {
    expect(inferIndustry({})).toBeUndefined();
    expect(inferIndustry({ email: "[email protected]" })).toBeUndefined();
    expect(
      inferIndustry({ email: "  ", organizationName: "  " }),
    ).toBeUndefined();
    expect(
      inferIndustry({ email: null, organizationName: null }),
    ).toBeUndefined();
  });

  it("tolerates a malformed email", () => {
    expect(inferIndustry({ email: "not-an-email" })).toBeUndefined();
    expect(
      inferIndustry({
        email: "not-an-email",
        organizationName: "City Council",
      }),
    ).toBe("government");
  });
});

describe("createSpaceDTO", () => {
  const space = {
    id: "space-1",
    ownerId: "user-1",
    name: "My Team",
    role: "MEMBER" as const,
    tier: "hobby" as const,
    showBranding: false,
    hideAttribution: false,
    shared: false,
    memberCount: 1,
    seatCount: 1,
  };

  it("keeps the stored sharing setting when spaces are not always shared", () => {
    expect(
      createSpaceDTO({ space, policy: { spacesAlwaysShared: false } }).shared,
    ).toBe(false);
    expect(
      createSpaceDTO({
        space: { ...space, shared: true },
        policy: { spacesAlwaysShared: false },
      }).shared,
    ).toBe(true);
  });

  it("coerces an unshared space to shared when spaces are always shared", () => {
    expect(
      createSpaceDTO({ space, policy: { spacesAlwaysShared: true } }).shared,
    ).toBe(true);
  });
});