Files
familienarchiv/frontend/src/lib/person/genealogy/layout/highlightLineage.test.ts
Marcel 7a655ce6f4 feat(stammbaum): add lineage highlight traversal module (#703)
Pure, DOM-free traversal over the family graph. Given the relationship
edges and a selected root, highlightLineage returns the active id set
(root + full pedigree upward + full descendant tree downward + every
spouse of those blood people, as active leaves) and a connector
predicate active only when both joined people are active.

The walk is guarded by the accumulating visited set, so cyclic PARENT_OF
data terminates (REQ-STAMMBAUM-04 / AC10). SIBLING_OF and social
relation types are ignored, so collaterals never enter the active set.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-05-31 16:26:24 +02:00

151 lines
4.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { buildLineageIndex, highlightLineage } from './highlightLineage';
import type { components } from '$lib/generated/api';
type RelationshipDTO = components['schemas']['RelationshipDTO'];
// Fixture builders mirror buildLayout.test.ts. Ids are plain readable strings —
// the traversal is id-agnostic, so UUIDs add no value here.
function parentEdge(parentId: string, childId: string, id = parentId + childId): RelationshipDTO {
return {
id,
personId: parentId,
relatedPersonId: childId,
personDisplayName: '',
relatedPersonDisplayName: '',
relationType: 'PARENT_OF'
};
}
function spouseEdge(a: string, b: string, id = a + b): RelationshipDTO {
return {
id,
personId: a,
relatedPersonId: b,
personDisplayName: '',
relatedPersonDisplayName: '',
relationType: 'SPOUSE_OF'
};
}
function siblingEdge(a: string, b: string, id = a + b): RelationshipDTO {
return {
id,
personId: a,
relatedPersonId: b,
personDisplayName: '',
relatedPersonDisplayName: '',
relationType: 'SIBLING_OF'
};
}
function lineage(edges: RelationshipDTO[], rootId: string) {
return highlightLineage(buildLineageIndex(edges), rootId);
}
function activeIds(edges: RelationshipDTO[], rootId: string): string[] {
return [...lineage(edges, rootId).active].sort();
}
describe('highlightLineage — active set (#703)', () => {
it('an isolated person highlights only themselves', () => {
expect(activeIds([], 'root')).toEqual(['root']);
});
it('walks the full pedigree upward (parents and grandparents)', () => {
const edges = [
parentEdge('father', 'root'),
parentEdge('mother', 'root'),
parentEdge('fatherFather', 'father'),
parentEdge('fatherMother', 'father'),
parentEdge('motherFather', 'mother'),
parentEdge('motherMother', 'mother')
];
expect(activeIds(edges, 'root')).toEqual([
'father',
'fatherFather',
'fatherMother',
'mother',
'motherFather',
'motherMother',
'root'
]);
});
it('walks the full descendant tree downward (children and grandchildren)', () => {
const edges = [
parentEdge('root', 'child'),
parentEdge('child', 'grandchild'),
parentEdge('child', 'grandchild2')
];
expect(activeIds(edges, 'root')).toEqual(['child', 'grandchild', 'grandchild2', 'root']);
});
it('activates all spouses of a blood person, including remarriages', () => {
// root's father remarried (mother + secondWife); root married twice (s1 + s2).
const edges = [
parentEdge('father', 'root'),
parentEdge('mother', 'root'),
spouseEdge('father', 'mother'),
spouseEdge('father', 'secondWife'),
spouseEdge('root', 's1'),
spouseEdge('root', 's2')
];
expect(activeIds(edges, 'root')).toEqual([
'father',
'mother',
'root',
's1',
's2',
'secondWife'
]);
});
it('stops at a married-in spouse — the in-law is active but their parents stay dimmed', () => {
const edges = [spouseEdge('root', 'inLaw'), parentEdge('inLawParent', 'inLaw')];
const { active } = lineage(edges, 'root');
expect(active.has('inLaw')).toBe(true);
expect(active.has('inLawParent')).toBe(false);
});
it('never pulls a collateral relative into the active set via a SIBLING_OF edge', () => {
const edges = [
parentEdge('parent', 'root'),
parentEdge('parent', 'sibling'),
siblingEdge('root', 'sibling')
];
const { active } = lineage(edges, 'root');
expect(active.has('parent')).toBe(true); // ancestor
expect(active.has('sibling')).toBe(false); // collateral — reached only down from an ancestor, never walked
});
});
describe('highlightLineage — connector predicate (#703)', () => {
it('is active only when both joined people are active', () => {
// root—inLaw (spouse), inLaw—inLawParent (parent). Only root + inLaw are active.
const edges = [
parentEdge('parent', 'root'),
spouseEdge('root', 'inLaw'),
parentEdge('inLawParent', 'inLaw')
];
const { isConnectorActive } = lineage(edges, 'root');
expect(isConnectorActive('root', 'parent')).toBe(true); // active ↔ active
expect(isConnectorActive('root', 'inLaw')).toBe(true); // active ↔ active spouse
expect(isConnectorActive('inLaw', 'inLawParent')).toBe(false); // active spouse ↔ dimmed in-law parent
});
});
describe('highlightLineage — cyclic data (REQ-STAMMBAUM-04 / AC10)', () => {
it('terminates on a PARENT_OF cycle and resolves the cycle members as active', () => {
// A is parent of B and B is parent of A — a cycle. The visited guard stops the
// walk re-visiting; both cycle members are reachable from root and stay active.
const edges = [parentEdge('a', 'b'), parentEdge('b', 'a')];
expect(activeIds(edges, 'a')).toEqual(['a', 'b']);
});
it('terminates on a self-referential PARENT_OF edge', () => {
const edges = [parentEdge('x', 'x')];
expect(activeIds(edges, 'x')).toEqual(['x']);
});
});