vin-ocr-bridge.test.mjs
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
const {
openVinOcr,
handleReWrite,
normalizeVin,
clearPending,
getPending,
OCR_ROUTE
} = require('../services/vinOcrBridge.js')
const {
resolveVinFieldContext,
inferGroupKeyFromChildKey
} = require('../services/vin-ocr-context.js')
describe('vinOcrBridge', () => {
it('IT-VIN-OCR-001: normalizeVin trims and uppercases', () => {
assert.equal(normalizeVin(' lsggf53w8ch045613 '), 'LSGGF53W8CH045613')
})
it('IT-VIN-OCR-002: handleReWrite invokes onResult and clears pending', () => {
clearPending()
let result = null
openVinOcr({
childKey: 'F3-1-7',
groupKey: 'F3-1',
rowIndex: 0,
onResult(vin) {
result = vin
}
})
assert.ok(getPending())
const handled = handleReWrite('standNo', 'lsggf53w8ch045613')
assert.equal(handled, true)
assert.equal(result, 'LSGGF53W8CH045613')
assert.equal(getPending(), null)
})
it('IT-VIN-OCR-003: handleReWrite returns false without pending', () => {
clearPending()
assert.equal(handleReWrite('standNo', 'ABC'), false)
})
it('IT-VIN-OCR-004: handleReWrite ignores non-standNo type', () => {
clearPending()
openVinOcr({
childKey: 'x',
onResult() {}
})
assert.equal(handleReWrite('plateNumber', '粤A12345'), false)
assert.ok(getPending())
clearPending()
})
it('IT-VIN-OCR-005: OCR_ROUTE uses cross-subpackage path', () => {
assert.match(OCR_ROUTE, /^tabWorkOrder\.ocr/)
})
})
describe('vin OCR group field write', async () => {
const { createRequire } = await import('node:module')
const require = createRequire(import.meta.url)
const { loadSchemaFixture } = await import('./schemaFixture.mjs')
const negotiationSchema = loadSchemaFixture('negotiation')
const { setRowFieldInCache, syncValuesFromGroupCache } = await import(
'../form-engine/group-rows-cache.js'
)
const { cloneRowTemplate } = await import(
'../form-engine/group-row-adapter.js'
)
const group = negotiationSchema.list.find(item => item.key === 'F3-1')
const template = cloneRowTemplate(group)
it('IT-VIN-OCR-006: seeded row syncs VIN into values[groupKey] array', () => {
const vin = 'LSGGF53W8CH045613'
const cache = setRowFieldInCache(
{ 'F3-1': [[]] },
'F3-1',
0,
'F3-1-7',
vin,
template
)
const values = syncValuesFromGroupCache({}, 'F3-1', cache['F3-1'])
const vinField = (cache['F3-1']?.[0] || []).find(f => f.key === 'F3-1-7')
assert.equal(vinField?.value, vin)
assert.equal(values['F3-1']?.[0]?.['F3-1-7'], vin)
})
it('IT-VIN-OCR-007: setRowFieldInCache seeds empty row from template', () => {
const next = setRowFieldInCache(
{ 'F3-1': [[]] },
'F3-1',
0,
'F3-1-7',
'ABC123',
template
)
const vinField = (next['F3-1']?.[0] || []).find(f => f.key === 'F3-1-7')
assert.equal(vinField?.value, 'ABC123')
})
})
describe('resolveVinFieldContext', () => {
it('IT-VIN-OCR-008: infers groupKey from childKey F3-1-7', () => {
assert.equal(inferGroupKeyFromChildKey('F3-1-7'), 'F3-1')
})
it('IT-VIN-OCR-009: undefined rowIndex defaults to active index 0', () => {
const resolved = resolveVinFieldContext(
{ childKey: 'F3-1-7', groupKey: 'F3-1', rowIndex: undefined },
{ 'F3-1': 0 }
)
assert.equal(resolved.isGroupField, true)
assert.equal(resolved.groupKey, 'F3-1')
assert.equal(resolved.rowIndex, 0)
})
it('IT-VIN-OCR-010: missing groupKey and rowIndex inferred for group child', () => {
const resolved = resolveVinFieldContext(
{ childKey: 'F3-1-7', rowIndex: undefined },
{ 'F3-1': 1 }
)
assert.equal(resolved.groupKey, 'F3-1')
assert.equal(resolved.rowIndex, 1)
})
it('IT-VIN-OCR-011: negative rowIndex falls back to groupActiveIndex', () => {
const resolved = resolveVinFieldContext(
{ childKey: 'F3-2-7', groupKey: 'F3-2', rowIndex: -1 },
{ 'F3-2': 2 }
)
assert.equal(resolved.rowIndex, 2)
})
})