const assert = require('node:assert/strict') const fs = require('node:fs') const path = require('node:path') const { test } = require('node:test') const ts = require('typescript') function loadTypeScript(file, mocks = {}) { const source = fs.readFileSync(path.join(__dirname, '../src', file), 'utf8') const { outputText } = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2017, esModuleInterop: true }, }) const module = { exports: {} } new Function('require', 'module', 'exports', outputText)( (id) => mocks[id] || require(id), module, module.exports, ) return module.exports } const { firmwareProgressView } = loadTypeScript('components/firmware-progress/view-model.ts') const status = (state, progress) => ({ device_id: 'test-device', update_status: state, progress, latest_version: '1.8.2' }) test('accepted update without reported progress stays indeterminate', () => { const view = firmwareProgressView(status('accepted', 0)) assert.equal(view.hasProgress, false) assert.match(view.message, /等待进度上报/) }) test('received progress is used directly, including fractional values', () => { const view = firmwareProgressView(status('updating', 65.4)) assert.equal(view.progress, 65.4) assert.equal(view.percent, 65) assert.equal(view.complete, false) }) test('nearly complete progress never rounds up to 100', () => { assert.equal(firmwareProgressView(status('updating', 99.9)).percent, 99) }) test('100 percent still waits for the device success result', () => { const view = firmwareProgressView(status('updating', 100)) assert.equal(view.complete, false) assert.match(view.message, /等待设备确认/) }) test('both backend completion statuses show a completed update', () => { for (const state of ['success', 'completed']) { const view = firmwareProgressView(status(state, 0)) assert.equal(view.complete, true) assert.equal(view.percent, 100) } }) test('failed update keeps the last real progress and offers recovery copy', () => { const view = firmwareProgressView(status('failed', 42)) assert.equal(view.failed, true) assert.equal(view.progress, 42) assert.match(view.message, /重试/) }) test('temporary query failure is not reported as an OTA failure', () => { const view = firmwareProgressView(status('updating', 42), true) assert.equal(view.failed, false) assert.equal(view.progress, 42) assert.match(view.message, /暂时无法刷新/) }) test('invalid progress cannot overflow or break the display', () => { for (const value of [undefined, NaN, Infinity, -10]) { assert.equal(firmwareProgressView(status('updating', value)).hasProgress, false) } assert.equal(firmwareProgressView(status('updating', 120)).progress, 100) }) test('backend placeholder version is not displayed as a release number', () => { const view = firmwareProgressView({ ...status('updating', 12), target_version: 'updating' }) assert.equal(view.version, '1.8.2') }) test('custom pages clear the capsule while native pages only add content spacing', () => { const { getPageStyle } = loadTypeScript('utils/page-style.ts', { '@tarojs/taro': { getSystemInfoSync: () => ({ statusBarHeight: 44 }), getMenuButtonBoundingClientRect: () => ({ bottom: 90 }), }, }) assert.equal(getPageStyle()['--bb-navigation-top'], '102px') assert.equal(getPageStyle(false)['--bb-navigation-top'], '12px') })