import test from 'node:test'; import assert from 'node:assert/strict'; import { createPushSubscriptionPendingStore } from '../push-subscription-pending.mjs'; import { formatPushSubscriptionCommittedReply } from '../push-subscription-service.mjs'; import { handlePushSubscriptionMessages } from './push-subscription.mjs'; test('handlePushSubscriptionMessages commits morning subscription on reply 1', async () => { const store = createPushSubscriptionPendingStore(); const calls = []; const scheduleService = { async listItems() { return []; }, async findActiveItemByMetadataSource() { return null; }, async createItem(payload) { calls.push(['createItem', payload]); return { id: 'item-1', ...payload }; }, async createReminder(payload) { calls.push(['createReminder', payload]); return { id: 'rem-1', ...payload }; }, }; await store.setPending({ appId: 'wx-app', openid: 'openid-a' }); const reply = await handlePushSubscriptionMessages({ appId: 'wx-app', openid: 'openid-a', text: '1 7点', pendingStore: store, scheduleService, boundUser: { userId: 'user-1' }, }); assert.equal(reply, formatPushSubscriptionCommittedReply({ catalogItem: { id: 1, emoji: '☀️', label: '早安问候' }, hour: 7, minute: 0, })); assert.equal(calls[0][1].metadata.source, 'wechat_push:morning'); assert.equal(calls[0][1].metadata.dailyHour, 7); }); test('handlePushSubscriptionMessages commits news subscription when delivery is ready', async () => { const calls = []; const reply = await handlePushSubscriptionMessages({ text: '2 7点', boundUser: { userId: 'user-1' }, scheduleService: { async listItems() { return []; }, async findActiveItemByMetadataSource() { return null; }, async createItem(payload) { calls.push(['createItem', payload]); return { id: 'item-news', ...payload }; }, async createReminder(payload) { calls.push(['createReminder', payload]); return { id: 'rem-news', ...payload }; }, }, }); assert.match(reply, /每日新闻早报/u); assert.equal(calls[0][1].metadata.source, 'wechat_push:news'); assert.equal(calls[0][1].metadata.dailyHour, 7); }); test('handlePushSubscriptionMessages commits weather subscription with default city', async () => { const calls = []; const reply = await handlePushSubscriptionMessages({ text: '3 上海', boundUser: { userId: 'user-1' }, scheduleService: { async listItems() { return []; }, async findActiveItemByMetadataSource() { return null; }, async createItem(payload) { calls.push(['createItem', payload]); return { id: 'item-weather', ...payload }; }, async createReminder(payload) { calls.push(['createReminder', payload]); return { id: 'rem-weather', ...payload }; }, }, }); assert.match(reply, /每日天气/u); assert.match(reply, /上海/u); assert.equal(calls[0][1].metadata.options.city, '上海'); }); test('handlePushSubscriptionMessages commits finance subscription', async () => { const calls = []; const reply = await handlePushSubscriptionMessages({ text: '4', boundUser: { userId: 'user-1' }, scheduleService: { async listItems() { return []; }, async findActiveItemByMetadataSource() { return null; }, async createItem(payload) { calls.push(['createItem', payload]); return { id: 'item-finance', ...payload }; }, async createReminder(payload) { calls.push(['createReminder', payload]); return { id: 'rem-finance', ...payload }; }, }, }); assert.match(reply, /财经市场/u); assert.equal(calls[0][1].metadata.source, 'wechat_push:finance'); }); test('handlePushSubscriptionMessages lists active subscriptions', async () => { const reply = await handlePushSubscriptionMessages({ text: '我的订阅', boundUser: { userId: 'user-1' }, scheduleService: { async listItems() { return [{ id: 'item-1', metadata: { source: 'wechat_push:morning', dailyHour: 8, dailyMinute: 0, }, }]; }, }, }); assert.match(reply, /你已开通/); assert.match(reply, /早安问候/); }); test('handlePushSubscriptionMessages returns help menu', async () => { const reply = await handlePushSubscriptionMessages({ text: '订阅帮助' }); assert.match(reply, /每日推送订阅/); assert.match(reply, /10 🎁 每日惊喜/); }); test('handlePushSubscriptionMessages opens morning subscription on secret code 美丽心情', async () => { const calls = []; const reply = await handlePushSubscriptionMessages({ text: '美丽心情', boundUser: { userId: 'user-1' }, scheduleService: { async listItems() { return []; }, async findActiveItemByMetadataSource() { return null; }, async createItem(payload) { calls.push(['createItem', payload]); return { id: 'item-morning', ...payload }; }, async createReminder(payload) { calls.push(['createReminder', payload]); return { id: 'rem-morning', ...payload }; }, }, }); assert.match(reply, /已开启 ☀️ 早安问候/u); assert.equal(calls[0][1].metadata.source, 'wechat_push:morning'); assert.equal(calls[0][1].metadata.dailyHour, 8); }); test('handlePushSubscriptionMessages pushes news digest on secret code 新闻早报', async () => { const reply = await handlePushSubscriptionMessages({ text: '新闻早报', env: {}, }); assert.match(reply, /📰 每日新闻早报/u); });