Merge remote-tracking branch 'origin/feat/coach-frame' into release/v1.40.2

This commit is contained in:
Marc-André Bombeck committed 2026-10-05 07:16:09 +02:00
commit 57a889ef3c
34 files changed
+2659 -428

No files matched your search

@@ -0,0 +1,48 @@
import { describe, expect, it } from "vitest";
import { groupConversationsByRecency } from "../coach-conversation-groups";
// The suite runs with TZ=UTC, so local day boundaries are UTC midnights.
const NOW = new Date("2026-10-04T15:00:00.000Z");
function c(id: string, updatedAt: string) {
return { id, updatedAt };
}
describe("groupConversationsByRecency", () => {
it("buckets by local day: today, yesterday, the six days before, earlier", () => {
const groups = groupConversationsByRecency(
[
c("a", "2026-10-04T00:00:00.000Z"), // today, at midnight
c("b", "2026-10-03T23:59:59.000Z"), // yesterday, last second
c("c", "2026-10-03T00:00:00.000Z"), // yesterday, first second
c("d", "2026-09-28T00:00:00.000Z"), // six days before today
c("e", "2026-09-27T23:59:59.000Z"), // one second older: earlier
],
NOW,
);
expect(groups.map((g) => [g.id, g.conversations.map((x) => x.id)])).toEqual(
[
["today", ["a"]],
["yesterday", ["b", "c"]],
["thisWeek", ["d"]],
["earlier", ["e"]],
],
);
});
it("drops empty groups and keeps the caller's order inside a group", () => {
const groups = groupConversationsByRecency(
[c("x", "2026-01-02T00:00:00.000Z"), c("y", "2026-03-02T00:00:00.000Z")],
NOW,
);
expect(groups).toHaveLength(1);
expect(groups[0].id).toBe("earlier");
expect(groups[0].labelKey).toBe("insights.coach.history.groupEarlier");
expect(groups[0].conversations.map((x) => x.id)).toEqual(["x", "y"]);
});
it("returns nothing for an empty list", () => {
expect(groupConversationsByRecency([], NOW)).toEqual([]);
});
});
@@ -0,0 +1,58 @@
/**
* Bucket Coach conversations by how recently they were last touched: Today,
* Yesterday, This week (the six days before yesterday), Earlier.
*
* Shared by the Coach page's conversations panel and the standalone
* conversation list, so both group a thread the same way. Comparisons run
* against LOCAL day boundaries, so "Today" follows the person's own midnight
* rather than UTC's. Empty groups are dropped; order inside a group is the
* caller's (the server already returns `updatedAt desc`).
*/
export type CoachRecencyGroupId =
"today" | "yesterday" | "thisWeek" | "earlier";
export interface CoachRecencyGroup<T> {
id: CoachRecencyGroupId;
labelKey: string;
conversations: T[];
}
const DAY_MS = 24 * 60 * 60 * 1000;
const GROUPS: ReadonlyArray<{ id: CoachRecencyGroupId; labelKey: string }> = [
{ id: "today", labelKey: "insights.coach.history.groupToday" },
{ id: "yesterday", labelKey: "insights.coach.history.groupYesterday" },
{ id: "thisWeek", labelKey: "insights.coach.history.groupThisWeek" },
{ id: "earlier", labelKey: "insights.coach.history.groupEarlier" },
];
export function groupConversationsByRecency<T extends { updatedAt: string }>(
conversations: readonly T[],
now: Date = new Date(),
): CoachRecencyGroup<T>[] {
const startOfToday = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
).getTime();
const startOfYesterday = startOfToday - DAY_MS;
const startOfWeek = startOfToday - 6 * DAY_MS;
const buckets: Record<CoachRecencyGroupId, T[]> = {
today: [],
yesterday: [],
thisWeek: [],
earlier: [],
};
for (const conversation of conversations) {
const updated = new Date(conversation.updatedAt).getTime();
if (updated >= startOfToday) buckets.today.push(conversation);
else if (updated >= startOfYesterday) buckets.yesterday.push(conversation);
else if (updated >= startOfWeek) buckets.thisWeek.push(conversation);
else buckets.earlier.push(conversation);
}
return GROUPS.map((g) => ({ ...g, conversations: buckets[g.id] })).filter(
(g) => g.conversations.length > 0,
);
}