from __future__ import annotations import importlib.util from pathlib import Path PATCHER_PATH = Path(__file__).resolve().parents[1] / 'fixes' / 'patch_openclaw_weixin_outbound_quotes.py' def load_patcher(): spec = importlib.util.spec_from_file_location('patch_openclaw_weixin_outbound_quotes', PATCHER_PATH) assert spec is not None and spec.loader is not None module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def test_send_patch_adds_exact_create_time_and_is_idempotent() -> None: patcher = load_patcher() ts = '''function buildTextMessageReq(params: { to: string; text: string; contextToken?: string; runId?: string; clientId: string; }): SendMessageReq { const { to, text, contextToken, runId, clientId } = params; return { msg: { client_id: clientId, message_type: MessageType.BOT } }; } export async function sendMessageWeixin(params: {}): Promise<{ messageId: string }> { const { to, text, opts } = params; const clientId = generateClientId(); const req = buildSendMessageReq({ payload: {}, clientId, }); try { await sendMessageApi({ body: req }); } catch (err) { throw err; } return { messageId: clientId }; } /** Send a single structured MessageItem downstream. */''' js = '''function buildTextMessageReq(params) { const { to, text, contextToken, runId, clientId } = params; return { msg: { client_id: clientId, message_type: MessageType.BOT } }; } export async function sendMessageWeixin(params) { const clientId = generateClientId(); const req = buildSendMessageReq({ payload: {}, clientId, }); try { await sendMessageApi({ body: req }); } catch (err) { throw err; } return { messageId: clientId }; } /** Send a single structured MessageItem downstream. */''' ts_once = patcher.patch_send_ts(ts) js_once = patcher.patch_send_js(js) assert patcher.patch_send_ts(ts_once) == ts_once assert patcher.patch_send_js(js_once) == js_once for result in (ts_once, js_once): assert 'const createTimeMs = Date.now();' in result assert 'text item missing' in result assert 'req.msg.item_list[0].create_time_ms = createTimeMs' in result assert 'req.msg.create_time_ms = createTimeMs' not in result assert 'return { messageId: clientId, createTimeMs }' in result