
给 Codex 一句「把日期筛选修好」,它得先猜什么叫「好」。这次要求查询 12 月 3 日的文章时包含 UTC 时间 23:59:59.999 发出的那篇,同时排除 12 月 4 日零点的文章。我们把这个条件写成测试,先复现问题,再检查修复后的行为。
一、先准备一个能复现的小项目
在 Windows PowerShell 里确认环境。本次样例使用 Node.js 22.21.0 和 Git 2.51.1.windows.1。跟做 Codex 操作前,先完成 CLI 登录或验证已有模型接入。已有可用配置就直接用;如果想试自定义模型供应商,可按 Windows 接入教程 配置的 来啦 API。只想复现测试和 Git 差异的话,不登录也能跟做。
node --version
git --version
codex --version
在空目录 codex-date-filter-demo 中创建下面两个文件。样例只使用 Node.js 内置模块,可以直接运行测试。
filter.mjs
export function filterPostsByDate(posts, from, to) {
const start = Date.parse(`${
from}T00:00:00.000Z`);
const end = Date.parse(`${
to}T00:00:00.000Z`);
return posts.filter(({
publishedAt }) => {
const time = Date.parse(publishedAt);
return time >= start && time <= end;
});
}
filter.test.mjs
import assert from 'node:assert/strict';
import test from 'node:test';
import {
filterPostsByDate } from './filter.mjs';
const posts = [
{
id: 'before', publishedAt: '2026-12-02T23:59:59.999Z' },
{
id: 'start', publishedAt: '2026-12-03T00:00:00.000Z' },
{
id: 'late', publishedAt: '2026-12-03T23:59:59.999Z' },
{
id: 'next', publishedAt: '2026-12-04T00:00:00.000Z' },
];
test('includes midnight at the start date', () => {
assert.deepEqual(filterPostsByDate([posts[1]], '2026-12-03', '2026-12-03'), [posts[1]]);
});
test('excludes posts outside the requested dates', () => {
assert.deepEqual(filterPostsByDate([posts[0], posts[3]], '2026-12-03', '2026-12-03'), []);
});
在两个文件所在的目录执行下面的命令。先保存一次基线提交,供后面审查改动。
node --test --test-reporter=spec
git init
git add -- filter.mjs filter.test.mjs
git -c user.name='Tutorial' -c user.email='tutorial@example.invalid' -c commit.gpgSign=false commit -m 'Baseline for UTC date filter'
git status --short
测试应当是 pass 2、fail 0。这两项检查了起始零点和范围外的文章,漏掉了截止日当天零点之后。

二、先让 Codex 写出失败测试
留在示例项目目录,通过 codex --sandbox workspace-write 启动 Codex CLI。把下面这段任务发给它,将第一轮改动限定在测试文件内。
请复现文章日期筛选的边界问题。本轮只改 filter.test.mjs。
输入日期是 YYYY-MM-DD,按 UTC 解释,截止日应包含整天。
用现有的四条 posts 样本查询 2026-12-03 到 2026-12-03。
新增一条回归测试,预期只返回 start 和 late,并保持原顺序。
运行 node --test --test-reporter=spec,指出实际少了哪条记录。
报告结果后暂停,等我确认再改实现。
也可以把下面的测试加到 filter.test.mjs 末尾,再运行同一条命令复现。
test('includes the entire end date through 23:59:59.999 UTC', () => {
assert.deepEqual(filterPostsByDate(posts, '2026-12-03', '2026-12-03'), [posts[1], posts[2]]);
});
此时应看到 pass 2、fail 1。差异里缺少 late,它的发布时间是 2026-12-03T23:59:59.999Z。检查 git diff --name-only HEAD,这一轮应只列出 filter.test.mjs。

三、让 Codex 修正日期边界
确认失败原因后,在同一个 Codex 会话里给出修复任务。
保留刚才的回归测试,只修改 filter.mjs。
范围从起始日的 UTC 零点开始,到截止日的次日 UTC 零点之前。
保留函数签名和结果顺序,只用现有的 Node.js 内置模块。
修改后运行 node --test --test-reporter=spec。
说明改动,报告测试结果,把 Git 改动留在工作区供我审查。
原实现把截止日当天的 00:00:00.000Z 当作上界,因此那天只有零点能通过。本次验证把上界推到次日零点,过滤条件使用 <,次日零点本身就会被排除。
const endExclusive = Date.parse(`${
to}T00:00:00.000Z`) + 86_400_000;
将原来的 const end 换成上面的定义,再把过滤条件改成 time >= start && time < endExclusive。86_400_000 是 24 小时的毫秒数,适用于这个按 UTC 自然日处理的示例。按当地日期筛选的项目应使用业务时区计算两个日界线。
四、自己再跑一遍测试
从 Codex 会话退出后,回到同一个 PowerShell 项目目录执行测试。
node --test --test-reporter=spec
$LASTEXITCODE
篇首的实测截图显示 pass 3、fail 0,退出码为 0。三条测试合起来覆盖了下面四个边界时间。
| 样本 | UTC 时间 | 应返回 |
|---|---|---|
before |
12 月 2 日 23:59:59.999 | 否 |
start |
12 月 3 日 00:00:00.000 | 是 |
late |
12 月 3 日 23:59:59.999 | 是 |
next |
12 月 4 日 00:00:00.000 | 否 |
五、用 Git 审查这次改动
先看整个仓库,再逐个读文件的具体差异。
git status --short
git --no-pager diff --no-color -- filter.mjs filter.test.mjs
git diff --check
git status --short应只列出两个示例文件。在
filter.mjs中确认开始边界用>=,结束边界用<。在
filter.test.mjs中确认原两项测试都保留,新增断言要求返回start和late。查看
git diff --check是否报空白错误,再结合测试结果核对筛选行为。

六、几个经常卡住的地方
| 遇到的情况 | 可以先这样排查 |
|---|---|
| CLI 无法接收任务 | 查看 codex login status,核对你的登录或模型接入配置 |
| 第一轮新增测试没有失败 | 用 git diff HEAD -- filter.mjs 核对实现是否提前被修改,再检查测试是否要求返回 late |
| 次日零点也被返回 | 查看截止条件是否写成了 < endExclusive,再用 next 样本确认 |
git diff 为空 |
先看 git status --short。如果改动已暂存,用 git diff HEAD 查看相对当前提交的差异 |
| 用本地日期测试时结果不一样 | 样例里的 Z 表示 UTC。核对业务采用的时区和输入解析方式 |
下次把缺陷交给 Codex 时,可以先写清输入和预期。看到旧实现确实失败,再修改代码,最后自己重跑测试并看一遍 diff。
本文由「来啦AI ·AI教程」原创整理。
测试和 Git 差异于 2026 年 12 月 3 日在 Windows 本地验证。配图为实际命令输出生成的本地报告截图。启动参数按 Codex CLI 0.148.0 核对。本机 CLI 当时未登录。两段提示词作为跟练步骤,待在已登录或已完成模型接入的环境实测。
参考资料:OpenAI Codex CLI 官方说明 · CLI 命令参考