|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { walkPageComponents, isSourceAuthoredPage } from './page-walk.js'; |
| 5 | + |
| 6 | +const paths = (page: Record<string, unknown>) => |
| 7 | + walkPageComponents(page, 'pages[0]').map((w) => w.path); |
| 8 | + |
| 9 | +describe('walkPageComponents — where components actually live', () => { |
| 10 | + it('walks regions[].components[]', () => { |
| 11 | + expect( |
| 12 | + paths({ |
| 13 | + regions: [ |
| 14 | + { name: 'main', components: [{ type: 'a' }, { type: 'b' }] }, |
| 15 | + { name: 'side', components: [{ type: 'c' }] }, |
| 16 | + ], |
| 17 | + }), |
| 18 | + ).toEqual([ |
| 19 | + 'pages[0].regions[0].components[0]', |
| 20 | + 'pages[0].regions[0].components[1]', |
| 21 | + 'pages[0].regions[1].components[0]', |
| 22 | + ]); |
| 23 | + }); |
| 24 | + |
| 25 | + it('walks slots, normalizing a single component to a list', () => { |
| 26 | + expect( |
| 27 | + paths({ |
| 28 | + kind: 'slotted', |
| 29 | + slots: { |
| 30 | + highlights: { type: 'a' }, |
| 31 | + tabs: [{ type: 'b' }, { type: 'c' }], |
| 32 | + }, |
| 33 | + }), |
| 34 | + ).toEqual([ |
| 35 | + 'pages[0].slots.highlights', |
| 36 | + 'pages[0].slots.tabs[0]', |
| 37 | + 'pages[0].slots.tabs[1]', |
| 38 | + ]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('finds nothing in a top-level `components` array — PageSchema has none', () => { |
| 42 | + // Guards the bug this module exists to prevent: a walker reading |
| 43 | + // `page.components` visits nothing on a real stack while looking correct. |
| 44 | + expect(paths({ components: [{ type: 'a' }] })).toEqual([]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('recurses through properties.children — the layout-container shape', () => { |
| 48 | + expect( |
| 49 | + paths({ |
| 50 | + regions: [ |
| 51 | + { |
| 52 | + name: 'main', |
| 53 | + components: [ |
| 54 | + { |
| 55 | + type: 'flex', |
| 56 | + properties: { |
| 57 | + children: [ |
| 58 | + { type: 'flex', properties: { children: [{ type: 'leaf' }] } }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + ], |
| 65 | + }), |
| 66 | + ).toEqual([ |
| 67 | + 'pages[0].regions[0].components[0]', |
| 68 | + 'pages[0].regions[0].components[0].properties.children[0]', |
| 69 | + 'pages[0].regions[0].components[0].properties.children[0].properties.children[0]', |
| 70 | + ]); |
| 71 | + }); |
| 72 | + |
| 73 | + it('recurses through properties.items[].children, body and footer', () => { |
| 74 | + expect( |
| 75 | + paths({ |
| 76 | + regions: [ |
| 77 | + { |
| 78 | + name: 'main', |
| 79 | + components: [ |
| 80 | + { type: 'page:tabs', properties: { items: [{ children: [{ type: 'x' }] }] } }, |
| 81 | + { type: 'page:card', properties: { body: [{ type: 'y' }], footer: [{ type: 'z' }] } }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + ], |
| 85 | + }), |
| 86 | + ).toEqual([ |
| 87 | + 'pages[0].regions[0].components[0]', |
| 88 | + 'pages[0].regions[0].components[0].properties.items[0].children[0]', |
| 89 | + 'pages[0].regions[0].components[1]', |
| 90 | + 'pages[0].regions[0].components[1].properties.body[0]', |
| 91 | + 'pages[0].regions[0].components[1].properties.footer[0]', |
| 92 | + ]); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('walkPageComponents — object binding precedence', () => { |
| 97 | + const bindings = (page: Record<string, unknown>) => |
| 98 | + walkPageComponents(page, 'pages[0]').map((w) => w.objectName); |
| 99 | + |
| 100 | + it('inherits the page object, and lets dataSource then properties override', () => { |
| 101 | + expect( |
| 102 | + bindings({ |
| 103 | + object: 'page_obj', |
| 104 | + regions: [ |
| 105 | + { |
| 106 | + name: 'main', |
| 107 | + components: [ |
| 108 | + { type: 'a' }, |
| 109 | + { type: 'b', dataSource: { object: 'ds_obj' } }, |
| 110 | + { type: 'c', properties: { object: 'prop_obj' } }, |
| 111 | + // dataSource wins over properties. |
| 112 | + { type: 'd', dataSource: { object: 'ds_obj' }, properties: { object: 'prop_obj' } }, |
| 113 | + ], |
| 114 | + }, |
| 115 | + ], |
| 116 | + }), |
| 117 | + ).toEqual(['page_obj', 'ds_obj', 'prop_obj', 'ds_obj']); |
| 118 | + }); |
| 119 | + |
| 120 | + it('propagates an overridden binding down to nested children', () => { |
| 121 | + const walked = walkPageComponents( |
| 122 | + { |
| 123 | + object: 'page_obj', |
| 124 | + regions: [ |
| 125 | + { |
| 126 | + name: 'main', |
| 127 | + components: [ |
| 128 | + { |
| 129 | + type: 'flex', |
| 130 | + dataSource: { object: 'ds_obj' }, |
| 131 | + properties: { children: [{ type: 'leaf' }] }, |
| 132 | + }, |
| 133 | + ], |
| 134 | + }, |
| 135 | + ], |
| 136 | + }, |
| 137 | + 'pages[0]', |
| 138 | + ); |
| 139 | + expect(walked.map((w) => w.objectName)).toEqual(['ds_obj', 'ds_obj']); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +describe('isSourceAuthoredPage', () => { |
| 144 | + it('treats html/react/jsx as source-authored and skips their regions', () => { |
| 145 | + for (const kind of ['html', 'react', 'jsx']) { |
| 146 | + expect(isSourceAuthoredPage({ kind })).toBe(true); |
| 147 | + expect( |
| 148 | + paths({ kind, regions: [{ name: 'main', components: [{ type: 'a' }] }] }), |
| 149 | + ).toEqual([]); |
| 150 | + } |
| 151 | + }); |
| 152 | + |
| 153 | + it('treats full/slotted (and an absent kind) as authored metadata', () => { |
| 154 | + expect(isSourceAuthoredPage({ kind: 'full' })).toBe(false); |
| 155 | + expect(isSourceAuthoredPage({ kind: 'slotted' })).toBe(false); |
| 156 | + expect(isSourceAuthoredPage({})).toBe(false); |
| 157 | + }); |
| 158 | +}); |
0 commit comments