/** 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 { 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 - Adapter Integration", () => { let mockAdapter: MockAdapter beforeEach(() => { mockAdapter = new MockAdapter({ adapterType: "test-adapter" }) }) it("should send messages through adapters", async () => { new Synchronizer({ identity: { peerId: "1", name: "test-synchronizer", type: "user" }, adapters: [mockAdapter as AnyAdapter], }) await mockAdapter.waitForStart() mockAdapter.simulateChannelAdded("test-channel") // Should have sent establish-request message expect(mockAdapter.sentMessages).toHaveLength(1) expect(mockAdapter.sentMessages[0].message.type).toBe( "channel/establish-request", ) }) it("should handle multiple adapters", async () => { const adapter1 = new MockAdapter({ adapterType: "adapter-1" }) const adapter2 = new MockAdapter({ adapterType: "adapter-2" }) const multiSync = new Synchronizer({ identity: { peerId: "2", name: "test", type: "user" }, adapters: [adapter1 as AnyAdapter, adapter2 as AnyAdapter], }) await adapter1.waitForStart() await adapter2.waitForStart() const channel1 = adapter1.simulateChannelAdded("channel-1") const channel2 = adapter2.simulateChannelAdded("channel-2") expect(multiSync.model.channels.get(channel1.channelId)).toBeDefined() expect(multiSync.model.channels.get(channel2.channelId)).toBeDefined() }) })