kundencenter/apps/api/test/drift.test.ts
Claude (KI) bcf188df34 Management-Auswertung: Rolle nur mit analytics.read, lesender MVP (#133)
- Konzept docs/management-auswertung.md: Rechte, Kennzahlen mit
  Definitionen und Quellen, fachlich offene Punkte (nichts erfunden).
- Neue Personalrolle management (Migration 045) mit genau analytics.read;
  niemandem automatisch zugewiesen, vergeben nur durch Superadmin.
- GET /analytics/summary (nur lesend): Kunden/Verträge, Rechnungen
  ausgestellt/Storno/nach Storno, als bezahlt markiert, offen/überfällig,
  nach Steuersatz, Kategorie und Monat; Datenstand, EUR, Definitionen.
- Seite /auswertung mit Zeitraumfilter.
Tests an festen Daten (Storno, 19/7 %, Zeitraumgrenze, Entwurf) und
Rechte-/Schreibweg-Tests. Drift-Test nutzt vorhandene Steuersätze.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 16:01:31 +02:00

42 lines
3.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from 'vitest';
import { randomUUID } from 'node:crypto';
import { one, run } from '../src/core/db.js';
import { expectedState, findDrift, matches } from '@kc/platform/drift';
describe('Soll-Ist-Abgleich Vertrag ↔ Anbieter (#21)', () => {
it('Sollzustand je Vertragsstatus, Vergleichsregel', () => {
expect(expectedState('active')).toBe('active'); expect(expectedState('suspended')).toBe('suspended');
expect(expectedState('cancelled')).toBe('inactive'); expect(expectedState('pending')).toBeNull(); expect(expectedState('failed')).toBeNull();
expect(matches('inactive', 'expired')).toBe(true); expect(matches('inactive', 'active')).toBe(false); expect(matches('active', 'suspended')).toBe(false);
});
it('erkennt Abweichungen, kennzeichnet unsichere Beobachtungen und schreibt nichts zurück', async () => {
const org = randomUUID(); await run("INSERT INTO organizations (id, customer_number, name, customer_type) VALUES (?, 'K-DRIFT', 'Drift GmbH', 'business')", [org]);
const tax = (await one('SELECT id FROM tax_rates WHERE rate_bp = 1900 LIMIT 1'))!.id; // vorhandene Stammdaten nutzen, nichts hinzufügen
const ok = randomUUID(); const down = randomUUID();
await run("INSERT INTO connector_instances (id, connector_key, name, config_json, health) VALUES (?, 'mock', 'Drift ok', '{}', 'ok'), (?, 'mock', 'Drift gestört', '{}', 'down')", [ok, down]);
const mk = async (status: string, state: string, inst: string, syncedMinAgo = 0) => {
const prod = randomUUID(), ver = randomUUID(), ord = randomUUID(), item = randomUUID(), res = randomUUID(), con = randomUUID();
await run("INSERT INTO products (id, sku, category) VALUES (?, ?, 'hosting')", [prod, `DRIFT-${prod.slice(0, 8)}`]);
await run("INSERT INTO product_versions (id, product_id, version, name, tax_rate_id, tax_bp, billing_interval) VALUES (?,?,1,'P',?,1900,'monthly')", [ver, prod, tax]);
await run("INSERT INTO orders (id, number, org_id, status, placed_via) VALUES (?, ?, ?, 'completed', 'staff')", [ord, `B-D${ord.slice(0, 6)}`, org]);
await run("INSERT INTO order_items (id, order_id, product_version_id, snapshot_json) VALUES (?,?,?,'{}')", [item, ord, ver]);
await run('INSERT INTO resources (id, instance_id, external_ref, type, name, state, org_id, synced_at) VALUES (?,?,?,\'hosting\',?,?,?, DATE_SUB(UTC_TIMESTAMP(3), INTERVAL ? MINUTE))', [res, inst, res.slice(0, 8), `Res ${status}/${state}`, state, org, syncedMinAgo]);
await run("INSERT INTO contracts (id, number, org_id, order_item_id, product_version_id, status, resource_id, price_snapshot_json) VALUES (?,?,?,?,?,?,?,'{}')", [con, `V-D${con.slice(0, 6)}`, org, item, ver, status, res]);
return { con, res };
};
const a = await mk('active', 'suspended', ok); // Abweichung: im Panel gesperrt
const b = await mk('cancelled', 'active', ok); // Abweichung: beendet, aber aktiv
const c = await mk('active', 'active', ok); // passt
const d = await mk('suspended', 'active', down); // Anbieter gestört → ungewiss
const e = await mk('active', 'expired', ok, 120); // veraltet (Intervall 300 s × 3) → ungewiss
const f = await mk('pending', 'unknown', ok); // kein Soll → nicht verglichen
const drift = (await findDrift()).filter((x) => x.orgId === org);
const by = Object.fromEntries(drift.map((x) => [x.contractId, x.kind]));
expect(by[a.con]).toBe('mismatch'); expect(by[b.con]).toBe('mismatch');
expect(by[d.con]).toBe('uncertain'); expect(by[e.con]).toBe('uncertain');
expect(by[c.con]).toBeUndefined(); expect(by[f.con]).toBeUndefined();
expect(drift[0]!.kind).toBe('mismatch'); // sichere Abweichungen zuerst
expect((await one('SELECT state FROM resources WHERE id = ?', [a.res]))!.state).toBe('suspended'); // nichts zurückgeschrieben
});
});