import assert from 'node:assert/strict' import test from 'node:test' import { mergeGraph } from '../app/utils/graph.ts' const cms = { '@context': 'https://schema.org', '@graph': [ { '@type': 'WebPage', '@id': 'https://acme.test/x#webpage', name: 'CMS name' }, { '@type': 'Organization', '@id': 'https://acme.test/#org', name: 'Acme' }, ], } test('mergeGraph: a layer node with a CMS @id merges into it, CMS values first', () => { const g = mergeGraph(cms, [{ '@id': 'https://acme.test/x#webpage', name: 'Layer name', inLanguage: 'pl' }]) assert.ok(g) assert.equal(g['@graph'].length, 2) assert.deepEqual(g['@graph'][0], { '@id': 'https://acme.test/x#webpage', name: 'CMS name', inLanguage: 'pl', '@type': 'WebPage' }) }) test('mergeGraph: a node with a new @id or none is appended, in order', () => { const g = mergeGraph(cms, [{ '@type': 'ItemList', '@id': 'https://acme.test/x#itemlist' }, { '@type': 'Thing' }]) assert.ok(g) assert.deepEqual( g['@graph'].map((n) => n['@type']), ['WebPage', 'Organization', 'ItemList', 'Thing'], ) }) test('mergeGraph: no duplicate @id, also within the CMS graph and among layer nodes', () => { const g = mergeGraph({ '@graph': [{ '@id': 'a', x: 1 }, { '@id': 'a', y: 2 }] }, [{ '@id': 'a', z: 3 }, { '@id': 'b' }, { '@id': 'b', w: 4 }]) assert.ok(g) const ids = g['@graph'].map((n) => n['@id']) assert.deepEqual(ids, ['a', 'b']) assert.deepEqual(g['@graph'][0], { '@id': 'a', x: 1, y: 2, z: 3 }) assert.deepEqual(g['@graph'][1], { '@id': 'b', w: 4 }) }) test('mergeGraph: one context, nested @context removed from nodes', () => { const g = mergeGraph(null, [{ '@context': 'https://schema.org', '@type': 'FAQPage', '@id': 'f' }]) assert.ok(g) assert.equal(g['@context'], 'https://schema.org') assert.deepEqual(g['@graph'], [{ '@type': 'FAQPage', '@id': 'f' }]) }) test('mergeGraph: a single-node jsonld counts as one node', () => { const g = mergeGraph({ '@context': 'https://schema.org', '@type': 'WebSite', '@id': 'w' }, []) assert.deepEqual(g, { '@context': 'https://schema.org', '@graph': [{ '@type': 'WebSite', '@id': 'w' }] }) }) test('mergeGraph: nothing to emit is null (no JSON-LD script)', () => { assert.equal(mergeGraph(null, []), null) assert.equal(mergeGraph(undefined), null) assert.equal(mergeGraph({ '@graph': [] }, [null, 'x', 3]), null) assert.equal(mergeGraph('nope', []), null) }) test('mergeGraph: inputs are not mutated', () => { const input = JSON.parse(JSON.stringify(cms)) const node = { '@id': 'https://acme.test/x#webpage', extra: true } mergeGraph(input, [node]) assert.deepEqual(input, cms) assert.deepEqual(node, { '@id': 'https://acme.test/x#webpage', extra: true }) })