test_store.uts 1.38 KB
import store from '@/store/index.uts'
import { watch } from 'vue'

export function testStore() {
    console.log("=== 开始 Store 测试 ===")

    // 1. 初始状态检查
    const initVal = store.state.customerKey
    console.log(`初始 customerKey: '${initVal}'`)
    
    // 2. 设置监听器 (模拟 RecordService)
    const stopWatch = watch(
        () => store.state.customerKey,
        (newVal, oldVal) => {
            console.log(`[Watch] customerKey 发生变化: '${oldVal}' -> '${newVal}'`)
        },
        { immediate: false }
    )

    // 3. 执行 Commit
    console.log("执行 commit('updateCustomerKey', 'TEST_KEY_123')...")
    store.commit('updateCustomerKey', 'TEST_KEY_123')

    // 4. 验证状态
    // 验证扁平化访问
    const flatState = store.state.customerKey
    const flatPass = flatState === 'TEST_KEY_123'
    console.log(`验证扁平化访问: ${flatPass ? 'PASS' : 'FAIL'} (期望: 'TEST_KEY_123', 实际: '${flatState}')`)

    // 验证模块化访问
    const moduleState = store.state.reception.customerKey
    const modulePass = moduleState === 'TEST_KEY_123'
    console.log(`验证模块化访问: ${modulePass ? 'PASS' : 'FAIL'} (期望: 'TEST_KEY_123', 实际: '${moduleState}')`)

    // 5. 清理
    stopWatch()
    
    // 恢复状态 (可选)
    // store.commit('updateCustomerKey', initVal)

    console.log("=== Store 测试结束 ===")
}