test.js: accept UNIX seconds/ms and date strings for timestamp arg (fixes #16493) (#16633)

Co-authored-by: waynebruce0x <waynebruce0x@gmail.com>
This commit is contained in:
jayssj11
2025-10-13 19:43:36 +05:30
committed by GitHub
parent 07368c1c5e
commit f98ad54e6f

21
test.js
View File

@@ -138,9 +138,26 @@ function validateHallmarks(hallmark) {
let unixTimestamp = Math.round(Date.now() / 1000) - 60;
let chainBlocks = {}
const passedTimestamp = process.argv[3] ? Math.floor(new Date(process.argv[3]) / 1000) : undefined
function parseTimestampArg(arg) {
if (!arg) return undefined;
// Accept pure numeric input as unix seconds or milliseconds
if (/^\d+$/.test(arg)) {
const num = Number(arg);
if (!Number.isFinite(num)) return undefined;
// 13+ digits -> milliseconds, else assume seconds
return num > 1e12 ? Math.floor(num / 1000) : num;
}
// Fallback to Date string parsing (e.g., YYYY-MM-DD)
const d = new Date(arg);
const ms = d.getTime();
if (!Number.isFinite(ms)) return undefined;
return Math.floor(ms / 1000);
}
const passedTimestamp = parseTimestampArg(process.argv[3]);
if (passedTimestamp !== undefined) {
unixTimestamp = Number(passedTimestamp)
unixTimestamp = passedTimestamp
// other chains than evm will fail to get block at timestamp
try {