fix: harden public download companion sync

This commit is contained in:
john
2026-07-01 11:10:58 +08:00
parent 6e4e0e2937
commit 6cb1a2475d
3 changed files with 160 additions and 32 deletions
+52
View File
@@ -258,6 +258,58 @@ test('syncPublicDocxDownloads skips public docx that is already up to date', ()
}
});
test('syncPublicDocxDownloads copies same-named oa docx even when it is small', () => {
const publishDir = fs.mkdtempSync(path.join(os.tmpdir(), 'public-docx-sync-'));
try {
const oaDir = path.join(publishDir, 'oa');
const publicDir = path.join(publishDir, 'public');
fs.mkdirSync(oaDir, { recursive: true });
fs.mkdirSync(publicDir, { recursive: true });
fs.writeFileSync(path.join(oaDir, 'industry-distribution-report.docx'), 'x'.repeat(3294));
fs.writeFileSync(
path.join(publicDir, 'industry-distribution-report.html'),
'<a href="industry-distribution-report.docx" download>download</a>',
);
const result = syncPublicDocxDownloads({ publishDir, minCompleteSize: 1024 });
assert.deepEqual(result.synced, ['public/industry-distribution-report.docx']);
assert.deepEqual(result.missing, []);
assert.equal(result.source, 'industry-distribution-report.docx');
assert.equal(
fs.readFileSync(path.join(publicDir, 'industry-distribution-report.docx'), 'utf8').length,
3294,
);
} finally {
fs.rmSync(publishDir, { recursive: true, force: true });
}
});
test('syncPublicDocxDownloads reports missing public docx when no safe source exists', () => {
const publishDir = fs.mkdtempSync(path.join(os.tmpdir(), 'public-docx-sync-'));
try {
const oaDir = path.join(publishDir, 'oa');
const publicDir = path.join(publishDir, 'public');
fs.mkdirSync(oaDir, { recursive: true });
fs.mkdirSync(publicDir, { recursive: true });
fs.writeFileSync(path.join(oaDir, '暑假计划.docx'), 'x'.repeat(4084));
fs.writeFileSync(path.join(oaDir, '韩国旅游攻略.docx'), 'x'.repeat(5889));
fs.writeFileSync(
path.join(publicDir, 'industry-distribution-report.html'),
'<a href="industry-distribution-report.docx" download>download</a>',
);
const result = syncPublicDocxDownloads({ publishDir, minCompleteSize: 1024 });
assert.deepEqual(result.synced, []);
assert.deepEqual(result.missing, ['public/industry-distribution-report.docx']);
assert.equal(
fs.existsSync(path.join(publicDir, 'industry-distribution-report.docx')),
false,
);
} finally {
fs.rmSync(publishDir, { recursive: true, force: true });
}
});
test('materializeMissingPublicHtmlWrites replaces existing real html when content changed', () => {
const publishDir = fs.mkdtempSync(path.join(os.tmpdir(), 'public-finish-sync-'));
try {