test: local storage specific test

This commit is contained in:
Danish Arora 2025-11-26 20:00:06 -05:00
parent d6412e5536
commit ad4973aa20
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E
2 changed files with 18 additions and 1 deletions

View File

@ -1260,7 +1260,14 @@ describe("MessageChannel", function () {
});
});
describe("Default localStorage persistence", () => {
describe("localStorage persistence", function () {
// LocalStorage specific tests (browser)
before(function () {
if (typeof localStorage === "undefined") {
this.skip();
}
});
it("should restore messages from localStorage on channel recreation", async () => {
const persistentChannelId = "persistent-channel";

View File

@ -1,8 +1,11 @@
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
import { Logger } from "@waku/utils";
import { ILocalHistory, MemLocalHistory } from "./mem_local_history.js";
import { ChannelId, ContentMessage, HistoryEntry } from "./message.js";
const log = new Logger("sds:persistent-history");
export interface HistoryStorage {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
@ -48,6 +51,13 @@ export class PersistentHistory implements ILocalHistory {
this.memory = new MemLocalHistory();
this.storage = options.storage ?? DEFAULT_HISTORY_STORAGE;
this.storageKey = `${HISTORY_STORAGE_PREFIX}${options.channelId}`;
if (!this.storage) {
log.warn(
"Storage backend unavailable (localStorage not found). Falling back to in-memory history. Messages will not persist across sessions."
);
}
this.load();
}