chore(小程序): 初始化工程仓库
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { EndpointRouter } from '../src/core/network.ts'
|
||||
|
||||
let local = 'https://lan.example.com'
|
||||
let remote = 'https://api.example.com'
|
||||
test('蜂窝网络与未知网络不探测内网', async () => {
|
||||
for (let type of ['4g', '5g', 'unknown']) {
|
||||
let probes = 0
|
||||
let r = new EndpointRouter({ local, remote, storeId: 's1' }, async () => { probes++; return 's1' })
|
||||
assert.equal(await r.select(type), remote)
|
||||
assert.equal(probes, 0)
|
||||
}
|
||||
})
|
||||
test('Wi-Fi 只有对应门店探测成功才走内网,失败进入冷却', async () => {
|
||||
let probes = 0
|
||||
let r = new EndpointRouter({ local, remote, storeId: 's1' }, async () => { probes++; return 's2' })
|
||||
assert.equal(await r.select('wifi'), remote)
|
||||
assert.equal(await r.select('wifi'), remote)
|
||||
assert.equal(probes, 1)
|
||||
let good = new EndpointRouter({ local, remote, storeId: 's1' }, async () => 's1')
|
||||
assert.equal(await good.select('wifi'), local)
|
||||
})
|
||||
test('网络切换后丢弃过期内网探测结果', async () => {
|
||||
let resolve!: (value: string) => void
|
||||
let r = new EndpointRouter({ local, remote, storeId: 's1' }, () => new Promise(res => { resolve = res }))
|
||||
let old = r.select('wifi')
|
||||
assert.equal(await r.select('4g'), remote)
|
||||
resolve('s1')
|
||||
assert.equal(await old, remote)
|
||||
assert.equal(r.current, remote)
|
||||
})
|
||||
test('无网络明确离线;连通失败切回公网', async () => {
|
||||
let r = new EndpointRouter({ local, remote, storeId: 's1' }, async () => 's1')
|
||||
await assert.rejects(r.select('none'), /网络/)
|
||||
await r.select('wifi')
|
||||
r.fallback()
|
||||
assert.equal(r.current, remote)
|
||||
})
|
||||
Reference in New Issue
Block a user