fix legacy attachment URLs in Vite web

This commit is contained in:
richjohnrambo
2026-10-14 13:58:33 +08:00
parent 0570f2c5bd
commit ec22b709b5
3 changed files with 66 additions and 2 deletions
+34
View File
@@ -0,0 +1,34 @@
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',
);
});