107 lines
3.7 KiB
TypeScript
107 lines
3.7 KiB
TypeScript
import { describe, test, expect } from 'vitest';
|
|||
|
|
import { findUnreachableNodeIds, repairOrphanNodes } from './orphan-repair';
|
||
|
|
|
||
|
|
/** Minimal Craft-shaped node. */
|
||
|
|
function node(over: Record<string, any> = {}) {
|
||
|
|
return {
|
||
|
|
type: { resolvedName: 'Container' },
|
||
|
|
isCanvas: false,
|
||
|
|
props: {},
|
||
|
|
displayName: 'Container',
|
||
|
|
custom: {},
|
||
|
|
hidden: false,
|
||
|
|
nodes: [],
|
||
|
|
linkedNodes: {},
|
||
|
|
parent: 'ROOT',
|
||
|
|
...over,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
const healthy = JSON.stringify({
|
||
|
|
ROOT: node({ isCanvas: true, parent: null, nodes: ['a'] }),
|
||
|
|
a: node({ parent: 'ROOT' }),
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('findUnreachableNodeIds', () => {
|
||
|
|
test('a healthy tree has no unreachable nodes', () => {
|
||
|
|
expect(findUnreachableNodeIds(JSON.parse(healthy))).toEqual([]);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('a node ROOT does not list is unreachable even when its parent says ROOT', () => {
|
||
|
|
const nodes = JSON.parse(healthy);
|
||
|
|
nodes.stray = node({ parent: 'ROOT', displayName: 'HTML' });
|
||
|
|
expect(findUnreachableNodeIds(nodes)).toEqual(['stray']);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('a node whose parent no longer exists is unreachable', () => {
|
||
|
|
const nodes = JSON.parse(healthy);
|
||
|
|
nodes.stray = node({ parent: 'ghost' });
|
||
|
|
expect(findUnreachableNodeIds(nodes)).toEqual(['stray']);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('children of an unreachable node are also unreachable', () => {
|
||
|
|
const nodes = JSON.parse(healthy);
|
||
|
|
nodes.stray = node({ parent: 'ghost', nodes: ['strayChild'] });
|
||
|
|
nodes.strayChild = node({ parent: 'stray' });
|
||
|
|
expect(findUnreachableNodeIds(nodes).sort()).toEqual(['stray', 'strayChild']);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('linkedNodes children count as reachable', () => {
|
||
|
|
const nodes = JSON.parse(healthy);
|
||
|
|
nodes.ROOT.linkedNodes = { inner: 'linked' };
|
||
|
|
nodes.linked = node({ parent: 'ROOT' });
|
||
|
|
expect(findUnreachableNodeIds(nodes)).toEqual([]);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('a cycle among orphans terminates instead of hanging', () => {
|
||
|
|
const nodes = JSON.parse(healthy);
|
||
|
|
nodes.x = node({ parent: 'y', nodes: ['y'] });
|
||
|
|
nodes.y = node({ parent: 'x', nodes: ['x'] });
|
||
|
|
expect(findUnreachableNodeIds(nodes).sort()).toEqual(['x', 'y']);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('repairOrphanNodes', () => {
|
||
|
|
test('a healthy tree is returned byte-identical with nothing repaired', () => {
|
||
|
|
const out = repairOrphanNodes(healthy);
|
||
|
|
expect(out.repaired).toEqual([]);
|
||
|
|
expect(out.state).toBe(healthy);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('an orphan is appended to the end of ROOT.nodes and reparented', () => {
|
||
|
|
const nodes = JSON.parse(healthy);
|
||
|
|
nodes.stray = node({ parent: 'ghost', displayName: 'HTML' });
|
||
|
|
const out = repairOrphanNodes(JSON.stringify(nodes));
|
||
|
|
|
||
|
|
expect(out.repaired).toEqual(['stray']);
|
||
|
|
const parsed = JSON.parse(out.state);
|
||
|
|
expect(parsed.ROOT.nodes).toEqual(['a', 'stray']);
|
||
|
|
expect(parsed.stray.parent).toBe('ROOT');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('only the top of an orphan subtree is reattached; its children ride along', () => {
|
||
|
|
const nodes = JSON.parse(healthy);
|
||
|
|
nodes.stray = node({ parent: 'ghost', nodes: ['strayChild'] });
|
||
|
|
nodes.strayChild = node({ parent: 'stray' });
|
||
|
|
const out = repairOrphanNodes(JSON.stringify(nodes));
|
||
|
|
|
||
|
|
expect(out.repaired).toEqual(['stray']);
|
||
|
|
const parsed = JSON.parse(out.state);
|
||
|
|
expect(parsed.ROOT.nodes).toEqual(['a', 'stray']);
|
||
|
|
expect(parsed.strayChild.parent).toBe('stray');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('malformed JSON comes back unchanged rather than throwing', () => {
|
||
|
|
const out = repairOrphanNodes('{not json');
|
||
|
|
expect(out.state).toBe('{not json');
|
||
|
|
expect(out.repaired).toEqual([]);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('state with no ROOT comes back unchanged', () => {
|
||
|
|
const orphanOnly = JSON.stringify({ a: node({ parent: null }) });
|
||
|
|
const out = repairOrphanNodes(orphanOnly);
|
||
|
|
expect(out.state).toBe(orphanOnly);
|
||
|
|
expect(out.repaired).toEqual([]);
|
||
|
|
});
|
||
|
|
});
|