GuDingZiChan/print-agent/tests/api-client.test.mjs

127 lines
3.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import test from 'node:test'
import assert from 'node:assert/strict'
import { createPrintAgentClient } from '../src/services/apiClient.js'
test('createPrintAgentClient 会用配对码请求长期令牌', async () => {
const calls = []
const client = createPrintAgentClient({
serverUrl: 'https://asset.example.com/',
fetchImpl: async (url, options) => {
calls.push({ url, options })
return {
ok: true,
async json() {
return {
code: 200,
message: '打印终端配对成功',
data: {
terminal_id: 12,
device_token: 'token-123'
}
}
}
}
}
})
const result = await client.pair({
pairCode: 'ABCD-EFGH',
terminalName: '前台标签机',
machineName: 'DESKTOP-001',
platform: 'win32'
})
assert.equal(calls[0].url, 'https://asset.example.com/api/print-agent/pair')
assert.equal(calls[0].options.method, 'POST')
assert.match(calls[0].options.body, /"code":"ABCD-EFGH"/)
assert.match(calls[0].options.body, /"terminal_name":"前台标签机"/)
assert.match(calls[0].options.body, /"machine_name":"DESKTOP-001"/)
assert.equal(result.terminal_id, 12)
assert.equal(result.device_token, 'token-123')
})
test('已配对请求会自动带上终端令牌', async () => {
const calls = []
const client = createPrintAgentClient({
serverUrl: 'https://asset.example.com',
getDeviceToken: () => 'token-xyz',
fetchImpl: async (url, options) => {
calls.push({ url, options })
return {
ok: true,
async json() {
return {
code: 200,
message: '领取成功',
data: {
job: {
id: 18,
status: 'claimed'
}
}
}
}
}
}
})
await client.claimNextJob({
terminalId: 7
})
assert.equal(calls[0].url, 'https://asset.example.com/api/print-agent/jobs/claim')
assert.equal(calls[0].options.headers.Authorization, 'Bearer token-xyz')
assert.match(calls[0].options.body, /"terminal_id":7/)
})
test('心跳会把字段转成后端要求的 snake_case', async () => {
const calls = []
const client = createPrintAgentClient({
serverUrl: 'https://asset.example.com',
getDeviceToken: () => 'token-xyz',
fetchImpl: async (url, options) => {
calls.push({ url, options })
return {
ok: true,
async json() {
return {
code: 200,
message: '心跳已接收',
data: {
id: 9
}
}
}
}
}
})
await client.sendHeartbeat({
terminalName: '行政标签机',
machineName: 'DESKTOP-001',
selectedPrinterName: 'Xprinter XP-420B',
printerSnapshot: [{ name: 'Xprinter XP-420B' }],
labelProfile: {
paper_size: '60x40mm'
}
})
assert.equal(calls[0].url, 'https://asset.example.com/api/print-agent/heartbeat')
assert.match(calls[0].options.body, /"terminal_name":"行政标签机"/)
assert.match(calls[0].options.body, /"machine_name":"DESKTOP-001"/)
assert.match(calls[0].options.body, /"selected_printer_name":"Xprinter XP-420B"/)
assert.match(calls[0].options.body, /"printer_snapshot":\[\{"name":"Xprinter XP-420B"\}\]/)
})
test('打印结果需服务端明确回执,200空对象不能确认完成', async () => {
let responseData = {}
const client = createPrintAgentClient({
serverUrl: 'https://asset.example.com',
fetchImpl: async () => ({ ok: true, async json() { return responseData } })
})
await assert.rejects(client.reportJobStatus(7, { status: 'completed', claimToken: 'claim-seven' }), /明确的任务结果/)
responseData = { id: 7, claim_token: 'claim-seven', status: 'completed' }
await client.reportJobStatus(7, { status: 'completed', claimToken: 'claim-seven' })
})