Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions packages/client-runtime/src/state/threadRecencyGroups.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,27 @@ import {
THREAD_RECENCY_BUCKET_LABELS,
} from "./threadRecencyGroups.ts";

/** Local calendar fixture; Date APIs are intentional for bucket tests. */
function localDate(
year: number,
monthIndex: number,
day: number,
hours = 0,
minutes = 0,
seconds = 0,
): Date {
// @effect-diagnostics-next-line globalDate:off
return new Date(year, monthIndex, day, hours, minutes, seconds);
}

function addMs(date: Date, ms: number): Date {
// @effect-diagnostics-next-line globalDate:off
return new Date(date.getTime() + ms);
}

describe("getThreadRecencyBucketId", () => {
// Fixed local morning so calendar math is stable across CI timezones.
const now = new Date(2026, 2, 15, 14, 30, 0); // 2026-03-15 local
const now = localDate(2026, 2, 15, 14, 30, 0); // 2026-03-15 local

it("classifies today, yesterday, previous 7, previous 30, and older", () => {
const startToday = startOfLocalDay(now).getTime();
Expand All @@ -31,7 +49,7 @@ describe("getThreadRecencyBucketId", () => {
});

describe("groupThreadsByRecency", () => {
const now = new Date(2026, 2, 15, 12, 0, 0);
const now = localDate(2026, 2, 15, 12, 0, 0);
const startToday = startOfLocalDay(now).getTime();

it("returns only non-empty buckets in order with labels", () => {
Expand Down Expand Up @@ -61,10 +79,10 @@ describe("groupThreadsByRecency", () => {

describe("groupSortedThreadsByRecency", () => {
it("groups using activity timestamps from ThreadSortInput", () => {
const now = new Date(2026, 2, 15, 12, 0, 0);
const now = localDate(2026, 2, 15, 12, 0, 0);
const startToday = startOfLocalDay(now);
const todayIso = new Date(startToday.getTime() + 3_600_000).toISOString();
const olderIso = new Date(startToday.getTime() - 40 * 24 * 60 * 60 * 1000).toISOString();
const todayIso = addMs(startToday, 3_600_000).toISOString();
const olderIso = addMs(startToday, -40 * 24 * 60 * 60 * 1000).toISOString();

const groups = groupSortedThreadsByRecency(
[
Expand Down
23 changes: 19 additions & 4 deletions packages/client-runtime/src/state/threadRecencyGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,23 @@ export const THREAD_RECENCY_BUCKET_LABELS: Record<ThreadRecencyBucketId, string>

const MS_PER_DAY = 24 * 60 * 60 * 1000;

function makeDateFromEpochMs(ms: number): Date {
// @effect-diagnostics-next-line globalDate:off
return new Date(ms);
}

function makeLocalDate(year: number, monthIndex: number, day: number): Date {
// @effect-diagnostics-next-line globalDate:off
return new Date(year, monthIndex, day);
}

function makeNow(): Date {
// @effect-diagnostics-next-line globalDate:off
return new Date();
}

export function startOfLocalDay(date: Date): Date {
return new Date(date.getFullYear(), date.getMonth(), date.getDate());
return makeLocalDate(date.getFullYear(), date.getMonth(), date.getDate());
}

/**
Expand All @@ -39,7 +54,7 @@ export function startOfLocalDay(date: Date): Date {
*/
export function getThreadRecencyBucketId(
timestampMs: number,
now: Date = new Date(),
now: Date = makeNow(),
): ThreadRecencyBucketId {
if (!Number.isFinite(timestampMs)) {
return "older";
Expand Down Expand Up @@ -81,7 +96,7 @@ export interface ThreadRecencyGroup<T> {
export function groupThreadsByRecency<T>(
threads: readonly T[],
getTimestampMs: (thread: T) => number,
now: Date = new Date(),
now: Date = makeNow(),
): ReadonlyArray<ThreadRecencyGroup<T>> {
const buckets = new Map<ThreadRecencyBucketId, T[]>();
for (const id of THREAD_RECENCY_BUCKET_ORDER) {
Expand Down Expand Up @@ -112,7 +127,7 @@ export function groupThreadsByRecency<T>(
*/
export function groupSortedThreadsByRecency<T extends { readonly id: string } & ThreadSortInput>(
threads: readonly T[],
now: Date = new Date(),
now: Date = makeNow(),
): ReadonlyArray<ThreadRecencyGroup<T>> {
return groupThreadsByRecency(
threads,
Expand Down
Loading