Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions test/integration/ContactService-e2e-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import "fake-indexeddb/auto";

import { ContactService } from "../../src/libp2p/services/ContactService";
import { IndexedDBStore } from "../../src/libp2p/storage/IndexedDBStore";

if (typeof global.structuredClone === "undefined") {
(global as any).structuredClone = (obj: any) => JSON.parse(JSON.stringify(obj));
}

describe("ContactService End-to-End", () => {
const dbName = "contact-service-e2e";

beforeEach(() => {
indexedDB.deleteDatabase(dbName);
});

it("should persist contacts across app restarts", async () => {
const store1 = new IndexedDBStore(dbName);
await store1.open();
const service1 = new ContactService(store1);
await service1.initialize();

await service1.addContact({ peerId: "peer", nickname: "Alice" });
store1.close();

const store2 = new IndexedDBStore(dbName);
await store2.open();
const service2 = new ContactService(store2);
await service2.initialize();

expect(service2.getContact("peer")?.nickname).toBe("Alice");
store2.close();
});
});
31 changes: 31 additions & 0 deletions test/unit-tests/ContactService-persistence-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ContactService } from "../../src/libp2p/services/ContactService";
import type { Contact } from "../../src/libp2p/types";

class MockIndexedDBStore {
public putContact = jest.fn();
public getAllContacts = jest.fn<Promise<Contact[]>, []>(() => Promise.resolve([]));
public deleteContact = jest.fn();
}

describe("ContactService Persistence", () => {
it("should persist contacts to IndexedDB on add", async () => {
const store = new MockIndexedDBStore();
const service = new ContactService(store as any);

await service.initialize();
await service.addContact({ peerId: "test", nickname: "Test" });

expect(store.putContact).toHaveBeenCalled();
});

it("should restore contacts from IndexedDB on initialize", async () => {
const stored: Contact = { peerId: "peer", nickname: "Alice", created: 1, lastSeen: 1 };
const store = new MockIndexedDBStore();
store.getAllContacts = jest.fn().mockResolvedValue([stored]);

const service = new ContactService(store as any);
await service.initialize();

expect(service.getContact("peer")).toEqual(stored);
});
});
Loading