fix: allocate export names in linear time

This commit is contained in:
2026-07-22 20:13:08 +02:00
parent 141a06dbf3
commit 6a87ef6510
2 changed files with 50 additions and 9 deletions

View File

@@ -436,6 +436,28 @@ describe('OneNote export filenames', () => {
expect(allocator.allocate('Report.txt')).toBe('Report.txt');
expect(allocator.allocate('report.TXT')).toBe('report-2.txt');
});
it('keeps collision suffixes within the filename cap', () => {
const allocator = new SafeExportNameAllocator();
const filename = `${'x'.repeat(116)}.txt`;
expect(filename).toHaveLength(120);
expect(allocator.allocate(filename)).toBe(filename);
const duplicate = allocator.allocate(filename);
expect(duplicate).toBe(`${'x'.repeat(114)}-2.txt`);
expect([...duplicate]).toHaveLength(120);
});
it('allocates many duplicate names without rescanning prior suffixes', () => {
const allocator = new SafeExportNameAllocator();
const names = Array.from({ length: 10_000 }, () =>
allocator.allocate('Report.txt')
);
expect(names[0]).toBe('Report.txt');
expect(names.at(-1)).toBe('Report-10000.txt');
expect(new Set(names)).toHaveLength(names.length);
});
});
function fixtureSnapshot(): OneNoteExportSnapshot {