/** 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() } // Override _start to track when it completes async _start(): Promise { this.startPromise = super._start() await this.startPromise } // Wait for adapter to be started async waitForStart(): Promise { if (this.startPromise) { await this.startPromise } } } describe("Synchronizer - Initialization", () => { let mockAdapter: MockAdapter beforeEach(() => { mockAdapter = new MockAdapter({ adapterType: "test-adapter" }) }) it("should initialize with provided identity", () => { const sync = new Synchronizer({ identity: { peerId: "1", name: "custom-name", type: "user" }, adapters: [], }) expect(sync.identity.name).toBe("custom-name") }) it("should generate identity name if not provided", () => { const sync = new Synchronizer({ identity: { peerId: "1", name: "1", type: "user" }, adapters: [], }) // When no identity is provided, both peerId and name are generated as UUIDs expect(sync.identity.peerId).toBeDefined() expect(sync.identity.name).toBe(sync.identity.peerId) }) it("should initialize adapters", () => { const synchronizer = new Synchronizer({ identity: { peerId: "1", name: "test-synchronizer", type: "user" }, adapters: [mockAdapter as AnyAdapter], permissions: createPermissions(), }) // Adapters are initialized via _initialize() in constructor expect(mockAdapter.channels).toBeDefined() expect(synchronizer).toBeDefined() }) it("should create permissions manager", () => { const restrictiveSync = new Synchronizer({ identity: { peerId: "1", name: "test", type: "user" }, adapters: [], permissions: createPermissions({ visibility: () => false, }), }) expect(restrictiveSync).toBeDefined() }) it("should set up patch callback if provided", () => { const onPatch = vi.fn() const synchronizer = new Synchronizer({ identity: { peerId: "1", name: "test-synchronizer", type: "user" }, adapters: [mockAdapter as AnyAdapter], permissions: createPermissions(), onUpdate: onPatch, }) // The onPatch callback should be set up during initialization expect(onPatch).toBeDefined() expect(synchronizer).toBeDefined() }) })