/** biome-ignore-all lint/suspicious/noExplicitAny: tests */ import { LoroDoc } from "loro-crdt" import { beforeEach, describe, expect, it, vi } from "vitest" import { Adapter, type AnyAdapter } from "../adapter/adapter.js" import type { Channel, 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 } } // Test helpers public simulateChannelAdded(name: string): ConnectedChannel { const channel = this.addChannel({ name }) this.testChannels.set(channel.channelId, channel) // Establish the channel to trigger the establishment handshake this.establishChannel(channel.channelId) return channel } public simulateChannelRemoved(channelId: ChannelId): Channel | undefined { const channel = this.removeChannel(channelId) if (channel) { this.testChannels.delete(channelId) } return channel } public simulateChannelMessage(channelId: ChannelId, message: ChannelMsg) { const channel = this.testChannels.get(channelId) if (channel?.onReceive) { channel.onReceive(message) } } public getTestChannels() { return this.testChannels } } // Helper to create a version vector function createVersionVector() { const doc = new LoroDoc() return doc.version() } describe("Synchronizer - Channel Queries", () => { 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 get document IDs for channel", async () => { await mockAdapter.waitForStart() const docId1 = "doc-1" const docId2 = "doc-2" const channel = mockAdapter.simulateChannelAdded("test-channel") synchronizer.getOrCreateDocumentState(docId1) synchronizer.getOrCreateDocumentState(docId2) // Establish the channel first mockAdapter.simulateChannelMessage(channel.channelId, { type: "channel/establish-request", identity: { peerId: "1", name: "test-peer", type: "user" }, }) // Simulate sync requests to establish subscriptions (now sent as batch) mockAdapter.simulateChannelMessage(channel.channelId, { type: "channel/batch", messages: [ { type: "channel/sync-request", docId: docId1, requesterDocVersion: createVersionVector(), bidirectional: false, }, { type: "channel/sync-request", docId: docId2, requesterDocVersion: createVersionVector(), bidirectional: false, }, ], }) const docIds = synchronizer.getChannelDocIds(channel.channelId) expect(docIds).toContain(docId1) expect(docIds).toContain(docId2) expect(docIds).toHaveLength(2) }) })