const assert = require('assert') const fs = require('fs') const path = require('path') let pageDefinition let currentProfile let currentSettings let savedSettings let toastTitle global.Page = definition => { pageDefinition = definition } global.wx = { showToast: options => { toastTitle = options.title } } const profileCache = require('../../utils/profile-cache') profileCache.getProfile = async () => currentProfile const shopApi = require('../../api/shop') shopApi.appointmentSettings = async () => ({ ...currentSettings }) shopApi.saveAppointmentSettings = async settings => { savedSettings = settings } require('./index') async function loadProfile(profile) { currentProfile = profile const page = { data: { ...pageDefinition.data }, setData(values) { Object.entries(values).forEach(([key, value]) => { const parts = key.split('.') if (parts.length === 1) this.data[key] = value else this.data[parts[0]] = { ...this.data[parts[0]], [parts[1]]: value } }) } } await pageDefinition.loadProfileAndSettings.call(page) return page.data } async function run() { currentSettings = { shopName: '薄荷美甲', businessHours: '09:30-21:30', defaultAppointmentDurationMinutes: 75, shopNameModerationStatus: 'PENDING', pendingShopName: '薄荷指尖' } const empty = await loadProfile({ type: 2, shopBusinessExpireTime: null, shopBusinessExpired: false, shopBusinessRemainingDays: null }) assert.strictEqual(empty.businessExpireDateText, '未设置') assert.strictEqual(empty.businessExpiryStatusText, '有效期未设置') const active = await loadProfile({ type: 2, shopBusinessExpireTime: '2026-12-31T23:59:59', shopBusinessExpired: false, shopBusinessRemainingDays: 66 }) assert.strictEqual(active.businessExpireDateText, '2026-12-31') assert.strictEqual(active.businessExpiryStatusText, '剩余 66 天') const expired = await loadProfile({ type: 2, shopBusinessExpireTime: '2026-10-01T00:00:00', shopBusinessExpired: true, shopBusinessRemainingDays: 0 }) assert.strictEqual(expired.businessExpiryStatusText, '已过期,暂不能新增预约') assert.strictEqual(expired.form.startTime, '09:30') assert.strictEqual(expired.form.endTime, '21:30') assert.strictEqual(expired.businessHoursConfigured, true) assert.strictEqual(expired.form.defaultDuration, 75) assert.strictEqual(expired.nameModerationText, '名称审核中') assert.strictEqual(expired.form.pendingShopName, '薄荷指尖') const shopSettingsWxml = fs.readFileSync(path.join(__dirname, 'index.wxml'), 'utf8') const accountSettingsWxml = fs.readFileSync(path.join(__dirname, '../account-settings/index.wxml'), 'utf8') assert(shopSettingsWxml.includes('预约运营设置')) assert(shopSettingsWxml.includes('营业时间')) assert(shopSettingsWxml.includes('默认预约时长')) assert(shopSettingsWxml.includes('catchtap="explainDefaultDuration"')) assert(shopSettingsWxml.includes('wx:if="{{profile.type == 2}}" class="card business-expiry-card"')) assert(!accountSettingsWxml.includes('business-expiry-card')) assert(!shopSettingsWxml.includes('预约规则')) assert(shopSettingsWxml.includes('提交名称审核')) assert(shopSettingsWxml.includes('保存预约设置')) assert(shopSettingsWxml.includes('设置开始时间')) assert(shopSettingsWxml.includes('营业时间尚未设置')) currentSettings = { shopName: '新门店', businessHours: null, defaultAppointmentDurationMinutes: 60 } const unconfigured = await loadProfile({ type: 2 }) assert.strictEqual(unconfigured.form.startTime, '') assert.strictEqual(unconfigured.form.endTime, '') assert.strictEqual(unconfigured.businessHoursConfigured, false) const savePage = { data: { ...pageDefinition.data, businessHoursConfigured: false, form: { ...pageDefinition.data.form, startTime: '', endTime: '' } }, setData(values) { Object.entries(values).forEach(([key, value]) => { const parts = key.split('.') if (parts.length === 1) this.data[key] = value else this.data[parts[0]] = { ...this.data[parts[0]], [parts[1]]: value } }) } } savedSettings = undefined await pageDefinition.saveAppointmentSettings.call(savePage) assert.strictEqual(savedSettings, undefined) assert.strictEqual(toastTitle, '请设置完整有效的营业时间') savePage.data.form.startTime = '09:00' savePage.data.form.endTime = '21:30' await pageDefinition.saveAppointmentSettings.call(savePage) assert.deepStrictEqual(savedSettings, { businessHours: '09:00-21:30', defaultAppointmentDurationMinutes: 60 }) assert.strictEqual(savePage.data.businessHoursConfigured, true) } run().catch(error => { console.error(error) process.exitCode = 1 })