-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy patherror-mapping.test.ts
More file actions
276 lines (233 loc) · 8.64 KB
/
error-mapping.test.ts
File metadata and controls
276 lines (233 loc) · 8.64 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { describe, expect, test } from 'bun:test';
import { mapInvokeError, mapFailedReceipt } from '../../lib/error-mapping';
import { CliError } from '../../lib/errors';
describe('mapInvokeError', () => {
test('maps blocks.delete INVALID_INPUT errors to INVALID_ARGUMENT', () => {
const error = Object.assign(new Error('blocks.delete requires a target.'), {
code: 'INVALID_INPUT',
details: { field: 'target' },
});
const mapped = mapInvokeError('blocks.delete', error);
expect(mapped.code).toBe('INVALID_ARGUMENT');
expect(mapped.message).toBe('blocks.delete requires a target.');
expect(mapped.details).toEqual({ operationId: 'blocks.delete', details: { field: 'target' } });
});
});
// ---------------------------------------------------------------------------
// T8: Plan-engine error code passthrough in CLI error mapping
// ---------------------------------------------------------------------------
describe('mapInvokeError: plan-engine error passthrough', () => {
const operationId = 'mutations.apply' as any;
test('REVISION_MISMATCH preserves code and structured details', () => {
const error = Object.assign(new Error('REVISION_MISMATCH — stale ref'), {
code: 'REVISION_MISMATCH',
details: {
refRevision: '0',
currentRevision: '2',
refStability: 'ephemeral',
remediation: 'Re-run query.match() to obtain a fresh ref.',
},
});
const result = mapInvokeError(operationId, error);
expect(result).toBeInstanceOf(CliError);
expect(result.code).toBe('REVISION_MISMATCH');
expect(result.details).toMatchObject({
operationId,
details: {
refRevision: '0',
currentRevision: '2',
refStability: 'ephemeral',
remediation: expect.any(String),
},
});
});
test('PLAN_CONFLICT_OVERLAP preserves code and matrix details', () => {
const error = Object.assign(new Error('overlap'), {
code: 'PLAN_CONFLICT_OVERLAP',
details: {
stepIdA: 'step-1',
stepIdB: 'step-2',
opKeyA: 'format.apply',
opKeyB: 'text.rewrite',
matrixVerdict: 'reject',
matrixKey: 'format.apply::text.rewrite::same_target',
},
});
const result = mapInvokeError(operationId, error);
expect(result.code).toBe('PLAN_CONFLICT_OVERLAP');
expect(result.details).toMatchObject({
details: {
stepIdA: 'step-1',
stepIdB: 'step-2',
matrixVerdict: 'reject',
},
});
});
test('DOCUMENT_IDENTITY_CONFLICT preserves code and remediation', () => {
const error = Object.assign(new Error('duplicate IDs'), {
code: 'DOCUMENT_IDENTITY_CONFLICT',
details: {
duplicateBlockIds: ['p3', 'p7'],
blockCount: 2,
remediation: 'Re-import the document.',
},
});
const result = mapInvokeError(operationId, error);
expect(result.code).toBe('DOCUMENT_IDENTITY_CONFLICT');
expect(result.details).toMatchObject({
details: {
duplicateBlockIds: ['p3', 'p7'],
remediation: expect.any(String),
},
});
});
test('REVISION_CHANGED_SINCE_COMPILE preserves code and details', () => {
const error = Object.assign(new Error('drift'), {
code: 'REVISION_CHANGED_SINCE_COMPILE',
details: {
compiledRevision: '3',
currentRevision: '5',
remediation: 'Re-compile the plan.',
},
});
const result = mapInvokeError(operationId, error);
expect(result.code).toBe('REVISION_CHANGED_SINCE_COMPILE');
expect(result.details).toMatchObject({
details: {
compiledRevision: '3',
currentRevision: '5',
},
});
});
test('INVALID_INSERTION_CONTEXT preserves code and details', () => {
const error = Object.assign(new Error('bad context'), {
code: 'INVALID_INSERTION_CONTEXT',
details: {
stepIndex: 0,
operation: 'create.heading',
parentType: 'table_cell',
},
});
const result = mapInvokeError(operationId, error);
expect(result.code).toBe('INVALID_INSERTION_CONTEXT');
expect(result.details).toMatchObject({
details: {
stepIndex: 0,
parentType: 'table_cell',
},
});
});
test('unknown error codes still fall through to COMMAND_FAILED', () => {
const error = Object.assign(new Error('something weird'), {
code: 'TOTALLY_UNKNOWN_CODE',
details: { foo: 'bar' },
});
const result = mapInvokeError(operationId, error);
expect(result.code).toBe('COMMAND_FAILED');
});
test('valid ref (no error) baseline — CliError passes through', () => {
const error = new CliError('COMMAND_FAILED', 'already a CliError');
const result = mapInvokeError(operationId, error);
expect(result).toBe(error);
expect(result.code).toBe('COMMAND_FAILED');
});
test('large revision gap stale ref still includes all structured details', () => {
const error = Object.assign(new Error('REVISION_MISMATCH'), {
code: 'REVISION_MISMATCH',
details: {
refRevision: '0',
currentRevision: '50',
refStability: 'ephemeral',
remediation: 'Re-run query.match()',
},
});
const result = mapInvokeError(operationId, error);
expect(result.code).toBe('REVISION_MISMATCH');
expect(result.details).toMatchObject({
details: {
refRevision: '0',
currentRevision: '50',
refStability: 'ephemeral',
remediation: expect.any(String),
},
});
});
});
// ---------------------------------------------------------------------------
// T8 extension: mapFailedReceipt — plan-engine code passthrough + envelope
// ---------------------------------------------------------------------------
describe('mapFailedReceipt: plan-engine code passthrough', () => {
const operationId = 'insert' as any;
test('returns null for successful receipts', () => {
expect(mapFailedReceipt(operationId, { success: true })).toBeNull();
});
test('returns null for non-receipt values', () => {
expect(mapFailedReceipt(operationId, 'not a receipt')).toBeNull();
expect(mapFailedReceipt(operationId, null)).toBeNull();
expect(mapFailedReceipt(operationId, 42)).toBeNull();
});
test('returns COMMAND_FAILED when failure has no code', () => {
const result = mapFailedReceipt(operationId, { success: false });
expect(result).toBeInstanceOf(CliError);
expect(result!.code).toBe('COMMAND_FAILED');
});
test('plan-engine code MATCH_NOT_FOUND passes through with structured details', () => {
const receipt = {
success: false,
failure: {
code: 'MATCH_NOT_FOUND',
message: 'No match found for selector',
details: { selectorType: 'text', selectorPattern: 'foo', candidateCount: 0 },
},
};
const result = mapFailedReceipt(operationId, receipt);
expect(result).toBeInstanceOf(CliError);
expect(result!.code).toBe('MATCH_NOT_FOUND');
expect(result!.details).toMatchObject({
operationId,
failure: { code: 'MATCH_NOT_FOUND', details: { selectorType: 'text' } },
});
});
test('plan-engine code PRECONDITION_FAILED passes through', () => {
const receipt = {
success: false,
failure: { code: 'PRECONDITION_FAILED', message: 'Assert failed' },
};
const result = mapFailedReceipt(operationId, receipt);
expect(result!.code).toBe('PRECONDITION_FAILED');
});
test('plan-engine code REVISION_MISMATCH passes through', () => {
const receipt = {
success: false,
failure: {
code: 'REVISION_MISMATCH',
message: 'stale ref',
details: { refRevision: '0', currentRevision: '3' },
},
};
const result = mapFailedReceipt(operationId, receipt);
expect(result!.code).toBe('REVISION_MISMATCH');
expect(result!.details).toMatchObject({
failure: { details: { refRevision: '0', currentRevision: '3' } },
});
});
test('non-plan-engine failure codes go through per-family normalization', () => {
const receipt = {
success: false,
failure: { code: 'NO_OP', message: 'no change' },
};
const result = mapFailedReceipt(operationId, receipt);
// NO_OP is not a plan-engine passthrough code, so it normalizes
expect(result).toBeInstanceOf(CliError);
expect(result!.code).not.toBe('NO_OP');
});
test('paragraph mutation receipt maps INVALID_TARGET to INVALID_ARGUMENT', () => {
const receipt = {
success: false,
failure: { code: 'INVALID_TARGET', message: 'Paragraph target is invalid.' },
};
const result = mapFailedReceipt('format.paragraph.setAlignment' as any, receipt);
expect(result).toBeInstanceOf(CliError);
expect(result!.code).toBe('INVALID_ARGUMENT');
});
});