@@ -2,23 +2,27 @@ import { expect } from "chai";
22
33import { LocalHistory } from "./local_history.js" ;
44import { ContentMessage } from "./message.js" ;
5- import { IStorage , PersistentStorage } from "./persistent_storage.js" ;
65
76const channelId = "channel-1" ;
87
9- describe ( "PersistentStorage" , ( ) => {
10- describe ( "Explicit storage" , ( ) => {
11- it ( "persists and restores messages" , ( ) => {
12- const storage = new MemoryStorage ( ) ;
13- const persistentStorage = PersistentStorage . create ( channelId , storage ) ;
8+ describe ( "Storage" , ( ) => {
9+ describe ( "Browser localStorage" , ( ) => {
10+ before ( function ( ) {
11+ if ( typeof localStorage === "undefined" ) {
12+ this . skip ( ) ;
13+ }
14+ } ) ;
1415
15- expect ( persistentStorage ) . to . not . be . undefined ;
16+ afterEach ( ( ) => {
17+ localStorage . removeItem ( `waku:sds:storage:${ channelId } ` ) ;
18+ } ) ;
1619
17- const history1 = new LocalHistory ( { storage : persistentStorage } ) ;
20+ it ( "persists and restores messages" , ( ) => {
21+ const history1 = new LocalHistory ( { storagePrefix : channelId } ) ;
1822 history1 . push ( createMessage ( "msg-1" , 1 ) ) ;
1923 history1 . push ( createMessage ( "msg-2" , 2 ) ) ;
2024
21- const history2 = new LocalHistory ( { storage : persistentStorage } ) ;
25+ const history2 = new LocalHistory ( { storagePrefix : channelId } ) ;
2226
2327 expect ( history2 . length ) . to . equal ( 2 ) ;
2428 expect ( history2 . slice ( 0 ) . map ( ( msg ) => msg . messageId ) ) . to . deep . equal ( [
@@ -27,39 +31,18 @@ describe("PersistentStorage", () => {
2731 ] ) ;
2832 } ) ;
2933
30- it ( "uses in-memory only when no storage is provided" , ( ) => {
31- const history = new LocalHistory ( { maxSize : 100 } ) ;
32- history . push ( createMessage ( "msg-3" , 3 ) ) ;
33-
34- expect ( history . length ) . to . equal ( 1 ) ;
35- expect ( history . slice ( 0 ) [ 0 ] . messageId ) . to . equal ( "msg-3" ) ;
36-
37- const history2 = new LocalHistory ( { maxSize : 100 } ) ;
38- expect ( history2 . length ) . to . equal ( 0 ) ;
39- } ) ;
40-
41- it ( "handles corrupt data in storage gracefully" , ( ) => {
42- const storage = new MemoryStorage ( ) ;
43- // Corrupt data
44- storage . setItem ( "waku:sds:messages:channel-1" , "{ invalid json }" ) ;
45-
46- const persistentStorage = PersistentStorage . create ( channelId , storage ) ;
47- const history = new LocalHistory ( { storage : persistentStorage } ) ;
34+ it ( "handles corrupt data gracefully" , ( ) => {
35+ localStorage . setItem ( `waku:sds:storage:${ channelId } ` , "{ invalid json }" ) ;
4836
37+ const history = new LocalHistory ( { storagePrefix : channelId } ) ;
4938 expect ( history . length ) . to . equal ( 0 ) ;
50-
51- // Corrupt data is not saved
52- expect ( storage . getItem ( "waku:sds:messages:channel-1" ) ) . to . equal ( null ) ;
39+ // Corrupt data is removed
40+ expect ( localStorage . getItem ( `waku:sds:storage:${ channelId } ` ) ) . to . be . null ;
5341 } ) ;
5442
5543 it ( "isolates history by channel ID" , ( ) => {
56- const storage = new MemoryStorage ( ) ;
57-
58- const storage1 = PersistentStorage . create ( "channel-1" , storage ) ;
59- const storage2 = PersistentStorage . create ( "channel-2" , storage ) ;
60-
61- const history1 = new LocalHistory ( { storage : storage1 } ) ;
62- const history2 = new LocalHistory ( { storage : storage2 } ) ;
44+ const history1 = new LocalHistory ( { storagePrefix : "channel-1" } ) ;
45+ const history2 = new LocalHistory ( { storagePrefix : "channel-2" } ) ;
6346
6447 history1 . push ( createMessage ( "msg-1" , 1 ) ) ;
6548 history2 . push ( createMessage ( "msg-2" , 2 ) ) ;
@@ -70,37 +53,34 @@ describe("PersistentStorage", () => {
7053 expect ( history2 . length ) . to . equal ( 1 ) ;
7154 expect ( history2 . slice ( 0 ) [ 0 ] . messageId ) . to . equal ( "msg-2" ) ;
7255
73- expect ( storage . getItem ( "waku:sds:messages:channel-1" ) ) . to . not . be . null ;
74- expect ( storage . getItem ( "waku:sds:messages:channel-2" ) ) . to . not . be . null ;
56+ localStorage . removeItem ( "waku:sds:storage:channel-2" ) ;
7557 } ) ;
7658
7759 it ( "saves messages after each push" , ( ) => {
78- const storage = new MemoryStorage ( ) ;
79- const persistentStorage = PersistentStorage . create ( channelId , storage ) ;
80- const history = new LocalHistory ( { storage : persistentStorage } ) ;
60+ const history = new LocalHistory ( { storagePrefix : channelId } ) ;
8161
82- expect ( storage . getItem ( " waku:sds:messages:channel-1" ) ) . to . be . null ;
62+ expect ( localStorage . getItem ( ` waku:sds:storage: ${ channelId } ` ) ) . to . be . null ;
8363
8464 history . push ( createMessage ( "msg-1" , 1 ) ) ;
8565
86- expect ( storage . getItem ( "waku:sds:messages:channel-1" ) ) . to . not . be . null ;
66+ expect ( localStorage . getItem ( `waku:sds:storage:${ channelId } ` ) ) . to . not . be
67+ . null ;
8768
88- const saved = JSON . parse ( storage . getItem ( "waku:sds:messages:channel-1" ) ! ) ;
69+ const saved = JSON . parse (
70+ localStorage . getItem ( `waku:sds:storage:${ channelId } ` ) !
71+ ) ;
8972 expect ( saved ) . to . have . lengthOf ( 1 ) ;
9073 expect ( saved [ 0 ] . messageId ) . to . equal ( "msg-1" ) ;
9174 } ) ;
9275
9376 it ( "loads messages on initialization" , ( ) => {
94- const storage = new MemoryStorage ( ) ;
95- const persistentStorage1 = PersistentStorage . create ( channelId , storage ) ;
96- const history1 = new LocalHistory ( { storage : persistentStorage1 } ) ;
77+ const history1 = new LocalHistory ( { storagePrefix : channelId } ) ;
9778
9879 history1 . push ( createMessage ( "msg-1" , 1 ) ) ;
9980 history1 . push ( createMessage ( "msg-2" , 2 ) ) ;
10081 history1 . push ( createMessage ( "msg-3" , 3 ) ) ;
10182
102- const persistentStorage2 = PersistentStorage . create ( channelId , storage ) ;
103- const history2 = new LocalHistory ( { storage : persistentStorage2 } ) ;
83+ const history2 = new LocalHistory ( { storagePrefix : channelId } ) ;
10484
10585 expect ( history2 . length ) . to . equal ( 3 ) ;
10686 expect ( history2 . slice ( 0 ) . map ( ( m ) => m . messageId ) ) . to . deep . equal ( [
@@ -111,59 +91,16 @@ describe("PersistentStorage", () => {
11191 } ) ;
11292 } ) ;
11393
114- describe ( "Node.js only (no localStorage)" , ( ) => {
115- before ( function ( ) {
116- if ( typeof localStorage !== "undefined" ) {
117- this . skip ( ) ;
118- }
119- } ) ;
120-
121- it ( "returns undefined when no storage is available" , ( ) => {
122- const persistentStorage = PersistentStorage . create ( channelId , undefined ) ;
123-
124- expect ( persistentStorage ) . to . equal ( undefined ) ;
125- } ) ;
126- } ) ;
127-
128- describe ( "Browser only (localStorage)" , ( ) => {
129- before ( function ( ) {
130- if ( typeof localStorage === "undefined" ) {
131- this . skip ( ) ;
132- }
133- } ) ;
134-
135- it ( "persists and restores messages with channelId" , ( ) => {
136- const testChannelId = `test-${ Date . now ( ) } ` ;
137- const history1 = new LocalHistory ( { storage : testChannelId } ) ;
138- history1 . push ( createMessage ( "msg-1" , 1 ) ) ;
139- history1 . push ( createMessage ( "msg-2" , 2 ) ) ;
140-
141- const history2 = new LocalHistory ( { storage : testChannelId } ) ;
142-
143- expect ( history2 . length ) . to . equal ( 2 ) ;
144- expect ( history2 . slice ( 0 ) . map ( ( msg ) => msg . messageId ) ) . to . deep . equal ( [
145- "msg-1" ,
146- "msg-2"
147- ] ) ;
148-
149- localStorage . removeItem ( `waku:sds:messages:${ testChannelId } ` ) ;
150- } ) ;
151-
152- it ( "auto-uses localStorage when channelId is provided" , ( ) => {
153- const testChannelId = `auto-storage-${ Date . now ( ) } ` ;
154-
155- const history = new LocalHistory ( { storage : testChannelId } ) ;
156- history . push ( createMessage ( "msg-auto-1" , 1 ) ) ;
157- history . push ( createMessage ( "msg-auto-2" , 2 ) ) ;
94+ describe ( "In-memory fallback" , ( ) => {
95+ it ( "uses in-memory only when no storage is provided" , ( ) => {
96+ const history = new LocalHistory ( { maxSize : 100 } ) ;
97+ history . push ( createMessage ( "msg-3" , 3 ) ) ;
15898
159- const history2 = new LocalHistory ( { storage : testChannelId } ) ;
160- expect ( history2 . length ) . to . equal ( 2 ) ;
161- expect ( history2 . slice ( 0 ) . map ( ( m ) => m . messageId ) ) . to . deep . equal ( [
162- "msg-auto-1" ,
163- "msg-auto-2"
164- ] ) ;
99+ expect ( history . length ) . to . equal ( 1 ) ;
100+ expect ( history . slice ( 0 ) [ 0 ] . messageId ) . to . equal ( "msg-3" ) ;
165101
166- localStorage . removeItem ( `waku:sds:messages:${ testChannelId } ` ) ;
102+ const history2 = new LocalHistory ( { maxSize : 100 } ) ;
103+ expect ( history2 . length ) . to . equal ( 0 ) ;
167104 } ) ;
168105 } ) ;
169106} ) ;
@@ -180,19 +117,3 @@ const createMessage = (id: string, timestamp: number): ContentMessage => {
180117 undefined
181118 ) ;
182119} ;
183-
184- class MemoryStorage implements IStorage {
185- private readonly store = new Map < string , string > ( ) ;
186-
187- public getItem ( key : string ) : string | null {
188- return this . store . get ( key ) ?? null ;
189- }
190-
191- public setItem ( key : string , value : string ) : void {
192- this . store . set ( key , value ) ;
193- }
194-
195- public removeItem ( key : string ) : void {
196- this . store . delete ( key ) ;
197- }
198- }
0 commit comments