import { describe, report } from '../core/_harness.mjs';
// Mock aggregation logic for testing conflict resolution
// Priority: component > service > time (newer wins)
function mockAggregateAnswers(searchResults) {
const byComponent = new Map();
const byService = new Map();
const fallback = [];
for (const result of searchResults) {
const metadata = result.metadata || {};
if (metadata.component) {
if (!byComponent.has(metadata.component)) {
byComponent.set(metadata.component, []);
}
byComponent.get(metadata.component).push(result);
} else if (metadata.service) {
if (!byService.has(metadata.service)) {
byService.set(metadata.service, []);
}
byService.get(metadata.service).push(result);
} else {
fallback.push(result);
}
}
// Sort by recency within each group
const sortByTime = (a, b) => {
const timeA = new Date(a.metadata?.updated || '1970-01-01');
const timeB = new Date(b.metadata?.updated || '1970-01-01');
return timeB - timeA;
};
const aggregated = [];
// Component level (highest priority)
for (const [component, results] of byComponent) {
aggregated.push(...results.sort(sortByTime));
}
// Service level
for (const [service, results] of byService) {
aggregated.push(...results.sort(sortByTime));
}
// Fallback by time only
aggregated.push(...fallback.sort(sortByTime));
return aggregated;
}
describe('answer aggregation', (it) => {
const mockResults = [
{
docId: 'service-a.md',
title: 'Service A Guide',
metadata: { service: 'serviceA', updated: '2025-09-01' }
},
{
docId: 'component-x.md',
title: 'Component X Manual',
metadata: { component: 'componentX', updated: '2025-08-15' }
},
{
docId: 'service-a-new.md',
title: 'Service A Updated',
metadata: { service: 'serviceA', updated: '2025-10-01' }
},
{
docId: 'general.md',
title: 'General Procedures',
metadata: { updated: '2025-07-01' }
}
];
it('prioritizes component over service over time', () => {
const result = mockAggregateAnswers(mockResults);
// Component should come first
if (result[0].metadata.component !== 'componentX') {
throw new Error('Component should have highest priority');
}
// Then services (newer first)
if (!result[1].docId.includes('service-a-new')) {
throw new Error('Newer service documentation should come before older');
}
});
it('handles empty results', () => {
const result = mockAggregateAnswers([]);
if (result.length !== 0) throw new Error('Empty input should yield empty output');
});
});
report();