|
| 1 | +import { describe, it, expect, mock, afterEach } from 'bun:test'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { randomBytes } from 'node:crypto'; |
| 5 | +import { readFile, unlink } from 'node:fs/promises'; |
| 6 | +import { SessionManager } from '../session-manager.js'; |
| 7 | + |
| 8 | +/** |
| 9 | + * `openRoom` orchestration (provider sync, sync timeout, awareness presence) and |
| 10 | + * the `save()` room-guard run against a live Yjs WebSocket server in production, |
| 11 | + * but the orchestration itself is socket-independent. Following the repo's collab |
| 12 | + * test idiom (createProviderStub in Editor.replace-file.test.ts), we inject a stub |
| 13 | + * provider so the logic is exercised without a server. The real socket transport |
| 14 | + * is the only part left to the end-to-end check in the PR description. |
| 15 | + */ |
| 16 | + |
| 17 | +type SyncHandler = (synced?: boolean) => void; |
| 18 | + |
| 19 | +function providerStub({ autoSync = true }: { autoSync?: boolean } = {}) { |
| 20 | + const syncHandlers = new Set<SyncHandler>(); |
| 21 | + const setLocalStateField = mock((_field: string, _value: unknown) => {}); |
| 22 | + const destroy = mock(() => {}); |
| 23 | + |
| 24 | + const provider = { |
| 25 | + awareness: { |
| 26 | + setLocalStateField, |
| 27 | + getStates: () => new Map(), |
| 28 | + on() {}, |
| 29 | + off() {}, |
| 30 | + }, |
| 31 | + on(event: string, handler: SyncHandler) { |
| 32 | + if (event !== 'sync') return; |
| 33 | + syncHandlers.add(handler); |
| 34 | + // Emit the truthy sync edge on the next microtask, after openRoom has |
| 35 | + // registered its handler and suspended on the await. |
| 36 | + if (autoSync) queueMicrotask(() => handler(true)); |
| 37 | + }, |
| 38 | + off(event: string, handler: SyncHandler) { |
| 39 | + if (event === 'sync') syncHandlers.delete(handler); |
| 40 | + }, |
| 41 | + destroy, |
| 42 | + }; |
| 43 | + |
| 44 | + return provider; |
| 45 | +} |
| 46 | + |
| 47 | +describe('superdoc_attach openRoom orchestration (stubbed provider)', () => { |
| 48 | + const sm = new SessionManager(); |
| 49 | + const opened: string[] = []; |
| 50 | + const tempFiles: string[] = []; |
| 51 | + |
| 52 | + afterEach(async () => { |
| 53 | + for (const id of opened.splice(0)) await sm.close(id).catch(() => {}); |
| 54 | + for (const f of tempFiles.splice(0)) await unlink(f).catch(() => {}); |
| 55 | + }); |
| 56 | + |
| 57 | + it('returns a registered room session once the provider syncs', async () => { |
| 58 | + const stub = providerStub(); |
| 59 | + const session = await sm.openRoom('ws://test/doc', 'room-sync', undefined, undefined, { |
| 60 | + createProvider: () => stub as unknown as never, |
| 61 | + }); |
| 62 | + opened.push(session.id); |
| 63 | + |
| 64 | + expect(session.id).toMatch(/^room-/); |
| 65 | + expect(session.filePath).toBeNull(); |
| 66 | + expect(sm.list().some((s) => s.id === session.id)).toBe(true); |
| 67 | + }); |
| 68 | + |
| 69 | + it('broadcasts awareness presence when a user is supplied', async () => { |
| 70 | + const stub = providerStub(); |
| 71 | + const user = { id: 'reviewer-1', name: 'Reviewer', email: 'reviewer@example.com' }; |
| 72 | + const session = await sm.openRoom('ws://test/doc', 'room-presence', undefined, user, { |
| 73 | + createProvider: () => stub as unknown as never, |
| 74 | + }); |
| 75 | + opened.push(session.id); |
| 76 | + |
| 77 | + expect(stub.awareness.setLocalStateField).toHaveBeenCalledWith('user', user); |
| 78 | + }); |
| 79 | + |
| 80 | + it('does not touch awareness when no user is supplied', async () => { |
| 81 | + const stub = providerStub(); |
| 82 | + const session = await sm.openRoom('ws://test/doc', 'room-nopresence', undefined, undefined, { |
| 83 | + createProvider: () => stub as unknown as never, |
| 84 | + }); |
| 85 | + opened.push(session.id); |
| 86 | + |
| 87 | + expect(stub.awareness.setLocalStateField).not.toHaveBeenCalled(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('rejects and tears down the provider when initial sync times out', async () => { |
| 91 | + const stub = providerStub({ autoSync: false }); |
| 92 | + await expect( |
| 93 | + sm.openRoom('ws://test/doc', 'room-timeout', undefined, undefined, { |
| 94 | + createProvider: () => stub as unknown as never, |
| 95 | + syncTimeoutMs: 10, |
| 96 | + }), |
| 97 | + ).rejects.toThrow(/sync timeout/); |
| 98 | + |
| 99 | + expect(stub.destroy).toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('refuses to save a room session without an explicit output path', async () => { |
| 103 | + const stub = providerStub(); |
| 104 | + const session = await sm.openRoom('ws://test/doc', 'room-save-guard', undefined, undefined, { |
| 105 | + createProvider: () => stub as unknown as never, |
| 106 | + }); |
| 107 | + opened.push(session.id); |
| 108 | + |
| 109 | + await expect(sm.save(session.id)).rejects.toThrow(/without specifying an output path/); |
| 110 | + }); |
| 111 | + |
| 112 | + it('saves a room session to an explicit output path', async () => { |
| 113 | + const stub = providerStub(); |
| 114 | + const session = await sm.openRoom('ws://test/doc', 'room-save-ok', undefined, undefined, { |
| 115 | + createProvider: () => stub as unknown as never, |
| 116 | + }); |
| 117 | + opened.push(session.id); |
| 118 | + |
| 119 | + const out = join(tmpdir(), `mcp-collab-${randomBytes(6).toString('hex')}.docx`); |
| 120 | + tempFiles.push(out); |
| 121 | + |
| 122 | + const result = await sm.save(session.id, out); |
| 123 | + expect(result.path).toBe(out); |
| 124 | + expect(result.byteLength).toBeGreaterThan(0); |
| 125 | + |
| 126 | + const bytes = await readFile(out); |
| 127 | + expect(bytes[0]).toBe(0x50); // 'P' — PK zip magic |
| 128 | + expect(bytes[1]).toBe(0x4b); // 'K' |
| 129 | + }); |
| 130 | +}); |
0 commit comments