Files
ihasmail-inbuxa/web/src/lib/__tests__/labelTree.test.ts
T
jcoffey-dev f7ef886b45 Nest labels, and let each one say how prominent it is
A flat list is fine at five labels and unreadable at thirty, and there was
no way to keep one that matters occasionally without it holding a row for
ever.

A label can now sit under another, and each says whether it belongs in the
sidebar always, only while it has unread mail, or never.

Nesting is display only. The keywords stay flat on the message, which is
what keeps them readable by every other client: moving a label under
another rewrites nothing in the mailbox, and a client that knows nothing
about ihasmail sees exactly what it always did. Both new fields are
optional, so a settings file written before this parses unchanged and
means what it did.

Settings sync between devices, so the tree has to survive shapes that
should not exist. A label whose parent was deleted on another device comes
back to the top level rather than vanishing -- a label that disappears
because something else was deleted is one the reader cannot get back. A
cycle arriving from an older device is broken by treating the label that
closes the loop as a root, so nothing is lost and nothing hangs. The
parent picker will not offer a label's own descendants, so one cannot be
built here in the first place.

A label kept by the unread rule keeps its ancestors, whatever they were
set to. A child cannot be drawn under a parent that is not there, and
promoting it to the top level would silently rearrange the tree at the
moment the reader is least able to explain why. The parent comes back as a
container instead, and its own count still says whether it has anything of
its own.

Unread counts come from one request carrying a query per label rather than
a request each, with limit 0 so the server does not send ids that would
only be thrown away. They refresh on the same beat as the folder counts,
since the things that move them are the same things, and a failure is
swallowed: a count is decoration, and the sidebar draws the label without
one.

Also corrects the Labels page, which said names and colours are kept in
this browser. They live in the account's own Files and follow it between
devices, like every other setting that is not about this screen.
2026-09-01 23:09:21 -07:00

110 lines
4.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { labelTree, visibleLabels, descendantKeywords } from "@/lib/labelTree";
import type { Label } from "@/store/settings";
const L = (keyword: string, over: Partial<Label> = {}): Label => ({ keyword, name: keyword, color: "#000", ...over });
const flat = (labels: Label[], counts: Record<string, number> = {}) =>
visibleLabels(labelTree(labels, counts)).map((n) => `${" ".repeat(n.depth)}${n.label.keyword}`);
describe("labelTree", () => {
it("nests a label under its parent and indents it", () => {
const roots = labelTree([L("work"), L("work_urgent", { parent: "work" })]);
expect(roots).toHaveLength(1);
expect(roots[0]!.label.keyword).toBe("work");
expect(roots[0]!.children[0]!.label.keyword).toBe("work_urgent");
expect(roots[0]!.children[0]!.depth).toBe(1);
});
it("nests three deep", () => {
expect(flat([L("a"), L("b", { parent: "a" }), L("c", { parent: "b" })])).toEqual(["a", " b", " c"]);
});
it("puts a label back at the top when its parent no longer exists", () => {
// Settings sync between devices; a parent can be deleted on one while
// another still points at it. Dropping the child would lose it for good.
expect(flat([L("orphan", { parent: "gone" })])).toEqual(["orphan"]);
});
it("survives a cycle rather than hanging", () => {
const out = flat([L("a", { parent: "b" }), L("b", { parent: "a" })]);
expect(out).toHaveLength(2);
expect(out.map((s) => s.trim()).sort()).toEqual(["a", "b"]);
});
it("survives a label parented to itself", () => {
expect(flat([L("a", { parent: "a" })])).toEqual(["a"]);
});
it("carries each label's own unread count, not its children's", () => {
const roots = labelTree([L("a"), L("b", { parent: "a" })], { a: 2, b: 5 });
expect(roots[0]!.unread).toBe(2);
expect(roots[0]!.children[0]!.unread).toBe(5);
});
});
describe("visibleLabels", () => {
it("draws everything set to always", () => {
expect(flat([L("a"), L("b")])).toEqual(["a", "b"]);
});
it("never draws a hidden label", () => {
expect(flat([L("a"), L("b", { visibility: "hidden" })], { b: 9 })).toEqual(["a"]);
});
it("draws an unread-only label just while it has unread mail", () => {
const labels = [L("a", { visibility: "unread" })];
expect(flat(labels, { a: 0 })).toEqual([]);
expect(flat(labels, { a: 1 })).toEqual(["a"]);
});
it("keeps a parent that would otherwise be dropped, when a child survives", () => {
// A child cannot be drawn under a parent that is not there, and promoting
// it would silently rearrange the tree. The parent comes back as a
// container instead.
const labels = [L("work", { visibility: "unread" }), L("work_urgent", { parent: "work" })];
expect(flat(labels, { work: 0 })).toEqual(["work", " work_urgent"]);
});
it("keeps a hidden parent too, when a child survives", () => {
const labels = [L("work", { visibility: "hidden" }), L("work_urgent", { parent: "work" })];
expect(flat(labels, {})).toEqual(["work", " work_urgent"]);
});
it("drops a whole branch when nothing in it survives", () => {
const labels = [
L("work", { visibility: "unread" }),
L("work_urgent", { parent: "work", visibility: "unread" }),
L("other"),
];
expect(flat(labels, { work: 0, work_urgent: 0 })).toEqual(["other"]);
});
it("keeps a grandparent when only a grandchild survives", () => {
const labels = [
L("a", { visibility: "hidden" }),
L("b", { parent: "a", visibility: "hidden" }),
L("c", { parent: "b" }),
];
expect(flat(labels, {})).toEqual(["a", " b", " c"]);
});
it("treats a label with no visibility set as always, so old settings parse unchanged", () => {
const l = L("a");
expect(l.visibility).toBeUndefined();
expect(flat([l], {})).toEqual(["a"]);
});
});
describe("descendantKeywords", () => {
it("names everything below a label, so the parent picker cannot offer a cycle", () => {
const roots = labelTree([L("a"), L("b", { parent: "a" }), L("c", { parent: "b" }), L("d")]);
expect([...descendantKeywords(roots, "a")].sort()).toEqual(["b", "c"]);
expect([...descendantKeywords(roots, "d")]).toEqual([]);
});
it("says nothing about a label that is not there", () => {
expect([...descendantKeywords(labelTree([L("a")]), "missing")]).toEqual([]);
});
});