|
| 1 | +import { describe, expect, it, mock } from 'bun:test'; |
| 2 | +import { executeScrollIntoView } from './scroll-into-view.js'; |
| 3 | +import type { RangeScrollAdapter, ScrollIntoViewInput, ScrollIntoViewOutput } from './ranges.types.js'; |
| 4 | + |
| 5 | +function makeAdapter(output: ScrollIntoViewOutput = { success: true }): RangeScrollAdapter & { |
| 6 | + scrollIntoView: ReturnType<typeof mock>; |
| 7 | +} { |
| 8 | + const scrollIntoView = mock(async () => output); |
| 9 | + return { scrollIntoView } as unknown as RangeScrollAdapter & { scrollIntoView: ReturnType<typeof mock> }; |
| 10 | +} |
| 11 | + |
| 12 | +async function expectValidationError(fn: () => Promise<unknown>, code: string, messageMatch: string): Promise<void> { |
| 13 | + try { |
| 14 | + await fn(); |
| 15 | + throw new Error(`expected ${code}, nothing thrown`); |
| 16 | + } catch (err: unknown) { |
| 17 | + const e = err as { name?: string; code?: string; message?: string }; |
| 18 | + expect(e.name).toBe('DocumentApiValidationError'); |
| 19 | + expect(e.code).toBe(code); |
| 20 | + expect(e.message ?? '').toContain(messageMatch); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +describe('executeScrollIntoView — validation', () => { |
| 25 | + it('rejects a null / undefined input', async () => { |
| 26 | + const adapter = makeAdapter(); |
| 27 | + await expectValidationError( |
| 28 | + () => executeScrollIntoView(adapter, null as unknown as ScrollIntoViewInput), |
| 29 | + 'INVALID_INPUT', |
| 30 | + 'non-null object', |
| 31 | + ); |
| 32 | + await expectValidationError( |
| 33 | + () => executeScrollIntoView(adapter, undefined as unknown as ScrollIntoViewInput), |
| 34 | + 'INVALID_INPUT', |
| 35 | + 'non-null object', |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('rejects inputs with unknown fields', async () => { |
| 40 | + const adapter = makeAdapter(); |
| 41 | + await expectValidationError( |
| 42 | + () => |
| 43 | + executeScrollIntoView(adapter, { |
| 44 | + target: { kind: 'text', blockId: 'p1', range: { start: 0, end: 5 } }, |
| 45 | + somethingElse: true, |
| 46 | + } as unknown as ScrollIntoViewInput), |
| 47 | + 'INVALID_INPUT', |
| 48 | + 'Unknown field', |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('rejects when target is missing', async () => { |
| 53 | + const adapter = makeAdapter(); |
| 54 | + await expectValidationError( |
| 55 | + () => executeScrollIntoView(adapter, {} as unknown as ScrollIntoViewInput), |
| 56 | + 'INVALID_TARGET', |
| 57 | + 'requires a target', |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it('rejects when target is malformed', async () => { |
| 62 | + const adapter = makeAdapter(); |
| 63 | + await expectValidationError( |
| 64 | + () => executeScrollIntoView(adapter, { target: { kind: 'nope' } } as unknown as ScrollIntoViewInput), |
| 65 | + 'INVALID_TARGET', |
| 66 | + 'TextAddress, TextTarget, or EntityAddress', |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('rejects entity targets with unknown entityType', async () => { |
| 71 | + const adapter = makeAdapter(); |
| 72 | + await expectValidationError( |
| 73 | + () => |
| 74 | + executeScrollIntoView(adapter, { |
| 75 | + target: { kind: 'entity', entityType: 'mystery', entityId: 'x_1' }, |
| 76 | + } as unknown as ScrollIntoViewInput), |
| 77 | + 'INVALID_TARGET', |
| 78 | + 'TextAddress, TextTarget, or EntityAddress', |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('rejects entity targets with empty entityId', async () => { |
| 83 | + const adapter = makeAdapter(); |
| 84 | + await expectValidationError( |
| 85 | + () => |
| 86 | + executeScrollIntoView(adapter, { |
| 87 | + target: { kind: 'entity', entityType: 'comment', entityId: '' }, |
| 88 | + } as unknown as ScrollIntoViewInput), |
| 89 | + 'INVALID_TARGET', |
| 90 | + 'TextAddress, TextTarget, or EntityAddress', |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it('rejects block outside the allowed enum', async () => { |
| 95 | + const adapter = makeAdapter(); |
| 96 | + await expectValidationError( |
| 97 | + () => |
| 98 | + executeScrollIntoView(adapter, { |
| 99 | + target: { kind: 'text', blockId: 'p1', range: { start: 0, end: 5 } }, |
| 100 | + block: 'top' as 'start', |
| 101 | + }), |
| 102 | + 'INVALID_INPUT', |
| 103 | + 'block must be', |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it('rejects behavior outside the allowed enum', async () => { |
| 108 | + const adapter = makeAdapter(); |
| 109 | + await expectValidationError( |
| 110 | + () => |
| 111 | + executeScrollIntoView(adapter, { |
| 112 | + target: { kind: 'text', blockId: 'p1', range: { start: 0, end: 5 } }, |
| 113 | + behavior: 'instant' as 'auto', |
| 114 | + }), |
| 115 | + 'INVALID_INPUT', |
| 116 | + 'behavior must be', |
| 117 | + ); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('executeScrollIntoView — delegation', () => { |
| 122 | + it('accepts a TextAddress target and forwards it unchanged', async () => { |
| 123 | + const adapter = makeAdapter(); |
| 124 | + const input: ScrollIntoViewInput = { |
| 125 | + target: { kind: 'text', blockId: 'p1', range: { start: 3, end: 9 } }, |
| 126 | + }; |
| 127 | + const out = await executeScrollIntoView(adapter, input); |
| 128 | + expect(out).toEqual({ success: true }); |
| 129 | + expect(adapter.scrollIntoView).toHaveBeenCalledWith(input); |
| 130 | + }); |
| 131 | + |
| 132 | + it('accepts a multi-segment TextTarget target', async () => { |
| 133 | + const adapter = makeAdapter(); |
| 134 | + const input: ScrollIntoViewInput = { |
| 135 | + target: { |
| 136 | + kind: 'text', |
| 137 | + segments: [ |
| 138 | + { blockId: 'p1', range: { start: 0, end: 5 } }, |
| 139 | + { blockId: 'p2', range: { start: 0, end: 3 } }, |
| 140 | + ], |
| 141 | + }, |
| 142 | + block: 'start', |
| 143 | + behavior: 'auto', |
| 144 | + }; |
| 145 | + const out = await executeScrollIntoView(adapter, input); |
| 146 | + expect(out).toEqual({ success: true }); |
| 147 | + expect(adapter.scrollIntoView).toHaveBeenCalledWith(input); |
| 148 | + }); |
| 149 | + |
| 150 | + it('accepts an EntityAddress (comment) target', async () => { |
| 151 | + const adapter = makeAdapter(); |
| 152 | + const input: ScrollIntoViewInput = { |
| 153 | + target: { kind: 'entity', entityType: 'comment', entityId: 'c_1' }, |
| 154 | + }; |
| 155 | + await executeScrollIntoView(adapter, input); |
| 156 | + expect(adapter.scrollIntoView).toHaveBeenCalledWith(input); |
| 157 | + }); |
| 158 | + |
| 159 | + it('accepts an EntityAddress (trackedChange) target', async () => { |
| 160 | + const adapter = makeAdapter(); |
| 161 | + const input: ScrollIntoViewInput = { |
| 162 | + target: { kind: 'entity', entityType: 'trackedChange', entityId: 'tc_1' }, |
| 163 | + }; |
| 164 | + await executeScrollIntoView(adapter, input); |
| 165 | + expect(adapter.scrollIntoView).toHaveBeenCalledWith(input); |
| 166 | + }); |
| 167 | + |
| 168 | + it('returns whatever the adapter returns (e.g. success: false)', async () => { |
| 169 | + const adapter = makeAdapter({ success: false }); |
| 170 | + const out = await executeScrollIntoView(adapter, { |
| 171 | + target: { kind: 'text', blockId: 'p1', range: { start: 0, end: 5 } }, |
| 172 | + }); |
| 173 | + expect(out).toEqual({ success: false }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments