-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathcreate.test.ts
More file actions
195 lines (166 loc) · 5.8 KB
/
create.test.ts
File metadata and controls
195 lines (166 loc) · 5.8 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
import { describe, expect, it, mock } from 'bun:test';
import {
executeCreateParagraph,
executeCreateSectionBreak,
executeCreateTable,
normalizeCreateParagraphInput,
} from './create.js';
describe('normalizeCreateParagraphInput', () => {
it('defaults location to documentEnd when at is omitted', () => {
const result = normalizeCreateParagraphInput({});
expect(result.at).toEqual({ kind: 'documentEnd' });
});
it('defaults text to empty string when omitted', () => {
const result = normalizeCreateParagraphInput({});
expect(result.text).toBe('');
});
it('defaults both at and text when input is empty', () => {
const result = normalizeCreateParagraphInput({});
expect(result).toEqual({
at: { kind: 'documentEnd' },
text: '',
});
});
it('preserves explicit documentStart location', () => {
const result = normalizeCreateParagraphInput({ at: { kind: 'documentStart' } });
expect(result.at).toEqual({ kind: 'documentStart' });
});
it('preserves explicit before location with target', () => {
const target = { kind: 'block' as const, nodeType: 'paragraph' as const, nodeId: 'p1' };
const result = normalizeCreateParagraphInput({ at: { kind: 'before', target } });
expect(result.at).toEqual({ kind: 'before', target });
});
it('preserves explicit after location with target', () => {
const target = { kind: 'block' as const, nodeType: 'heading' as const, nodeId: 'h1' };
const result = normalizeCreateParagraphInput({ at: { kind: 'after', target } });
expect(result.at).toEqual({ kind: 'after', target });
});
it('preserves explicit text', () => {
const result = normalizeCreateParagraphInput({ text: 'Hello world' });
expect(result.text).toBe('Hello world');
});
it('preserves both explicit at and text', () => {
const result = normalizeCreateParagraphInput({
at: { kind: 'documentStart' },
text: 'First paragraph',
});
expect(result).toEqual({
at: { kind: 'documentStart' },
text: 'First paragraph',
});
});
});
describe('executeCreateTable', () => {
it('accepts nodeId-based before/after placement without requiring at.target', () => {
const adapter = {
paragraph: () => ({ success: true }),
heading: () => ({ success: true }),
table: () => ({
success: true,
table: { kind: 'block', nodeType: 'table', nodeId: 'new-table' },
}),
} as any;
expect(() =>
executeCreateTable(adapter, {
rows: 2,
columns: 2,
at: { kind: 'after', nodeId: 'p1' },
}),
).not.toThrow();
});
it('rejects ambiguous before/after placement when both at.target and at.nodeId are provided', () => {
let tableCalled = false;
const adapter = {
paragraph: () => ({ success: true }),
heading: () => ({ success: true }),
table: () => {
tableCalled = true;
return {
success: true,
table: { kind: 'block', nodeType: 'table', nodeId: 'new-table' },
};
},
} as any;
const target = { kind: 'block' as const, nodeType: 'paragraph' as const, nodeId: 'p1' };
expect(() =>
executeCreateTable(adapter, {
rows: 2,
columns: 2,
at: { kind: 'after', target, nodeId: 'p1' } as any,
}),
).toThrow(/Cannot combine/i);
expect(tableCalled).toBe(false);
});
});
describe('create target validation', () => {
it('rejects nodeId-based before/after placement for create.paragraph', () => {
let paragraphCalled = false;
const adapter = {
paragraph: () => {
paragraphCalled = true;
return {
success: true,
paragraph: { kind: 'block', nodeType: 'paragraph', nodeId: 'p2' },
insertionPoint: { kind: 'text', blockId: 'p2', range: { start: 0, end: 0 } },
};
},
heading: () => ({ success: true }),
table: () => ({ success: true }),
sectionBreak: () => ({ success: true }),
} as any;
expect(() =>
executeCreateParagraph(adapter, {
at: { kind: 'after', nodeId: 'p1' } as any,
}),
).toThrow(/does not support at\.nodeId/i);
expect(paragraphCalled).toBe(false);
});
});
describe('executeCreateSectionBreak', () => {
it('defaults create.sectionBreak location to documentEnd', () => {
const adapter = {
paragraph: () => ({ success: true }),
heading: () => ({ success: true }),
table: () => ({ success: true }),
sectionBreak: mock(() => ({
success: true,
section: { kind: 'section', sectionId: 'section-1' },
})),
} as any;
executeCreateSectionBreak(adapter, { breakType: 'nextPage' });
expect(adapter.sectionBreak).toHaveBeenCalledWith(
expect.objectContaining({
at: { kind: 'documentEnd' },
breakType: 'nextPage',
}),
{ changeMode: 'direct', dryRun: false, expectedRevision: undefined },
);
});
it('rejects invalid section break type', () => {
const adapter = {
paragraph: () => ({ success: true }),
heading: () => ({ success: true }),
table: () => ({ success: true }),
sectionBreak: mock(() => ({ success: true })),
} as any;
expect(() =>
executeCreateSectionBreak(adapter, {
breakType: 'invalidBreakType' as any,
}),
).toThrow(/create\.sectionBreak breakType must be one of/i);
});
it('rejects nodeId-based before/after placement', () => {
const adapter = {
paragraph: () => ({ success: true }),
heading: () => ({ success: true }),
table: () => ({ success: true }),
sectionBreak: mock(() => ({ success: true })),
} as any;
expect(() =>
executeCreateSectionBreak(adapter, {
at: { kind: 'before', nodeId: 'p1' } as any,
}),
).toThrow(/does not support at\.nodeId/i);
expect(adapter.sectionBreak).not.toHaveBeenCalled();
});
});