Files
native/src/lib/__tests__/file-errors.test.ts
T
Linus Rath 3f4a81b7ad fix: handle shared folders, symlinks and file names on Stalwart 1.0
Files in the app assumed Stalwart 0.16 in several places:

- Stalwart 1.0 lists the owner's folders above a node shared with the
  user, without dates and with every right false. The app took the
  owner's top folder for the shared root, so the folder actually shared
  sat several levels down, under folders where rename, delete and move
  could only fail. Those placeholders are now left out of the root and
  of folder listings.
- A node counted as a folder when it had no blobId, so 1.0's symlinks
  showed up as empty folders that could be opened. Listings ask for
  `nodeType` (returned since 0.16.6, never sent in FileNode/set), and
  a symlink is an inert row with a link icon: it can be renamed, moved
  and deleted, but not opened, downloaded, duplicated or shared.
- Only "/" was checked in names, and uploads were not checked at all,
  so `<>:"\|?*` already failed on 0.16. New folder and rename now apply
  the naming rules from the filenode capability plus 1.0's stricter
  ones (control characters, 255 bytes, reserved stems such as
  `con.txt`) and say what is wrong. Uploaded names are made acceptable
  instead: `con.txt` becomes `con_.txt`, also on 0.16.6 to 0.16.23.
- One refused file stopped a whole upload. The batch now goes on and
  reports the files that failed at the end.
- Every media type over 30 characters, which is every Office document,
  was stored as application/octet-stream. New files get the bare
  lowercase media type 1.0 requires, and no `size`, which 1.0 refuses
  when it differs from the blob.
- File errors keep their JMAP type, and the alerts say why an action
  failed: too many files or folders (`overQuota`), folders nested too
  deeply, a refused name, missing rights, or a file too large.
2026-09-25 20:46:41 +02:00

54 lines
3.0 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { fileErrorKind, fileErrorMessage } from '../file-errors';
import { JMAPMethodError, JMAPSetError } from '../../api/jmap-result';
// SetErrors as Stalwart words them (1.0: jmap/src/file/*.rs; 0.16.23:
// jmap/src/file/set.rs), thrown by the files API as JMAPSetErrors.
const setError = (type: string, description: string, properties?: string[]) =>
new JMAPSetError(type, description, properties);
describe('fileErrorKind', () => {
it('explains Stalwart 1.0\'s limits on files and folders', () => {
expect(fileErrorKind(setError('overQuota', 'There are too many files, please delete some before adding a new one.'))).toBe('overQuota');
expect(fileErrorKind(setError('overQuota', 'There are too many folders, please delete some before adding a new one.'))).toBe('overQuota');
expect(fileErrorKind(setError('invalidProperties', 'Maximum folder depth exceeded.', ['parentId']))).toBe('tooDeep');
expect(fileErrorKind(setError('tooLarge', 'File is too large.'))).toBe('tooLarge');
});
it('explains refused names on 0.16 and 1.0', () => {
expect(fileErrorKind(setError('invalidProperties', 'Name is reserved and cannot be used.', ['name']))).toBe('invalidName');
expect(fileErrorKind(setError('invalidProperties', 'Name contains a forbidden character.', ['name']))).toBe('invalidName');
expect(fileErrorKind(setError('invalidProperties', 'Name must be between 1 and 255 octets.', ['name']))).toBe('invalidName');
});
it('explains missing rights, also as a method error', () => {
expect(fileErrorKind(setError('forbidden', 'You are not allowed to create file nodes in this folder.'))).toBe('forbidden');
expect(fileErrorKind(new JMAPMethodError('forbidden', 'No write access'))).toBe('forbidden');
});
it('has nothing to add for other errors', () => {
// The folder is gone, or a folder was moved into itself.
expect(fileErrorKind(setError('invalidProperties', 'Parent ID does not exist or is not a folder.', ['parentId']))).toBeNull();
expect(fileErrorKind(setError('invalidProperties', 'Circular reference in parent ids.', ['parentId']))).toBeNull();
expect(fileErrorKind(setError('notFound', 'Not found'))).toBeNull();
expect(fileErrorKind(new Error('Upload failed: 500'))).toBeNull();
expect(fileErrorKind(undefined)).toBeNull();
});
});
describe('fileErrorMessage', () => {
const t = (key: string, fallback?: string) => fallback ?? key;
it('says what a known refusal means', () => {
expect(fileErrorMessage(setError('invalidProperties', 'Name is reserved and cannot be used.', ['name']), t))
.toBe('The server does not accept this name.');
expect(fileErrorMessage(setError('overQuota', 'There are too many files'), t))
.toBe('You have reached the limit on files or folders. Delete some and try again.');
});
it('shows the server\'s own words for anything else', () => {
expect(fileErrorMessage(new Error('Upload failed: 413 too big'), t)).toBe('Upload failed: 413 too big');
expect(fileErrorMessage('boom', t)).toBe('boom');
});
});