fix(documents): sort newest rows first

This commit is contained in:
caoxiaozhu
2026-06-03 16:52:49 +08:00
parent 4717ee6086
commit 8c2f301d85
4 changed files with 84 additions and 4 deletions

View File

@@ -0,0 +1,33 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {
compareDocumentRowsByLatestTime,
sortDocumentRowsByLatestTime
} from '../src/utils/documentCenterSort.js'
test('document center sorts newest document rows first without mutating input', () => {
const rows = [
{ documentNo: 'AP-001', sortTime: 1000 },
{ documentNo: 'AP-003', sortTime: 3000 },
{ documentNo: 'AP-002', sortTime: 2000 }
]
const sortedRows = sortDocumentRowsByLatestTime(rows)
assert.deepEqual(sortedRows.map((row) => row.documentNo), ['AP-003', 'AP-002', 'AP-001'])
assert.deepEqual(rows.map((row) => row.documentNo), ['AP-001', 'AP-003', 'AP-002'])
})
test('document center sort falls back to created time and stable document keys', () => {
const rows = [
{ documentKey: 'owned:AP-001', documentNo: 'AP-001', sortTime: 1000, createdSortTime: 1000 },
{ documentKey: 'owned:AP-002', documentNo: 'AP-002', sortTime: 1000, createdSortTime: 2000 },
{ documentKey: 'owned:AP-003', documentNo: 'AP-003', sortTime: 1000, createdSortTime: 2000 }
]
const sortedRows = sortDocumentRowsByLatestTime(rows)
assert.deepEqual(sortedRows.map((row) => row.documentNo), ['AP-003', 'AP-002', 'AP-001'])
assert.equal(compareDocumentRowsByLatestTime(rows[1], rows[2]) > 0, true)
})