145 lines
7.4 KiB
TypeScript
145 lines
7.4 KiB
TypeScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readdirSync, statSync } from 'node:fs'
|
||
import { join } from 'node:path'
|
||
import { readSource } from './helpers.ts'
|
||
import { todayDateInput, todayDateTimeRange } from '../src/utils/datetime.ts'
|
||
|
||
// 后台日期筛选默认值口径(2026-10-04 定,同日三轮收窄):
|
||
// 首屏拉「一段时间的存量记录」的页 ⇒ 初值留空;首屏本就是「今天这件事」的页 ⇒ 仍默认当天。
|
||
// 凡渲染了日期筛选控件的页面/组件,必须落在下面两个名单之一,新增页未登记会被本用例拦下。
|
||
|
||
const DATE_CONTROL_RES = [/type="date"/, /type="datetime-local"/, /<el-date-picker/]
|
||
|
||
/** 默认当天:告警按天巡检、日志与用量按天聚合,首屏看的就是今天。 */
|
||
const DEFAULTS_TO_TODAY = new Set([
|
||
'src/components/NotificationBell.vue',
|
||
'src/pages/account/UserSecretUsagePage.vue',
|
||
'src/pages/records/DeviceLogsPage.vue',
|
||
])
|
||
|
||
/** 有意留空:日期语义不是"操作发生时间"、或默认当天会让首屏几乎为空(均为用户 2026-10-04 确认)。
|
||
* - DuplicateCheckPage:该页日期是 ASIN「上架时间」。
|
||
* - DedupeRegistryPage:去重数据汇总是累计台账,默认当天只剩今天新增的 ASIN。
|
||
* - TaskCenterBoardPage:该页首屏要与汇总卡「进行中任务」同数,而卡片的 SQL 不带创建时间窗;
|
||
* 默认锁当天必然少掉跨天仍在上报的长任务(10-04 实测 21 vs 39,差 18 个),用户当日定案
|
||
* 「默认不带时间窗」。时间控件保留,只是初值为空。
|
||
* - ShopDataTasksPage / ImageVideoTasksPage / RecordsHistoryPage:三张任务记录列表页同因——
|
||
* 任务按创建/提交时间落在过去某天,默认当天把首屏收成"只有今天",用户 10-04 见到
|
||
* 「店铺数据记录」首屏"暂无符合条件的店铺数据记录"后定案「也不要加默认当天时间」。 */
|
||
const EXEMPT = new Set([
|
||
'src/pages/tasks/DuplicateCheckPage.vue',
|
||
'src/pages/asin/DedupeRegistryPage.vue',
|
||
'src/pages/task-center/TaskCenterBoardPage.vue',
|
||
'src/pages/tasks/ShopDataTasksPage.vue',
|
||
'src/pages/tasks/ImageVideoTasksPage.vue',
|
||
'src/pages/records/RecordsHistoryPage.vue',
|
||
// 2026-11-10(v1.3):外观专利无侵权数据页与去重库同形状——出表成功自动同步的累计台账,
|
||
// 默认当天会让首屏只剩今天同步的行,历史同步行整批"消失"。
|
||
'src/pages/patent/AppearancePatentCleanItemsPage.vue',
|
||
// 2026-11-14(v1.4 Phase 5):京东数据库页与外观专利页同形状——京东商品同步落库的累计台账,
|
||
// 默认当天会让首屏只剩今天同步的行(页面注释与 filter 工厂同口径留空)。
|
||
'src/pages/jd/JdDatabasePage.vue',
|
||
// 2026-10-05(V146):两处是「到期时间」表单字段而非列表筛选——留空 = 永不过期,
|
||
// 是有业务语义的默认值,绝不能默认当天(默认当天等于建号即过期)。
|
||
'src/pages/account/CreateUserDialog.vue',
|
||
'src/pages/account/EditUserDialog.vue',
|
||
])
|
||
|
||
/** 把日期筛选清回空值的写法(默认当天的页里初值或重置都不许出现)。
|
||
* dateRange 侧要同时认 `[]` 与 `['', '']`(后者是留空页的写法,正则漏掉它会让"移出豁免名单"时本断言恒绿)。 */
|
||
const DATE_CLEARED =
|
||
/(?:startDate|endDate|timeStart|timeEnd|startTime|endTime|dateFrom|dateTo)(?:\.value)?\s*[:=]\s*''|dateRange\s*[:=]\s*\[\s*(?:''\s*,?\s*){0,2}\]/
|
||
|
||
/** 去掉 import 行:`import { todayDateInput }` 这种"只引不用"不能算接入(变异实测会造恒绿)。 */
|
||
function withoutImportLines(src: string): string {
|
||
return src.split(/\r?\n/).filter((line) => !/^\s*import\b/.test(line)).join('\n')
|
||
}
|
||
|
||
function hasDateControl(src: string): boolean {
|
||
return DATE_CONTROL_RES.some((re) => re.test(src))
|
||
}
|
||
|
||
function vueFiles(): string[] {
|
||
const out: string[] = []
|
||
const walk = (dir: string): void => {
|
||
for (const name of readdirSync(dir)) {
|
||
const abs = join(dir, name)
|
||
if (statSync(abs).isDirectory()) walk(abs)
|
||
else if (name.endsWith('.vue')) out.push(abs.split(/[\\/]+/).join('/'))
|
||
}
|
||
}
|
||
walk('src')
|
||
return out
|
||
}
|
||
|
||
const withDateControl = vueFiles().filter((rel) => hasDateControl(readSource(rel)))
|
||
/** 带日期控件、且未登记为「留空」的页 ⇒ 必须默认当天。 */
|
||
const managed = withDateControl.filter((rel) => !EXEMPT.has(rel))
|
||
|
||
test('test_default_today_normal_inventory_is_not_empty', () => {
|
||
// 分母由扫描得出、不自挑:带日期控件的文件共 9 个 ⇒ 默认当天 3 + 有意留空 6。
|
||
// 阈值 8:留一档余量,再掉才说明控件识别正则失效;单纯又多豁免一页不该假红。
|
||
assert.ok(withDateControl.length >= 8, `只扫到 ${withDateControl.length} 个带日期筛选的文件,扫描规则可能已失效`)
|
||
})
|
||
|
||
test('test_default_today_normal_two_lists_are_complementary', () => {
|
||
// 两张名单互为补集:带日期控件的页要么默认当天、要么登记留空,漏登记会被点名而不是悄悄漏掉。
|
||
// 也顺带拦住「同一页写进两张名单」——那会让它从 managed 消失、默认当天断言对它恒绿。
|
||
assert.deepEqual(
|
||
[...managed].sort(),
|
||
[...DEFAULTS_TO_TODAY].sort(),
|
||
`默认当天名单与实际接入不一致,未登记留空的页=${managed.filter((r) => !DEFAULTS_TO_TODAY.has(r))}`,
|
||
)
|
||
})
|
||
|
||
test('test_default_today_normal_every_date_filter_page_is_wired', () => {
|
||
for (const rel of managed) {
|
||
const src = readSource(rel)
|
||
assert.match(
|
||
withoutImportLines(src),
|
||
/todayDateInput\(|todayDateTimeRange\(/,
|
||
`${rel} 有日期筛选控件但未默认当天`,
|
||
)
|
||
assert.match(src, /from ['"]@\/utils\/datetime['"]/, `${rel} 的 today* 取值须来自 @/utils/datetime`)
|
||
}
|
||
})
|
||
|
||
test('test_default_today_normal_reset_keeps_today', () => {
|
||
for (const rel of managed) {
|
||
const hit = readSource(rel)
|
||
.split(/\r?\n/)
|
||
.filter((line) => DATE_CLEARED.test(line))
|
||
assert.deepEqual(hit, [], `${rel} 仍把日期筛选清成空值:\n${hit.join('\n')}`)
|
||
}
|
||
})
|
||
|
||
test('test_default_today_boundary_exempt_still_intentional', () => {
|
||
for (const rel of EXEMPT) {
|
||
const src = readSource(rel)
|
||
assert.ok(hasDateControl(src), `${rel} 已无日期筛选控件,应从豁免清单移除`)
|
||
assert.equal(
|
||
/todayDateInput\(|todayDateTimeRange\(/.test(withoutImportLines(src)),
|
||
false,
|
||
`${rel} 已被排除,不得接入默认当天`,
|
||
)
|
||
}
|
||
})
|
||
|
||
test('test_default_today_boundary_values_match_backend_patterns', () => {
|
||
const day = todayDateInput()
|
||
assert.match(day, /^\d{4}-\d{2}-\d{2}$/, 'type="date" 与各页 ISO 日期校验')
|
||
const [start, end] = todayDateTimeRange()
|
||
const LOCAL_DATETIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2})?$/
|
||
assert.match(start, LOCAL_DATETIME, 'datetime-local 各页的秒级校验')
|
||
assert.match(end, LOCAL_DATETIME)
|
||
assert.equal(day, start.slice(0, 10), '日期与区间必须同一天,不能跨零点各算各的')
|
||
assert.ok(start < end, '起须早于止,否则各页「开始日期不能晚于结束」校验会弹警告')
|
||
})
|
||
|
||
test('test_default_today_invalid_input_pads_and_uses_local_time', () => {
|
||
const fixed = new Date(2026, 0, 5, 8, 30, 0)
|
||
assert.equal(todayDateInput(fixed), '2026-01-05', '月/日须补零')
|
||
assert.deepEqual(todayDateTimeRange(fixed), ['2026-01-05T00:00', '2026-01-05T23:59'])
|
||
})
|