Rechtliches: Fristen- und Versionslogik mit Tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5.5 committed 2026-09-30 10:17:13 +00:00
1 parent 701ff1a8e8
commit 0105179e21
3 files changed
+253 -1

No files matched your search

+126
View File
@@ -0,0 +1,126 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
import {
addMonthsClamped,
nextCancellationDate,
deadlineTone,
currentVersion,
planNewVersion,
formatDate,
} from '../src/lib/legal.js'
const yearly = {
startDate: '2025-03-12',
termMonths: 12,
renewalMonths: 12,
noticeValue: 1,
noticeUnit: 'months',
}
test('addMonthsClamped klemmt auf Monatsende', () => {
assert.equal(addMonthsClamped('2026-01-31', 1), '2026-02-28')
assert.equal(addMonthsClamped('2028-01-31', 1), '2028-02-29')
assert.equal(addMonthsClamped('2026-03-15', 12), '2027-03-15')
assert.equal(addMonthsClamped('2026-03-31', -1), '2026-02-28')
})
test('laufender Jahresvertrag', () => {
assert.deepEqual(nextCancellationDate(yearly, '2026-09-30'), {
state: 'active',
periodEnd: '2027-03-12',
noticeBy: '2027-02-12',
})
})
test('Kündigungstermin heute zählt noch, danach nächste Periode', () => {
assert.equal(nextCancellationDate(yearly, '2027-02-12').noticeBy, '2027-02-12')
const next = nextCancellationDate(yearly, '2027-02-13')
assert.equal(next.periodEnd, '2028-03-12')
assert.equal(next.noticeBy, '2028-02-12')
})
test('Kündigungsfrist in Tagen', () => {
const r = nextCancellationDate({ ...yearly, noticeValue: 30, noticeUnit: 'days' }, '2026-09-30')
assert.equal(r.periodEnd, '2027-03-12')
assert.equal(r.noticeBy, '2027-02-10')
})
test('Start am 31. läuft nicht in den Folgemonat über', () => {
const r = nextCancellationDate(
{ startDate: '2026-01-31', termMonths: 1, renewalMonths: 1, noticeValue: 0, noticeUnit: 'days' },
'2026-02-01'
)
assert.equal(r.periodEnd, '2026-02-28')
})
test('jederzeit kündbar', () => {
const r = nextCancellationDate({ startDate: '2025-01-01', termMonths: 0 }, '2026-09-30')
assert.equal(r.state, 'anytime')
assert.equal(r.noticeBy, null)
})
test('ohne Verlängerung: endet, danach beendet', () => {
const c = { startDate: '2026-01-01', termMonths: 12, renewalMonths: 0 }
assert.deepEqual(nextCancellationDate(c, '2026-09-30'), { state: 'ends', periodEnd: '2027-01-01', noticeBy: null })
assert.equal(nextCancellationDate(c, '2027-01-02').state, 'ended')
})
test('gekündigt', () => {
const r = nextCancellationDate({ ...yearly, cancelledAt: '2026-09-01', endsAt: '2027-03-12' }, '2026-09-30')
assert.deepEqual(r, { state: 'cancelled', periodEnd: '2027-03-12', noticeBy: null })
})
test('unvollständige Angaben', () => {
assert.equal(nextCancellationDate({ ...yearly, startDate: '' }, '2026-09-30').state, 'incomplete')
assert.equal(nextCancellationDate({ ...yearly, noticeValue: null }, '2026-09-30').state, 'incomplete')
assert.equal(nextCancellationDate({ startDate: '2026-01-01' }, '2026-09-30').state, 'incomplete')
})
test('deadlineTone', () => {
const today = '2026-09-30'
const at = (noticeBy) => deadlineTone({ state: 'active', noticeBy, periodEnd: null }, today)
assert.equal(at('2026-10-30'), 'red')
assert.equal(at('2026-10-31'), 'orange')
assert.equal(at('2026-12-29'), 'orange')
assert.equal(at('2026-12-30'), 'normal')
assert.equal(deadlineTone({ state: 'cancelled' }, today), 'grey')
assert.equal(deadlineTone({ state: 'ended' }, today), 'grey')
assert.equal(deadlineTone({ state: 'incomplete' }, today), 'missing')
assert.equal(deadlineTone({ state: 'anytime' }, today), 'normal')
assert.equal(deadlineTone({ state: 'ends' }, today), 'normal')
})
const v5 = { $id: 'v5', documentKey: 'agb', version: 'v5', status: 'current', validFrom: '2026-05-01', validTo: null }
const v4 = { $id: 'v4', documentKey: 'agb', version: 'v4', status: 'superseded', validFrom: '2026-01-10', validTo: '2026-04-30' }
test('neue gültige Version löst die aktuelle ab', () => {
const r = planNewVersion([v4, v5], { documentKey: 'agb', version: 'v6', status: 'current', validFrom: '2026-09-01' })
assert.deepEqual(r.updates, [{ id: 'v5', patch: { status: 'superseded', validTo: '2026-08-31' } }])
assert.equal(r.create.validTo, null)
assert.equal(r.create.status, 'current')
})
test('Entwurf löst nichts ab', () => {
const r = planNewVersion([v4, v5], { documentKey: 'agb', version: 'v6', status: 'draft', validFrom: '2026-12-01' })
assert.deepEqual(r.updates, [])
assert.equal(r.create.status, 'draft')
})
test('Nachtrag einer alten Fassung löst die aktuelle nicht ab', () => {
const r = planNewVersion([v4, v5], { documentKey: 'agb', version: 'v4b', status: 'current', validFrom: '2026-02-01' })
assert.deepEqual(r.updates, [])
assert.equal(r.create.status, 'superseded')
assert.equal(r.create.validTo, '2026-04-30')
})
test('currentVersion nimmt bei zwei gültigen die jüngere', () => {
const twin = { ...v5, $id: 'v6', version: 'v6', validFrom: '2026-09-01' }
assert.equal(currentVersion([v4, v5, twin]).$id, 'v6')
assert.equal(currentVersion([v4]), null)
})
test('formatDate', () => {
assert.equal(formatDate('2026-09-30'), '30.09.2026')
assert.equal(formatDate(''), '–')
assert.equal(formatDate(null), '–')
})