|
|
@@ -0,0 +1,74 @@
|
|
|
+import assert from 'node:assert/strict'
|
|
|
+import { test } from 'node:test'
|
|
|
+
|
|
|
+import { createPinia, setActivePinia } from 'pinia'
|
|
|
+
|
|
|
+import { DEMO_DEVICE_CODE, sharedPreloadDeviceCode, useDetailStore } from '../src/modules/cockpit/stores/detailStore.js'
|
|
|
+
|
|
|
+const BASE_URL = 'http://test.local/api/cockpit/v1'
|
|
|
+
|
|
|
+/** 记录所有请求 URL 的 fetch 替身;返回空成功载荷,让分区各自走完自己的降级路径。 */
|
|
|
+function recordingFetch(urls) {
|
|
|
+ return async (url) => {
|
|
|
+ urls.push(String(url))
|
|
|
+ return {
|
|
|
+ ok: true,
|
|
|
+ status: 200,
|
|
|
+ json: async () => ({ code: 200, data: {} }),
|
|
|
+ text: async () => '{"code":200,"data":{}}',
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+async function preload(dataSource) {
|
|
|
+ setActivePinia(createPinia())
|
|
|
+ const urls = []
|
|
|
+ const store = useDetailStore()
|
|
|
+ try {
|
|
|
+ await store.loadSharedSections({
|
|
|
+ config: { dataSource, api: { baseUrl: BASE_URL } },
|
|
|
+ fetchImpl: recordingFetch(urls),
|
|
|
+ })
|
|
|
+ } catch {
|
|
|
+ // 地图摘要分区在替身下的失败与本次断言无关;只看设备分区是否被请求。
|
|
|
+ }
|
|
|
+ return urls.filter((url) => /\/equipments\//.test(url))
|
|
|
+}
|
|
|
+
|
|
|
+test('real 模式:启动预载不请求演示设备', async () => {
|
|
|
+ const deviceUrls = await preload('real')
|
|
|
+ assert.deepEqual(deviceUrls, [], 'real 模式没有真实设备可预载,不应请求 /equipments/ 分区(曾打出必然 404)')
|
|
|
+})
|
|
|
+
|
|
|
+test('显式 deviceCode:真实设备仍按请求预载', async () => {
|
|
|
+ setActivePinia(createPinia())
|
|
|
+ const urls = []
|
|
|
+ const store = useDetailStore()
|
|
|
+ try {
|
|
|
+ await store.loadSharedSections({
|
|
|
+ deviceCode: '21119740',
|
|
|
+ config: { dataSource: 'real', api: { baseUrl: BASE_URL } },
|
|
|
+ fetchImpl: recordingFetch(urls),
|
|
|
+ })
|
|
|
+ } catch {
|
|
|
+ // 同上
|
|
|
+ }
|
|
|
+ const deviceUrls = urls.filter((url) => /\/equipments\//.test(url))
|
|
|
+ assert.ok(deviceUrls.length > 0, '显式给出设备编码时应照常预载')
|
|
|
+ assert.ok(
|
|
|
+ deviceUrls.every((url) => url.includes('21119740')),
|
|
|
+ '预载只应请求显式给出的设备,不得混入演示设备',
|
|
|
+ )
|
|
|
+})
|
|
|
+
|
|
|
+test('mock 模式:仍以演示设备为预载目标', () => {
|
|
|
+ const code = sharedPreloadDeviceCode({ config: { dataSource: 'mock' } })
|
|
|
+ assert.equal(code, DEMO_DEVICE_CODE, 'mock 的契约样例里有这台设备,保持原行为')
|
|
|
+})
|
|
|
+
|
|
|
+test('决策:显式编码优先,real 无编码不回退演示设备', () => {
|
|
|
+ assert.equal(sharedPreloadDeviceCode({ deviceCode: 'X', config: { dataSource: 'real' } }), 'X')
|
|
|
+ assert.equal(sharedPreloadDeviceCode({ config: { dataSource: 'real' } }), '')
|
|
|
+ assert.equal(sharedPreloadDeviceCode({ config: { dataSource: 'mock' } }), DEMO_DEVICE_CODE)
|
|
|
+ assert.equal(sharedPreloadDeviceCode(), '', '无配置时不得凭空选一台演示设备')
|
|
|
+})
|