Skip to content

Commit b987aae

Browse files
CodeRabbit Generated Unit Tests: Add unit tests for PR changes
1 parent 4ac094f commit b987aae

3 files changed

Lines changed: 323 additions & 4 deletions

File tree

‎apps/mobile/src/features/home/homeListItems.test.ts‎

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,31 @@ function makeThread(id: string, projectId: ProjectId): EnvironmentThreadShell {
5757

5858
function makeGroup(key: string, threadCount: number): HomeThreadGroup {
5959
const project = makeProject(key, key);
60+
const threads = Array.from({ length: threadCount }, (_, index) =>
61+
makeThread(`${key}-thread-${index}`, project.id),
62+
);
6063
return {
6164
key,
6265
title: key,
6366
representative: project,
6467
projects: [project],
6568
pendingTasks: [],
66-
threads: Array.from({ length: threadCount }, (_, index) =>
67-
makeThread(`${key}-thread-${index}`, project.id),
68-
),
69+
threads,
70+
// All threads inside the recency window, so the baseline stays at the
71+
// initial page size and the pagination expectations below hold.
72+
recentThreads: threads,
6973
};
7074
}
7175

76+
function makeGroupWithRecentCount(
77+
key: string,
78+
threadCount: number,
79+
recentCount: number,
80+
): HomeThreadGroup {
81+
const group = makeGroup(key, threadCount);
82+
return { ...group, recentThreads: group.threads.slice(0, recentCount) };
83+
}
84+
7285
function itemTypes(items: ReadonlyArray<HomeListItem>): string[] {
7386
return items.map((item) => item.type);
7487
}
@@ -190,4 +203,88 @@ describe("buildHomeListLayout", () => {
190203
expect(layout.stickyHeaderIndices).toEqual([0, 8]);
191204
expect(layout.items[8]).toMatchObject({ type: "header", isFirst: false });
192205
});
206+
207+
it("uses the group's recent-thread count as the default baseline when smaller than the page size", () => {
208+
// Only 2 of the 10 threads are "recent"; the rest should stay hidden
209+
// behind a show-more row until the user asks for more, even though the
210+
// page size constant (6) is larger than the recent count.
211+
const layout = buildHomeListLayout({
212+
groups: [makeGroupWithRecentCount("alpha", 10, 2)],
213+
displayStates: displayStates({}),
214+
});
215+
216+
const threadItems = layout.items.filter((item) => item.type === "thread");
217+
expect(threadItems).toHaveLength(2);
218+
expect(layout.items.at(-1)).toMatchObject({
219+
type: "show-more",
220+
groupKey: "alpha",
221+
hiddenCount: 8,
222+
canShowLess: false,
223+
});
224+
});
225+
226+
it("does not shrink the baseline below the page size when recent threads exceed it", () => {
227+
// 9 of 10 threads are "recent", which is still capped at the initial
228+
// page size constant rather than showing all 9 by default.
229+
const layout = buildHomeListLayout({
230+
groups: [makeGroupWithRecentCount("alpha", 10, 9)],
231+
displayStates: displayStates({}),
232+
});
233+
234+
const threadItems = layout.items.filter((item) => item.type === "thread");
235+
expect(threadItems).toHaveLength(HOME_INITIAL_VISIBLE_THREADS);
236+
expect(layout.items.at(-1)).toMatchObject({
237+
type: "show-more",
238+
hiddenCount: 10 - HOME_INITIAL_VISIBLE_THREADS,
239+
});
240+
});
241+
242+
it("shows no show-more row when the recent-thread baseline covers every thread", () => {
243+
const layout = buildHomeListLayout({
244+
groups: [makeGroupWithRecentCount("alpha", 3, 3)],
245+
displayStates: displayStates({}),
246+
});
247+
248+
expect(itemTypes(layout.items)).toEqual(["header", "thread", "thread", "thread"]);
249+
expect(layout.items.some((item) => item.type === "show-more")).toBe(false);
250+
});
251+
252+
it("expands past a small recent-thread baseline once show-more is tapped", () => {
253+
const group = makeGroupWithRecentCount("alpha", 10, 2);
254+
255+
const expanded = buildHomeListLayout({
256+
groups: [group],
257+
displayStates: displayStates({
258+
alpha: nextGroupDisplayState(DEFAULT_GROUP_DISPLAY_STATE, "show-more"),
259+
}),
260+
});
261+
262+
// show-more adds HOME_SHOW_MORE_STEP on top of the initial page size
263+
// constant, not the smaller recency baseline, so every thread is now
264+
// visible even though the group only had 2 "recent" threads.
265+
expect(expanded.items.filter((item) => item.type === "thread")).toHaveLength(10);
266+
// The show-more row still renders (canShowLess) because the baseline used
267+
// to decide whether to offer it is fixed at the recency count, not the
268+
// currently revealed count; there's simply nothing left hidden.
269+
expect(expanded.items.at(-1)).toMatchObject({
270+
type: "show-more",
271+
hiddenCount: 0,
272+
canShowLess: true,
273+
});
274+
});
275+
276+
it("resets an expanded group back to the recent-thread baseline on show-less", () => {
277+
const group = makeGroupWithRecentCount("alpha", 10, 2);
278+
const expandedState = nextGroupDisplayState(DEFAULT_GROUP_DISPLAY_STATE, "show-more");
279+
const resetState = nextGroupDisplayState(expandedState, "show-less");
280+
281+
const layout = buildHomeListLayout({
282+
groups: [group],
283+
displayStates: displayStates({ alpha: resetState }),
284+
});
285+
286+
const threadItems = layout.items.filter((item) => item.type === "thread");
287+
expect(threadItems).toHaveLength(2);
288+
expect(layout.items.at(-1)).toMatchObject({ type: "show-more", hiddenCount: 8 });
289+
});
193290
});

‎apps/mobile/src/features/home/homeThreadList.test.ts‎

Lines changed: 174 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ import type {
22
EnvironmentProject,
33
EnvironmentThreadShell,
44
} from "@t3tools/client-runtime/state/shell";
5-
import { EnvironmentId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
5+
import {
6+
CommandId,
7+
EnvironmentId,
8+
MessageId,
9+
ProjectId,
10+
ProviderInstanceId,
11+
ThreadId,
12+
} from "@t3tools/contracts";
613
import { describe, expect, it } from "vite-plus/test";
714

15+
import type { PendingNewTask } from "../../state/use-pending-new-tasks";
816
import { buildHomeThreadGroups } from "./homeThreadList";
917

1018
function makeProject(
@@ -44,6 +52,33 @@ function makeThread(
4452
};
4553
}
4654

55+
function makePendingTask(
56+
input: Pick<EnvironmentProject, "environmentId" | "id" | "title" | "workspaceRoot">,
57+
): PendingNewTask {
58+
return {
59+
title: input.title,
60+
message: {
61+
environmentId: input.environmentId,
62+
threadId: ThreadId.make(`pending-thread-${input.id}`),
63+
messageId: MessageId.make(`pending-message-${input.id}`),
64+
commandId: CommandId.make(`pending-command-${input.id}`),
65+
text: input.title,
66+
attachments: [],
67+
createdAt: "2026-06-29T00:00:00.000Z",
68+
},
69+
creation: {
70+
projectId: input.id,
71+
projectTitle: input.title,
72+
projectCwd: input.workspaceRoot,
73+
workspaceMode: "local",
74+
branch: null,
75+
worktreePath: null,
76+
},
77+
};
78+
}
79+
80+
const NOW = Date.parse("2026-06-29T00:00:00.000Z");
81+
4782
function buildGroups(
4883
projects: ReadonlyArray<EnvironmentProject>,
4984
threads: ReadonlyArray<EnvironmentThreadShell>,
@@ -57,6 +92,7 @@ function buildGroups(
5792
projectSortOrder: "updated_at",
5893
threadSortOrder: "updated_at",
5994
projectGroupingMode: "repository",
95+
now: NOW,
6096
...overrides,
6197
});
6298
}
@@ -220,4 +256,141 @@ describe("buildHomeThreadGroups", () => {
220256
);
221257
expect(buildGroups(projects, threads, { projectGroupingMode: "separate" })).toHaveLength(2);
222258
});
259+
260+
it("default view shows only threads from the last 5 days", () => {
261+
const environmentId = EnvironmentId.make("environment-1");
262+
const project = makeProject({
263+
environmentId,
264+
id: ProjectId.make("project-1"),
265+
title: "T3 Code",
266+
});
267+
const threads = [
268+
makeThread({
269+
environmentId,
270+
id: ThreadId.make("recent-1"),
271+
projectId: project.id,
272+
title: "Today",
273+
updatedAt: "2026-06-28T00:00:00.000Z",
274+
}),
275+
makeThread({
276+
environmentId,
277+
id: ThreadId.make("recent-2"),
278+
projectId: project.id,
279+
title: "Within window",
280+
updatedAt: "2026-06-25T00:00:00.000Z",
281+
}),
282+
makeThread({
283+
environmentId,
284+
id: ThreadId.make("old"),
285+
projectId: project.id,
286+
title: "Two weeks ago",
287+
updatedAt: "2026-06-14T00:00:00.000Z",
288+
}),
289+
];
290+
291+
const group = buildGroups([project], threads)[0];
292+
// Default view trims to recent threads...
293+
expect(group?.recentThreads.map((thread) => thread.id)).toEqual(["recent-1", "recent-2"]);
294+
// ...while full history stays available for the expanded view.
295+
expect(group?.threads.map((thread) => thread.id)).toEqual(["recent-1", "recent-2", "old"]);
296+
});
297+
298+
it("falls back to the most recent 3 threads when none are within 5 days", () => {
299+
const environmentId = EnvironmentId.make("environment-1");
300+
const project = makeProject({
301+
environmentId,
302+
id: ProjectId.make("project-1"),
303+
title: "T3 Code",
304+
});
305+
const threads = ["2026-06-01", "2026-06-02", "2026-06-03", "2026-06-04", "2026-06-05"].map(
306+
(day, index) =>
307+
makeThread({
308+
environmentId,
309+
id: ThreadId.make(`thread-${index}`),
310+
projectId: project.id,
311+
title: `Thread ${index}`,
312+
updatedAt: `${day}T00:00:00.000Z`,
313+
}),
314+
);
315+
316+
const group = buildGroups([project], threads)[0];
317+
expect(group?.recentThreads.map((thread) => thread.id)).toEqual([
318+
"thread-4",
319+
"thread-3",
320+
"thread-2",
321+
]);
322+
expect(group?.threads).toHaveLength(5);
323+
});
324+
325+
it("includes a thread exactly at the 5-day cutoff", () => {
326+
const environmentId = EnvironmentId.make("environment-1");
327+
const project = makeProject({
328+
environmentId,
329+
id: ProjectId.make("project-1"),
330+
title: "T3 Code",
331+
});
332+
const exactlyAtCutoff = new Date(NOW - 5 * 24 * 60 * 60 * 1000).toISOString();
333+
const justPastCutoff = new Date(NOW - 5 * 24 * 60 * 60 * 1000 - 1).toISOString();
334+
const threads = [
335+
makeThread({
336+
environmentId,
337+
id: ThreadId.make("at-cutoff"),
338+
projectId: project.id,
339+
title: "At cutoff",
340+
updatedAt: exactlyAtCutoff,
341+
}),
342+
makeThread({
343+
environmentId,
344+
id: ThreadId.make("past-cutoff"),
345+
projectId: project.id,
346+
title: "Past cutoff",
347+
updatedAt: justPastCutoff,
348+
}),
349+
];
350+
351+
const group = buildGroups([project], threads)[0];
352+
expect(group?.recentThreads.map((thread) => thread.id)).toEqual(["at-cutoff"]);
353+
});
354+
355+
it("returns an empty recentThreads list for a group with no threads", () => {
356+
const environmentId = EnvironmentId.make("environment-1");
357+
const project = makeProject({
358+
environmentId,
359+
id: ProjectId.make("project-1"),
360+
title: "T3 Code",
361+
});
362+
363+
const group = buildGroups([project], [], {
364+
pendingTasks: [makePendingTask(project)],
365+
})[0];
366+
367+
expect(group?.threads).toEqual([]);
368+
expect(group?.recentThreads).toEqual([]);
369+
});
370+
371+
it("does not apply the recency window while searching", () => {
372+
const environmentId = EnvironmentId.make("environment-1");
373+
const project = makeProject({
374+
environmentId,
375+
id: ProjectId.make("project-1"),
376+
title: "T3 Code",
377+
});
378+
const threads = ["2026-06-01", "2026-06-02", "2026-06-03", "2026-06-04", "2026-06-05"].map(
379+
(day, index) =>
380+
makeThread({
381+
environmentId,
382+
id: ThreadId.make(`thread-${index}`),
383+
projectId: project.id,
384+
title: `Thread ${index}`,
385+
updatedAt: `${day}T00:00:00.000Z`,
386+
}),
387+
);
388+
389+
const group = buildGroups([project], threads, { searchQuery: "T3 Code" })[0];
390+
// Search reaches the full history rather than the 3-thread fallback.
391+
expect(group?.recentThreads).toHaveLength(5);
392+
expect(group?.recentThreads.map((thread) => thread.id)).toEqual(
393+
group?.threads.map((thread) => thread.id),
394+
);
395+
});
223396
});

‎apps/mobile/src/lib/time.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2+
3+
import { relativeTime } from "./time";
4+
5+
const NOW = Date.parse("2026-06-29T12:00:00.000Z");
6+
7+
describe("relativeTime", () => {
8+
beforeEach(() => {
9+
vi.useFakeTimers();
10+
vi.setSystemTime(NOW);
11+
});
12+
13+
afterEach(() => {
14+
vi.useRealTimers();
15+
});
16+
17+
it("returns '<1m' for unparseable input", () => {
18+
expect(relativeTime("not-a-date")).toBe("<1m");
19+
});
20+
21+
it("returns '<1m' for a timestamp that is right now", () => {
22+
expect(relativeTime(new Date(NOW).toISOString())).toBe("<1m");
23+
});
24+
25+
it("returns '<1m' for anything under a minute old, instead of a live seconds count", () => {
26+
expect(relativeTime(new Date(NOW - 1_000).toISOString())).toBe("<1m");
27+
expect(relativeTime(new Date(NOW - 59_000).toISOString())).toBe("<1m");
28+
});
29+
30+
it("clamps timestamps in the future to '<1m' rather than a negative duration", () => {
31+
expect(relativeTime(new Date(NOW + 60_000).toISOString())).toBe("<1m");
32+
});
33+
34+
it("renders whole minutes once a minute has elapsed", () => {
35+
expect(relativeTime(new Date(NOW - 60_000).toISOString())).toBe("1m");
36+
expect(relativeTime(new Date(NOW - 90_000).toISOString())).toBe("1m");
37+
expect(relativeTime(new Date(NOW - 59 * 60_000).toISOString())).toBe("59m");
38+
});
39+
40+
it("switches to hours once 60 minutes have elapsed", () => {
41+
expect(relativeTime(new Date(NOW - 60 * 60_000).toISOString())).toBe("1h");
42+
expect(relativeTime(new Date(NOW - 23 * 60 * 60_000).toISOString())).toBe("23h");
43+
});
44+
45+
it("switches to days once 24 hours have elapsed", () => {
46+
expect(relativeTime(new Date(NOW - 24 * 60 * 60_000).toISOString())).toBe("1d");
47+
expect(relativeTime(new Date(NOW - 9 * 24 * 60 * 60_000).toISOString())).toBe("9d");
48+
});
49+
});

0 commit comments

Comments
 (0)