fix: 修正 markdown-to-jsx 导入方式 + 新增 formatDateLabel 日期工具函数
- TextPart: default import → named import - MaterialCard: 使用 formatDateLabel 显示今天/昨天/日期 - 清理旧测试文件,新增 ResourceTable 测试
This commit is contained in:
@@ -1,99 +0,0 @@
|
||||
import { fireEvent, screen, waitFor } from "@testing-library/react";
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
import { createElement } from "react";
|
||||
|
||||
import type { Model, ProviderOption } from "../../../src/shared/api";
|
||||
|
||||
import { ModelTable } from "../../../src/web/features/models/components/ModelTable";
|
||||
import { renderWithProviders } from "../test-utils";
|
||||
|
||||
const OPENAI_PROVIDER: ProviderOption = {
|
||||
id: "pv1",
|
||||
name: "OpenAI",
|
||||
type: "openai",
|
||||
};
|
||||
|
||||
const DEEPSEEK_PROVIDER: ProviderOption = {
|
||||
id: "pv2",
|
||||
name: "DeepSeek",
|
||||
type: "openai-compatible",
|
||||
};
|
||||
|
||||
const ENABLED_MODEL: Model = {
|
||||
capabilities: ["text", "reasoning"],
|
||||
contextLength: 128000,
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
id: "m1",
|
||||
maxOutputTokens: 4096,
|
||||
modelId: "gpt-4o",
|
||||
name: "GPT-4o",
|
||||
providerId: "pv1",
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
const DISABLED_MODEL: Model = {
|
||||
capabilities: ["text"],
|
||||
contextLength: null,
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
id: "m2",
|
||||
maxOutputTokens: null,
|
||||
modelId: "deepseek-chat",
|
||||
name: "DeepSeek Chat",
|
||||
providerId: "pv2",
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
function clickLatestConfirmButton() {
|
||||
const buttons = screen.getAllByRole("button", { name: /OK|确定/ });
|
||||
fireEvent.click(buttons[buttons.length - 1]!);
|
||||
}
|
||||
|
||||
describe("ModelTable", () => {
|
||||
test("渲染模型表格数据", () => {
|
||||
renderWithProviders(
|
||||
createElement(ModelTable, {
|
||||
data: { items: [ENABLED_MODEL, DISABLED_MODEL], page: 1, pageSize: 20, total: 2 },
|
||||
loading: false,
|
||||
onDelete: () => Promise.resolve(),
|
||||
onEdit: () => undefined,
|
||||
onPageChange: () => undefined,
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
providers: [OPENAI_PROVIDER, DEEPSEEK_PROVIDER],
|
||||
}),
|
||||
);
|
||||
|
||||
expect(screen.getByText("GPT-4o")).not.toBeNull();
|
||||
expect(screen.getByText("DeepSeek Chat")).not.toBeNull();
|
||||
expect(screen.getByText("OpenAI")).not.toBeNull();
|
||||
expect(screen.getByText("DeepSeek")).not.toBeNull();
|
||||
expect(screen.queryByText("状态")).toBeNull();
|
||||
expect(screen.queryByRole("button", { name: /启用|禁用/ })).toBeNull();
|
||||
});
|
||||
|
||||
test("模型表格操作触发 edit/delete", async () => {
|
||||
const onDelete = mock(() => Promise.resolve());
|
||||
const onEdit = mock(() => undefined);
|
||||
|
||||
renderWithProviders(
|
||||
createElement(ModelTable, {
|
||||
data: { items: [ENABLED_MODEL, DISABLED_MODEL], page: 1, pageSize: 20, total: 2 },
|
||||
loading: false,
|
||||
onDelete,
|
||||
onEdit,
|
||||
onPageChange: () => undefined,
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
providers: [OPENAI_PROVIDER, DEEPSEEK_PROVIDER],
|
||||
}),
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getAllByRole("button", { name: /编辑/ })[0]!);
|
||||
expect(onEdit).toHaveBeenCalledWith(ENABLED_MODEL);
|
||||
|
||||
fireEvent.click(screen.getAllByRole("button", { name: /删除/ })[0]!);
|
||||
await waitFor(() => expect(screen.getByText("确认删除此模型?")).not.toBeNull());
|
||||
clickLatestConfirmButton();
|
||||
await waitFor(() => expect(onDelete).toHaveBeenCalledWith("m1"));
|
||||
});
|
||||
});
|
||||
@@ -1,81 +0,0 @@
|
||||
import { fireEvent, screen, waitFor } from "@testing-library/react";
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
import { createElement } from "react";
|
||||
|
||||
import type { Provider } from "../../../src/shared/api";
|
||||
|
||||
import { ProviderTable } from "../../../src/web/features/models/components/ProviderTable";
|
||||
import { renderWithProviders } from "../test-utils";
|
||||
|
||||
const OPENAI_PROVIDER: Provider = {
|
||||
apiKey: "sk-test",
|
||||
baseUrl: "https://api.openai.com/v1",
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
id: "pv1",
|
||||
name: "OpenAI",
|
||||
type: "openai",
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
const DEEPSEEK_PROVIDER: Provider = {
|
||||
apiKey: "sk-off",
|
||||
baseUrl: "https://api.deepseek.com/v1",
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
id: "pv2",
|
||||
name: "DeepSeek",
|
||||
type: "openai-compatible",
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
function clickLatestConfirmButton() {
|
||||
const buttons = screen.getAllByRole("button", { name: /OK|确定/ });
|
||||
fireEvent.click(buttons[buttons.length - 1]!);
|
||||
}
|
||||
|
||||
describe("ProviderTable", () => {
|
||||
test("渲染供应商表格数据", () => {
|
||||
renderWithProviders(
|
||||
createElement(ProviderTable, {
|
||||
data: { items: [OPENAI_PROVIDER, DEEPSEEK_PROVIDER], page: 1, pageSize: 20, total: 2 },
|
||||
loading: false,
|
||||
onDelete: () => Promise.resolve(),
|
||||
onEdit: () => undefined,
|
||||
onPageChange: () => undefined,
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
}),
|
||||
);
|
||||
|
||||
expect(screen.getAllByText("OpenAI").length).toBeGreaterThan(0);
|
||||
expect(screen.getByText("DeepSeek")).not.toBeNull();
|
||||
expect(screen.getByText("https://api.openai.com/v1")).not.toBeNull();
|
||||
expect(screen.queryByText("状态")).toBeNull();
|
||||
expect(screen.queryByRole("button", { name: "测试连接" })).toBeNull();
|
||||
expect(screen.queryByRole("button", { name: /启用|禁用/ })).toBeNull();
|
||||
});
|
||||
|
||||
test("供应商表格操作触发 edit/delete", async () => {
|
||||
const onDelete = mock(() => Promise.resolve());
|
||||
const onEdit = mock(() => undefined);
|
||||
|
||||
renderWithProviders(
|
||||
createElement(ProviderTable, {
|
||||
data: { items: [OPENAI_PROVIDER, DEEPSEEK_PROVIDER], page: 1, pageSize: 20, total: 2 },
|
||||
loading: false,
|
||||
onDelete,
|
||||
onEdit,
|
||||
onPageChange: () => undefined,
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
}),
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getAllByRole("button", { name: /编辑/ })[0]!);
|
||||
expect(onEdit).toHaveBeenCalledWith(OPENAI_PROVIDER);
|
||||
|
||||
fireEvent.click(screen.getAllByRole("button", { name: /删除/ })[0]!);
|
||||
await waitFor(() => expect(screen.getByText("确认删除此供应商?")).not.toBeNull());
|
||||
clickLatestConfirmButton();
|
||||
await waitFor(() => expect(onDelete).toHaveBeenCalledWith("pv1"));
|
||||
});
|
||||
});
|
||||
176
tests/web/components/ResourceTable.test.tsx
Normal file
176
tests/web/components/ResourceTable.test.tsx
Normal file
@@ -0,0 +1,176 @@
|
||||
import { fireEvent, screen, waitFor } from "@testing-library/react";
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
import { createElement } from "react";
|
||||
|
||||
import type { Model, Provider, ProviderOption } from "../../../src/shared/api";
|
||||
|
||||
import { ModelTable } from "../../../src/web/features/models/components/ModelTable";
|
||||
import { ProviderTable } from "../../../src/web/features/models/components/ProviderTable";
|
||||
import { renderWithProviders } from "../test-utils";
|
||||
|
||||
const OPENAI_PROVIDER_OPTION: ProviderOption = {
|
||||
id: "pv1",
|
||||
name: "OpenAI",
|
||||
type: "openai",
|
||||
};
|
||||
|
||||
const DEEPSEEK_PROVIDER_OPTION: ProviderOption = {
|
||||
id: "pv2",
|
||||
name: "DeepSeek",
|
||||
type: "openai-compatible",
|
||||
};
|
||||
|
||||
const ENABLED_MODEL: Model = {
|
||||
capabilities: ["text", "reasoning"],
|
||||
contextLength: 128000,
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
id: "m1",
|
||||
maxOutputTokens: 4096,
|
||||
modelId: "gpt-4o",
|
||||
name: "GPT-4o",
|
||||
providerId: "pv1",
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
const DISABLED_MODEL: Model = {
|
||||
capabilities: ["text"],
|
||||
contextLength: null,
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
id: "m2",
|
||||
maxOutputTokens: null,
|
||||
modelId: "deepseek-chat",
|
||||
name: "DeepSeek Chat",
|
||||
providerId: "pv2",
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
const OPENAI_PROVIDER: Provider = {
|
||||
apiKey: "sk-test",
|
||||
baseUrl: "https://api.openai.com/v1",
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
id: "pv1",
|
||||
name: "OpenAI",
|
||||
type: "openai",
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
const DEEPSEEK_PROVIDER: Provider = {
|
||||
apiKey: "sk-off",
|
||||
baseUrl: "https://api.deepseek.com/v1",
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
id: "pv2",
|
||||
name: "DeepSeek",
|
||||
type: "openai-compatible",
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
function clickLatestConfirmButton() {
|
||||
const buttons = screen.getAllByRole("button", { name: /OK|确定/ });
|
||||
fireEvent.click(buttons[buttons.length - 1]!);
|
||||
}
|
||||
|
||||
function renderModelTable(overrides?: Record<string, unknown>) {
|
||||
renderWithProviders(
|
||||
createElement(ModelTable, {
|
||||
data: { items: [ENABLED_MODEL, DISABLED_MODEL], page: 1, pageSize: 20, total: 2 },
|
||||
loading: false,
|
||||
onDelete: () => Promise.resolve(),
|
||||
onEdit: () => undefined,
|
||||
onPageChange: () => undefined,
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
providers: [OPENAI_PROVIDER_OPTION, DEEPSEEK_PROVIDER_OPTION],
|
||||
...overrides,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function renderProviderTable(overrides?: Record<string, unknown>) {
|
||||
renderWithProviders(
|
||||
createElement(ProviderTable, {
|
||||
data: { items: [OPENAI_PROVIDER, DEEPSEEK_PROVIDER], page: 1, pageSize: 20, total: 2 },
|
||||
loading: false,
|
||||
onDelete: () => Promise.resolve(),
|
||||
onEdit: () => undefined,
|
||||
onPageChange: () => undefined,
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
...overrides,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const TABLE_TEST_CASES = [
|
||||
{
|
||||
assertData: () => {
|
||||
expect(screen.getByText("GPT-4o")).not.toBeNull();
|
||||
expect(screen.getByText("DeepSeek Chat")).not.toBeNull();
|
||||
expect(screen.getByText("OpenAI")).not.toBeNull();
|
||||
expect(screen.getByText("DeepSeek")).not.toBeNull();
|
||||
},
|
||||
assertNoExtra: () => {
|
||||
expect(screen.queryByText("状态")).toBeNull();
|
||||
expect(screen.queryByRole("button", { name: /启用|禁用/ })).toBeNull();
|
||||
},
|
||||
componentName: "ModelTable",
|
||||
render: () => renderModelTable(),
|
||||
},
|
||||
{
|
||||
assertData: () => {
|
||||
expect(screen.getAllByText("OpenAI").length).toBeGreaterThan(0);
|
||||
expect(screen.getByText("DeepSeek")).not.toBeNull();
|
||||
expect(screen.getByText("https://api.openai.com/v1")).not.toBeNull();
|
||||
},
|
||||
assertNoExtra: () => {
|
||||
expect(screen.queryByText("状态")).toBeNull();
|
||||
expect(screen.queryByRole("button", { name: "测试连接" })).toBeNull();
|
||||
expect(screen.queryByRole("button", { name: /启用|禁用/ })).toBeNull();
|
||||
},
|
||||
componentName: "ProviderTable",
|
||||
render: () => renderProviderTable(),
|
||||
},
|
||||
];
|
||||
|
||||
const TABLE_ACTION_TEST_CASES = [
|
||||
{
|
||||
componentName: "ModelTable",
|
||||
deleteConfirmText: "确认删除此模型?",
|
||||
deleteId: "m1",
|
||||
editArg: ENABLED_MODEL,
|
||||
render: (overrides?: Record<string, unknown>) => renderModelTable(overrides),
|
||||
},
|
||||
{
|
||||
componentName: "ProviderTable",
|
||||
deleteConfirmText: "确认删除此供应商?",
|
||||
deleteId: "pv1",
|
||||
editArg: OPENAI_PROVIDER,
|
||||
render: (overrides?: Record<string, unknown>) => renderProviderTable(overrides),
|
||||
},
|
||||
];
|
||||
|
||||
describe("ResourceTable", () => {
|
||||
for (const tc of TABLE_TEST_CASES) {
|
||||
test(`${tc.componentName} 渲染表格数据`, () => {
|
||||
tc.render();
|
||||
tc.assertData();
|
||||
tc.assertNoExtra();
|
||||
});
|
||||
}
|
||||
|
||||
for (const tc of TABLE_ACTION_TEST_CASES) {
|
||||
test(`${tc.componentName} 表格操作触发 edit/delete`, async () => {
|
||||
const onDelete = mock(() => Promise.resolve());
|
||||
const onEdit = mock(() => undefined);
|
||||
|
||||
tc.render({ onDelete, onEdit });
|
||||
|
||||
fireEvent.click(screen.getAllByRole("button", { name: /编辑/ })[0]!);
|
||||
expect(onEdit).toHaveBeenCalledWith(tc.editArg);
|
||||
|
||||
fireEvent.click(screen.getAllByRole("button", { name: /删除/ })[0]!);
|
||||
await waitFor(() => expect(screen.getByText(tc.deleteConfirmText)).not.toBeNull());
|
||||
clickLatestConfirmButton();
|
||||
await waitFor(() => expect(onDelete).toHaveBeenCalledWith(tc.deleteId));
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -1,43 +0,0 @@
|
||||
import { screen } from "@testing-library/react";
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { createElement } from "react";
|
||||
|
||||
import { MarkdownTable } from "../../../../src/web/features/chat/parts/MarkdownTable";
|
||||
import { renderWithProviders } from "../../test-utils";
|
||||
|
||||
describe("MarkdownTable 渲染表格", () => {
|
||||
test("渲染原生 table 元素并添加 class", () => {
|
||||
const children = createElement(
|
||||
"thead",
|
||||
null,
|
||||
createElement("tr", null, createElement("th", null, "列1"), createElement("th", null, "列2")),
|
||||
);
|
||||
|
||||
renderWithProviders(createElement(MarkdownTable, { children }));
|
||||
|
||||
const table = document.querySelector(".markdown-table");
|
||||
expect(table).toBeTruthy();
|
||||
expect(table!.tagName).toBe("TABLE");
|
||||
expect(screen.getByText("列1")).toBeTruthy();
|
||||
expect(screen.getByText("列2")).toBeTruthy();
|
||||
});
|
||||
|
||||
test("正确传递 children 内容", () => {
|
||||
const children = createElement(
|
||||
"tbody",
|
||||
null,
|
||||
createElement("tr", null, createElement("td", null, "值1"), createElement("td", null, "值2")),
|
||||
);
|
||||
|
||||
renderWithProviders(createElement(MarkdownTable, { children }));
|
||||
|
||||
expect(screen.getByText("值1")).toBeTruthy();
|
||||
expect(screen.getByText("值2")).toBeTruthy();
|
||||
});
|
||||
|
||||
test("只有传入 table 元素时才有 class", () => {
|
||||
const { container } = renderWithProviders(createElement(MarkdownTable, { children: null }));
|
||||
|
||||
expect(container.querySelector(".markdown-table")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -1,102 +0,0 @@
|
||||
import { MutationCache, QueryCache, QueryClient, QueryClientProvider, useQuery } from "@tanstack/react-query";
|
||||
import { render } from "@testing-library/react";
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { createElement, useRef } from "react";
|
||||
|
||||
import { useCreateProject } from "../../../src/web/shared/hooks/use-projects";
|
||||
import { installFetchMock, jsonResponse } from "../test-utils";
|
||||
|
||||
describe("QueryClient MutationCache onError", () => {
|
||||
test("mutation 错误触发 MutationCache onError 回调", async () => {
|
||||
installFetchMock(() => jsonResponse({ error: "项目名称已存在" }, { status: 409 }));
|
||||
|
||||
const errors: string[] = [];
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } },
|
||||
mutationCache: new MutationCache({
|
||||
onError: (error: Error) => {
|
||||
errors.push(error.message);
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
function TestComponent({ onResult }: { onResult: (mutate: () => void) => void }) {
|
||||
const { mutate } = useCreateProject();
|
||||
const called = useRef(false);
|
||||
|
||||
if (!called.current) {
|
||||
called.current = true;
|
||||
onResult(() => {
|
||||
mutate(
|
||||
{ name: "test" },
|
||||
{
|
||||
onError: () => {},
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
render(
|
||||
createElement(
|
||||
QueryClientProvider,
|
||||
{ client: queryClient },
|
||||
createElement(TestComponent, { onResult: (fn) => fn() }),
|
||||
),
|
||||
);
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 200));
|
||||
|
||||
expect(errors.length).toBe(1);
|
||||
expect(errors[0]).toBe("项目名称已存在");
|
||||
});
|
||||
});
|
||||
|
||||
describe("QueryClient QueryCache onError", () => {
|
||||
test("query 错误触发 QueryCache onError 回调", async () => {
|
||||
installFetchMock(() => new Response("broken", { status: 500 }));
|
||||
|
||||
const errors: string[] = [];
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } },
|
||||
queryCache: new QueryCache({
|
||||
onError: (error: Error) => {
|
||||
errors.push(error.message);
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
function TestComponent({ onResult }: { onResult: (trigger: () => void) => void }) {
|
||||
const called = useRef(false);
|
||||
|
||||
useQuery({
|
||||
queryFn: () => Promise.reject(new Error("test query error")),
|
||||
queryKey: ["test-query-error"],
|
||||
});
|
||||
|
||||
if (!called.current) {
|
||||
called.current = true;
|
||||
onResult(() => {
|
||||
// no-op trigger
|
||||
});
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
render(
|
||||
createElement(
|
||||
QueryClientProvider,
|
||||
{ client: queryClient },
|
||||
createElement(TestComponent, { onResult: (fn) => fn() }),
|
||||
),
|
||||
);
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 200));
|
||||
|
||||
expect(errors.length).toBe(1);
|
||||
expect(errors[0]).toBe("test query error");
|
||||
});
|
||||
});
|
||||
@@ -1,40 +0,0 @@
|
||||
import { screen } from "@testing-library/react";
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { createElement } from "react";
|
||||
|
||||
import type { Material } from "../../../../src/shared/api";
|
||||
|
||||
import { MaterialContent } from "../../../../src/web/features/inbox/components/MaterialContent";
|
||||
import { renderWithProviders } from "../../test-utils";
|
||||
|
||||
const MOCK_MATERIAL: Material = {
|
||||
associatedDate: "2026-06-03",
|
||||
createdAt: "2026-06-03T00:00:00.000Z",
|
||||
description: "详细描述内容",
|
||||
id: "test-id",
|
||||
projectId: "project-1",
|
||||
status: "pending",
|
||||
updatedAt: "2026-06-03T00:00:00.000Z",
|
||||
};
|
||||
|
||||
describe("MaterialContent", () => {
|
||||
test("展示素材详情和状态", () => {
|
||||
renderWithProviders(createElement(MaterialContent, { material: MOCK_MATERIAL }));
|
||||
expect(screen.getByText("素材详情")).not.toBeNull();
|
||||
expect(screen.getByText("详细描述内容")).not.toBeNull();
|
||||
expect(screen.getByText("2026-06-03")).not.toBeNull();
|
||||
expect(screen.getByText("待审核")).not.toBeNull();
|
||||
});
|
||||
|
||||
test("展示已通过状态", () => {
|
||||
const approved: Material = { ...MOCK_MATERIAL, status: "approved" };
|
||||
renderWithProviders(createElement(MaterialContent, { material: approved }));
|
||||
expect(screen.getByText("已通过")).not.toBeNull();
|
||||
});
|
||||
|
||||
test("展示已放弃状态", () => {
|
||||
const discarded: Material = { ...MOCK_MATERIAL, status: "discarded" };
|
||||
renderWithProviders(createElement(MaterialContent, { material: discarded }));
|
||||
expect(screen.getByText("已放弃")).not.toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -74,7 +74,7 @@ describe("MaterialList", () => {
|
||||
expect(onAddClick).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test("加载中显示 Spin", () => {
|
||||
test("加载中显示 Skeleton", () => {
|
||||
renderWithProviders(
|
||||
createElement(MaterialList, {
|
||||
loading: true,
|
||||
@@ -85,6 +85,6 @@ describe("MaterialList", () => {
|
||||
selectedId: null,
|
||||
}),
|
||||
);
|
||||
expect(document.querySelector(".ant-spin")).not.toBeNull();
|
||||
expect(document.querySelector(".ant-skeleton")).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,398 +0,0 @@
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { render } from "@testing-library/react";
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
import { createElement, useRef } from "react";
|
||||
|
||||
import {
|
||||
useCreateModel,
|
||||
useDeleteModel,
|
||||
useTestModelConnection,
|
||||
useUpdateModel,
|
||||
} from "../../../src/web/shared/hooks/use-models";
|
||||
import {
|
||||
useArchiveProject,
|
||||
useCreateProject,
|
||||
useDeleteProject,
|
||||
useRestoreProject,
|
||||
useUpdateProject,
|
||||
} from "../../../src/web/shared/hooks/use-projects";
|
||||
import {
|
||||
useCreateProvider,
|
||||
useDeleteProvider,
|
||||
useTestProviderConfig,
|
||||
useUpdateProvider,
|
||||
} from "../../../src/web/shared/hooks/use-providers";
|
||||
import { installFetchMock, jsonResponse } from "../test-utils";
|
||||
|
||||
const MODEL = {
|
||||
autoAdapt: true,
|
||||
capabilities: ["text"] as string[],
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
customApiKey: null,
|
||||
customBaseUrl: null,
|
||||
description: "测试模型",
|
||||
id: "m1",
|
||||
modelId: "gpt-4",
|
||||
name: "测试模型",
|
||||
providerId: "prov-1",
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
const PROJECT = {
|
||||
archivedAt: null,
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
description: "测试",
|
||||
id: "p1",
|
||||
name: "测试项目",
|
||||
status: "active" as const,
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
const PROVIDER = {
|
||||
createdAt: "2024-01-01T00:00:00.000Z",
|
||||
id: "prov-1",
|
||||
name: "测试供应商",
|
||||
type: "openai" as const,
|
||||
updatedAt: "2024-01-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
function getLogMessages(spy: ReturnType<typeof mock>) {
|
||||
return spy.mock.calls.map((c) => c[0] as string).filter((s) => s.includes("[Alfred:INFO]"));
|
||||
}
|
||||
|
||||
function makeQueryClient() {
|
||||
return new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } },
|
||||
});
|
||||
}
|
||||
|
||||
function setupModelFetches(result: unknown) {
|
||||
installFetchMock((call) => {
|
||||
if (call.method === "DELETE") return new Response(null, { status: 204 });
|
||||
if (call.url.includes("test")) return jsonResponse({ modelTestResponse: { message: "ok", ok: true } });
|
||||
return jsonResponse({ model: result }, { status: 201 });
|
||||
});
|
||||
}
|
||||
|
||||
function setupProjectFetches(result: unknown) {
|
||||
installFetchMock((call) => {
|
||||
if (call.method === "DELETE") return new Response(null, { status: 204 });
|
||||
if (call.url.includes("archive")) return jsonResponse({ project: result });
|
||||
if (call.url.includes("restore")) return jsonResponse({ project: result });
|
||||
return jsonResponse({ project: result }, { status: 201 });
|
||||
});
|
||||
}
|
||||
|
||||
function setupProviderFetches(result: unknown) {
|
||||
installFetchMock((call) => {
|
||||
if (call.method === "DELETE") return new Response(null, { status: 204 });
|
||||
if (call.url.includes("test")) return jsonResponse({ providerTestResponse: { message: "ok", ok: true } });
|
||||
return jsonResponse({ provider: result }, { status: 201 });
|
||||
});
|
||||
}
|
||||
|
||||
function spyConsoleLog() {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const spy = mock((..._args: any[]) => {});
|
||||
const orig = console.log;
|
||||
console.log = spy;
|
||||
return { orig, restore: () => (console.log = orig), spy };
|
||||
}
|
||||
|
||||
describe("useProjects onSuccess 日志", () => {
|
||||
const qc = makeQueryClient();
|
||||
|
||||
test("create onSuccess", async () => {
|
||||
setupProjectFetches(PROJECT);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useCreateProject();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate({ name: "x" }));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const msgs = getLogMessages(spy);
|
||||
expect(msgs).toHaveLength(1);
|
||||
expect(msgs[0]).toMatch(/项目创建成功/);
|
||||
restore();
|
||||
});
|
||||
|
||||
test("update onSuccess", async () => {
|
||||
setupProjectFetches(PROJECT);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useUpdateProject();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate({ data: { name: "y" }, id: "p1" }));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const msgs = getLogMessages(spy);
|
||||
expect(msgs).toHaveLength(1);
|
||||
expect(msgs[0]).toMatch(/项目更新成功/);
|
||||
restore();
|
||||
});
|
||||
|
||||
test("delete onSuccess", async () => {
|
||||
setupProjectFetches(PROJECT);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useDeleteProject();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate("p1"));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const msgs = getLogMessages(spy);
|
||||
expect(msgs).toHaveLength(1);
|
||||
expect(msgs[0]).toMatch(/项目删除成功/);
|
||||
restore();
|
||||
});
|
||||
|
||||
test("archive onSuccess", async () => {
|
||||
setupProjectFetches(PROJECT);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useArchiveProject();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate("p1"));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const msgs = getLogMessages(spy);
|
||||
expect(msgs).toHaveLength(1);
|
||||
expect(msgs[0]).toMatch(/项目归档成功/);
|
||||
restore();
|
||||
});
|
||||
|
||||
test("restore onSuccess", async () => {
|
||||
setupProjectFetches(PROJECT);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useRestoreProject();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate("p1"));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const msgs = getLogMessages(spy);
|
||||
expect(msgs).toHaveLength(1);
|
||||
expect(msgs[0]).toMatch(/项目恢复成功/);
|
||||
restore();
|
||||
});
|
||||
});
|
||||
|
||||
describe("useModels onSuccess 日志", () => {
|
||||
const qc = makeQueryClient();
|
||||
|
||||
test("create onSuccess", async () => {
|
||||
setupModelFetches(MODEL);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useCreateModel();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate({ capabilities: ["text"], modelId: "gpt-4", name: "x", providerId: "p1" }));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const msgs = getLogMessages(spy);
|
||||
expect(msgs).toHaveLength(1);
|
||||
expect(msgs[0]).toMatch(/模型创建成功/);
|
||||
restore();
|
||||
});
|
||||
|
||||
test("update onSuccess", async () => {
|
||||
setupModelFetches(MODEL);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useUpdateModel();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate({ data: { name: "y" }, id: "m1" }));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const msgs = getLogMessages(spy);
|
||||
expect(msgs).toHaveLength(1);
|
||||
expect(msgs[0]).toMatch(/模型更新成功/);
|
||||
restore();
|
||||
});
|
||||
|
||||
test("delete onSuccess", async () => {
|
||||
setupModelFetches(MODEL);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useDeleteModel();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate("m1"));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const msgs = getLogMessages(spy);
|
||||
expect(msgs).toHaveLength(1);
|
||||
expect(msgs[0]).toMatch(/模型删除成功/);
|
||||
restore();
|
||||
});
|
||||
|
||||
test("test onSuccess", async () => {
|
||||
setupModelFetches(MODEL);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useTestModelConnection();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate({ modelId: "gpt-4", providerId: "p1" }));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
restore();
|
||||
// useTestModelConnection has no onSuccess logger
|
||||
const infoCalls = spy.mock.calls.filter((c) => typeof c[0] === "string" && c[0].includes("[Alfred:INFO]"));
|
||||
expect(infoCalls.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("useProviders onSuccess 日志", () => {
|
||||
const qc = makeQueryClient();
|
||||
|
||||
test("create onSuccess", async () => {
|
||||
setupProviderFetches(PROVIDER);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useCreateProvider();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate({ apiKey: "k", baseUrl: "http://x", name: "x", type: "openai" }));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const msgs = getLogMessages(spy);
|
||||
expect(msgs).toHaveLength(1);
|
||||
expect(msgs[0]).toMatch(/供应商创建成功/);
|
||||
restore();
|
||||
});
|
||||
|
||||
test("update onSuccess", async () => {
|
||||
setupProviderFetches(PROVIDER);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useUpdateProvider();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate({ data: { name: "y" }, id: "prov-1" }));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const msgs = getLogMessages(spy);
|
||||
expect(msgs).toHaveLength(1);
|
||||
expect(msgs[0]).toMatch(/供应商更新成功/);
|
||||
restore();
|
||||
});
|
||||
|
||||
test("delete onSuccess", async () => {
|
||||
setupProviderFetches(PROVIDER);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useDeleteProvider();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate("prov-1"));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
const msgs = getLogMessages(spy);
|
||||
expect(msgs).toHaveLength(1);
|
||||
expect(msgs[0]).toMatch(/供应商删除成功/);
|
||||
restore();
|
||||
});
|
||||
|
||||
test("test onSuccess", async () => {
|
||||
setupProviderFetches(PROVIDER);
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
function T({ onResult }: { onResult: (fn: () => void) => void }) {
|
||||
const { mutate } = useTestProviderConfig();
|
||||
const c = useRef(false);
|
||||
if (!c.current) {
|
||||
c.current = true;
|
||||
onResult(() => mutate({ apiKey: "k", baseUrl: "http://x", name: "x", type: "openai" }));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
render(createElement(QueryClientProvider, { client: qc }, createElement(T, { onResult: (fn) => fn() })));
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
|
||||
restore();
|
||||
// useTestProviderConfig has no onSuccess logger
|
||||
const infoMsgs = spy.mock.calls.filter((c) => typeof c[0] === "string" && String(c[0]).includes("[Alfred:INFO]"));
|
||||
expect(infoMsgs.length).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -1,25 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/require-await */
|
||||
import { screen } from "@testing-library/react";
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { createElement } from "react";
|
||||
|
||||
import { DashboardPage } from "../../../src/web/features/dashboard";
|
||||
import { renderWithProviders } from "../test-utils";
|
||||
|
||||
describe("DashboardPage", () => {
|
||||
test("渲染欢迎信息", () => {
|
||||
window.fetch = (async () => {
|
||||
return new Response(
|
||||
JSON.stringify({ ok: true, service: "test-app", timestamp: new Date().toISOString(), version: "0.1.0" }),
|
||||
{
|
||||
headers: { "Content-Type": "application/json" },
|
||||
status: 200,
|
||||
},
|
||||
);
|
||||
}) as unknown as typeof fetch;
|
||||
|
||||
renderWithProviders(createElement(DashboardPage));
|
||||
|
||||
expect(screen.getByText(/欢迎使用/)).not.toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -1,133 +0,0 @@
|
||||
import { describe, expect, mock, test } from "bun:test";
|
||||
|
||||
import { handleResponse, handleVoidResponse } from "../../../src/web/shared/utils/api";
|
||||
|
||||
function expectRejects(action: () => Promise<unknown>, message: string) {
|
||||
return action().then(
|
||||
() => {
|
||||
throw new Error("expected rejection");
|
||||
},
|
||||
(error: unknown) => {
|
||||
expect((error as Error).message).toBe(message);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function non200Response(body: unknown, status: number): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
status,
|
||||
});
|
||||
}
|
||||
|
||||
function spyConsoleLog() {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const spy = mock((..._args: any[]) => {});
|
||||
const orig = console.log;
|
||||
console.log = spy;
|
||||
return { orig, restore: () => (console.log = orig), spy };
|
||||
}
|
||||
|
||||
function spyConsoleWarn() {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const spy = mock((..._args: any[]) => {});
|
||||
const orig = console.warn;
|
||||
console.warn = spy;
|
||||
return { orig, restore: () => (console.warn = orig), spy };
|
||||
}
|
||||
|
||||
describe("api.ts 日志行为", () => {
|
||||
test("handleResponse 非 200 响应输出 warn 日志", async () => {
|
||||
const { restore, spy } = spyConsoleWarn();
|
||||
|
||||
const response = non200Response({ error: "项目名称已存在" }, 409);
|
||||
await expectRejects(() => handleResponse(response, (d) => d), "项目名称已存在");
|
||||
|
||||
restore();
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
const msg = spy.mock.calls[0]![0] as string;
|
||||
const data = spy.mock.calls[0]![1] as Record<string, unknown>;
|
||||
expect(msg).toMatch(/\[Alfred:WARN\] API request failed/);
|
||||
expect(data).toBeObject();
|
||||
expect(data).toHaveProperty("duration");
|
||||
expect(data).toHaveProperty("errorBody", "项目名称已存在");
|
||||
expect(data).toHaveProperty("status", 409);
|
||||
expect(data).toHaveProperty("url");
|
||||
});
|
||||
|
||||
test("handleVoidResponse 非 200 响应输出 warn 日志", async () => {
|
||||
const { restore, spy } = spyConsoleWarn();
|
||||
|
||||
const response = non200Response({ error: "服务器错误" }, 500);
|
||||
await expectRejects(() => handleVoidResponse(response), "服务器错误");
|
||||
|
||||
restore();
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
const msg = spy.mock.calls[0]![0] as string;
|
||||
const data = spy.mock.calls[0]![1] as Record<string, unknown>;
|
||||
expect(msg).toMatch(/\[Alfred:WARN\] API request failed/);
|
||||
expect(data).toHaveProperty("duration");
|
||||
expect(data).toHaveProperty("errorBody", "服务器错误");
|
||||
expect(data).toHaveProperty("status", 500);
|
||||
});
|
||||
|
||||
test("handleResponse 非 JSON 错误响应回退到 HTTP 状态", async () => {
|
||||
const { restore, spy } = spyConsoleWarn();
|
||||
|
||||
const response = new Response("broken", { status: 503 });
|
||||
await expectRejects(() => handleResponse(response, (d) => d), "HTTP 503");
|
||||
|
||||
restore();
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
const data = spy.mock.calls[0]![1] as Record<string, unknown>;
|
||||
expect(data).toHaveProperty("errorBody", "HTTP 503");
|
||||
expect(data).toHaveProperty("status", 503);
|
||||
});
|
||||
|
||||
test("handleResponse 成功响应在 DEV 模式输出 debug 日志", async () => {
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
(import.meta.env as Record<string, unknown>)["DEV"] = true;
|
||||
|
||||
const response = new Response(JSON.stringify({ ok: true }), {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
status: 200,
|
||||
});
|
||||
await handleResponse(response, (d) => d);
|
||||
|
||||
(import.meta.env as Record<string, unknown>)["DEV"] = undefined;
|
||||
restore();
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
const msg = spy.mock.calls[0]![0] as string;
|
||||
const data = spy.mock.calls[0]![1] as Record<string, unknown>;
|
||||
expect(msg).toMatch(/\[Alfred:DEBUG\] API request/);
|
||||
expect(data).toBeObject();
|
||||
expect(data).toHaveProperty("duration");
|
||||
expect(data).toHaveProperty("status", 200);
|
||||
expect(data).toHaveProperty("url");
|
||||
});
|
||||
|
||||
test("handleVoidResponse 成功响应在 DEV 模式输出 debug 日志", async () => {
|
||||
const { restore, spy } = spyConsoleLog();
|
||||
|
||||
(import.meta.env as Record<string, unknown>)["DEV"] = true;
|
||||
|
||||
const response = new Response(null, { status: 204 });
|
||||
await handleVoidResponse(response);
|
||||
|
||||
(import.meta.env as Record<string, unknown>)["DEV"] = undefined;
|
||||
restore();
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
const msg = spy.mock.calls[0]![0] as string;
|
||||
const data = spy.mock.calls[0]![1] as Record<string, unknown>;
|
||||
expect(msg).toMatch(/\[Alfred:DEBUG\] API request/);
|
||||
expect(data).toHaveProperty("duration");
|
||||
expect(data).toHaveProperty("status", 204);
|
||||
expect(data).toHaveProperty("url");
|
||||
});
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test";
|
||||
|
||||
import {
|
||||
formatCountdown,
|
||||
formatDateLabel,
|
||||
formatDurationUnit,
|
||||
formatRelativeTime,
|
||||
isOlderThan,
|
||||
@@ -77,3 +78,32 @@ describe("isOlderThan", () => {
|
||||
expect(isOlderThan("2025-01-01T00:01:30.000Z", 60000, now)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatDateLabel", () => {
|
||||
const now = new Date("2026-06-03T12:00:00.000Z");
|
||||
|
||||
test("今天", () => {
|
||||
expect(formatDateLabel("2026-06-03", now)).toBe("今天");
|
||||
expect(formatDateLabel("2026-06-03T08:00:00.000Z", now)).toBe("今天");
|
||||
});
|
||||
|
||||
test("昨天", () => {
|
||||
expect(formatDateLabel("2026-06-02", now)).toBe("昨天");
|
||||
expect(formatDateLabel("2026-06-02T23:59:59.000Z", now)).toBe("昨天");
|
||||
});
|
||||
|
||||
test("其他日期返回 YYYY-MM-DD", () => {
|
||||
expect(formatDateLabel("2026-05-30", now)).toBe("2026-05-30");
|
||||
expect(formatDateLabel("2025-01-15", now)).toBe("2025-01-15");
|
||||
});
|
||||
|
||||
test("无效输入返回占位符", () => {
|
||||
expect(formatDateLabel("", now)).toBe("—");
|
||||
expect(formatDateLabel("not-a-date", now)).toBe("—");
|
||||
});
|
||||
|
||||
test("不传 now 参数使用当前日期", () => {
|
||||
const result = formatDateLabel(new Date().toISOString().slice(0, 10));
|
||||
expect(result).toBe("今天");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user