76 lines
2.0 KiB
JavaScript
76 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const LicensePlateService = require('../../../../app/service/systems/license_plate');
|
|
|
|
const fixtureImagePath = path.join(__dirname, '../../../../temp/cp.png');
|
|
const fixtureImageBase64 = fs.readFileSync(fixtureImagePath).toString('base64');
|
|
|
|
class TestLicensePlateService extends LicensePlateService {
|
|
buildTencentCloudRequest(requestConfig) {
|
|
this.requestAction = requestConfig.action;
|
|
return super.buildTencentCloudRequest(requestConfig);
|
|
}
|
|
|
|
async sendTencentCloudRequest(requestConfig) {
|
|
this.requestConfig = requestConfig;
|
|
return {
|
|
Response: {
|
|
LicensePlate: '桂MMU905',
|
|
Confidence: 98,
|
|
Color: '蓝色',
|
|
RequestId: 'req-test-001',
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
function createService(overrides = {}) {
|
|
const ctx = {
|
|
app: {
|
|
config: {
|
|
tencentCloud: {
|
|
appId: 1234567890,
|
|
secretId: 'AKID-public-1234567890',
|
|
secretKey: 'PUBLICSECRETKEY1234567890',
|
|
region: 'ap-guangzhou',
|
|
},
|
|
cos: {
|
|
region: 'ap-guangzhou',
|
|
},
|
|
},
|
|
},
|
|
logger: {
|
|
info() {},
|
|
warn() {},
|
|
error() {},
|
|
},
|
|
...overrides,
|
|
};
|
|
|
|
return new TestLicensePlateService(ctx);
|
|
}
|
|
|
|
describe('test/app/service/systems/license_plate.test.js', () => {
|
|
it('should read fixture image and return normalized recognition result', async () => {
|
|
const service = createService();
|
|
|
|
const result = await service.recognizeLicensePlate(fixtureImageBase64);
|
|
|
|
assert.equal(service.requestAction, 'LicensePlateOCR');
|
|
assert.equal(JSON.parse(service.requestConfig.payloadString).ImageBase64, fixtureImageBase64);
|
|
assert.equal(result.licensePlate, '桂MMU905');
|
|
});
|
|
|
|
it('should reject empty or invalid base64 input', async () => {
|
|
const service = createService();
|
|
|
|
await assert.rejects(
|
|
() => service.recognizeLicensePlate('not-a-base64-image'),
|
|
/base64 图片格式不正确/
|
|
);
|
|
});
|
|
});
|