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://asset.jiansun.vip/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() })