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"/, / !/^\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 managed = vueFiles().filter((rel) => !EXEMPT.has(rel) && hasDateControl(readSource(rel))) test('test_default_today_normal_inventory_is_not_empty', () => { // 分母由扫描得出、不自挑:掉到 7 以下说明控件识别正则已失效(会造出恒绿) assert.ok(managed.length >= 7, `只扫到 ${managed.length} 个带日期筛选的文件,扫描规则可能已失效`) }) 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']) })