feat(wechat): add image generation delivery policy
Memind CI / Test, build, and release guards (pull_request) Successful in 2m45s

This commit is contained in:
john
2026-07-21 23:21:44 +08:00
parent bfb1f6fea9
commit 28581310da
13 changed files with 1078 additions and 19 deletions
+38
View File
@@ -0,0 +1,38 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import sharp from 'sharp';
import { uploadWechatGeneratedImage } from './wechat-media.mjs';
test('uploadWechatGeneratedImage converts a generated asset and uploads WeChat image media', async () => {
const source = await sharp({
create: { width: 32, height: 32, channels: 3, background: '#336699' },
}).webp().toBuffer();
const calls = [];
const result = await uploadWechatGeneratedImage(
'access-1',
'/MindSpace/user/public/images/fresh.webp',
{
publicBaseUrl: 'https://example.com',
wechatFetch: async (url, init = {}) => {
calls.push({ url: String(url), init });
if (String(url).startsWith('https://example.com/')) {
return new Response(source, { status: 200, headers: { 'Content-Type': 'image/webp' } });
}
if (String(url).includes('/cgi-bin/media/upload')) {
assert.equal(init.method, 'POST');
assert.ok(init.body instanceof FormData);
return new Response(JSON.stringify({ type: 'image', media_id: 'wx-media-1', created_at: 1 }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
}
throw new Error(`unexpected url: ${url}`);
},
},
);
assert.equal(result.mediaId, 'wx-media-1');
assert.equal(result.contentType, 'image/jpeg');
assert.ok(result.bytes > 0);
assert.match(calls[1].url, /access_token=access-1/);
assert.match(calls[1].url, /type=image/);
});