import { describe, it, expect } from "vitest"; import { COMPLETED_WINDOW_DAYS, completedWindowCutoff, completedTabFilter, } from "./orders-service"; describe("completedWindowCutoff", () => { it("returns an ISO string COMPLETED_WINDOW_DAYS before `now`", () => { const now = new Date("2026-08-23T12:00:00.000Z"); const cutoff = completedWindowCutoff(now); const expected = new Date( now.getTime() - COMPLETED_WINDOW_DAYS * 24 * 60 * 60 * 1000, ); expect(cutoff).toBe(expected.toISOString()); }); it("uses the supplied `now` argument (pure function, no clock dep)", () => { const fixed = new Date("2026-01-15T00:00:00.000Z"); const cutoff = completedWindowCutoff(fixed); // 30 days before 2026-01-15 = 2025-12-16 expect(cutoff).toBe("2025-12-16T00:00:00.000Z"); }); it("defaults to wall-clock `new Date()` when called with no args", () => { const before = Date.now(); const cutoff = completedWindowCutoff(); const cutoffMs = new Date(cutoff).getTime(); const expectedLatest = before - COMPLETED_WINDOW_DAYS * 24 * 60 * 60 * 1000 + 50; expect(cutoffMs).toBeLessThanOrEqual(expectedLatest); }); }); describe("completedTabFilter", () => { it("applies the time-windowed predicate for both terminal statuses", () => { const now = new Date("2026-08-23T00:00:00.000Z"); const cutoff = completedWindowCutoff(now); const filter = completedTabFilter(now); // delivered items are filtered by delivered_at, not updated, so // older rows (delivered before the window) drop out. expect(filter).toContain( `(item_status = "delivered" && delivered_at >= "${cutoff}")`, ); // completed items have no dedicated timestamp, so we use updated. expect(filter).toContain( `(item_status = "completed" && updated >= "${cutoff}")`, ); // The two clauses must be OR-joined (PB doesn't have an IN-list // for date predicates). expect(filter).toContain(") || ("); }); it("uses the supplied `now` so the filter is stable for tests", () => { const now = new Date("2026-07-01T00:00:00.000Z"); expect(completedTabFilter(now)).toBe( `(item_status = "delivered" && delivered_at >= "${completedWindowCutoff(now)}")` + ` || (item_status = "completed" && updated >= "${completedWindowCutoff(now)}")`, ); }); });