


























11// Covers cron-style current-time formatting and invalid Date fallbacks.
22import { afterEach, describe, expect, it, vi } from "vitest";
3-import { resolveCronStyleNow } from "./current-time.js";
3+import { appendCronStyleCurrentTimeLine, resolveCronStyleNow } from "./current-time.js";
4455describe("resolveCronStyleNow", () => {
66afterEach(() => {
@@ -33,3 +33,75 @@ describe("resolveCronStyleNow", () => {
3333expect(result.timeLine).toContain("Reference UTC: 1970-01-01 00:00 UTC");
3434});
3535});
36+37+const CFG = {
38+agents: {
39+defaults: {
40+userTimezone: "UTC",
41+},
42+},
43+};
44+45+describe("appendCronStyleCurrentTimeLine", () => {
46+it("returns the empty input unchanged", () => {
47+expect(appendCronStyleCurrentTimeLine("", CFG, Date.now())).toBe("");
48+});
49+50+it("appends a Current time line when none is present", () => {
51+const out = appendCronStyleCurrentTimeLine(
52+"Heartbeat tick",
53+CFG,
54+Date.parse("2026-04-30T10:00:00Z"),
55+);
56+expect(out).toContain("Heartbeat tick");
57+expect(out).toMatch(/Reference UTC: 2026-04-30 10:00 UTC/);
58+});
59+60+it("refreshes an existing Current time line on subsequent calls (#44993)", () => {
61+const oldNow = Date.parse("2026-04-30T08:00:00Z");
62+const newNow = Date.parse("2026-04-30T10:00:00Z");
63+const firstPass = appendCronStyleCurrentTimeLine("Heartbeat tick", CFG, oldNow);
64+expect(firstPass).toMatch(/Reference UTC: 2026-04-30 08:00 UTC/);
65+66+const secondPass = appendCronStyleCurrentTimeLine(firstPass, CFG, newNow);
67+expect(secondPass).toContain("Heartbeat tick");
68+expect(secondPass).toMatch(/Reference UTC: 2026-04-30 10:00 UTC/);
69+expect(secondPass).not.toMatch(/Reference UTC: 2026-04-30 08:00 UTC/);
70+expect(secondPass.match(/Current time:/g)?.length).toBe(1);
71+});
72+73+it("collapses multiple Current time blocks into a single fresh entry", () => {
74+const stale = [
75+"Heartbeat tick",
76+"Current time: Wednesday, January 1st, 2025 - 12:00 AM (UTC)\nReference UTC: 2025-01-01 00:00 UTC",
77+"Current time: Thursday, January 2nd, 2025 - 12:00 AM (UTC)\nReference UTC: 2025-01-02 00:00 UTC",
78+].join("\n");
79+const newNow = Date.parse("2026-04-30T10:00:00Z");
80+const out = appendCronStyleCurrentTimeLine(stale, CFG, newNow);
81+expect(out).toContain("Heartbeat tick");
82+expect(out).toMatch(/Reference UTC: 2026-04-30 10:00 UTC/);
83+expect(out).not.toMatch(/Reference UTC: 2025-01-01 00:00 UTC/);
84+expect(out).not.toMatch(/Reference UTC: 2025-01-02 00:00 UTC/);
85+expect(out.match(/Current time:/g)?.length).toBe(1);
86+});
87+88+it("matches helper blocks with natural-language formattedTime (#44993 codex P1)", () => {
89+const helperShape =
90+"Heartbeat tick\nCurrent time: Thursday, April 30th, 2026 - 10:00 AM (Asia/Seoul)\nReference UTC: 2026-04-30 01:00 UTC";
91+const newNow = Date.parse("2026-04-30T10:00:00Z");
92+const out = appendCronStyleCurrentTimeLine(helperShape, CFG, newNow);
93+expect(out).not.toMatch(/Asia\/Seoul/);
94+expect(out.match(/Current time:/g)?.length).toBe(1);
95+expect(out).toMatch(/Reference UTC: 2026-04-30 10:00 UTC/);
96+});
97+98+it("preserves user-authored content that starts with 'Current time:'", () => {
99+const userContent = "Reminder from cron:\nCurrent time: please check the dashboard before EOD";
100+const newNow = Date.parse("2026-04-30T10:00:00Z");
101+const out = appendCronStyleCurrentTimeLine(userContent, CFG, newNow);
102+expect(out).toContain("Reminder from cron:");
103+expect(out).toContain("Current time: please check the dashboard before EOD");
104+expect(out).toMatch(/Current time: .+? \(UTC\)\nReference UTC: 2026-04-30 10:00 UTC/);
105+expect(out.match(/Current time:/g)?.length).toBe(2);
106+});
107+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。