/** biome-ignore-all lint/suspicious/noExplicitAny: tests */ import { beforeEach, describe, expect, it, vi } from "vitest" import { Adapter, type AnyAdapter } from "../adapter/adapter.js" import type { ChannelMsg, ConnectedChannel, GeneratedChannel, } from "../channel.js" import { createPermissions } from "../permissions.js" import { Synchronizer } from "../synchronizer.js" import type { ChannelId } from "../types.js" // Mock adapter for testing class MockAdapter extends Adapter<{ name: string }> { public sentMessages: any[] = [] private testChannels: Map = new Map() private startPromise: Promise | null = null protected generate(context: { name: string }): GeneratedChannel { return { kind: "network", adapterType: this.adapterType, send: vi.fn((message: ChannelMsg) => { this.sentMessages.push({ channelId: context.name, message }) }), stop: vi.fn(), } } async onStart(): Promise { // Nothing to do for mock adapter } async onStop(): Promise { this.testChannels.clear() } async _start(): Promise { this.startPromise = super._start() await this.startPromise } async waitForStart(): Promise { if (this.startPromise) { await this.startPromise } } public simulateChannelAdded(name: string): ConnectedChannel { const channel = this.addChannel({ name }) this.testChannels.set(channel.channelId, channel) this.establishChannel(channel.channelId) return channel } } describe("Synchronizer - Reset Functionality", () => { let synchronizer: Synchronizer let mockAdapter: MockAdapter beforeEach(() => { mockAdapter = new MockAdapter({ adapterType: "test-adapter" }) synchronizer = new Synchronizer({ identity: { peerId: "1", name: "test-synchronizer", type: "user" }, adapters: [mockAdapter as AnyAdapter], permissions: createPermissions(), }) }) it("should reset synchronizer state", async () => { await mockAdapter.waitForStart() const docId = "test-doc" const channel = mockAdapter.simulateChannelAdded("test-channel") synchronizer.getOrCreateDocumentState(docId) // Verify initial state expect(synchronizer.getDocumentState(docId)).toBeDefined() expect(synchronizer.model.channels.get(channel.channelId)).toBeDefined() // Reset (now async) await synchronizer.reset() // State should be reset expect(synchronizer.getDocumentState(docId)).toBeUndefined() expect(synchronizer.model.channels.get(channel.channelId)).toBeUndefined() expect(mockAdapter.channels.size).toBe(0) }) })