brain/scripts/test/person-matcher.test.js

113 lines
3.2 KiB
JavaScript

const { describe, it } = require('node:test');
const assert = require('node:assert/strict');
const {
parseFrontmatter,
matchAttendeeToPersons,
buildNewPersonNote,
resolveCompanyFromDomain
} = require('../lib/person-matcher.js');
describe('parseFrontmatter', () => {
it('extracts YAML frontmatter from markdown', () => {
const content = `---
tags: [person, mitarbeiter]
vorname: Christopher
nachname: Klein
email: c.klein@krah.de
kategorie: Mitarbeiter
---
# Christopher Klein
`;
const fm = parseFrontmatter(content);
assert.equal(fm.vorname, 'Christopher');
assert.equal(fm.nachname, 'Klein');
assert.equal(fm.email, 'c.klein@krah.de');
});
it('returns empty object for no frontmatter', () => {
const fm = parseFrontmatter('# Just a heading');
assert.deepEqual(fm, {});
});
});
describe('matchAttendeeToPersons', () => {
const persons = [
{ file: 'Christopher Klein.md', fm: { vorname: 'Christopher', nachname: 'Klein', email: 'c.klein@krah.de' } },
{ file: 'Philip Losch.md', fm: { vorname: 'Philip', nachname: 'Losch', email: 'p.losch@krah.de' } }
];
it('matches by email (priority)', () => {
const result = matchAttendeeToPersons(
{ name: 'Chris K.', email: 'c.klein@krah.de' },
persons
);
assert.equal(result.matched, true);
assert.equal(result.file, 'Christopher Klein.md');
assert.equal(result.matchType, 'email');
});
it('falls back to name match', () => {
const result = matchAttendeeToPersons(
{ name: 'Philip Losch', email: 'philip@private.de' },
persons
);
assert.equal(result.matched, true);
assert.equal(result.file, 'Philip Losch.md');
assert.equal(result.matchType, 'name');
});
it('returns unmatched for unknown attendee', () => {
const result = matchAttendeeToPersons(
{ name: 'Max Müller', email: 'max@landata.de' },
persons
);
assert.equal(result.matched, false);
});
});
describe('resolveCompanyFromDomain', () => {
const companies = [
{ file: 'KRAH.md', fm: { domain: 'krah.de', kurzname: 'KRAH' } }
];
it('resolves known domain to wikilink', () => {
const result = resolveCompanyFromDomain('c.klein@krah.de', companies);
assert.equal(result, '[[00 Kontext/Firmen/KRAH]]');
});
it('returns plain domain for unknown company', () => {
const result = resolveCompanyFromDomain('max@landata.de', companies);
assert.equal(result, 'landata.de');
});
});
describe('buildNewPersonNote', () => {
it('generates markdown for new person', () => {
const note = buildNewPersonNote({
name: 'Max Müller',
email: 'max.mueller@landata.de',
firma: 'landata.de'
});
assert.ok(note.includes('vorname: Max'));
assert.ok(note.includes('nachname: Müller'));
assert.ok(note.includes('email: max.mueller@landata.de'));
assert.ok(note.includes('kategorie: Extern'));
assert.ok(note.includes('status: ungeprüft'));
assert.ok(note.includes('# Max Müller'));
});
it('handles single-word display names', () => {
const note = buildNewPersonNote({
name: 'Siri',
email: 'siri@example.com',
firma: 'example.com'
});
assert.ok(note.includes('vorname: Siri'));
assert.ok(note.includes('nachname: ""'));
});
});