| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import assert from 'node:assert/strict';
- import { createServer } from 'node:http';
- import { mkdtemp, readFile, rm } from 'node:fs/promises';
- import os from 'node:os';
- import path from 'node:path';
- const tempUploadPath = await mkdtemp(path.join(os.tmpdir(), 'work-cover-cache-'));
- process.env.UPLOAD_PATH = tempUploadPath;
- const { cacheRemoteWorkCoverUrl, isLocalUploadUrl } = await import('../server/src/utils/workCoverCache.js');
- const png = Buffer.from(
- 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8Xw8AAoMBgB6WJ6sAAAAASUVORK5CYII=',
- 'base64'
- );
- const server = createServer((req, res) => {
- if (req.url === '/cover.png') {
- res.writeHead(200, {
- 'content-type': 'image/png',
- 'content-length': String(png.length),
- });
- res.end(png);
- return;
- }
- res.writeHead(404, { 'content-type': 'text/plain' });
- res.end('not found');
- });
- await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
- try {
- const address = server.address();
- assert.ok(address && typeof address === 'object');
- const remoteUrl = `http://127.0.0.1:${address.port}/cover.png`;
- const cachedUrl = await cacheRemoteWorkCoverUrl({
- url: remoteUrl,
- platform: 'weixin_video',
- accountId: 123,
- platformVideoId: 'export/test-video-id',
- });
- assert.match(cachedUrl, /^\/uploads\/covers\/synced\/weixin_video\/123\/[a-f0-9]{32}\.png$/);
- assert.equal(isLocalUploadUrl(cachedUrl), true);
- const diskPath = path.join(tempUploadPath, ...cachedUrl.replace('/uploads/', '').split('/'));
- assert.deepEqual(await readFile(diskPath), png);
- const cachedAgain = await cacheRemoteWorkCoverUrl({
- url: remoteUrl,
- platform: 'weixin_video',
- accountId: 123,
- platformVideoId: 'export/test-video-id',
- });
- assert.equal(cachedAgain, cachedUrl);
- console.log('work cover cache checks passed');
- } finally {
- server.close();
- await rm(tempUploadPath, { recursive: true, force: true });
- }
|