Assessor_2.0/tests/server/regulatory-chunker.test.ts

39 lines
2.0 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import type { RegulatoryControlDocument } from '../../src/domain/regulatory-rag';
import { chunkRegulatoryControl, regulatoryChunkEmbeddingText, validateRegulatoryControlDocument } from '../../server/regulatory/regulatory-chunker';
const document: RegulatoryControlDocument = {
schemaVersion: 1,
corpusId: 'iso-27001',
corpusVersion: '2022-de-1',
documentId: 'iso-27001-2022-a.5.1-de',
framework: 'ISO/IEC 27001',
frameworkVersion: '2022',
controlId: 'A.5.1',
language: 'de',
title: 'Informationssicherheitsrichtlinien',
text: `${'Erster kontrollierter Absatz. '.repeat(8)}\r\n\r\n${'Second controlled paragraph. '.repeat(8)}`,
sourceUri: 'https://example.invalid/iso-27001/a-5-1'
};
describe('regulatory chunker', () => {
it('creates deterministic bounded chunks with complete metadata', () => {
const first = chunkRegulatoryControl(document, { maximumCharacters: 220 });
const second = chunkRegulatoryControl(document, { maximumCharacters: 220 });
expect(first).toEqual(second);
expect(first.length).toBeGreaterThan(1);
expect(first.every(chunk => chunk.text.length <= 220)).toBe(true);
expect(first.map(chunk => chunk.chunkIndex)).toEqual(first.map((_, index) => index));
expect(first.every(chunk => chunk.framework === 'ISO/IEC 27001' && chunk.language === 'de')).toBe(true);
expect(first.every(chunk => /^[a-f0-9]{64}$/.test(chunk.textSha256))).toBe(true);
expect(regulatoryChunkEmbeddingText(first[0])).toContain('ISO/IEC 27001 2022 | A.5.1');
});
it('validates the version, required values and language before indexing', () => {
expect(() => validateRegulatoryControlDocument({ ...document, schemaVersion: 3 })).toThrow(/schemaVersion 1 or 2/);
expect(() => validateRegulatoryControlDocument({ ...document, controlId: ' ' })).toThrow(/controlId/);
expect(() => validateRegulatoryControlDocument({ ...document, language: 'not_a_tag' })).toThrow(/BCP 47/);
});
});