11import getAllPageIds from '@/lib/db/notion/getAllPageIds'
2- import { adapterNotionBlockMap } from '@/lib/utils/notion.util'
2+ import {
3+ adapterNotionBlockMap ,
4+ normalizeNotionBlockType
5+ } from '@/lib/utils/notion.util'
36
47jest . mock ( 'p-limit' , ( ) => ( {
58 __esModule : true ,
69 default : jest . fn ( ( ) => fn => fn ( ) )
710} ) )
811
12+ jest . mock ( 'notion-utils' , ( ) => ( {
13+ getTextContent : jest . fn ( value => {
14+ if ( ! Array . isArray ( value ) ) return ''
15+ return value . map ( item => item ?. [ 0 ] || '' ) . join ( '' )
16+ } )
17+ } ) )
18+
919jest . mock ( '@/lib/cache/cache_manager' , ( ) => ( {
1020 getDataFromCache : jest . fn ( ) ,
1121 getOrSetDataWithCache : jest . fn ( ) ,
@@ -21,6 +31,7 @@ jest.mock('@/lib/db/notion/getNotionAPI', () => ({
2131} ) )
2232
2333const { formatNotionBlock } = require ( '@/lib/db/notion/getPostBlocks' )
34+ const { getPageTableOfContents } = require ( '@/lib/db/notion/getPageTableOfContents' )
2435
2536describe ( 'Notion data format compatibility' , ( ) => {
2637 it ( 'unwraps nested block values returned by newer Notion payloads' , ( ) => {
@@ -121,4 +132,58 @@ describe('Notion data format compatibility', () => {
121132 'https://example.com/image.png'
122133 )
123134 } )
135+
136+ it ( 'downgrades newer heading block types for older renderers' , ( ) => {
137+ expect ( normalizeNotionBlockType ( 'heading_1' ) ) . toBe ( 'header' )
138+ expect ( normalizeNotionBlockType ( 'heading_2' ) ) . toBe ( 'sub_header' )
139+ expect ( normalizeNotionBlockType ( 'heading_3' ) ) . toBe ( 'sub_sub_header' )
140+ expect ( normalizeNotionBlockType ( 'heading_4' ) ) . toBe ( 'sub_sub_header' )
141+ expect ( normalizeNotionBlockType ( 'header_4' ) ) . toBe ( 'sub_sub_header' )
142+ } )
143+
144+ it ( 'formats header_4 blocks into a renderable fallback heading type' , ( ) => {
145+ const formatted = formatNotionBlock ( {
146+ page_1 : {
147+ value : {
148+ id : 'page_1' ,
149+ type : 'header_4' ,
150+ properties : {
151+ title : [ [ 'Section 4' ] ]
152+ }
153+ }
154+ }
155+ } )
156+
157+ expect ( formatted . page_1 . value . type ) . toBe ( 'sub_sub_header' )
158+ } )
159+
160+ it ( 'builds a stable toc for newer heading block types' , ( ) => {
161+ const page = {
162+ id : 'page_root' ,
163+ content : [ 'h1' , 'h4' ]
164+ }
165+ const recordMap = {
166+ block : {
167+ h1 : {
168+ value : {
169+ id : 'h1' ,
170+ type : 'header' ,
171+ properties : { title : [ [ 'Top' ] ] }
172+ }
173+ } ,
174+ h4 : {
175+ value : {
176+ id : 'h4' ,
177+ type : 'header_4' ,
178+ properties : { title : [ [ 'Deep' ] ] }
179+ }
180+ }
181+ }
182+ }
183+
184+ expect ( getPageTableOfContents ( page , recordMap ) ) . toEqual ( [
185+ expect . objectContaining ( { id : 'h1' , indentLevel : 0 } ) ,
186+ expect . objectContaining ( { id : 'h4' , indentLevel : 1 } )
187+ ] )
188+ } )
124189} )
0 commit comments