WMS_MAIN/test/app/controller/tempFileWriter.test.js
liufumingren e73a311076 ok
2026-04-11 17:09:06 +08:00

86 lines
3.8 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { app } = require('egg-mock/bootstrap');
describe('test/app/controller/tempFileWriter.test.js', () => {
it('should consume fileWriter data through GET entry without date and skip completed data on second request', async () => {
const businessName = `temp-file-writer-${Date.now()}`;
const consumerName = 'controller-test';
const firstArchiveDate = new Date(2026, 3, 10);
const secondArchiveDate = new Date(2026, 3, 9);
const firstWrittenRecord = {
id: Date.now(),
name: 'temp-http-consume-record-1',
};
const secondWrittenRecord = {
id: Date.now() + 1,
name: 'temp-http-consume-record-2',
};
const firstWriteResult = await app.fileWriter.writeRecord(businessName, firstWrittenRecord, {
date: firstArchiveDate,
});
const secondWriteResult = await app.fileWriter.writeRecord(businessName, secondWrittenRecord, {
date: secondArchiveDate,
});
fs.utimesSync(firstWriteResult.filePath, new Date('2026-04-09T08:00:00.000Z'), new Date('2026-04-09T08:00:00.000Z'));
fs.utimesSync(secondWriteResult.filePath, new Date('2026-04-09T08:00:01.000Z'), new Date('2026-04-09T08:00:01.000Z'));
const progressFilePath = app.fileWriter.getProgressFilePath(businessName, null, consumerName);
try {
const firstRes = await app.httpRequest()
.get('/__temp/file-writer/consume')
.query({
businessName,
consumerName,
})
.expect('Content-Type', /json/)
.expect(200);
assert.equal(firstRes.body.flag, 'success');
assert.equal(Number(firstRes.body.code), 1);
assert.equal(firstRes.body.data.businessName, businessName);
assert.equal(firstRes.body.data.date, undefined);
assert.equal(firstRes.body.data.processedCount, 2);
assert.equal(Array.isArray(firstRes.body.data.consumedRecords), true);
assert.equal(firstRes.body.data.consumedRecords.length, 2);
assert.equal(Array.isArray(firstRes.body.data.dataFiles), true);
assert.equal(firstRes.body.data.dataFiles.length, 2);
assert.equal(firstRes.body.data.consumedRecords[0].name, secondWrittenRecord.name);
assert.equal(firstRes.body.data.consumedRecords[1].name, firstWrittenRecord.name);
assert.equal(firstRes.body.data.progress.filePath, path.resolve(firstWriteResult.filePath));
assert.equal(firstRes.body.data.progress.processedCount, 2);
const secondRes = await app.httpRequest()
.get('/__temp/file-writer/consume')
.query({
businessName,
consumerName,
})
.expect('Content-Type', /json/)
.expect(200);
assert.equal(secondRes.body.flag, 'success');
assert.equal(Number(secondRes.body.code), 1);
assert.equal(secondRes.body.data.businessName, businessName);
assert.equal(secondRes.body.data.date, undefined);
assert.equal(secondRes.body.data.processedCount, 0);
assert.equal(Array.isArray(secondRes.body.data.consumedRecords), true);
assert.equal(secondRes.body.data.consumedRecords.length, 0);
assert.equal(Array.isArray(secondRes.body.data.dataFiles), true);
assert.equal(secondRes.body.data.dataFiles.length, 2);
assert.equal(secondRes.body.data.progress.filePath, path.resolve(firstWriteResult.filePath));
assert.equal(fs.existsSync(progressFilePath), true);
assert.equal(fs.existsSync(app.fileWriter.getDoneFilePath(firstWriteResult.filePath)), true);
assert.equal(fs.existsSync(app.fileWriter.getDoneFilePath(secondWriteResult.filePath)), true);
} finally {
const businessDir = path.join(app.fileWriter.baseDir, app.fileWriter.sanitizeSegment(businessName));
fs.rmSync(businessDir, { recursive: true, force: true });
}
});
});