import test from 'node:test'; import assert from 'node:assert/strict'; import { composePassiveReplyWithDeferred, createWechatCustomerServiceDeferredStore, DEFERRED_DELIVERY_PRIORITY, isWechatCustomerServiceQuotaError, resolveDeferredDeliveryPriority, } from './customer-service-deferred.mjs'; test('customer-service deferred store keeps one formal reply per openid', async () => { const store = createWechatCustomerServiceDeferredStore(); const first = await store.upsertDeferred({ appId: 'wx1', openid: 'openid-1', priority: 'formal_reply', content: '页面链接 https://example.com/page.html', }); assert.equal(first.stored, true); const lower = await store.upsertDeferred({ appId: 'wx1', openid: 'openid-1', priority: 'failure_notice', content: '失败通知', }); assert.equal(lower.stored, false); assert.equal(lower.reason, 'lower_priority'); const claimed = await store.claimDeferred({ appId: 'wx1', openid: 'openid-1' }); assert.match(claimed.content, /page\.html/); assert.equal(claimed.priority, DEFERRED_DELIVERY_PRIORITY.formal_reply); const empty = await store.claimDeferred({ appId: 'wx1', openid: 'openid-1' }); assert.equal(empty, null); }); test('customer-service deferred store replaces with higher priority content', async () => { const store = createWechatCustomerServiceDeferredStore(); await store.upsertDeferred({ appId: 'wx1', openid: 'openid-2', priority: 'failure_notice', content: '旧失败通知', }); await store.upsertDeferred({ appId: 'wx1', openid: 'openid-2', priority: 'formal_reply', content: '正式链接 https://example.com/new.html', }); const pending = await store.peekDeferred({ appId: 'wx1', openid: 'openid-2' }); assert.equal(pending.priority, resolveDeferredDeliveryPriority('formal_reply')); assert.match(pending.content, /new\.html/); }); test('customer-service quota errcodes include 45047 and 45015', () => { assert.equal(isWechatCustomerServiceQuotaError(45047), true); assert.equal(isWechatCustomerServiceQuotaError(45015), true); assert.equal(isWechatCustomerServiceQuotaError(40001), false); }); test('composePassiveReplyWithDeferred merges fallback content', () => { assert.equal( composePassiveReplyWithDeferred('上一条结果', 'ack'), '上一条结果\n\nack', ); assert.equal(composePassiveReplyWithDeferred('', 'ack'), 'ack'); });