|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { clone_subtree_with_new_ids } from './document_graph.js'; |
| 3 | + |
| 4 | +function make_id_generator() { |
| 5 | + let counter = 0; |
| 6 | + return () => `new_${++counter}`; |
| 7 | +} |
| 8 | + |
| 9 | +// A page with a body containing a prose block, a heading whose text carries a |
| 10 | +// link mark pointing at a link node, and shared nav/footer references that live |
| 11 | +// in other documents. |
| 12 | +function make_page_nodes() { |
| 13 | + return { |
| 14 | + page_1: { |
| 15 | + id: 'page_1', |
| 16 | + type: 'page', |
| 17 | + title: { content: 'Hello', marks: [], annotations: [] }, |
| 18 | + description: { content: '', marks: [], annotations: [] }, |
| 19 | + image: 'image_1', |
| 20 | + nav: 'nav_doc', |
| 21 | + footer: 'footer_doc', |
| 22 | + body: { nodes: ['prose_1'], marks: [], annotations: [] } |
| 23 | + }, |
| 24 | + image_1: { id: 'image_1', type: 'image', src: '/a.png' }, |
| 25 | + prose_1: { |
| 26 | + id: 'prose_1', |
| 27 | + type: 'prose', |
| 28 | + layout: 'narrow-left', |
| 29 | + body: { nodes: ['heading_1'], marks: [], annotations: [] } |
| 30 | + }, |
| 31 | + heading_1: { |
| 32 | + id: 'heading_1', |
| 33 | + type: 'heading_1', |
| 34 | + content: { |
| 35 | + content: 'Linked', |
| 36 | + marks: [{ start_offset: 0, end_offset: 6, node_id: 'link_1' }], |
| 37 | + annotations: [] |
| 38 | + } |
| 39 | + }, |
| 40 | + link_1: { id: 'link_1', type: 'link', href: '/somewhere' } |
| 41 | + } as any; |
| 42 | +} |
| 43 | + |
| 44 | +const shared_roots = new Set(['nav_doc', 'footer_doc']); |
| 45 | + |
| 46 | +describe('clone_subtree_with_new_ids', () => { |
| 47 | + it('gives every copied node a fresh id', () => { |
| 48 | + const nodes = make_page_nodes(); |
| 49 | + const result = clone_subtree_with_new_ids('page_1', nodes, make_id_generator(), shared_roots); |
| 50 | + |
| 51 | + const source_ids = Object.keys(nodes); |
| 52 | + const cloned_ids = Object.keys(result.nodes); |
| 53 | + |
| 54 | + expect(cloned_ids).toHaveLength(source_ids.length); |
| 55 | + for (const id of cloned_ids) { |
| 56 | + expect(source_ids).not.toContain(id); |
| 57 | + } |
| 58 | + // The stored key and the node's own id agree. |
| 59 | + for (const [id, node] of Object.entries(result.nodes)) { |
| 60 | + expect((node as any).id).toBe(id); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + it('rewrites node and node_array references to the copies', () => { |
| 65 | + const nodes = make_page_nodes(); |
| 66 | + const result = clone_subtree_with_new_ids('page_1', nodes, make_id_generator(), shared_roots); |
| 67 | + |
| 68 | + const page = result.nodes[result.root_id] as any; |
| 69 | + const prose_id = page.body.nodes[0]; |
| 70 | + const prose = result.nodes[prose_id] as any; |
| 71 | + |
| 72 | + expect(result.nodes[page.image]).toBeDefined(); |
| 73 | + expect(prose).toBeDefined(); |
| 74 | + expect(result.nodes[prose.body.nodes[0]]).toBeDefined(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('rewrites mark references inside annotated text', () => { |
| 78 | + const nodes = make_page_nodes(); |
| 79 | + const result = clone_subtree_with_new_ids('page_1', nodes, make_id_generator(), shared_roots); |
| 80 | + |
| 81 | + const page = result.nodes[result.root_id] as any; |
| 82 | + const prose = result.nodes[page.body.nodes[0]] as any; |
| 83 | + const heading = result.nodes[prose.body.nodes[0]] as any; |
| 84 | + const link_id = heading.content.marks[0].node_id; |
| 85 | + |
| 86 | + expect(link_id).not.toBe('link_1'); |
| 87 | + expect(result.nodes[link_id]).toBeDefined(); |
| 88 | + expect((result.nodes[link_id] as any).type).toBe('link'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('leaves excluded shared roots pointing at their own documents', () => { |
| 92 | + const nodes = make_page_nodes(); |
| 93 | + const result = clone_subtree_with_new_ids('page_1', nodes, make_id_generator(), shared_roots); |
| 94 | + |
| 95 | + const page = result.nodes[result.root_id] as any; |
| 96 | + expect(page.nav).toBe('nav_doc'); |
| 97 | + expect(page.footer).toBe('footer_doc'); |
| 98 | + expect(result.nodes.nav_doc).toBeUndefined(); |
| 99 | + expect(result.nodes.footer_doc).toBeUndefined(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('leaves the source document untouched', () => { |
| 103 | + const nodes = make_page_nodes(); |
| 104 | + const before = structuredClone(nodes); |
| 105 | + clone_subtree_with_new_ids('page_1', nodes, make_id_generator(), shared_roots); |
| 106 | + expect(nodes).toEqual(before); |
| 107 | + }); |
| 108 | +}); |
0 commit comments