Files
ihasmail/web/src/views/admin/__tests__/domain-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

86 lines
3.5 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";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const domain = {
id: "d1",
name: "example.com",
aliases: {},
subAddressing: { "@type": "Custom" },
dnsManagement: { "@type": "Manual" },
dkimManagement: { "@type": "Automatic" },
certificateManagement: { "@type": "Manual" },
dnsZoneFile: 'example.com. IN MX 10 mail.example.com.\nexample.com. IN TXT "v=spf1 mx -all"\n',
};
vi.mock("@/lib/admin/adminDomains", async (original) => ({
...(await original<typeof import("@/lib/admin/adminDomains")>()),
getDomains: vi.fn(async () => [domain]),
listDkimKeys: vi.fn(async () => [{ id: "k1", "@type": "Dkim1Ed25519Sha256", selector: "v1-ed25519", stage: "active" }]),
}));
const { DomainSheet } = await import("../DomainSheet");
const signIn = (permissions: string[]) =>
useSession.setState({ session: { capabilities: {}, accounts: {}, primaryAccounts: {}, username: "[email protected]", ihasmail: { permissions } } as unknown as JmapSession });
const button = (host: HTMLElement, text: string) => [...host.querySelectorAll("button")].find((b) => b.textContent?.includes(text));
/**
* What decides whether a domain can be removed is not the button but what
* still uses it, and some of that is the domain's own keys.
*/
describe("the domain sheet", () => {
let host: HTMLDivElement;
let root: Root;
const render = async (accountCount: number | undefined) => {
await act(async () => {
root.render(<DomainSheet id="d1" accountCount={accountCount} onClose={() => {}} onChanged={() => {}} onCreated={() => {}} onDeleted={() => {}} />);
});
await act(async () => {});
};
beforeEach(() => {
host = document.createElement("div");
document.body.appendChild(host);
root = createRoot(host);
});
afterEach(async () => {
await act(async () => root.unmount());
host.remove();
});
it("lists the records one per row, unquoted", async () => {
signIn(["sysDomainGet", "sysDomainQuery"]);
await render(0);
expect(host.querySelectorAll(".admin-dns-row").length).toBe(2);
expect(host.textContent).toContain("v=spf1 mx -all");
expect(host.textContent).not.toContain('"v=spf1');
});
it("will not offer removal while accounts use the domain", async () => {
signIn(["sysDomainGet", "sysDomainQuery", "sysDomainDestroy", "sysDkimSignatureQuery", "sysDkimSignatureGet", "sysDkimSignatureDestroy"]);
await render(3);
expect(host.textContent).toContain("3 accounts use this domain");
expect(button(host, "Remove domain")?.disabled).toBe(true);
});
it("will not offer removal when the keys that must go first cannot be removed", async () => {
signIn(["sysDomainGet", "sysDomainQuery", "sysDomainDestroy", "sysDkimSignatureQuery", "sysDkimSignatureGet"]);
await render(0);
expect(host.textContent).toContain("your role can't remove them");
expect(button(host, "Remove domain")?.disabled).toBe(true);
});
it("leaves a plus-addressing rule set on the server alone", async () => {
signIn(["sysDomainGet", "sysDomainQuery", "sysDomainUpdate"]);
await render(0);
expect(host.textContent).toContain("Set by a custom rule on the server.");
expect((host.querySelector('button[role="switch"]') as HTMLButtonElement).disabled).toBe(true);
});
});