@bevel-software/platform-core-frontend 0.11.1 → 0.12.0

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 (50) hide show
  1. package/THIRD-PARTY-NOTICES.md +670 -395
  2. package/package.json +6 -2
  3. package/src/lib/__tests__/api.test.ts +95 -0
  4. package/src/lib/api.ts +30 -2
  5. package/src/lib/clipboard.ts +25 -0
  6. package/src/modules/access/__tests__/useFileAccess.test.tsx +47 -0
  7. package/src/modules/access/hooks/useFileAccess.ts +23 -5
  8. package/src/modules/library/components/tool-page/McpServerSection.tsx +1 -1
  9. package/src/modules/library/utils/clipboard.ts +4 -20
  10. package/src/modules/onboarding/__tests__/creator-welcome.test.tsx +16 -7
  11. package/src/modules/onboarding/__tests__/onboarding.test.tsx +1 -1
  12. package/src/modules/toolbar/components/__tests__/ExternalAgentAccessPage.test.tsx +7 -7
  13. package/src/modules/workspace/components/FileExplorer.tsx +6 -14
  14. package/src/modules/workspace/components/FileViewer.tsx +17 -4
  15. package/src/modules/workspace/components/KbPageHeader.tsx +1 -1
  16. package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +3 -1
  17. package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +17 -0
  18. package/src/modules/workspace/components/__tests__/KbPageHeader.test.tsx +15 -0
  19. package/src/modules/workspace/components/renderers/DocxRenderer.tsx +11 -2
  20. package/src/modules/workspace/components/renderers/DownloadFileButton.tsx +88 -0
  21. package/src/modules/workspace/components/renderers/EmailRenderer.tsx +309 -0
  22. package/src/modules/workspace/components/renderers/LegacyOfficeRenderer.tsx +60 -0
  23. package/src/modules/workspace/components/renderers/PdfRenderer.tsx +261 -33
  24. package/src/modules/workspace/components/renderers/PptxRenderer.tsx +184 -0
  25. package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +195 -45
  26. package/src/modules/workspace/components/renderers/__tests__/DownloadFileButton.test.tsx +48 -0
  27. package/src/modules/workspace/components/renderers/__tests__/EmailRenderer.test.tsx +176 -0
  28. package/src/modules/workspace/components/renderers/__tests__/PdfRenderer.test.tsx +214 -76
  29. package/src/modules/workspace/components/renderers/__tests__/PptxRenderer.test.tsx +125 -0
  30. package/src/modules/workspace/components/renderers/__tests__/XlsxRenderer.test.tsx +245 -0
  31. package/src/modules/workspace/components/renderers/__tests__/emailBody.test.ts +117 -0
  32. package/src/modules/workspace/components/renderers/__tests__/emailMessage.test.ts +404 -0
  33. package/src/modules/workspace/components/renderers/__tests__/pptxOutline.test.ts +448 -0
  34. package/src/modules/workspace/components/renderers/__tests__/readBodyCapped.test.ts +117 -0
  35. package/src/modules/workspace/components/renderers/__tests__/rendererLayout.test.ts +73 -7
  36. package/src/modules/workspace/components/renderers/__tests__/rendererSelection.test.tsx +107 -0
  37. package/src/modules/workspace/components/renderers/downloadFile.ts +33 -0
  38. package/src/modules/workspace/components/renderers/emailBody.ts +168 -0
  39. package/src/modules/workspace/components/renderers/emailMessage.ts +223 -0
  40. package/src/modules/workspace/components/renderers/emlMessage.ts +91 -0
  41. package/src/modules/workspace/components/renderers/htmlSandbox.ts +9 -2
  42. package/src/modules/workspace/components/renderers/index.ts +82 -6
  43. package/src/modules/workspace/components/renderers/lazyRenderer.tsx +9 -2
  44. package/src/modules/workspace/components/renderers/msgMessage.ts +219 -0
  45. package/src/modules/workspace/components/renderers/pptxOutline.ts +317 -0
  46. package/src/modules/workspace/components/renderers/readBodyCapped.ts +97 -0
  47. package/src/modules/workspace/components/renderers/xmlReading.ts +0 -0
  48. package/src/shared/mcp/__tests__/connect-snippets.test.ts +8 -8
  49. package/src/shared/mcp/connect-snippets.ts +4 -4
  50. package/src/url-suffix-imports.d.ts +12 -0
@@ -0,0 +1,404 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { fileURLToPath } from 'node:url';
3
+ import { describe, it, expect } from 'vitest';
4
+ import * as XLSX from 'xlsx';
5
+ import {
6
+ MAX_INLINE_IMAGE_BYTES,
7
+ MAX_INLINE_IMAGE_TOTAL_BYTES,
8
+ attachmentLine,
9
+ inlineBudgeted,
10
+ mailboxText,
11
+ referencedContentIds,
12
+ type EmailAttachmentView,
13
+ } from '../emailMessage';
14
+ import { parseEmlMessage } from '../emlMessage';
15
+ import { parseMsgMessage } from '../msgMessage';
16
+
17
+ /**
18
+ * The two client-side email parsers against REAL bytes — hand-written MIME
19
+ * for `.eml`, real CFB containers (built with SheetJS's CFB writer, the same
20
+ * library the parser reads them with) for `.msg`. The rendering of the model
21
+ * is `EmailRenderer.test.tsx`'s job; this suite pins the model itself.
22
+ */
23
+
24
+ // ── fixture builders ───────────────────────────────────────────────────────
25
+
26
+ const emlBytes = (lines: string[]): ArrayBuffer =>
27
+ new TextEncoder().encode(lines.join('\r\n')).buffer as ArrayBuffer;
28
+
29
+ const utf16 = (s: string): Uint8Array => {
30
+ const out = new Uint8Array(s.length * 2);
31
+ for (let i = 0; i < s.length; i++) {
32
+ const c = s.charCodeAt(i);
33
+ out[i * 2] = c & 0xff;
34
+ out[i * 2 + 1] = c >> 8;
35
+ }
36
+ return out;
37
+ };
38
+
39
+ interface CfbUtilsModule {
40
+ utils: { cfb_new(): unknown; cfb_add(cfb: unknown, path: string, bytes: Uint8Array): void };
41
+ write(cfb: unknown, opts: { type: 'array' }): Uint8Array | number[];
42
+ }
43
+
44
+ /** A MAPI properties stream: header zeros + 16-byte [tag, flags, value] records. */
45
+ function propertiesStream(headerLen: number, records: Array<[number, Uint8Array]>): Uint8Array {
46
+ const out = new Uint8Array(headerLen + records.length * 16);
47
+ records.forEach(([tag, value], i) => {
48
+ const dv = new DataView(out.buffer, headerLen + i * 16, 16);
49
+ dv.setUint32(0, tag, true);
50
+ out.set(value.subarray(0, 8), headerLen + i * 16 + 8);
51
+ });
52
+ return out;
53
+ }
54
+
55
+ const longValue = (n: number): Uint8Array => {
56
+ const b = new Uint8Array(8);
57
+ new DataView(b.buffer).setUint32(0, n, true);
58
+ return b;
59
+ };
60
+
61
+ function filetimeValue(iso: string): Uint8Array {
62
+ const b = new Uint8Array(8);
63
+ new DataView(b.buffer).setBigUint64(0, (BigInt(Date.parse(iso)) + 11644473600000n) * 10000n, true);
64
+ return b;
65
+ }
66
+
67
+ /** REAL `.msg` bytes: a CFB container carrying the MAPI streams Outlook writes. */
68
+ function msgBytes(
69
+ streams: Record<string, Uint8Array>, // path under the root → bytes
70
+ ): ArrayBuffer {
71
+ const CFB = XLSX.CFB as unknown as CfbUtilsModule;
72
+ const cfb = CFB.utils.cfb_new();
73
+ for (const [path, bytes] of Object.entries(streams)) CFB.utils.cfb_add(cfb, `/${path}`, bytes);
74
+ const written = CFB.write(cfb, { type: 'array' });
75
+ const u8 = written instanceof Uint8Array ? written : Uint8Array.from(written);
76
+ return u8.slice().buffer as ArrayBuffer;
77
+ }
78
+
79
+ // ── .eml ───────────────────────────────────────────────────────────────────
80
+
81
+ describe('parseEmlMessage', () => {
82
+ it('parses headers, prefers the plain-text part, and lists attachments with type and size', async () => {
83
+ const view = await parseEmlMessage(
84
+ emlBytes([
85
+ 'From: Ada Lovelace <ada@example.com>',
86
+ 'To: Bob <bob@example.com>, carol@example.com',
87
+ 'Cc: Dan <dan@example.com>',
88
+ 'Subject: Quarterly numbers',
89
+ 'Date: Mon, 5 Jan 2026 10:00:00 +0000',
90
+ 'MIME-Version: 1.0',
91
+ 'Content-Type: multipart/mixed; boundary="b1"',
92
+ '',
93
+ '--b1',
94
+ 'Content-Type: multipart/alternative; boundary="b2"',
95
+ '',
96
+ '--b2',
97
+ 'Content-Type: text/plain; charset=utf-8',
98
+ '',
99
+ 'Please see attached.',
100
+ '--b2',
101
+ 'Content-Type: text/html; charset=utf-8',
102
+ '',
103
+ '<p>Please see <b>attached</b>.</p>',
104
+ '--b2--',
105
+ '--b1',
106
+ 'Content-Type: application/pdf; name="report.pdf"',
107
+ 'Content-Disposition: attachment; filename="report.pdf"',
108
+ 'Content-Transfer-Encoding: base64',
109
+ '',
110
+ 'JVBERi0xLjQ=',
111
+ '--b1--',
112
+ '',
113
+ ]),
114
+ );
115
+ expect(view.from).toBe('Ada Lovelace <ada@example.com>');
116
+ expect(view.to).toBe('Bob <bob@example.com>, carol@example.com');
117
+ expect(view.cc).toBe('Dan <dan@example.com>');
118
+ expect(view.subject).toBe('Quarterly numbers');
119
+ expect(view.date).toBe('2026-01-05T10:00:00.000Z');
120
+ expect(view.body).toBe('Please see attached.');
121
+ expect(view.bodySource).toBe('text');
122
+ expect(view.attachments).toEqual([{ name: 'report.pdf', mimeType: 'application/pdf', sizeBytes: 8 }]);
123
+ });
124
+
125
+ it('strips an HTML-only body to text and says so through bodySource', async () => {
126
+ const view = await parseEmlMessage(
127
+ emlBytes([
128
+ 'From: ada@example.com',
129
+ 'Subject: styled',
130
+ 'Content-Type: text/html; charset=utf-8',
131
+ '',
132
+ '<style>p{color:red}</style><h1>Title</h1><p>Para&nbsp;one &amp; two</p>',
133
+ ]),
134
+ );
135
+ expect(view.body).toBe('Title\n\nPara one & two');
136
+ expect(view.bodySource).toBe('html');
137
+ });
138
+
139
+ it('leaves absent headers undefined instead of inventing empty ones', async () => {
140
+ const view = await parseEmlMessage(
141
+ emlBytes(['From: ada@example.com', 'Subject: no recipients on file', '', 'draft body']),
142
+ );
143
+ expect(view.to).toBeUndefined();
144
+ expect(view.cc).toBeUndefined();
145
+ expect(view.date).toBeUndefined();
146
+ expect(view.body).toBe('draft body');
147
+ });
148
+
149
+ it('throws the could-not-be-parsed error for bytes with no email headers', async () => {
150
+ await expect(
151
+ parseEmlMessage(new Uint8Array([0xd0, 0xcf, 0x11, 0xe0, 0x01, 0x02, 0xff, 0xfe]).buffer as ArrayBuffer),
152
+ ).rejects.toThrow('could not be parsed as a .eml (no email headers found)');
153
+ });
154
+
155
+ it('a valid email whose ONLY header is Bcc: is recognized, not rejected as header-less', async () => {
156
+ const view = await parseEmlMessage(emlBytes(['Bcc: Eve <eve@example.com>', '', 'quiet copy']));
157
+ expect(view.bcc).toBe('Eve <eve@example.com>');
158
+ expect(view.body).toBe('quiet copy');
159
+ });
160
+ });
161
+
162
+ // ── .msg ───────────────────────────────────────────────────────────────────
163
+
164
+ describe('parseMsgMessage', () => {
165
+ it('parses sender, typed recipients, subject, date, body and attachments from real CFB bytes', () => {
166
+ const view = parseMsgMessage(
167
+ msgBytes({
168
+ '__substg1.0_0037001F': utf16('Quarterly numbers'),
169
+ '__substg1.0_0C1A001F': utf16('Ada Lovelace'),
170
+ '__substg1.0_0C1F001F': utf16('ada@example.com'),
171
+ '__substg1.0_1000001F': utf16('Please see attached.'),
172
+ '__recip_version1.0_#00000000/__substg1.0_3001001F': utf16('Bob'),
173
+ '__recip_version1.0_#00000000/__substg1.0_3003001F': utf16('bob@example.com'),
174
+ '__recip_version1.0_#00000000/__properties_version1.0': propertiesStream(8, [[0x0c150003, longValue(1)]]),
175
+ '__recip_version1.0_#00000001/__substg1.0_3001001F': utf16('Dan'),
176
+ '__recip_version1.0_#00000001/__substg1.0_3003001F': utf16('dan@example.com'),
177
+ '__recip_version1.0_#00000001/__properties_version1.0': propertiesStream(8, [[0x0c150003, longValue(2)]]),
178
+ '__attach_version1.0_#00000000/__substg1.0_3707001F': utf16('report.pdf'),
179
+ '__attach_version1.0_#00000000/__substg1.0_370E001F': utf16('application/pdf'),
180
+ '__attach_version1.0_#00000000/__substg1.0_37010102': new TextEncoder().encode('PDFDATA'),
181
+ '__properties_version1.0': propertiesStream(32, [[0x00390040, filetimeValue('2026-01-05T10:00:00Z')]]),
182
+ }),
183
+ );
184
+ expect(view.from).toBe('Ada Lovelace <ada@example.com>');
185
+ expect(view.to).toBe('Bob <bob@example.com>');
186
+ expect(view.cc).toBe('Dan <dan@example.com>');
187
+ expect(view.subject).toBe('Quarterly numbers');
188
+ expect(view.date).toBe('2026-01-05T10:00:00.000Z');
189
+ expect(view.body).toBe('Please see attached.');
190
+ expect(view.bodySource).toBe('text');
191
+ expect(view.attachments).toEqual([{ name: 'report.pdf', mimeType: 'application/pdf', sizeBytes: 7 }]);
192
+ });
193
+
194
+ it('strips an HTML-only body, and reports an RTF-only body honestly', () => {
195
+ const html = parseMsgMessage(
196
+ msgBytes({ '__substg1.0_1013001F': utf16('<p>Hello <b>there</b></p><div>bye</div>') }),
197
+ );
198
+ expect(html.body).toBe('Hello there\n\nbye');
199
+ expect(html.bodySource).toBe('html');
200
+
201
+ const rtf = parseMsgMessage(
202
+ msgBytes({
203
+ '__substg1.0_0037001F': utf16('compressed'),
204
+ '__substg1.0_10090102': new Uint8Array([1, 2, 3, 4]),
205
+ }),
206
+ );
207
+ expect(rtf.body).toBe('');
208
+ expect(rtf.bodySource).toBe('rtf-only');
209
+ });
210
+
211
+ it('keeps a recipient whose PidTagRecipientType carries MAPI flag bits', () => {
212
+ // The raw PT_LONG may be MAPI_P1 (0x10000000) | the base type on a
213
+ // resubmitted message. Masking the flag bits off is what the backend's
214
+ // `extract-msg.ts` `recipientBucket` does, so the viewer and an agent's
215
+ // `read_file` file the same recipient under the same header.
216
+ const view = parseMsgMessage(
217
+ msgBytes({
218
+ '__substg1.0_0037001F': utf16('resubmitted'),
219
+ '__recip_version1.0_#00000000/__substg1.0_3001001F': utf16('Bob'),
220
+ '__recip_version1.0_#00000000/__substg1.0_3003001F': utf16('bob@example.com'),
221
+ '__recip_version1.0_#00000000/__properties_version1.0': propertiesStream(8, [
222
+ [0x0c150003, longValue(0x10000001)],
223
+ ]),
224
+ '__recip_version1.0_#00000001/__substg1.0_3001001F': utf16('Dan'),
225
+ '__recip_version1.0_#00000001/__substg1.0_3003001F': utf16('dan@example.com'),
226
+ '__recip_version1.0_#00000001/__properties_version1.0': propertiesStream(8, [
227
+ [0x0c150003, longValue(0x10000002)],
228
+ ]),
229
+ '__recip_version1.0_#00000002/__substg1.0_3001001F': utf16('Eve'),
230
+ '__recip_version1.0_#00000002/__substg1.0_3003001F': utf16('eve@example.com'),
231
+ '__recip_version1.0_#00000002/__properties_version1.0': propertiesStream(8, [
232
+ [0x0c150003, longValue(0x10000003)],
233
+ ]),
234
+ }),
235
+ );
236
+ expect(view.to).toBe('Bob <bob@example.com>');
237
+ expect(view.cc).toBe('Dan <dan@example.com>');
238
+ expect(view.bcc).toBe('Eve <eve@example.com>');
239
+ });
240
+
241
+ it('decodes ANSI (001E) string properties when the unicode variant is absent', () => {
242
+ const view = parseMsgMessage(
243
+ msgBytes({ '__substg1.0_0037001E': new TextEncoder().encode('plain ansi subject') }),
244
+ );
245
+ expect(view.subject).toBe('plain ansi subject');
246
+ });
247
+
248
+ it('throws the could-not-be-parsed error for garbage bytes and for a CFB with no MAPI streams', () => {
249
+ expect(() =>
250
+ parseMsgMessage(new TextEncoder().encode('total garbage, not a CFB container').buffer as ArrayBuffer),
251
+ ).toThrow('could not be parsed as a .msg');
252
+ expect(() => parseMsgMessage(msgBytes({ SomeStream: utf16('x') }))).toThrow(
253
+ 'could not be parsed as a .msg (not an Outlook message file)',
254
+ );
255
+ });
256
+ });
257
+
258
+ // ── shared helpers ─────────────────────────────────────────────────────────
259
+
260
+ describe('an HTML body, as the viewer renders it', () => {
261
+ /** The body text of an HTML-only message, through the parser the viewer uses. */
262
+ const bodyOf = async (html: string): Promise<string> => {
263
+ const view = await parseEmlMessage(
264
+ emlBytes(['From: ada@example.com', 'Content-Type: text/html; charset=utf-8', '', html]),
265
+ );
266
+ return view.body;
267
+ };
268
+
269
+ it('drops style/script/head whole and keeps paragraph structure', async () => {
270
+ expect(await bodyOf('<style>b{}</style><script>x()</script><p>one</p><p></p><p>two &#8212; three</p>')).toBe(
271
+ 'one\n\ntwo — three',
272
+ );
273
+ expect(await bodyOf('<SCRIPT TYPE="text/javascript">evil()</SCRIPT>after')).toBe('after');
274
+ expect(await bodyOf('<Style Media="print">b{}</STYLE>rest')).toBe('rest');
275
+ expect(await bodyOf('<head><TITLE>t</TITLE></head>body')).toBe('body');
276
+ });
277
+
278
+ it('never truncates a tag at a > inside a quoted attribute value', async () => {
279
+ expect(await bodyOf('<a title="a > b">x</a>')).toBe('x');
280
+ expect(await bodyOf("<p align='x>y'>para</p>")).toBe('para');
281
+ expect(await bodyOf('<br data-note="1 > 0"/>line')).toBe('line');
282
+ expect(await bodyOf('<div class="a>b">block</div>')).toBe('block');
283
+ expect(await bodyOf('<style media="x>y">b{}</style>rest')).toBe('rest');
284
+ });
285
+
286
+ it('turns br and block-tag boundaries into line breaks', async () => {
287
+ expect(await bodyOf('a<br>b<br/>c<BR />d')).toBe('a\nb\nc\nd');
288
+ expect(await bodyOf('<h1>T</h1><div>d</div><li>i</li>')).toBe('T\n\nd\n\ni');
289
+ });
290
+
291
+ it('keeps a comparison in the body: `2 < 3` is text, not a tag', async () => {
292
+ expect(await bodyOf('<p>total</p> 2 < 3')).toBe('total\n2 < 3');
293
+ });
294
+
295
+ it('CHANGED: reads malformed markup the way a mail client would', async () => {
296
+ // These are htmlparser2's answers, which are a browser's — the useful
297
+ // standard for a mail body, because it is what the sender's recipient saw.
298
+ // The hand-rolled strip had rules of its own and they differed here.
299
+ expect(await bodyOf('<a href="never closed>tail')).toBe('');
300
+ expect(await bodyOf('<script>alert(1)')).toBe('');
301
+ expect(await bodyOf('<script/x>secret</script>tail')).toBe('tail');
302
+ expect(await bodyOf('x </ 2 > y')).toBe('x y');
303
+ });
304
+
305
+ it('survives an adversarial body: 20k < plus an attribute quote that never closes', async () => {
306
+ const bomb = '<'.repeat(20_000) + '<b title="never closed ' + 'x'.repeat(2_000) + '>';
307
+ const t0 = performance.now();
308
+ const out = await bodyOf(bomb);
309
+ const ms = performance.now() - t0;
310
+ expect(out).toBe('<'.repeat(20_000));
311
+ expect(ms).toBeLessThan(5_000);
312
+ });
313
+
314
+ it('survives a body nested past any sane depth, keeping what it read', async () => {
315
+ const t0 = performance.now();
316
+ const out = await bodyOf('<p>top</p>' + '<div>'.repeat(200_000));
317
+ const ms = performance.now() - t0;
318
+ expect(out.startsWith('top')).toBe(true);
319
+ expect(ms).toBeLessThan(5_000);
320
+ });
321
+
322
+ it('an İ before a container does not shift what gets dropped', async () => {
323
+ // The strip this replaced kept an index-parallel lowercase copy, and
324
+ // `String.toLowerCase` is not length-preserving — `İ` (U+0130) becomes two
325
+ // code units — so after one of these a `<script>` stopped being
326
+ // recognized and its source was shown to the reader.
327
+ expect(await bodyOf('İ<script>alert(1)</script>after')).toBe('İafter');
328
+ expect(await bodyOf('İ<p>one</p><p>two</p>')).toBe('İ\none\n\ntwo');
329
+ expect(await bodyOf('<p>aİb</p><style>c{}</style>tail')).toBe('aİb\ntail');
330
+ });
331
+
332
+ it('a &nbsp; lands as an ordinary space, not U+00A0', async () => {
333
+ // It LOOKS like a space and is not one: a reader searching the message for
334
+ // "one two" would miss a line that reads exactly that.
335
+ expect(await bodyOf('<p>one&nbsp;two</p>')).toBe('one two');
336
+ });
337
+ });
338
+
339
+ describe('email helpers', () => {
340
+ it('mailboxText renders whichever of name/address exists', () => {
341
+ expect(mailboxText('Ada', 'ada@example.com')).toBe('Ada <ada@example.com>');
342
+ expect(mailboxText(undefined, 'ada@example.com')).toBe('ada@example.com');
343
+ expect(mailboxText('Ada', undefined)).toBe('Ada');
344
+ expect(mailboxText(undefined, undefined)).toBeUndefined();
345
+ });
346
+
347
+ it('attachmentLine prints only what is known', () => {
348
+ expect(attachmentLine({ name: 'a.pdf', mimeType: 'application/pdf', sizeBytes: 8 })).toBe(
349
+ 'a.pdf (application/pdf, 8 bytes)',
350
+ );
351
+ expect(attachmentLine({ name: 'a.pdf' })).toBe('a.pdf');
352
+ });
353
+
354
+ it('emailMessage never imports pptxOutline — jszip must not be evaluated (or bundled) by the email chunk', () => {
355
+ const read = (rel: string): string => readFileSync(fileURLToPath(new URL(rel, import.meta.url)), 'utf8');
356
+ expect(read('../emailMessage.ts')).not.toContain("'./pptxOutline'");
357
+ // The shared module both sides lean on must stay dependency-free.
358
+ // It may import a parser; what it must NOT drag in is the presentation code.
359
+ expect(read('../xmlReading.ts')).not.toContain('jszip');
360
+ expect(read('../xmlReading.ts')).not.toContain("'./pptxOutline'");
361
+ });
362
+ });
363
+
364
+ describe('the inline-image budget', () => {
365
+ const part = (id: string, size: number): EmailAttachmentView => ({
366
+ name: `${id}.png`,
367
+ mimeType: 'image/png',
368
+ sizeBytes: size,
369
+ contentId: id,
370
+ bytes: new Uint8Array(size),
371
+ });
372
+
373
+ it('spends the budget on the images the BODY shows, not on the ones that come first', () => {
374
+ // Four unreferenced parts at the per-part maximum come to EXACTLY the
375
+ // aggregate budget, so in document order there is nothing left for the
376
+ // small image the body actually displays and it is the one that goes
377
+ // undrawn. The sizes are the whole test: pick them so the budget is not
378
+ // reached (as this test first did) and it passes either way, pinning
379
+ // nothing — which is how the broken regex under it went unnoticed.
380
+ const four = MAX_INLINE_IMAGE_TOTAL_BYTES / MAX_INLINE_IMAGE_BYTES;
381
+ const parts = [
382
+ ...Array.from({ length: four }, (_, i) => part(`unused-${i}`, MAX_INLINE_IMAGE_BYTES)),
383
+ part('shown', 1_000),
384
+ ];
385
+ const out = inlineBudgeted(parts, referencedContentIds('<img src="cid:shown">'));
386
+ expect(out.find((p) => p.contentId === 'shown')?.bytes).toBeDefined();
387
+ // …and the budget still BINDS: one unreferenced part lost its bytes to
388
+ // make room, rather than everything being kept.
389
+ expect(out.filter((p) => p.bytes !== undefined).length).toBe(four);
390
+ });
391
+
392
+ it('reads a cid: whose id contains an s — the letter is not a delimiter', () => {
393
+ // Written with an unescaped `s`, the class excluded the LETTER s: an id
394
+ // like `photos@x` truncated to `photo` and `shown` matched nothing at all,
395
+ // which silently turned the priority sort back into document order.
396
+ expect([...referencedContentIds('<img src="cid:photos@x">')]).toEqual(['photos@x']);
397
+ expect([...referencedContentIds('<img src="cid:shown">')]).toEqual(['shown']);
398
+ });
399
+
400
+ it('reads the cid: references an HTML body makes, however they are quoted', () => {
401
+ const ids = referencedContentIds(`<img src="cid:A@x"><img src='cid:b@y'><td background="cid:<C@z>">`);
402
+ expect([...ids].sort()).toEqual(['a@x', 'b@y', 'c@z']);
403
+ });
404
+ });