Files
it-tools/src/tools/xml-diff/xml-diff.reconcile.test.ts
T
e8f0677f94 feat(xml-diff): add XML diff tool
Compare two pasted XML documents and highlight the differences, mirroring
the JSON diff tool's UI and diff engine. XML is parsed with a native
DOMParser-based normalizer (attributes as @name, text-only leaves as plain
strings) instead of xml-js, and a reconciliation step coerces mismatched
single/repeated element shapes into arrays before diffing so a repeated
element compares cleanly instead of producing a garbled type-mismatch diff.

From PR #1840
Fix #1839

Co-Authored-By: @sharevb
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-22 21:06:08 +01:00

64 lines
2.2 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { diff } from '../json-diff/json-diff.models';
import { reconcileArrayShapes } from './xml-diff.reconcile';
describe('xml-diff reconcile', () => {
describe('reconcileArrayShapes', () => {
it('leaves matching plain values untouched', () => {
expect(reconcileArrayShapes({ item: 'A' }, { item: 'B' })).toEqual([{ item: 'A' }, { item: 'B' }]);
});
it('pads the shorter side when both are arrays of different lengths', () => {
expect(reconcileArrayShapes({ item: ['A'] }, { item: ['A', 'B'] })).toEqual([
{ item: ['A', undefined] },
{ item: ['A', 'B'] },
]);
});
it('wraps a single value in an array when the other side has an array for the same key', () => {
const [left, right] = reconcileArrayShapes({ item: 'A' }, { item: ['A', 'B'] });
expect(left).toEqual({ item: ['A', undefined] });
expect(right).toEqual({ item: ['A', 'B'] });
});
it('recurses into nested objects', () => {
const [left, right] = reconcileArrayShapes({ root: { item: 'A' } }, { root: { item: ['A', 'B'] } });
expect(left).toEqual({ root: { item: ['A', undefined] } });
expect(right).toEqual({ root: { item: ['A', 'B'] } });
});
});
describe('diff after reconciliation', () => {
it('produces a clean diff when an element goes from single to repeated', () => {
const left = { item: 'A' };
const right = { item: ['A', 'B'] };
const [reconciledLeft, reconciledRight] = reconcileArrayShapes(left, right);
const result = diff(reconciledLeft, reconciledRight);
expect(result).toEqual({
key: '',
type: 'object',
oldValue: { item: ['A', undefined] },
value: { item: ['A', 'B'] },
status: 'children-updated',
children: [
{
key: 'item',
type: 'array',
oldValue: ['A', undefined],
value: ['A', 'B'],
status: 'children-updated',
children: [
{ key: 0, type: 'value', oldValue: 'A', value: 'A', status: 'unchanged' },
{ key: 1, type: 'value', oldValue: undefined, value: 'B', status: 'added' },
],
},
],
});
});
});
});