Files
im_webapp/web/tests/file-url.test.mjs
T

35 lines
1.1 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { createJiti } from 'jiti';
const jiti = createJiti(import.meta.url);
const { normalizeFileDownloadUrl } = await jiti.import('../src/lib/api/file-url.ts');
const pageOrigin = 'https://web.118im.com';
const apiEndpoint = 'https://api.118im.com/v1/im';
const fileEndpoint = 'https://web.118im.com/v0/file';
test('routes historical API-host attachment refs through the configured file endpoint', () => {
assert.equal(
normalizeFileDownloadUrl(
'https://api.118im.com/v0/file/s/example.jpg?size=large#preview',
apiEndpoint,
fileEndpoint,
pageOrigin,
),
'https://web.118im.com/v0/file/s/example.jpg?size=large#preview',
);
});
test('leaves current and unrelated attachment refs unchanged', () => {
assert.equal(
normalizeFileDownloadUrl('/v0/file/s/current.jpg', apiEndpoint, fileEndpoint, pageOrigin),
'/v0/file/s/current.jpg',
);
assert.equal(
normalizeFileDownloadUrl('https://cdn.example.com/image.jpg', apiEndpoint, fileEndpoint, pageOrigin),
'https://cdn.example.com/image.jpg',
);
});