fix(splash): select the latest history message

This commit is contained in:
2026-07-13 18:16:25 +08:00
parent d8cd05b228
commit 328c4c8127
2 changed files with 42 additions and 1 deletions
@@ -0,0 +1,41 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { Result } from "@/utils";
const getHistoryMock = vi.hoisted(() => vi.fn());
vi.mock("@/data/repositories/chat_repository", () => ({
getChatRepository: () => ({ getHistory: getHistoryMock }),
}));
import { fetchSplashLatestMessagePreview } from "../splash_latest_message";
describe("fetchSplashLatestMessagePreview", () => {
beforeEach(() => {
getHistoryMock.mockReset();
});
it("uses the last message returned by history", async () => {
getHistoryMock.mockResolvedValue(
Result.ok({
messages: [
{ type: "text", content: "Older message" },
{ type: "text", content: "Latest message" },
],
}),
);
const result = await fetchSplashLatestMessagePreview();
expect(getHistoryMock).toHaveBeenCalledWith(1, 0);
expect(Result.isOk(result) && result.data).toBe("Latest message");
});
it("returns null when history is empty", async () => {
getHistoryMock.mockResolvedValue(Result.ok({ messages: [] }));
const result = await fetchSplashLatestMessagePreview();
expect(Result.isOk(result) && result.data).toBeNull();
});
});
+1 -1
View File
@@ -11,6 +11,6 @@ export async function fetchSplashLatestMessagePreview(): Promise<
const result = await getChatRepository().getHistory(1, 0);
if (Result.isErr(result)) return Result.err(result.error);
const latestMessage = result.data.messages[1] ?? null;
const latestMessage = result.data.messages.at(-1) ?? null;
return Result.ok(getSplashLatestMessagePreview(latestMessage));
}