Släktträd och tidslinje
Relationer får ett släktband (förälder, partner, syskon) och en period. Nytt släktträd som ritas av släktbanden, och en tidslinje som samlar födelse och död, scenarier, egna händelser och relationsperioder med ålder vid varje tillfälle. Datum skrivs som ÅÅÅÅ, ÅÅÅÅ-MM eller ÅÅÅÅ-MM-DD. Följer med i export, import och papperskorg. Migreringen 0006 ger gamla relationer som "Förälder till" rätt släktband. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dcb5d43429
commit
bd269d70a5
32 files changed
+2285
-25
No files matched your search
@@ -0,0 +1,188 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { familyComponent, layoutFamily, parseBond, NODE_HEIGHT, NODE_WIDTH, GAP_X, type FamilyBondInput, type FamilyLayout, type FamilyPerson } from "@/lib/family";
|
||||
|
||||
const person = (id: string, name = id.toUpperCase()): FamilyPerson => ({ id, name });
|
||||
const parent = (fromId: string, toId: string, ...memberIds: string[]): FamilyBondInput => ({ bond: "PARENT", fromId, toId, memberIds });
|
||||
const partner = (fromId: string, toId: string, ...memberIds: string[]): FamilyBondInput => ({ bond: "PARTNER", fromId, toId, memberIds });
|
||||
const sibling = (fromId: string, toId: string, ...memberIds: string[]): FamilyBondInput => ({ bond: "SIBLING", fromId, toId, memberIds });
|
||||
|
||||
const node = (layout: FamilyLayout, id: string) => layout.nodes.find((n) => n.id === id)!;
|
||||
const centre = (layout: FamilyLayout, id: string) => node(layout, id).x + NODE_WIDTH / 2;
|
||||
|
||||
/** Ingen nod i samma rad överlappar en annan. */
|
||||
function expectNoOverlap(layout: FamilyLayout) {
|
||||
for (const a of layout.nodes) {
|
||||
for (const b of layout.nodes) {
|
||||
if (a.id < b.id && a.y === b.y) expect(Math.abs(a.x - b.x)).toBeGreaterThanOrEqual(NODE_WIDTH + GAP_X - 0.5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe("parseBond", () => {
|
||||
it("godtar bara kända släktband", () => {
|
||||
expect(parseBond("PARENT")).toBe("PARENT");
|
||||
expect(parseBond("PARTNER")).toBe("PARTNER");
|
||||
expect(parseBond("SIBLING")).toBe("SIBLING");
|
||||
expect(parseBond("FRIEND")).toBeNull();
|
||||
expect(parseBond(null)).toBeNull();
|
||||
expect(parseBond(undefined)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("layoutFamily", () => {
|
||||
it("ger tomt resultat utan släktband", () => {
|
||||
expect(layoutFamily([person("a")], [])).toMatchObject({ nodes: [], partners: [], children: [], width: 0, height: 0 });
|
||||
});
|
||||
|
||||
it("lägger barn en generation under föräldern", () => {
|
||||
const layout = layoutFamily([person("mor"), person("barn")], [parent("mor", "barn")]);
|
||||
expect(node(layout, "barn").generation).toBe(1);
|
||||
expect(node(layout, "barn").y).toBeGreaterThan(node(layout, "mor").y);
|
||||
expect(layout.children).toEqual([{ childId: "barn", parentIds: ["mor"] }]);
|
||||
});
|
||||
|
||||
it("ritar partner bredvid varandra i samma rad och barnet mitt under dem", () => {
|
||||
const people = ["far", "mor", "barn"].map((id) => person(id));
|
||||
const layout = layoutFamily(people, [partner("far", "mor"), parent("far", "barn"), parent("mor", "barn")]);
|
||||
|
||||
expect(node(layout, "far").y).toBe(node(layout, "mor").y);
|
||||
expect(Math.abs(node(layout, "far").x - node(layout, "mor").x)).toBe(NODE_WIDTH + GAP_X);
|
||||
expect(layout.partners).toHaveLength(1);
|
||||
expect(centre(layout, "barn")).toBeCloseTo((centre(layout, "far") + centre(layout, "mor")) / 2, 0);
|
||||
expect(layout.children[0].parentIds).toEqual(["far", "mor"]);
|
||||
});
|
||||
|
||||
it("tre generationer, syskon sida vid sida utan överlapp", () => {
|
||||
const people = ["farfar", "far", "mor", "a", "b", "c"].map((id) => person(id));
|
||||
const layout = layoutFamily(people, [
|
||||
parent("farfar", "far"),
|
||||
partner("far", "mor"),
|
||||
parent("far", "a"),
|
||||
parent("far", "b", "c"),
|
||||
parent("mor", "a", "b", "c"),
|
||||
]);
|
||||
expect([node(layout, "farfar").generation, node(layout, "far").generation, node(layout, "a").generation]).toEqual([0, 1, 2]);
|
||||
expect(node(layout, "mor").generation).toBe(1);
|
||||
expect(new Set(["a", "b", "c"].map((id) => node(layout, id).y)).size).toBe(1);
|
||||
expectNoOverlap(layout);
|
||||
// Barnen sitter ihop under föräldrarna
|
||||
const kids = ["a", "b", "c"].map((id) => centre(layout, id));
|
||||
expect(Math.min(...kids)).toBeGreaterThanOrEqual(Math.min(centre(layout, "far"), centre(layout, "mor")) - NODE_WIDTH * 2);
|
||||
});
|
||||
|
||||
it("syskon utan kända föräldrar hamnar i samma generation", () => {
|
||||
const layout = layoutFamily([person("a"), person("b"), person("c")], [sibling("a", "b"), parent("a", "c")]);
|
||||
expect(node(layout, "a").generation).toBe(node(layout, "b").generation);
|
||||
expect(node(layout, "c").generation).toBe(1);
|
||||
});
|
||||
|
||||
it("partner som gifts in följer med sin partners generation", () => {
|
||||
// Anna har föräldrar, Bo har inga. Bo ska hamna i Annas generation, inte överst.
|
||||
const people = ["mormor", "anna", "bo", "barn"].map((id) => person(id));
|
||||
const layout = layoutFamily(people, [parent("mormor", "anna"), partner("anna", "bo"), parent("anna", "barn"), parent("bo", "barn")]);
|
||||
expect(node(layout, "bo").generation).toBe(1);
|
||||
expect(node(layout, "barn").generation).toBe(2);
|
||||
expectNoOverlap(layout);
|
||||
});
|
||||
|
||||
it("två släkter som möts i ett barn", () => {
|
||||
const people = ["a1", "a2", "b1", "b2", "ma", "mb", "barn"].map((id) => person(id));
|
||||
const layout = layoutFamily(people, [
|
||||
parent("a1", "ma"),
|
||||
parent("a2", "ma"),
|
||||
parent("b1", "mb"),
|
||||
parent("b2", "mb"),
|
||||
partner("ma", "mb"),
|
||||
parent("ma", "barn"),
|
||||
parent("mb", "barn"),
|
||||
]);
|
||||
expect(node(layout, "barn").generation).toBe(2);
|
||||
expect(node(layout, "a1").generation).toBe(0);
|
||||
expectNoOverlap(layout);
|
||||
expect(layout.width).toBeGreaterThan(NODE_WIDTH * 4);
|
||||
});
|
||||
|
||||
it("flera partner i en kedja ritas bredvid varandra", () => {
|
||||
const layout = layoutFamily([person("a"), person("b"), person("c")], [partner("a", "b"), partner("b", "c")]);
|
||||
const xs = ["a", "b", "c"].map((id) => node(layout, id).x).sort((p, q) => p - q);
|
||||
expect(xs[1] - xs[0]).toBe(NODE_WIDTH + GAP_X);
|
||||
expect(xs[2] - xs[1]).toBe(NODE_WIDTH + GAP_X);
|
||||
expect(layout.partners).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("partnergrupp med fler än två", () => {
|
||||
const layout = layoutFamily(["a", "b", "c"].map((id) => person(id)), [partner("a", "b", "c")]);
|
||||
expect(layout.nodes).toHaveLength(3);
|
||||
expectNoOverlap(layout);
|
||||
});
|
||||
|
||||
it("cirkulära släktband hänger inte och ger ändå en layout", () => {
|
||||
const layout = layoutFamily(["a", "b", "c"].map((id) => person(id)), [parent("a", "b"), parent("b", "c"), parent("c", "a")]);
|
||||
expect(layout.nodes).toHaveLength(3);
|
||||
expect(layout.nodes.every((n) => Number.isFinite(n.x) && Number.isFinite(n.y))).toBe(true);
|
||||
});
|
||||
|
||||
it("en person som är sin egen förälder ignoreras", () => {
|
||||
const layout = layoutFamily([person("a"), person("b")], [parent("a", "a"), parent("a", "b")]);
|
||||
expect(layout.children).toEqual([{ childId: "b", parentIds: ["a"] }]);
|
||||
});
|
||||
|
||||
it("ignorerar personer som inte finns i listan", () => {
|
||||
const layout = layoutFamily([person("a")], [parent("a", "okänd"), parent("okänd", "a")]);
|
||||
expect(layout.nodes).toEqual([]);
|
||||
});
|
||||
|
||||
it("nodernas läge är inom bilden", () => {
|
||||
const layout = layoutFamily(["a", "b", "c", "d"].map((id) => person(id)), [parent("a", "b"), parent("a", "c"), parent("b", "d")]);
|
||||
for (const n of layout.nodes) {
|
||||
expect(n.x).toBeGreaterThanOrEqual(0);
|
||||
expect(n.y).toBeGreaterThanOrEqual(0);
|
||||
expect(n.x + NODE_WIDTH).toBeLessThanOrEqual(layout.width);
|
||||
expect(n.y + NODE_HEIGHT).toBeLessThanOrEqual(layout.height);
|
||||
}
|
||||
});
|
||||
|
||||
it("samma indata ger samma layout", () => {
|
||||
const people = ["a", "b", "c", "d", "e"].map((id) => person(id));
|
||||
const bonds = [partner("a", "b"), parent("a", "c"), parent("b", "c"), parent("a", "d"), sibling("c", "e")];
|
||||
expect(layoutFamily(people, bonds)).toEqual(layoutFamily([...people].reverse(), bonds));
|
||||
});
|
||||
|
||||
it("klarar en stor släkt snabbt", () => {
|
||||
const people: FamilyPerson[] = [];
|
||||
const bonds: FamilyBondInput[] = [];
|
||||
// 5 generationer, varje person har tre barn
|
||||
let current = ["p0"];
|
||||
people.push(person("p0"));
|
||||
let counter = 1;
|
||||
for (let gen = 0; gen < 5; gen++) {
|
||||
const next: string[] = [];
|
||||
for (const p of current) {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const id = `p${counter++}`;
|
||||
people.push(person(id));
|
||||
bonds.push(parent(p, id));
|
||||
next.push(id);
|
||||
}
|
||||
}
|
||||
current = next;
|
||||
}
|
||||
const started = Date.now();
|
||||
const layout = layoutFamily(people, bonds);
|
||||
expect(layout.nodes).toHaveLength(people.length);
|
||||
expect(Date.now() - started).toBeLessThan(5000);
|
||||
expectNoOverlap(layout);
|
||||
});
|
||||
});
|
||||
|
||||
describe("familyComponent", () => {
|
||||
it("följer alla släktband åt alla håll", () => {
|
||||
const bonds = [parent("farfar", "far"), partner("far", "mor"), parent("far", "barn"), parent("annan", "annanbarn")];
|
||||
expect([...familyComponent(bonds, "barn")].sort()).toEqual(["barn", "far", "farfar", "mor"]);
|
||||
expect([...familyComponent(bonds, "annanbarn")].sort()).toEqual(["annan", "annanbarn"]);
|
||||
});
|
||||
|
||||
it("en okänd person ger bara sig själv", () => {
|
||||
expect([...familyComponent([parent("a", "b")], "x")]).toEqual(["x"]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user