task-41: storage 恢复与清理测试
恢复过滤非正数/非数字、空列表清除过期 key、终态同步 storage、 写失败不抛、坏 JSON 容错、恢复即轮询、无 key 不操作。修复恢复 出空列表时遗留 key 未清理的问题。共 8 个测试。
This commit is contained in:
@@ -108,7 +108,13 @@ function readIdsFromStorage(key?: string): number[] {
|
||||
const raw = window.localStorage.getItem(key)
|
||||
if (!raw) return []
|
||||
const parsed = JSON.parse(raw) as unknown
|
||||
return Array.isArray(parsed) ? parsed.filter((n): n is number => typeof n === 'number' && n > 0) : []
|
||||
if (!Array.isArray(parsed)) return []
|
||||
const ids = parsed.filter((n): n is number => typeof n === 'number' && n > 0)
|
||||
if (ids.length === 0) {
|
||||
// 恢复出空列表时顺手清掉过期 key(历史遗留的空数组)
|
||||
window.localStorage.removeItem(key)
|
||||
}
|
||||
return ids
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user