Files
ihasmail-inbuxa/web/src/views/admin/__tests__/group-sheet.test.tsx
T
jcoffey-dev f7712b1c1e Group the admin and calendar modules, and stop calling screenshots docs
web/src/lib had grown to 85 flat modules -- 42% of the web source, about
12,800 lines -- with one subdirectory (smime/) to its name. The tell was
that a naming prefix had taken over a directory's job: eight adminX.ts
files sat adjacent because alphabetical order put them there, not because
anything said they belonged together.

  lib/admin/     adminAccess, adminDashboard, adminDirectory, adminDomains,
                 adminGroups, adminLists, adminRoles, adminTenants
  lib/calendar/  appointment, availabilityWindow, eventDrag, ics, recurrence

Tests move with their modules into lib/admin/__tests__ and
lib/calendar/__tests__, which is what views/ already does. describeRules
stays in lib/__tests__: it checks that sieve's describeRule and
recurrence's agree, so it belongs to neither.

recurrence.ts joins the calendar group and archiveDate.ts does not, which
is the opposite of the first guess from the filenames. archiveDate picks
the Archive/2026/09 mailbox for a message -- mail, not calendar --
while recurrence reads JSCalendarRecurrenceRule. schedule.ts is scheduled
*send*, so it stays put too. birthdays.ts is left alone deliberately: it
is read off the contact cards and only rendered by the calendar, so it
belongs to whichever of the two you ask.

docs/ held no documentation. It held ten JPEGs and the two scripts that
capture them, while the actual documentation is a separate site in the
ihasmail.org repository -- so anyone opening docs/ expecting prose found
a headless-Chrome driver. The images are now screenshots/, and the two
capture scripts join the other .mjs tooling in scripts/, which is where a
generator belongs. Renaming docs/ to screenshots/ wholesale would have
produced screenshots/screenshots/inbox-dark.jpg.

No behavior changes: every import was already on the @/ alias, so this is
path rewrites and nothing else.
2026-09-15 22:44:53 -07:00

105 lines
4.9 KiB
TypeScript

import { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { useSession } from "@/store/session";
import type { JmapSession } from "@/jmap/types";
import type { DirectoryGroup } from "@/lib/admin/adminGroups";
import type { DirectoryContext } from "../directoryContext";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const api = vi.hoisted(() => ({
members: [
{ id: "me", name: "demo", emailAddress: "[email protected]", description: "Demo User" },
{ id: "u2", name: "ada", emailAddress: "[email protected]", description: "Ada Lovelace" },
],
setMembership: vi.fn(async () => {}),
destroyGroup: vi.fn(async () => {}),
}));
vi.mock("@/lib/admin/adminGroups", async (original) => ({
...(await original<typeof import("@/lib/admin/adminGroups")>()),
listMembers: vi.fn(async () => ({ members: api.members, total: api.members.length })),
searchUsers: vi.fn(async () => []),
setMembership: api.setMembership,
destroyGroup: api.destroyGroup,
}));
const { GroupSheet } = await import("../GroupSheet");
const group: DirectoryGroup = { id: "g1", "@type": "Group", name: "support", domainId: "d1", emailAddress: "[email protected]", description: "Support", roles: { "@type": "Default" }, aliases: {} };
const ctx: DirectoryContext = { domains: [{ id: "d1", name: "example.com" }], roles: new Map(), groups: new Map(), self: { ids: new Set(["me"]), address: "[email protected]" } };
const signIn = (permissions: string[]) =>
useSession.setState({ session: { capabilities: {}, accounts: {}, primaryAccounts: {}, username: "[email protected]", ihasmail: { permissions } } as unknown as JmapSession });
const button = (host: HTMLElement, label: string) => [...host.querySelectorAll("button")].find((b) => b.getAttribute("aria-label") === label || b.textContent?.includes(label));
/** The group panel's guards: what a role may change, and what nobody may change for themselves. */
describe("the group sheet", () => {
let host: HTMLDivElement;
let root: Root;
const render = async () => {
await act(async () => {
root.render(<GroupSheet group={group} ctx={ctx} onClose={() => {}} onChanged={() => {}} onCreated={() => {}} onDeleted={() => {}} />);
});
await act(async () => {});
};
beforeEach(() => {
host = document.createElement("div");
document.body.appendChild(host);
root = createRoot(host);
api.setMembership.mockClear();
api.destroyGroup.mockClear();
});
afterEach(async () => {
await act(async () => root.unmount());
host.remove();
});
it("lists the members, and will not take the viewer out of a group themselves", async () => {
signIn(["sysAccountGet", "sysAccountQuery", "sysAccountUpdate"]);
await render();
expect(host.querySelectorAll(".admin-members li")).toHaveLength(2);
expect(button(host, "Remove [email protected] from the group")?.disabled).toBe(true);
const ada = button(host, "Remove [email protected] from the group")!;
expect(ada.disabled).toBe(false);
await act(async () => ada.click());
expect(api.setMembership).toHaveBeenCalledWith(["u2"], "g1", false);
});
it("offers no changes to a role that can only read", async () => {
signIn(["sysAccountGet", "sysAccountQuery"]);
await render();
expect(host.textContent).toContain("Your role lets you view groups but not change them.");
expect(button(host, "Remove [email protected] from the group")).toBeUndefined();
expect(host.querySelector(".admin-add-member")).toBeNull();
expect(button(host, "Save changes")).toBeUndefined();
});
it("will not start a delete it could only half finish", async () => {
// Deleting takes the members out first, which is an update to each of them.
signIn(["sysAccountGet", "sysAccountQuery", "sysAccountDestroy"]);
await render();
expect(button(host, "Delete group…")?.disabled).toBe(true);
expect(host.querySelector(".admin-danger")?.textContent).toContain("your role can't change their accounts");
});
it("deletes with every member taken out, once the address is typed", async () => {
signIn(["sysAccountGet", "sysAccountQuery", "sysAccountUpdate", "sysAccountDestroy"]);
await render();
await act(async () => button(host, "Delete group…")!.click());
const input = document.querySelector<HTMLInputElement>("#admin-group-delete-confirm")!;
const confirm = [...document.querySelectorAll<HTMLButtonElement>("button")].find((b) => b.textContent === "Delete group")!;
expect(confirm.disabled).toBe(true);
await act(async () => {
const set = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!;
set.call(input, "[email protected]");
input.dispatchEvent(new Event("input", { bubbles: true }));
});
expect(confirm.disabled).toBe(false);
await act(async () => confirm.click());
expect(api.destroyGroup).toHaveBeenCalledWith("g1", ["me", "u2"]);
});
});