GuDingZiChan/print-agent/tests/agent-runtime.test.mjs
WuYongJie 0dc3c09315 feat(资产与打印): 提交近期借还盘点与打印助手源码
- 提交借用单缺失异常处理、扫码借还搜索与站内消息快捷处理源码
- 提交盘点范围、多终端打印、组织同步与认证模式相关源码
- 提交 .NET/Node 打印助手及版本清单源码
2026-03-27 20:18:31 +08:00

105 lines
2.6 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { createAgentRuntime } from '../src/services/agentRuntime.js'
test('start 会立即发送一次心跳并尝试领取任务', async () => {
const calls = []
const intervals = []
const runtime = createAgentRuntime({
store: {
async load() {
return {
serverUrl: 'https://www.jsgdzc.cn/api',
deviceToken: 'device-token',
terminalId: 18,
terminalName: '行政标签机',
selectedPrinterName: 'Xprinter XP-420B',
heartbeatIntervalMs: 30000,
pollIntervalMs: 3000
}
},
async save(patch) {
return patch
}
},
clientFactory: () => ({
async getPrintAgentReleases() {
calls.push(['release-check'])
return {
latest_version: '0.1.0'
}
},
async sendHeartbeat(payload) {
calls.push(['heartbeat', payload])
return {
message: '心跳已接收'
}
},
async claimNextJob() {
calls.push(['claim'])
return {
job: {
id: 31,
job_type: 'asset_label',
payload: {
items: [
{
id: 1,
equipment_code: 'JS-PC-2101'
}
]
}
}
}
},
async reportJobStatus(jobId, payload) {
calls.push(['status', jobId, payload.status])
return {}
}
}),
getPrinters: async () => [{ name: 'Xprinter XP-420B' }],
jobProcessor: {
async process(job, context) {
calls.push(['process', job.id, context.selectedPrinterName])
return {
ok: true,
nextStatus: 'completed',
message: '已发送打印'
}
},
async runTestPrint() {
return {
ok: true,
message: '测试标签已发送'
}
}
},
setIntervalImpl(handler, timeoutMs) {
intervals.push(timeoutMs)
return { handler, timeoutMs }
},
clearIntervalImpl() {},
onStateChange() {}
})
await runtime.start()
assert.deepEqual(intervals, [30000, 3000])
assert.equal(calls[0][0], 'release-check')
assert.equal(calls[1][0], 'heartbeat')
assert.equal(calls[2][0], 'claim')
assert.deepEqual(
calls.filter((entry) => entry[0] === 'status').map((entry) => entry[2]),
['printing', 'completed']
)
assert.deepEqual(calls.find((entry) => entry[0] === 'process'), [
'process',
31,
'Xprinter XP-420B'
])
runtime.stop()
})