@jsb188/mday 1.1.10 → 1.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/package.json +105 -54
  2. package/src/constants/agentDoc.ts +12 -0
  3. package/src/constants/agentMemory.ts +154 -0
  4. package/src/constants/aiChat.ts +5 -0
  5. package/src/constants/campaign.ts +127 -0
  6. package/src/constants/dataTable.ts +35 -9
  7. package/src/constants/dataTableFieldTypes.generated.ts +63 -0
  8. package/src/constants/docAccess.ts +32 -0
  9. package/src/constants/email.ts +20 -4
  10. package/src/constants/fieldDoc.ts +34 -0
  11. package/src/constants/graphqlReactivity.ts +113 -0
  12. package/src/constants/integration.ts +8 -9
  13. package/src/constants/log.ts +7 -410
  14. package/src/constants/logActivityGroupColors.generated.ts +19 -0
  15. package/src/constants/logTaxonomy.generated.ts +1103 -0
  16. package/src/constants/noteDoc.ts +77 -0
  17. package/src/constants/organization.ts +13 -15
  18. package/src/constants/pageRoute.ts +23 -0
  19. package/src/constants/schedule.ts +220 -0
  20. package/src/constants/scheduleRuleTemplateSeed.ts +138 -0
  21. package/src/constants/scheduleRuleTemplates.generated.ts +145 -0
  22. package/src/constants/sheet.ts +154 -6
  23. package/src/constants/sheetRegionSources.generated.ts +578 -0
  24. package/src/constants/square.ts +6 -6
  25. package/src/constants/storage.ts +56 -7
  26. package/src/constants/usage.ts +79 -0
  27. package/src/schemas/editLogEntry.ts +1010 -777
  28. package/src/schemas/fieldDocsQuery.ts +12 -0
  29. package/src/schemas/inboundContact.ts +60 -55
  30. package/src/schemas/logsQuery.ts +55 -49
  31. package/src/schemas/organization.ts +49 -46
  32. package/src/types/account.d.ts +4 -1
  33. package/src/types/agentDoc.d.ts +64 -0
  34. package/src/types/agentMemory.d.ts +74 -0
  35. package/src/types/campaign.d.ts +683 -0
  36. package/src/types/dataTable.d.ts +175 -256
  37. package/src/types/docAccess.d.ts +96 -0
  38. package/src/types/email.d.ts +83 -12
  39. package/src/types/fieldDoc.d.ts +273 -0
  40. package/src/types/inboundContact.d.ts +2 -1
  41. package/src/types/log.d.ts +209 -341
  42. package/src/types/noteDoc.d.ts +86 -0
  43. package/src/types/organization.d.ts +83 -40
  44. package/src/types/pageRoute.d.ts +48 -0
  45. package/src/types/schedule.d.ts +391 -0
  46. package/src/types/sheet.d.ts +862 -30
  47. package/src/types/storage.d.ts +206 -125
  48. package/src/types/usage.d.ts +106 -0
  49. package/src/types/workflow.d.ts +149 -18
  50. package/src/utils/agentDoc.ts +109 -0
  51. package/src/utils/aiChat.ts +26 -0
  52. package/src/utils/campaign.ts +741 -0
  53. package/src/utils/campaignFormEditor.ts +871 -0
  54. package/src/utils/dataTable.ts +425 -410
  55. package/src/utils/dataTableFieldTypeRegistry.ts +105 -0
  56. package/src/utils/dataTableRealtimeAuthority.ts +209 -0
  57. package/src/utils/dataTableRealtimeSelection.ts +134 -0
  58. package/src/utils/dataTableRowDocument.ts +508 -0
  59. package/src/utils/dataTableSource.ts +230 -0
  60. package/src/utils/docAccess.ts +383 -0
  61. package/src/utils/documentName.ts +53 -0
  62. package/src/utils/email.ts +112 -53
  63. package/src/utils/fieldDoc.ts +113 -0
  64. package/src/utils/log.ts +169 -176
  65. package/src/utils/logRegistryValue.ts +90 -0
  66. package/src/utils/logStoreRead.ts +266 -0
  67. package/src/utils/logTaxonomy.ts +76 -0
  68. package/src/utils/logTaxonomyDisplay.ts +439 -0
  69. package/src/utils/noteDoc.ts +827 -0
  70. package/src/utils/organization.ts +510 -91
  71. package/src/utils/pageRoute.ts +71 -0
  72. package/src/utils/schedule.ts +799 -0
  73. package/src/utils/scheduleEvaluation.ts +1994 -0
  74. package/src/utils/scheduleICS.ts +246 -0
  75. package/src/utils/scheduleRuleTemplates.ts +188 -0
  76. package/src/utils/sheet.ts +4842 -39
  77. package/src/utils/sheetAxisMove.ts +830 -0
  78. package/src/utils/sheetBootstrap.ts +1769 -0
  79. package/src/utils/sheetCommand.ts +423 -0
  80. package/src/utils/sheetExport.ts +238 -0
  81. package/src/utils/sheetFormulaEvaluation.ts +914 -0
  82. package/src/utils/sheetFormulaEvaluator.ts +852 -0
  83. package/src/utils/sheetJournal.ts +78 -0
  84. package/src/utils/sheetNumberFormat.ts +1211 -0
  85. package/src/utils/sheetRegion.ts +374 -0
  86. package/src/utils/sheetRegionFilter.ts +1259 -0
  87. package/src/utils/sheetRegionSourceRegistry.ts +594 -0
  88. package/src/utils/sheetSequence.ts +148 -0
  89. package/src/utils/sheetSnapshot.ts +992 -0
  90. package/src/utils/sheetStructureEdit.ts +1284 -0
  91. package/src/utils/square.ts +22 -12
  92. package/src/utils/storage.ts +152 -0
  93. package/src/utils/table-route.ts +127 -11
  94. package/src/utils/workflow.ts +191 -23
  95. package/tests/dataTableFieldTypeRegistry.test.ts +110 -0
  96. package/tests/docAccess.test.ts +525 -0
  97. package/tests/editLogEntry.test.ts +112 -0
  98. package/tests/noteDoc.test.ts +669 -0
  99. package/tests/schedule.test.ts +693 -0
  100. package/tests/scheduleEvaluation.test.ts +1970 -0
  101. package/tests/scheduleICS.test.ts +208 -0
  102. package/tests/scheduleRuleTemplates.test.ts +176 -0
  103. package/tests/scheduleSweepInvariants.test.ts +428 -0
  104. package/tests/sheetAxisMove.test.ts +343 -0
  105. package/tests/sheetFormulaEvaluator.test.ts +575 -0
  106. package/tests/sheetRegionFilter.test.ts +1135 -0
  107. package/tests/sheetSequence.test.ts +41 -0
  108. package/tests/sheetStructureEdit.test.ts +82 -0
  109. package/tsconfig.json +10 -10
  110. package/vitest.config.ts +8 -0
  111. package/src/constants/log-activityTexts.ts +0 -235
  112. package/src/constants/product.ts +0 -42
  113. package/src/constants/report.ts +0 -44
  114. package/src/schemas/productsQuery.ts +0 -53
  115. package/src/schemas/reportsQuery.ts +0 -20
  116. package/src/types/product.d.ts +0 -84
  117. package/src/types/report.d.ts +0 -290
  118. package/src/utils/product-calEvent.ts +0 -455
  119. package/src/utils/report.ts +0 -158
@@ -0,0 +1,669 @@
1
+ import {
2
+ NOTE_DOC_VERSION,
3
+ NOTE_MAX_BLOCKS,
4
+ NOTE_MAX_TEXT_CHARS,
5
+ } from '@jsb188/mday/constants/noteDoc.ts';
6
+ import type {
7
+ NoteBlockNode,
8
+ NoteDoc,
9
+ NoteInlineNode,
10
+ } from '@jsb188/mday/types/noteDoc.d.ts';
11
+ import {
12
+ collectNoteImageStorageIds,
13
+ createEmptyNoteDoc,
14
+ filterImageNodesByStorageRels,
15
+ noteDocToPlainText,
16
+ sanitizeNoteDoc,
17
+ } from '@jsb188/mday/utils/noteDoc.ts';
18
+ import { describe, expect, it } from 'vitest';
19
+
20
+ /*
21
+ * Build one doc root around the given raw block list.
22
+ */
23
+ function buildDoc(content: any[]): any {
24
+ return {
25
+ type: 'doc',
26
+ version: NOTE_DOC_VERSION,
27
+ content,
28
+ };
29
+ }
30
+
31
+ /*
32
+ * Build one paragraph with a single plain text run.
33
+ */
34
+ function buildParagraph(text: string): any {
35
+ return {
36
+ type: 'paragraph',
37
+ content: [{ type: 'text', text }],
38
+ };
39
+ }
40
+
41
+ /*
42
+ * Build one image block with valid attrs.
43
+ */
44
+ function buildImage(
45
+ storageId: string | number,
46
+ extraAttrs: Record<string, any> = {},
47
+ ): any {
48
+ return {
49
+ type: 'image',
50
+ attrs: {
51
+ storageId,
52
+ fileUri: 'uploads/img-' + storageId + '.png',
53
+ ...extraAttrs,
54
+ },
55
+ };
56
+ }
57
+
58
+ describe('createEmptyNoteDoc', () => {
59
+ it('builds one versioned doc with a single empty paragraph', () => {
60
+ expect(createEmptyNoteDoc()).toEqual({
61
+ type: 'doc',
62
+ version: NOTE_DOC_VERSION,
63
+ content: [{ type: 'paragraph', content: [] }],
64
+ });
65
+ });
66
+ });
67
+
68
+ describe('sanitizeNoteDoc', () => {
69
+ it('coerces unknown roots to an empty doc', () => {
70
+ for (const input of [
71
+ null,
72
+ undefined,
73
+ 'hello',
74
+ 42,
75
+ [],
76
+ { type: 'paragraph' },
77
+ { content: [] },
78
+ ]) {
79
+ expect(sanitizeNoteDoc(input)).toEqual(createEmptyNoteDoc());
80
+ }
81
+ });
82
+
83
+ it('guarantees a non-empty doc and always stamps the current version', () => {
84
+ const output = sanitizeNoteDoc({ type: 'doc', version: 99, content: [] });
85
+ expect(output.version).toBe(NOTE_DOC_VERSION);
86
+ expect(output.content).toEqual([{ type: 'paragraph', content: [] }]);
87
+ });
88
+
89
+ it('keeps a legal document intact', () => {
90
+ const input = buildDoc([
91
+ {
92
+ type: 'heading',
93
+ attrs: { level: 2 },
94
+ content: [{ type: 'text', text: 'Title' }],
95
+ },
96
+ {
97
+ type: 'paragraph',
98
+ content: [{ type: 'text', text: 'Body', marks: [{ type: 'bold' }] }],
99
+ },
100
+ { type: 'divider' },
101
+ {
102
+ type: 'bulletList',
103
+ content: [
104
+ { type: 'listItem', content: [buildParagraph('one')] },
105
+ { type: 'listItem', content: [buildParagraph('two')] },
106
+ ],
107
+ },
108
+ { type: 'blockquote', content: [buildParagraph('quoted')] },
109
+ buildImage('55', { alt: 'photo', width: 320 }),
110
+ ]);
111
+ expect(sanitizeNoteDoc(input).content).toEqual(input.content);
112
+ });
113
+
114
+ it('drops unknown block types and unknown inline types', () => {
115
+ const output = sanitizeNoteDoc(
116
+ buildDoc([
117
+ { type: 'iframe', content: [{ type: 'text', text: 'evil' }] },
118
+ {
119
+ type: 'paragraph',
120
+ content: [
121
+ { type: 'emoji', name: 'x' },
122
+ { type: 'text', text: 'ok' },
123
+ ],
124
+ },
125
+ ]),
126
+ );
127
+ expect(output.content).toEqual([
128
+ { type: 'paragraph', content: [{ type: 'text', text: 'ok' }] },
129
+ ]);
130
+ });
131
+
132
+ it('strips client-only and unknown fields from nodes', () => {
133
+ const output = sanitizeNoteDoc(
134
+ buildDoc([
135
+ {
136
+ type: 'paragraph',
137
+ _key: 'k1',
138
+ junk: true,
139
+ attrs: { junk: 1 },
140
+ content: [{ type: 'text', text: 'a', junk: 2 }],
141
+ },
142
+ { ...buildImage('9'), _uploading: true, _key: 'k2' },
143
+ ]),
144
+ );
145
+ expect(output.content[0]).toEqual({
146
+ type: 'paragraph',
147
+ content: [{ type: 'text', text: 'a' }],
148
+ });
149
+ expect(output.content[1]).toEqual({
150
+ type: 'image',
151
+ attrs: { storageId: '9', fileUri: 'uploads/img-9.png' },
152
+ });
153
+ });
154
+
155
+ it('clamps heading levels into 1-3', () => {
156
+ const output = sanitizeNoteDoc(
157
+ buildDoc([
158
+ { type: 'heading', attrs: { level: 0 }, content: [] },
159
+ { type: 'heading', attrs: { level: 5 }, content: [] },
160
+ { type: 'heading', attrs: { level: '2' }, content: [] },
161
+ { type: 'heading', content: [] },
162
+ ]),
163
+ );
164
+ expect(
165
+ output.content.map((block: NoteBlockNode) => block.attrs?.level),
166
+ ).toEqual([1, 3, 2, 1]);
167
+ });
168
+
169
+ it('whitelists mark types and drops duplicates', () => {
170
+ const output = sanitizeNoteDoc(
171
+ buildDoc([
172
+ {
173
+ type: 'paragraph',
174
+ content: [
175
+ {
176
+ type: 'text',
177
+ text: 'x',
178
+ marks: [
179
+ { type: 'bold' },
180
+ { type: 'bold' },
181
+ { type: 'blink' },
182
+ { type: 'italic', attrs: { junk: 1 } },
183
+ ],
184
+ },
185
+ ],
186
+ },
187
+ ]),
188
+ );
189
+ expect((output.content[0].content?.[0] as NoteInlineNode).marks).toEqual([
190
+ { type: 'bold' },
191
+ { type: 'italic' },
192
+ ]);
193
+ });
194
+
195
+ it('validates color/highlight tokens against the palette', () => {
196
+ const output = sanitizeNoteDoc(
197
+ buildDoc([
198
+ {
199
+ type: 'paragraph',
200
+ content: [
201
+ {
202
+ type: 'text',
203
+ text: 'x',
204
+ marks: [
205
+ { type: 'color', attrs: { token: 'red' } },
206
+ { type: 'highlight', attrs: { token: '#ff0000' } },
207
+ ],
208
+ },
209
+ ],
210
+ },
211
+ ]),
212
+ );
213
+ expect((output.content[0].content?.[0] as NoteInlineNode).marks).toEqual([
214
+ { type: 'color', attrs: { token: 'red' } },
215
+ ]);
216
+ });
217
+
218
+ it('keeps http(s)/mailto links and kills javascript: URLs', () => {
219
+ const output = sanitizeNoteDoc(
220
+ buildDoc([
221
+ {
222
+ type: 'paragraph',
223
+ content: [
224
+ {
225
+ type: 'text',
226
+ text: 'a',
227
+ marks: [{ type: 'link', attrs: { href: 'https://example.com' } }],
228
+ },
229
+ {
230
+ type: 'text',
231
+ text: 'b',
232
+ marks: [{ type: 'link', attrs: { href: 'mailto:x@y.com' } }],
233
+ },
234
+ {
235
+ type: 'text',
236
+ text: 'c',
237
+ marks: [
238
+ { type: 'link', attrs: { href: ' HTTP://example.com ' } },
239
+ ],
240
+ },
241
+ {
242
+ type: 'text',
243
+ text: 'd',
244
+ marks: [{ type: 'link', attrs: { href: 'javascript:alert(1)' } }],
245
+ },
246
+ {
247
+ type: 'text',
248
+ text: 'e',
249
+ marks: [
250
+ { type: 'link', attrs: { href: ' JaVaScRiPt:alert(1)' } },
251
+ ],
252
+ },
253
+ {
254
+ type: 'text',
255
+ text: 'f',
256
+ marks: [{ type: 'link', attrs: { href: 'data:text/html,x' } }],
257
+ },
258
+ ],
259
+ },
260
+ ]),
261
+ );
262
+ const runs = output.content[0].content as NoteInlineNode[];
263
+ expect(runs[0].marks).toEqual([
264
+ { type: 'link', attrs: { href: 'https://example.com' } },
265
+ ]);
266
+ expect(runs[1].marks).toEqual([
267
+ { type: 'link', attrs: { href: 'mailto:x@y.com' } },
268
+ ]);
269
+ expect(runs[2].marks).toEqual([
270
+ { type: 'link', attrs: { href: 'HTTP://example.com' } },
271
+ ]);
272
+ // The three unsafe runs lose their link mark and merge into one run
273
+ expect(runs[3]).toEqual({ type: 'text', text: 'def' });
274
+ expect(runs).toHaveLength(4);
275
+ });
276
+
277
+ it('drops empty text runs and merges adjacent runs with identical mark sets', () => {
278
+ const output = sanitizeNoteDoc(
279
+ buildDoc([
280
+ {
281
+ type: 'paragraph',
282
+ content: [
283
+ { type: 'text', text: '' },
284
+ {
285
+ type: 'text',
286
+ text: 'a',
287
+ marks: [{ type: 'bold' }, { type: 'italic' }],
288
+ },
289
+ {
290
+ type: 'text',
291
+ text: 'b',
292
+ marks: [{ type: 'italic' }, { type: 'bold' }],
293
+ },
294
+ { type: 'text', text: 'c', marks: [{ type: 'bold' }] },
295
+ { type: 'hardBreak' },
296
+ { type: 'text', text: 'd', marks: [{ type: 'bold' }] },
297
+ ],
298
+ },
299
+ ]),
300
+ );
301
+ expect(output.content[0].content).toEqual([
302
+ {
303
+ type: 'text',
304
+ text: 'ab',
305
+ marks: [{ type: 'bold' }, { type: 'italic' }],
306
+ },
307
+ { type: 'text', text: 'c', marks: [{ type: 'bold' }] },
308
+ { type: 'hardBreak' },
309
+ { type: 'text', text: 'd', marks: [{ type: 'bold' }] },
310
+ ]);
311
+ });
312
+
313
+ it('drops image blocks missing storageId or fileUri and coerces numeric ids', () => {
314
+ const output = sanitizeNoteDoc(
315
+ buildDoc([
316
+ { type: 'image', attrs: { fileUri: 'uploads/no-id.png' } },
317
+ { type: 'image', attrs: { storageId: '3' } },
318
+ buildImage(7, { width: '300', alt: '' }),
319
+ ]),
320
+ );
321
+ expect(output.content).toEqual([
322
+ {
323
+ type: 'image',
324
+ attrs: { storageId: '7', fileUri: 'uploads/img-7.png' },
325
+ },
326
+ ]);
327
+ });
328
+
329
+ it('wraps stray text blocks and inline runs found inside lists / at doc level', () => {
330
+ const output = sanitizeNoteDoc(
331
+ buildDoc([
332
+ { type: 'text', text: 'stray' },
333
+ {
334
+ type: 'bulletList',
335
+ content: [
336
+ buildParagraph('loose'),
337
+ {
338
+ type: 'heading',
339
+ attrs: { level: 1 },
340
+ content: [{ type: 'text', text: 'head' }],
341
+ },
342
+ ],
343
+ },
344
+ ]),
345
+ );
346
+ expect(output.content).toEqual([
347
+ { type: 'paragraph', content: [{ type: 'text', text: 'stray' }] },
348
+ {
349
+ type: 'bulletList',
350
+ content: [
351
+ {
352
+ type: 'listItem',
353
+ content: [
354
+ { type: 'paragraph', content: [{ type: 'text', text: 'loose' }] },
355
+ ],
356
+ },
357
+ {
358
+ type: 'listItem',
359
+ content: [
360
+ { type: 'paragraph', content: [{ type: 'text', text: 'head' }] },
361
+ ],
362
+ },
363
+ ],
364
+ },
365
+ ]);
366
+ });
367
+
368
+ it('unwraps illegal containers instead of keeping bad nesting', () => {
369
+ const output = sanitizeNoteDoc(
370
+ buildDoc([
371
+ // listItem directly under doc splices its children in
372
+ { type: 'listItem', content: [buildParagraph('lifted')] },
373
+ // lists and headings inside a blockquote flatten to paragraphs
374
+ {
375
+ type: 'blockquote',
376
+ content: [
377
+ {
378
+ type: 'heading',
379
+ attrs: { level: 2 },
380
+ content: [{ type: 'text', text: 'h' }],
381
+ },
382
+ {
383
+ type: 'bulletList',
384
+ content: [{ type: 'listItem', content: [buildParagraph('li')] }],
385
+ },
386
+ ],
387
+ },
388
+ ]),
389
+ );
390
+ expect(output.content).toEqual([
391
+ { type: 'paragraph', content: [{ type: 'text', text: 'lifted' }] },
392
+ {
393
+ type: 'blockquote',
394
+ content: [
395
+ { type: 'paragraph', content: [{ type: 'text', text: 'h' }] },
396
+ { type: 'paragraph', content: [{ type: 'text', text: 'li' }] },
397
+ ],
398
+ },
399
+ ]);
400
+ });
401
+
402
+ it('drops void leaves with no legal spot and lists left without items', () => {
403
+ const output = sanitizeNoteDoc(
404
+ buildDoc([
405
+ { type: 'bulletList', content: [{ type: 'divider' }, buildImage('4')] },
406
+ buildParagraph('kept'),
407
+ ]),
408
+ );
409
+ expect(output.content).toEqual([
410
+ { type: 'paragraph', content: [{ type: 'text', text: 'kept' }] },
411
+ ]);
412
+ });
413
+
414
+ it('fills emptied listItems and blockquotes with one empty paragraph', () => {
415
+ const output = sanitizeNoteDoc(
416
+ buildDoc([
417
+ {
418
+ type: 'bulletList',
419
+ content: [{ type: 'listItem', content: [{ type: 'divider' }] }],
420
+ },
421
+ { type: 'blockquote', content: [] },
422
+ ]),
423
+ );
424
+ expect(output.content).toEqual([
425
+ {
426
+ type: 'bulletList',
427
+ content: [
428
+ { type: 'listItem', content: [{ type: 'paragraph', content: [] }] },
429
+ ],
430
+ },
431
+ { type: 'blockquote', content: [{ type: 'paragraph', content: [] }] },
432
+ ]);
433
+ });
434
+
435
+ it('flattens lists nested past NOTE_MAX_DEPTH into text-preserving paragraphs', () => {
436
+ // list(1) > li(2) > list(3) > li(4) > list(5) > li(6): the depth-6
437
+ // container flattens into one paragraph, its nested text joined by
438
+ // hard breaks, wrapped back into the depth-5 list's listItem
439
+ const overDeepItem = {
440
+ type: 'listItem',
441
+ content: [
442
+ buildParagraph('a'),
443
+ {
444
+ type: 'bulletList',
445
+ content: [{ type: 'listItem', content: [buildParagraph('b')] }],
446
+ },
447
+ ],
448
+ };
449
+ const output = sanitizeNoteDoc(
450
+ buildDoc([
451
+ {
452
+ type: 'bulletList',
453
+ content: [
454
+ {
455
+ type: 'listItem',
456
+ content: [
457
+ {
458
+ type: 'bulletList',
459
+ content: [
460
+ {
461
+ type: 'listItem',
462
+ content: [
463
+ { type: 'bulletList', content: [overDeepItem] },
464
+ ],
465
+ },
466
+ ],
467
+ },
468
+ ],
469
+ },
470
+ ],
471
+ },
472
+ ]),
473
+ );
474
+
475
+ const level3List = (output.content[0] as any).content[0].content[0]
476
+ .content[0].content[0] as NoteBlockNode;
477
+ expect(level3List.type).toBe('bulletList');
478
+ expect(level3List.content).toEqual([
479
+ {
480
+ type: 'listItem',
481
+ content: [
482
+ {
483
+ type: 'paragraph',
484
+ content: [
485
+ { type: 'text', text: 'a' },
486
+ { type: 'hardBreak' },
487
+ { type: 'text', text: 'b' },
488
+ ],
489
+ },
490
+ ],
491
+ },
492
+ ]);
493
+ });
494
+
495
+ it('truncates the tail past NOTE_MAX_BLOCKS', () => {
496
+ const output = sanitizeNoteDoc(
497
+ buildDoc(
498
+ Array.from({ length: NOTE_MAX_BLOCKS + 50 }, (_, i) =>
499
+ buildParagraph('p' + i),
500
+ ),
501
+ ),
502
+ );
503
+ expect(output.content).toHaveLength(NOTE_MAX_BLOCKS);
504
+ expect(
505
+ (output.content[NOTE_MAX_BLOCKS - 1].content?.[0] as NoteInlineNode).text,
506
+ ).toBe('p' + (NOTE_MAX_BLOCKS - 1));
507
+ });
508
+
509
+ it('truncates text past NOTE_MAX_TEXT_CHARS', () => {
510
+ const output = sanitizeNoteDoc(
511
+ buildDoc([
512
+ buildParagraph('x'.repeat(NOTE_MAX_TEXT_CHARS - 5)),
513
+ buildParagraph('y'.repeat(50)),
514
+ buildParagraph('z'),
515
+ ]),
516
+ );
517
+ expect(
518
+ (output.content[0].content?.[0] as NoteInlineNode).text,
519
+ ).toHaveLength(NOTE_MAX_TEXT_CHARS - 5);
520
+ // Second paragraph keeps only the 5 chars left in the budget
521
+ expect((output.content[1].content?.[0] as NoteInlineNode).text).toBe(
522
+ 'yyyyy',
523
+ );
524
+ // Later blocks survive with their text dropped
525
+ expect(output.content[2]).toEqual({ type: 'paragraph', content: [] });
526
+ });
527
+
528
+ it('spends the text budget on hardBreaks so a hardBreak flood truncates', () => {
529
+ const output = sanitizeNoteDoc(
530
+ buildDoc([
531
+ {
532
+ type: 'paragraph',
533
+ content: Array.from({ length: NOTE_MAX_TEXT_CHARS + 5000 }, () => ({
534
+ type: 'hardBreak',
535
+ })),
536
+ },
537
+ ]),
538
+ );
539
+ // Only NOTE_MAX_TEXT_CHARS hardBreaks survive (1 char each)
540
+ expect(output.content[0].content).toHaveLength(NOTE_MAX_TEXT_CHARS);
541
+ // The spent budget also blocks text in later blocks
542
+ const after = sanitizeNoteDoc(
543
+ buildDoc([
544
+ {
545
+ type: 'paragraph',
546
+ content: Array.from({ length: NOTE_MAX_TEXT_CHARS }, () => ({
547
+ type: 'hardBreak',
548
+ })),
549
+ },
550
+ buildParagraph('overflow'),
551
+ ]),
552
+ );
553
+ expect(after.content[1]).toEqual({ type: 'paragraph', content: [] });
554
+ });
555
+
556
+ it('survives hostile deeply-nested input without overflowing', () => {
557
+ // 500 levels of listItem-in-listItem raw nesting (every level illegal)
558
+ let node: any = buildParagraph('deep');
559
+ for (let i = 0; i < 500; i++) {
560
+ node = { type: 'listItem', content: [node] };
561
+ }
562
+ const output = sanitizeNoteDoc(buildDoc([node]));
563
+ expect(output.type).toBe('doc');
564
+ expect(output.content.length).toBeGreaterThan(0);
565
+ });
566
+ });
567
+
568
+ describe('filterImageNodesByStorageRels', () => {
569
+ it('keeps only image blocks whose storageId has a rel row', () => {
570
+ const doc = sanitizeNoteDoc(
571
+ buildDoc([buildParagraph('text'), buildImage('1'), buildImage('2')]),
572
+ );
573
+ const output = filterImageNodesByStorageRels(doc, new Set(['2']));
574
+ expect(output.content).toEqual([
575
+ { type: 'paragraph', content: [{ type: 'text', text: 'text' }] },
576
+ {
577
+ type: 'image',
578
+ attrs: { storageId: '2', fileUri: 'uploads/img-2.png' },
579
+ },
580
+ ]);
581
+ // Pure: the input doc is untouched
582
+ expect(doc.content).toHaveLength(3);
583
+ });
584
+
585
+ it('never leaves the doc empty', () => {
586
+ const doc = sanitizeNoteDoc(buildDoc([buildImage('1')]));
587
+ const output = filterImageNodesByStorageRels(doc, new Set());
588
+ expect(output.content).toEqual([{ type: 'paragraph', content: [] }]);
589
+ });
590
+
591
+ it('string-compares storageIds', () => {
592
+ const doc: NoteDoc = {
593
+ type: 'doc',
594
+ version: NOTE_DOC_VERSION,
595
+ // Bypass sanitize to simulate a legacy numeric storageId
596
+ content: [
597
+ {
598
+ type: 'image',
599
+ attrs: { storageId: 7 as any, fileUri: 'uploads/x.png' },
600
+ },
601
+ ],
602
+ };
603
+ expect(
604
+ filterImageNodesByStorageRels(doc, new Set(['7'])).content[0].type,
605
+ ).toBe('image');
606
+ expect(
607
+ filterImageNodesByStorageRels(doc, new Set(['8'])).content[0].type,
608
+ ).toBe('paragraph');
609
+ });
610
+ });
611
+
612
+ describe('collectNoteImageStorageIds', () => {
613
+ it('collects image storage ids in document order, deduped and stringified', () => {
614
+ const doc: NoteDoc = {
615
+ type: 'doc',
616
+ version: NOTE_DOC_VERSION,
617
+ content: [
618
+ buildImage('5'),
619
+ buildParagraph('x'),
620
+ buildImage(3),
621
+ buildImage('5'),
622
+ ],
623
+ };
624
+ expect(collectNoteImageStorageIds(doc)).toEqual(['5', '3']);
625
+ });
626
+
627
+ it('returns an empty list for docs without images', () => {
628
+ expect(collectNoteImageStorageIds(createEmptyNoteDoc())).toEqual([]);
629
+ });
630
+ });
631
+
632
+ describe('noteDocToPlainText', () => {
633
+ it('renders one line per text-bearing block, with hardBreaks as newlines', () => {
634
+ const doc = sanitizeNoteDoc(
635
+ buildDoc([
636
+ {
637
+ type: 'heading',
638
+ attrs: { level: 1 },
639
+ content: [{ type: 'text', text: 'Title' }],
640
+ },
641
+ {
642
+ type: 'paragraph',
643
+ content: [
644
+ { type: 'text', text: 'line one' },
645
+ { type: 'hardBreak' },
646
+ { type: 'text', text: 'line two' },
647
+ ],
648
+ },
649
+ { type: 'divider' },
650
+ buildImage('1'),
651
+ {
652
+ type: 'bulletList',
653
+ content: [
654
+ { type: 'listItem', content: [buildParagraph('item a')] },
655
+ { type: 'listItem', content: [buildParagraph('item b')] },
656
+ ],
657
+ },
658
+ { type: 'paragraph', content: [] },
659
+ ]),
660
+ );
661
+ expect(noteDocToPlainText(doc)).toBe(
662
+ 'Title\nline one\nline two\nitem a\nitem b',
663
+ );
664
+ });
665
+
666
+ it('returns an empty string for an empty doc', () => {
667
+ expect(noteDocToPlainText(createEmptyNoteDoc())).toBe('');
668
+ });
669
+ });