@@ -23,6 +23,23 @@ const createStorage = () => {
2323}
2424
2525describe ( 'reading bookmarks' , ( ) => {
26+ it ( 'uses root path defaults for missing path values' , ( ) => {
27+ const storage = createStorage ( )
28+ const bookmark = createReadingBookmark ( {
29+ title : ' Root page ' ,
30+ section : ' Intro '
31+ } )
32+
33+ assert . equal ( bookmark . path , '/' )
34+ assert . equal ( bookmark . title , 'Root page' )
35+ assert . equal ( bookmark . section , 'Intro' )
36+ assert . equal ( Number . isFinite ( bookmark . updatedAt ) , true )
37+ assert . equal ( getReadingBookmarkKey ( ) , 'ev-reading-bookmark:/' )
38+
39+ assert . equal ( writeReadingBookmark ( storage , bookmark ) , true )
40+ assert . equal ( readReadingBookmark ( storage , undefined ) . path , '/' )
41+ } )
42+
2643 it ( 'stores bookmarks by full path so each locale is independent' , ( ) => {
2744 const storage = createStorage ( )
2845 const zhBookmark = createReadingBookmark ( {
@@ -119,6 +136,26 @@ describe('reading bookmarks', () => {
119136 updatedAt : 1
120137 }
121138 )
139+
140+ assert . deepEqual (
141+ createReadingBookmark ( {
142+ path : '/easy-vibe/ja-jp/page/' ,
143+ title : null ,
144+ section : null ,
145+ scrollY : Number . NaN ,
146+ progress : Number . NaN ,
147+ now : ( ) => 2
148+ } ) ,
149+ {
150+ version : 1 ,
151+ path : '/easy-vibe/ja-jp/page/' ,
152+ title : '' ,
153+ section : '' ,
154+ scrollY : 0 ,
155+ progress : 0 ,
156+ updatedAt : 2
157+ }
158+ )
122159 } )
123160
124161 it ( 'ignores malformed or mismatched stored values' , ( ) => {
@@ -143,6 +180,33 @@ describe('reading bookmarks', () => {
143180 readReadingBookmark ( storage , '/easy-vibe/ko-kr/page/' , 1000 ) ,
144181 null
145182 )
183+
184+ storage . setItem (
185+ getReadingBookmarkKey ( '/easy-vibe/ko-kr/page/' ) ,
186+ JSON . stringify ( null )
187+ )
188+ assert . equal (
189+ readReadingBookmark ( storage , '/easy-vibe/ko-kr/page/' , 1000 ) ,
190+ null
191+ )
192+
193+ storage . setItem ( getReadingBookmarkKey ( '/easy-vibe/ko-kr/page/' ) , '42' )
194+ assert . equal (
195+ readReadingBookmark ( storage , '/easy-vibe/ko-kr/page/' , 1000 ) ,
196+ null
197+ )
198+
199+ storage . setItem (
200+ getReadingBookmarkKey ( '/easy-vibe/ko-kr/page/' ) ,
201+ JSON . stringify ( {
202+ version : 2 ,
203+ path : '/easy-vibe/ko-kr/page/'
204+ } )
205+ )
206+ assert . equal (
207+ readReadingBookmark ( storage , '/easy-vibe/ko-kr/page/' , 1000 ) ,
208+ null
209+ )
146210 } )
147211
148212 it ( 'clamps restored scroll position to current document height' , ( ) => {
@@ -161,4 +225,62 @@ describe('reading bookmarks', () => {
161225
162226 assert . equal ( readReadingBookmark ( storage , path , 640 ) . scrollY , 640 )
163227 } )
228+
229+ it ( 'normalizes sparse stored bookmark values' , ( ) => {
230+ const storage = createStorage ( )
231+ const path = '/easy-vibe/es-es/page/'
232+
233+ storage . setItem (
234+ getReadingBookmarkKey ( path ) ,
235+ JSON . stringify ( {
236+ version : 1 ,
237+ path,
238+ scrollY : 20 ,
239+ progress : - 50
240+ } )
241+ )
242+
243+ assert . deepEqual ( readReadingBookmark ( storage , path , - 1 ) , {
244+ version : 1 ,
245+ path,
246+ title : '' ,
247+ section : '' ,
248+ scrollY : 0 ,
249+ progress : 0 ,
250+ updatedAt : 0
251+ } )
252+ } )
253+
254+ it ( 'returns null when storage is missing, empty, or throws' , ( ) => {
255+ assert . equal ( readReadingBookmark ( null , '/missing/' ) , null )
256+ assert . equal ( readReadingBookmark ( createStorage ( ) , '/missing/' ) , null )
257+ assert . equal (
258+ readReadingBookmark (
259+ {
260+ getItem ( ) {
261+ throw new Error ( 'storage read failed' )
262+ }
263+ } ,
264+ '/missing/'
265+ ) ,
266+ null
267+ )
268+ } )
269+
270+ it ( 'returns false when bookmark cannot be written' , ( ) => {
271+ assert . equal ( writeReadingBookmark ( null , { path : '/x/' } ) , false )
272+ assert . equal ( writeReadingBookmark ( createStorage ( ) , null ) , false )
273+ assert . equal ( writeReadingBookmark ( createStorage ( ) , { path : '' } ) , false )
274+ assert . equal (
275+ writeReadingBookmark (
276+ {
277+ setItem ( ) {
278+ throw new Error ( 'storage write failed' )
279+ }
280+ } ,
281+ createReadingBookmark ( { path : '/x/' } )
282+ ) ,
283+ false
284+ )
285+ } )
164286} )
0 commit comments