78b7d546c2
Let users bind their own official account credentials in M 配置 and push MindSpace pages to the WeChat draft box during or after publish, reusing the existing draft conversion pipeline. Co-authored-by: Cursor <cursoragent@cursor.com>
105 lines
2.9 KiB
JavaScript
105 lines
2.9 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { attachPortalMindSpaceWechatRoutes } from './portal-mindspace-wechat-routes.mjs';
|
|
|
|
function createRouterRecorder() {
|
|
const routes = new Map();
|
|
return {
|
|
routes,
|
|
get(path, handler) {
|
|
routes.set(`GET ${path}`, handler);
|
|
},
|
|
post(path, handler) {
|
|
routes.set(`POST ${path}`, handler);
|
|
},
|
|
put(path, handler) {
|
|
routes.set(`PUT ${path}`, handler);
|
|
},
|
|
delete(path, handler) {
|
|
routes.set(`DELETE ${path}`, handler);
|
|
},
|
|
};
|
|
}
|
|
|
|
function createResponseRecorder() {
|
|
return {
|
|
statusCode: 200,
|
|
body: undefined,
|
|
status(code) {
|
|
this.statusCode = code;
|
|
return this;
|
|
},
|
|
json(body) {
|
|
this.body = body;
|
|
return this;
|
|
},
|
|
};
|
|
}
|
|
|
|
function createRequest(overrides = {}) {
|
|
return {
|
|
body: {},
|
|
params: { pageId: 'page-1' },
|
|
query: {},
|
|
currentUser: { id: 'user-1' },
|
|
requestId: 'request-1',
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
test('wechat config routes delegate to services', async () => {
|
|
const calls = [];
|
|
const api = createRouterRecorder();
|
|
attachPortalMindSpaceWechatRoutes(api, {
|
|
getWechatMpConfig: () => ({
|
|
async getConfig(userId) {
|
|
calls.push(['getConfig', userId]);
|
|
return { configured: true, appId: 'wx-test' };
|
|
},
|
|
async upsertConfig(userId, payload) {
|
|
calls.push(['upsertConfig', userId, payload]);
|
|
return { configured: true, appId: payload.appId };
|
|
},
|
|
async verifyConfig(userId) {
|
|
calls.push(['verifyConfig', userId]);
|
|
return { configured: true, verifiedAt: 1 };
|
|
},
|
|
async deleteConfig(userId) {
|
|
calls.push(['deleteConfig', userId]);
|
|
return { ok: true };
|
|
},
|
|
}),
|
|
getWechatPageDraft: () => ({
|
|
async pushPageDraft(userId, pageId) {
|
|
calls.push(['pushPageDraft', userId, pageId]);
|
|
return { ok: true, draftMediaId: 'draft-1' };
|
|
},
|
|
async listRuns(userId, options) {
|
|
calls.push(['listRuns', userId, options]);
|
|
return [];
|
|
},
|
|
}),
|
|
sendData(res, req, data) {
|
|
return res.status(200).json({ data });
|
|
},
|
|
handleMindSpaceError(res, req, error) {
|
|
return res.status(400).json({ message: error.message });
|
|
},
|
|
});
|
|
|
|
const getRes = createResponseRecorder();
|
|
await api.routes.get('GET /mindspace/v1/wechat-mp/config')(createRequest(), getRes);
|
|
assert.deepEqual(calls[0], ['getConfig', 'user-1']);
|
|
|
|
const putRes = createResponseRecorder();
|
|
await api.routes.get('PUT /mindspace/v1/wechat-mp/config')(
|
|
createRequest({ body: { app_id: 'wx-new', app_secret: 'secret' } }),
|
|
putRes,
|
|
);
|
|
assert.equal(calls.at(-1)[0], 'upsertConfig');
|
|
|
|
const pushRes = createResponseRecorder();
|
|
await api.routes.get('POST /mindspace/v1/pages/:pageId/wechat-draft')(createRequest(), pushRes);
|
|
assert.deepEqual(calls.at(-1), ['pushPageDraft', 'user-1', 'page-1']);
|
|
});
|