-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathformat.test.ts
More file actions
261 lines (226 loc) Β· 10.2 KB
/
format.test.ts
File metadata and controls
261 lines (226 loc) Β· 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { describe, expect, it, mock } from 'bun:test';
import type { FormatInlineAliasInput, StyleApplyInput } from './format.js';
/** Type-only assertion β validates at compile time, no-op at runtime. */
function assertType<T>(_value: T): void {}
import { executeStyleApply, executeInlineAlias } from './format.js';
import { DocumentApiValidationError } from '../errors.js';
import type { TextMutationReceipt } from '../types/index.js';
import type { SelectionMutationAdapter } from '../selection-mutation.js';
import type { SelectionTarget } from '../types/address.js';
const TARGET: SelectionTarget = {
kind: 'selection',
start: { kind: 'text', blockId: 'p1', offset: 0 },
end: { kind: 'text', blockId: 'p1', offset: 5 },
};
function makeReceipt(): TextMutationReceipt {
return {
success: true,
resolution: {
blockId: 'p1',
blockType: 'paragraph',
text: 'Hello',
target: TARGET,
range: { start: 0, end: 5 },
},
};
}
function makeAdapter(): SelectionMutationAdapter & Record<string, ReturnType<typeof mock>> {
return {
execute: mock(() => makeReceipt()),
};
}
describe('executeStyleApply validation', () => {
it('rejects non-object input', () => {
const adapter = makeAdapter();
expect(() => executeStyleApply(adapter, null as any)).toThrow(DocumentApiValidationError);
expect(() => executeStyleApply(adapter, 42 as any)).toThrow('non-null object');
expect(() => executeStyleApply(adapter, 'bad' as any)).toThrow('non-null object');
});
it('rejects unknown top-level fields', () => {
const adapter = makeAdapter();
const input = { target: TARGET, inline: { bold: true }, extra: 1 };
expect(() => executeStyleApply(adapter, input as any)).toThrow('extra');
});
it('rejects missing target', () => {
const adapter = makeAdapter();
const input = { inline: { bold: true } };
expect(() => executeStyleApply(adapter, input as any)).toThrow('either "target" or "ref"');
});
it('rejects invalid target', () => {
const adapter = makeAdapter();
const input = { target: 'not-an-address', inline: { bold: true } };
expect(() => executeStyleApply(adapter, input as any)).toThrow('SelectionTarget');
});
it('accepts valid target', () => {
const adapter = makeAdapter();
const input: StyleApplyInput = { target: TARGET, inline: { bold: true } };
const result = executeStyleApply(adapter, input);
expect(result.success).toBe(true);
});
it('rejects missing inline', () => {
const adapter = makeAdapter();
const input = { target: TARGET };
expect(() => executeStyleApply(adapter, input as any)).toThrow('requires an inline object');
});
it('rejects non-object inline', () => {
const adapter = makeAdapter();
const input = { target: TARGET, inline: 'bold' };
expect(() => executeStyleApply(adapter, input as any)).toThrow('non-null object');
});
it('rejects empty inline object', () => {
const adapter = makeAdapter();
const input = { target: TARGET, inline: {} };
expect(() => executeStyleApply(adapter, input as any)).toThrow('at least one known key');
});
it('rejects unknown inline keys', () => {
const adapter = makeAdapter();
const input = { target: TARGET, inline: { superscript: true } };
expect(() => executeStyleApply(adapter, input as any)).toThrow('Unknown inline property: "superscript".');
});
it('rejects invalid boolean payload type', () => {
const adapter = makeAdapter();
const input = { target: TARGET, inline: { bold: 'yes' } };
expect(() => executeStyleApply(adapter, input as any)).toThrow('inline.bold must be boolean or null');
});
it('rejects empty object patch values', () => {
const adapter = makeAdapter();
const input = { target: TARGET, inline: { shading: {} } };
expect(() => executeStyleApply(adapter, input as any)).toThrow('inline.shading object must not be empty');
});
it('accepts boolean tri-state payloads', () => {
const adapter = makeAdapter();
const input: StyleApplyInput = { target: TARGET, inline: { bold: null, italic: false } };
const result = executeStyleApply(adapter, input);
expect(result.success).toBe(true);
expect(adapter.execute).toHaveBeenCalledWith(
{ kind: 'format', target: TARGET, ref: undefined, inline: { bold: null, italic: false } },
expect.objectContaining({ changeMode: 'direct' }),
);
});
it('accepts numeric and object inline properties in one call', () => {
const adapter = makeAdapter();
const input: StyleApplyInput = {
target: TARGET,
inline: {
fontSize: 12,
underline: { style: 'single', color: 'FF0000' },
},
};
const result = executeStyleApply(adapter, input);
expect(result.success).toBe(true);
});
it('passes through tracked and dryRun options', () => {
const adapter = makeAdapter();
const input: StyleApplyInput = { target: TARGET, inline: { color: '00AA00' } };
executeStyleApply(adapter, input, { changeMode: 'tracked', dryRun: true });
expect(adapter.execute).toHaveBeenCalledWith(
{ kind: 'format', target: TARGET, ref: undefined, inline: { color: '00AA00' } },
{ changeMode: 'tracked', dryRun: true },
);
});
});
// executeInlineAlias β runtime + type contract
// ---------------------------------------------------------------------------
describe('executeInlineAlias', () => {
it('format.bold accepts omitted value (defaults to true)', () => {
const adapter = makeAdapter();
executeInlineAlias(adapter, 'bold', { target: TARGET });
expect(adapter.execute).toHaveBeenCalledWith(
{ kind: 'format', target: TARGET, ref: undefined, inline: { bold: true } },
expect.objectContaining({ changeMode: 'direct' }),
);
});
it('format.underline accepts omitted value (defaults to true)', () => {
const adapter = makeAdapter();
executeInlineAlias(adapter, 'underline', { target: TARGET });
expect(adapter.execute).toHaveBeenCalledWith(
{ kind: 'format', target: TARGET, ref: undefined, inline: { underline: true } },
expect.objectContaining({ changeMode: 'direct' }),
);
});
it('format.color requires value β throws when omitted', () => {
const adapter = makeAdapter();
expect(() => executeInlineAlias(adapter, 'color', { target: TARGET } as any)).toThrow(
'format.color requires a value field',
);
});
it('format.rFonts requires value β throws when omitted', () => {
const adapter = makeAdapter();
expect(() => executeInlineAlias(adapter, 'rFonts', { target: TARGET } as any)).toThrow(
'format.rFonts requires a value field',
);
});
it('format.fontSize requires value β throws when omitted', () => {
const adapter = makeAdapter();
expect(() => executeInlineAlias(adapter, 'fontSize', { target: TARGET } as any)).toThrow(
'format.fontSize requires a value field',
);
});
it('format.color accepts explicit value', () => {
const adapter = makeAdapter();
executeInlineAlias(adapter, 'color', { target: TARGET, value: 'FF0000' });
expect(adapter.execute).toHaveBeenCalledWith(
{ kind: 'format', target: TARGET, ref: undefined, inline: { color: 'FF0000' } },
expect.objectContaining({ changeMode: 'direct' }),
);
});
});
describe('executeInlineAlias: format.caps', () => {
it('format.caps accepts omitted value (defaults to true)', () => {
const adapter = makeAdapter();
executeInlineAlias(adapter, 'caps', { target: TARGET });
expect(adapter.execute).toHaveBeenCalledWith(
{ kind: 'format', target: TARGET, ref: undefined, inline: { caps: true } },
expect.objectContaining({ changeMode: 'direct' }),
);
});
it('format.caps accepts explicit false', () => {
const adapter = makeAdapter();
executeInlineAlias(adapter, 'caps', { target: TARGET, value: false });
expect(adapter.execute).toHaveBeenCalledWith(
{ kind: 'format', target: TARGET, ref: undefined, inline: { caps: false } },
expect.objectContaining({ changeMode: 'direct' }),
);
});
it('format.caps accepts null to clear', () => {
const adapter = makeAdapter();
executeInlineAlias(adapter, 'caps', { target: TARGET, value: null });
expect(adapter.execute).toHaveBeenCalledWith(
{ kind: 'format', target: TARGET, ref: undefined, inline: { caps: null } },
expect.objectContaining({ changeMode: 'direct' }),
);
});
});
// ---------------------------------------------------------------------------
// FormatInlineAliasInput β compile-time type shape assertions
// ---------------------------------------------------------------------------
describe('FormatInlineAliasInput type contract', () => {
it('boolean keys allow omitted value', () => {
// These should all compile β value is optional for boolean keys.
assertType<FormatInlineAliasInput<'bold'>>({ target: TARGET });
assertType<FormatInlineAliasInput<'bold'>>({ target: TARGET, value: true });
assertType<FormatInlineAliasInput<'italic'>>({ target: TARGET });
assertType<FormatInlineAliasInput<'strike'>>({ target: TARGET });
assertType<FormatInlineAliasInput<'dstrike'>>({ target: TARGET });
assertType<FormatInlineAliasInput<'vanish'>>({ target: TARGET });
});
it('underline allows omitted value', () => {
assertType<FormatInlineAliasInput<'underline'>>({ target: TARGET });
assertType<FormatInlineAliasInput<'underline'>>({ target: TARGET, value: true });
assertType<FormatInlineAliasInput<'underline'>>({ target: TARGET, value: { style: 'single' } });
});
it('non-boolean keys require value', () => {
// color requires value
assertType<FormatInlineAliasInput<'color'>>({ target: TARGET, value: 'FF0000' });
// @ts-expect-error β value is required for color
assertType<FormatInlineAliasInput<'color'>>({ target: TARGET });
// fontSize requires value
assertType<FormatInlineAliasInput<'fontSize'>>({ target: TARGET, value: 12 });
// @ts-expect-error β value is required for fontSize
assertType<FormatInlineAliasInput<'fontSize'>>({ target: TARGET });
// rFonts requires value
assertType<FormatInlineAliasInput<'rFonts'>>({ target: TARGET, value: { ascii: 'Arial' } });
// @ts-expect-error β value is required for rFonts
assertType<FormatInlineAliasInput<'rFonts'>>({ target: TARGET });
});
});