Assessor_2.0/tests/importers/json-importer.test.ts

35 lines
1.3 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { flattenObject, importJson } from '../../src/importers/json-importer';
import { sourceMetadata } from './import-source-fixtures';
describe('JSON importer', () => {
it('flattens nested objects and preserves arrays as JSON', () => {
expect(flattenObject({ a: { b: 2 }, values: ['x', 'y'] })).toEqual({
'a.b': 2,
values: '["x","y"]'
});
});
it('imports an object array as a tabular dataset', () => {
const text = JSON.stringify([
{ id: 'RULE-1', finding: { title: 'Weak policy' }, severity: 'High' },
{ id: 'RULE-2', finding: { title: 'Legacy protocol' }, severity: 'Medium' }
]);
const source = sourceMetadata('findings.json', text.length, 'application/json');
const imported = importJson(source, text);
expect(imported.format).toBe('JSON');
expect(imported.sheets[0].rows[0]).toEqual(['id', 'finding.title', 'severity']);
expect(imported.sheets[0].analysis.records).toHaveLength(2);
expect(imported.sheets[0].analysis.records[0].title).toBe('Weak policy');
});
it('rejects malformed JSON', () => {
const text = '{"finding":';
const source = sourceMetadata('broken.json', text.length, 'application/json');
expect(() => importJson(source, text)).toThrow(SyntaxError);
});
});