buildForwardAsAttachmentPayload called emailExportFilename(email) with no options, always using the default naming template regardless of the user's configured emailDownloadTemplate (and space/case/diacritics transforms) - the same settings the neighboring "Export as .eml" action already respects. That could produce inconsistent .eml filenames between the two actions for the same message. Accept an optional EmailFilenameOptions parameter and pass it through. handleForwardAsAttachment now reads the same settings email-viewer.tsx's emailFilenameOptions useMemo does, via useSettingsStore.getState() (a one-off read inside an event handler, matching this file's existing pattern, rather than a new reactive subscription). Add a unit test covering a custom template. Caught by GitHub Copilot's automated PR review.
59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { buildForwardAsAttachmentPayload } from '@/lib/forward-as-attachment';
|
|
import type { Email } from '@/lib/jmap/types';
|
|
|
|
function makeEmail(overrides: Partial<Email> = {}): Email {
|
|
return {
|
|
id: 'e1',
|
|
threadId: 't1',
|
|
mailboxIds: { inbox: true },
|
|
keywords: {},
|
|
size: 12345,
|
|
receivedAt: '2026-07-26T22:25:22Z',
|
|
subject: 'Your waste service day is changing',
|
|
hasAttachment: false,
|
|
blobId: 'blob123',
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe('buildForwardAsAttachmentPayload', () => {
|
|
it('returns null when the email has no blobId', () => {
|
|
const email = makeEmail({ blobId: undefined });
|
|
expect(buildForwardAsAttachmentPayload(email, 'Fwd:')).toBeNull();
|
|
});
|
|
|
|
it('prefixes the subject using the given forward prefix', () => {
|
|
const email = makeEmail({ subject: 'Missed spam example' });
|
|
const payload = buildForwardAsAttachmentPayload(email, 'Fwd:');
|
|
expect(payload?.subject).toBe('Fwd: Missed spam example');
|
|
});
|
|
|
|
it('builds a message/rfc822 attachment referencing the email\'s own blobId, not a new upload', () => {
|
|
const email = makeEmail({ blobId: 'the-real-blob-id', size: 26489 });
|
|
const payload = buildForwardAsAttachmentPayload(email, 'Fwd:');
|
|
expect(payload?.attachment).toEqual({
|
|
blobId: 'the-real-blob-id',
|
|
name: expect.stringMatching(/\.eml$/),
|
|
type: 'message/rfc822',
|
|
size: 26489,
|
|
});
|
|
});
|
|
|
|
it('is idempotent - repeated forwarding does not stack prefixes', () => {
|
|
const email = makeEmail({ subject: 'Fwd: already forwarded once' });
|
|
const payload = buildForwardAsAttachmentPayload(email, 'Fwd:');
|
|
expect(payload?.subject).toBe('Fwd: already forwarded once');
|
|
});
|
|
|
|
it('honors a custom filename template, matching "Export as .eml" naming instead of always using the default', () => {
|
|
const email = makeEmail({ subject: 'Missed spam example' });
|
|
const payload = buildForwardAsAttachmentPayload(email, 'Fwd:', {
|
|
template: 'custom-{subject}',
|
|
lowercase: true,
|
|
spaceReplacement: 'dash',
|
|
});
|
|
expect(payload?.attachment.name).toBe('custom-missed-spam-example.eml');
|
|
});
|
|
});
|