import { test } from 'node:test'; import assert from 'node:assert/strict'; import { MatrixError } from 'matrix-js-sdk'; import { describeUploadError } from './uploadError'; const err = (status: number | undefined, body: { errcode?: string; error?: string }) => new MatrixError(body, status, 'http://hs/_matrix/media/v3/upload?filename=x.png'); test('413 names the limit when known and never leaks the URL', () => { const e = err(413, { errcode: 'M_TOO_LARGE', error: 'nope' }); assert.equal( describeUploadError(e, 50 * 1024 * 1024), 'This file is larger than the server allows (limit 52.4 MB).', ); assert.equal(describeUploadError(e), 'This file is larger than the server allows.'); assert.ok(!describeUploadError(e).includes('http')); }); test('429, 403, 5xx and transport failures each get their sentence', () => { assert.equal( describeUploadError(err(429, { errcode: 'M_LIMIT_EXCEEDED', error: 'Too Many Requests' })), 'Slow down — try again in a moment.', ); assert.equal( describeUploadError(err(403, { errcode: 'M_FORBIDDEN', error: 'Uploads disabled' })), 'The server refused this upload: Uploads disabled', ); assert.equal( describeUploadError(err(502, { errcode: 'M_UNKNOWN', error: 'Bad Gateway' })), "Couldn't reach the server. Check your connection and retry.", ); assert.equal( describeUploadError(new MatrixError({ error: 'request failed' })), "Couldn't reach the server. Check your connection and retry.", ); }); test('a cancel and an unknown 4xx fall through to the server text', () => { assert.equal( describeUploadError(new MatrixError({ error: 'Upload cancelled' })), 'Upload cancelled.', ); assert.equal( describeUploadError(err(400, { errcode: 'M_BAD_JSON', error: 'Unsupported file type' })), 'Unsupported file type', ); assert.equal(describeUploadError(err(400, {})), 'Upload failed.'); });