|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { createSuperDocUI } from './create-super-doc-ui.js'; |
| 4 | +import type { SuperDocLike } from './types.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Stub mirroring the controller test pattern. Adds `setDocumentMode` |
| 8 | + * and `export` so ui.document can route through them; both are vi.fn |
| 9 | + * so call counts and arguments are observable. |
| 10 | + */ |
| 11 | +function makeStubs(initialMode: 'editing' | 'suggesting' | 'viewing' = 'editing') { |
| 12 | + const editorListeners = new Map<string, Set<(...args: unknown[]) => void>>(); |
| 13 | + const superdocListeners = new Map<string, Set<(...args: unknown[]) => void>>(); |
| 14 | + |
| 15 | + const editor = { |
| 16 | + on: vi.fn((event: string, handler: (...args: unknown[]) => void) => { |
| 17 | + if (!editorListeners.has(event)) editorListeners.set(event, new Set()); |
| 18 | + editorListeners.get(event)!.add(handler); |
| 19 | + }), |
| 20 | + off: vi.fn((event: string, handler: (...args: unknown[]) => void) => { |
| 21 | + editorListeners.get(event)?.delete(handler); |
| 22 | + }), |
| 23 | + state: { selection: { empty: true, from: 0, to: 0 } }, |
| 24 | + options: { documentId: 'doc-1', isHeaderOrFooter: false }, |
| 25 | + commands: {}, |
| 26 | + isEditable: true, |
| 27 | + doc: { |
| 28 | + selection: { |
| 29 | + current: vi.fn(() => ({ empty: true, text: '', target: null })), |
| 30 | + }, |
| 31 | + }, |
| 32 | + }; |
| 33 | + |
| 34 | + const superdoc: SuperDocLike & { |
| 35 | + fireEditor(event: string, ...args: unknown[]): void; |
| 36 | + fireSuperdoc(event: string, ...args: unknown[]): void; |
| 37 | + setDocumentMode: ReturnType<typeof vi.fn>; |
| 38 | + export: ReturnType<typeof vi.fn>; |
| 39 | + } = { |
| 40 | + activeEditor: editor as never, |
| 41 | + config: { documentMode: initialMode }, |
| 42 | + on: vi.fn((event: string, handler: (...args: unknown[]) => void) => { |
| 43 | + if (!superdocListeners.has(event)) superdocListeners.set(event, new Set()); |
| 44 | + superdocListeners.get(event)!.add(handler); |
| 45 | + }), |
| 46 | + off: vi.fn((event: string, handler: (...args: unknown[]) => void) => { |
| 47 | + superdocListeners.get(event)?.delete(handler); |
| 48 | + }), |
| 49 | + setDocumentMode: vi.fn((_mode: 'editing' | 'suggesting' | 'viewing') => { |
| 50 | + // Plain spy: don't mirror host behavior (mutate + emit) here. |
| 51 | + // Tests that need the controller to observe a mode change call |
| 52 | + // `fireSuperdoc('document-mode-change', { documentMode })` |
| 53 | + // explicitly, which keeps the spy's call list tight and avoids |
| 54 | + // accidentally re-entering the controller graph. |
| 55 | + }), |
| 56 | + export: vi.fn(async (_options?: unknown) => ({ ok: true })), |
| 57 | + fireEditor(event, ...args) { |
| 58 | + const handlers = editorListeners.get(event); |
| 59 | + if (!handlers) return; |
| 60 | + [...handlers].forEach((handler) => handler(...args)); |
| 61 | + }, |
| 62 | + fireSuperdoc(event, ...args) { |
| 63 | + const handlers = superdocListeners.get(event); |
| 64 | + if (!handlers) return; |
| 65 | + [...handlers].forEach((handler) => handler(...args)); |
| 66 | + }, |
| 67 | + }; |
| 68 | + |
| 69 | + return { superdoc, editor }; |
| 70 | +} |
| 71 | + |
| 72 | +let warnSpy: ReturnType<typeof vi.spyOn>; |
| 73 | +let errorSpy: ReturnType<typeof vi.spyOn>; |
| 74 | + |
| 75 | +beforeEach(() => { |
| 76 | + warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 77 | + errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 78 | +}); |
| 79 | + |
| 80 | +afterEach(() => { |
| 81 | + warnSpy.mockRestore(); |
| 82 | + errorSpy.mockRestore(); |
| 83 | +}); |
| 84 | + |
| 85 | +describe('ui.document', () => { |
| 86 | + it('exposes the slice via getSnapshot with ready + mode mirrored from state', () => { |
| 87 | + const { superdoc } = makeStubs('suggesting'); |
| 88 | + const ui = createSuperDocUI({ superdoc }); |
| 89 | + |
| 90 | + expect(ui.document.getSnapshot()).toEqual({ ready: true, mode: 'suggesting' }); |
| 91 | + |
| 92 | + ui.destroy(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('subscribe fires once synchronously with the initial snapshot', () => { |
| 96 | + const { superdoc } = makeStubs('editing'); |
| 97 | + const ui = createSuperDocUI({ superdoc }); |
| 98 | + |
| 99 | + const listener = vi.fn(); |
| 100 | + ui.document.subscribe(listener); |
| 101 | + |
| 102 | + expect(listener).toHaveBeenCalledTimes(1); |
| 103 | + expect(listener.mock.calls[0][0]).toEqual({ snapshot: { ready: true, mode: 'editing' } }); |
| 104 | + |
| 105 | + ui.destroy(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('subscribe re-fires when document-mode-change fires from the host', async () => { |
| 109 | + const { superdoc } = makeStubs('editing'); |
| 110 | + const ui = createSuperDocUI({ superdoc }); |
| 111 | + |
| 112 | + const listener = vi.fn(); |
| 113 | + ui.document.subscribe(listener); |
| 114 | + expect(listener).toHaveBeenCalledTimes(1); |
| 115 | + |
| 116 | + // Mirror SuperDoc's emit pattern: mutate config, then fire the |
| 117 | + // event the controller is bound to so the next snapshot reflects |
| 118 | + // the new mode. |
| 119 | + superdoc.config!.documentMode = 'viewing'; |
| 120 | + superdoc.fireSuperdoc('document-mode-change', { documentMode: 'viewing' }); |
| 121 | + await Promise.resolve(); |
| 122 | + await Promise.resolve(); |
| 123 | + |
| 124 | + expect(listener).toHaveBeenCalledTimes(2); |
| 125 | + expect(listener.mock.calls[1][0].snapshot).toEqual({ ready: true, mode: 'viewing' }); |
| 126 | + |
| 127 | + ui.destroy(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('subscribe does not re-fire on transactions that leave the slice unchanged', async () => { |
| 131 | + const { superdoc, editor } = makeStubs('editing'); |
| 132 | + const ui = createSuperDocUI({ superdoc }); |
| 133 | + |
| 134 | + const listener = vi.fn(); |
| 135 | + ui.document.subscribe(listener); |
| 136 | + expect(listener).toHaveBeenCalledTimes(1); |
| 137 | + |
| 138 | + // Fire a typing-only transaction. ready/mode are both unchanged |
| 139 | + // so shallowEqual should short-circuit the subscriber. Without |
| 140 | + // the documentMemo, every transaction allocates a fresh slice |
| 141 | + // and re-fires listeners. |
| 142 | + const handlers = (editor.on as ReturnType<typeof vi.fn>).mock.calls |
| 143 | + .filter((c) => c[0] === 'transaction') |
| 144 | + .map((c) => c[1]) as Array<() => void>; |
| 145 | + handlers.forEach((h) => h()); |
| 146 | + await Promise.resolve(); |
| 147 | + await Promise.resolve(); |
| 148 | + |
| 149 | + expect(listener).toHaveBeenCalledTimes(1); |
| 150 | + |
| 151 | + ui.destroy(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('setMode forwards to superdoc.setDocumentMode with the passed mode', () => { |
| 155 | + const { superdoc } = makeStubs('editing'); |
| 156 | + const ui = createSuperDocUI({ superdoc }); |
| 157 | + |
| 158 | + ui.document.setMode('viewing'); |
| 159 | + expect(superdoc.setDocumentMode).toHaveBeenCalledTimes(1); |
| 160 | + expect(superdoc.setDocumentMode).toHaveBeenCalledWith('viewing'); |
| 161 | + |
| 162 | + ui.destroy(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('setMode is a no-op when the host omits the setter', () => { |
| 166 | + const { superdoc } = makeStubs('editing'); |
| 167 | + delete (superdoc as { setDocumentMode?: unknown }).setDocumentMode; |
| 168 | + const ui = createSuperDocUI({ superdoc }); |
| 169 | + |
| 170 | + // Should not throw. |
| 171 | + expect(() => ui.document.setMode('viewing')).not.toThrow(); |
| 172 | + |
| 173 | + ui.destroy(); |
| 174 | + }); |
| 175 | + |
| 176 | + it('setMode swallows host errors and reports to console.error', () => { |
| 177 | + const { superdoc } = makeStubs('editing'); |
| 178 | + superdoc.setDocumentMode = vi.fn(() => { |
| 179 | + throw new Error('host explosion'); |
| 180 | + }) as ReturnType<typeof vi.fn>; |
| 181 | + const ui = createSuperDocUI({ superdoc }); |
| 182 | + |
| 183 | + expect(() => ui.document.setMode('viewing')).not.toThrow(); |
| 184 | + expect(errorSpy).toHaveBeenCalled(); |
| 185 | + expect(String(errorSpy.mock.calls[0][0])).toContain('ui.document.setMode failed'); |
| 186 | + |
| 187 | + ui.destroy(); |
| 188 | + }); |
| 189 | + |
| 190 | + it('export forwards options to superdoc.export and returns its result', async () => { |
| 191 | + const { superdoc } = makeStubs('editing'); |
| 192 | + const ui = createSuperDocUI({ superdoc }); |
| 193 | + |
| 194 | + const result = await ui.document.export({ exportType: ['docx'], triggerDownload: false }); |
| 195 | + expect(result).toEqual({ ok: true }); |
| 196 | + expect(superdoc.export).toHaveBeenCalledTimes(1); |
| 197 | + expect(superdoc.export).toHaveBeenCalledWith({ exportType: ['docx'], triggerDownload: false }); |
| 198 | + |
| 199 | + ui.destroy(); |
| 200 | + }); |
| 201 | + |
| 202 | + it('export throws a clear error when the host omits export()', async () => { |
| 203 | + const { superdoc } = makeStubs('editing'); |
| 204 | + delete (superdoc as { export?: unknown }).export; |
| 205 | + const ui = createSuperDocUI({ superdoc }); |
| 206 | + |
| 207 | + await expect(ui.document.export()).rejects.toThrow(/host SuperDoc instance does not implement export/); |
| 208 | + |
| 209 | + ui.destroy(); |
| 210 | + }); |
| 211 | + |
| 212 | + it('export propagates host rejections to the caller', async () => { |
| 213 | + const { superdoc } = makeStubs('editing'); |
| 214 | + superdoc.export = vi.fn(async () => { |
| 215 | + throw new Error('export blew up'); |
| 216 | + }) as ReturnType<typeof vi.fn>; |
| 217 | + const ui = createSuperDocUI({ superdoc }); |
| 218 | + |
| 219 | + await expect(ui.document.export()).rejects.toThrow('export blew up'); |
| 220 | + |
| 221 | + ui.destroy(); |
| 222 | + }); |
| 223 | + |
| 224 | + it('document slice on getSnapshot returns the same reference across reads when unchanged', () => { |
| 225 | + const { superdoc } = makeStubs('editing'); |
| 226 | + const ui = createSuperDocUI({ superdoc }); |
| 227 | + |
| 228 | + const a = ui.document.getSnapshot(); |
| 229 | + const b = ui.document.getSnapshot(); |
| 230 | + expect(a).toBe(b); |
| 231 | + |
| 232 | + ui.destroy(); |
| 233 | + }); |
| 234 | + |
| 235 | + it('document slice identity changes when ready or mode flips', async () => { |
| 236 | + const { superdoc } = makeStubs('editing'); |
| 237 | + const ui = createSuperDocUI({ superdoc }); |
| 238 | + |
| 239 | + const before = ui.document.getSnapshot(); |
| 240 | + |
| 241 | + superdoc.config!.documentMode = 'viewing'; |
| 242 | + superdoc.fireSuperdoc('document-mode-change', { documentMode: 'viewing' }); |
| 243 | + await Promise.resolve(); |
| 244 | + await Promise.resolve(); |
| 245 | + const after = ui.document.getSnapshot(); |
| 246 | + |
| 247 | + expect(before).not.toBe(after); |
| 248 | + expect(after.mode).toBe('viewing'); |
| 249 | + |
| 250 | + ui.destroy(); |
| 251 | + }); |
| 252 | +}); |
0 commit comments